ROL
ROL_HS2.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
14
15#ifndef USE_HESSVEC
16#define USE_HESSVEC 1
17#endif
18
19#ifndef ROL_HS2_HPP
20#define ROL_HS2_HPP
21
22#include "ROL_StdVector.hpp"
23#include "ROL_TestProblem.hpp"
24#include "ROL_Bounds.hpp"
25#include "ROL_Types.hpp"
26
27namespace ROL {
28namespace ZOO {
29
32 template<class Real>
33 class Objective_HS2 : public Objective<Real> {
34
35 typedef std::vector<Real> vector;
36 typedef Vector<Real> V;
38
39 private:
40
41 Ptr<const vector> getVector( const V& x ) {
42
43 return dynamic_cast<const SV&>(x).getVector();
44 }
45
46 Ptr<vector> getVector( V& x ) {
47
48 return dynamic_cast<SV&>(x).getVector();
49 }
50
51 public:
53
54 Real value( const Vector<Real> &x, Real &tol ) {
55
56
57 Ptr<const vector> ex = getVector(x);
58 return static_cast<Real>(100) * std::pow((*ex)[1] - std::pow((*ex)[0],2),2)
59 + std::pow(static_cast<Real>(1)-(*ex)[0],2);
60 }
61
62 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
63
64
65 Ptr<const vector> ex = getVector(x);
66 Ptr<vector> eg = getVector(g);
67 (*eg)[0] = static_cast<Real>(-400) * ((*ex)[1] - std::pow((*ex)[0],2))
68 * (*ex)[0] - static_cast<Real>(2) * (static_cast<Real>(1)-(*ex)[0]);
69 (*eg)[1] = static_cast<Real>(200) * ((*ex)[1] - std::pow((*ex)[0],2));
70 }
71#if USE_HESSVEC
72 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
73
74
75 Ptr<const vector> ex = getVector(x);
76 Ptr<const vector> ev = getVector(v);
77 Ptr<vector> ehv = getVector(hv);
78
79 Real h11 = static_cast<Real>(-400) * (*ex)[1]
80 + static_cast<Real>(1200) * std::pow((*ex)[0],2)
81 + static_cast<Real>(2);
82 Real h22 = static_cast<Real>(200);
83 Real h12 = static_cast<Real>(-400) * (*ex)[0];
84 Real h21 = static_cast<Real>(-400) * (*ex)[0];
85
86 Real alpha(0);
87
88 (*ehv)[0] = (h11+alpha) * (*ev)[0] + h12 * (*ev)[1];
89 (*ehv)[1] = h21 * (*ev)[0] + (h22+alpha) * (*ev)[1];
90 }
91#endif
92 void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
93
94
95 Ptr<const vector> ex = getVector(x);
96 Ptr<const vector> ev = getVector(v);
97 Ptr< vector> ehv = getVector(hv);
98
99 Real h11 = static_cast<Real>(-400) * (*ex)[1]
100 + static_cast<Real>(1200) * std::pow((*ex)[0],2)
101 + static_cast<Real>(2);
102 Real h22 = static_cast<Real>(200);
103 Real h12 = static_cast<Real>(-400) * (*ex)[0];
104 Real h21 = static_cast<Real>(-400) * (*ex)[0];
105
106 (*ehv)[0] = static_cast<Real>(1)/(h11*h22 - h12*h21)
107 * (h22 * (*ev)[0] - h12 * (*ev)[1]);
108 (*ehv)[1] = static_cast<Real>(1)/(h11*h22 - h12*h21)
109 * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
110 }
111 };
112
113template<class Real>
114class getHS2 : public TestProblem<Real> {
115public:
116 getHS2(void) {}
117
118 Ptr<Objective<Real>> getObjective(void) const {
119 // Instantiate Objective Function
120 return makePtr<Objective_HS2<Real>>();
121 }
122
123 Ptr<Vector<Real>> getInitialGuess(void) const {
124 // Problem dimension
125 int n = 2;
126 // Get Initial Guess
127 Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,0.0);
128 (*x0p)[0] = -2.0; (*x0p)[1] = 1.0;
129 return makePtr<StdVector<Real>>(x0p);
130 }
131
132 Ptr<Vector<Real>> getSolution(const int i = 0) const {
133 // Problem dimension
134 int n = 2;
135 // Get Solution
136 Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,0.0);
137 Real a = std::sqrt(598.0/1200.0);
138 Real b = 400.0 * std::pow(a,3.0);
139 (*xp)[0] = 2.0*a*std::cos(1.0/3.0 * std::acos(1.0/b));
140 (*xp)[1] = 1.5;
141 return makePtr<StdVector<Real>>(xp);
142 }
143
144 Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
145 // Problem dimension
146 int n = 2;
147 // Instantiate BoundConstraint
148 Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(n,0.0);
149 (*lp)[0] = ROL_NINF<Real>(); (*lp)[1] = 1.5;
150 Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
151 Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(n,0.0);
152 (*up)[0] = ROL_INF<Real>(); (*up)[1] = ROL_INF<Real>();
153 Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
154 return makePtr<Bounds<Real>>(l,u);
155 }
156};
157
158} // End ZOO Namespace
159} // End ROL Namespace
160
161#endif
Contains definitions of test objective functions.
Contains definitions of custom data types in ROL.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
std::vector< Real > vector
Definition ROL_HS2.hpp:35
Real value(const Vector< Real > &x, Real &tol)
Definition ROL_HS2.hpp:54
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Definition ROL_HS2.hpp:92
Ptr< const vector > getVector(const V &x)
Definition ROL_HS2.hpp:41
Ptr< vector > getVector(V &x)
Definition ROL_HS2.hpp:46
StdVector< Real > SV
Definition ROL_HS2.hpp:37
Vector< Real > V
Definition ROL_HS2.hpp:36
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Definition ROL_HS2.hpp:62
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition ROL_HS2.hpp:132
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition ROL_HS2.hpp:144
Ptr< Objective< Real > > getObjective(void) const
Definition ROL_HS2.hpp:118
Ptr< Vector< Real > > getInitialGuess(void) const
Definition ROL_HS2.hpp:123
Real ROL_NINF(void)
Definition ROL_Types.hpp:74
Real ROL_INF(void)
Definition ROL_Types.hpp:71