ROL
ROL_HS32.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
15#ifndef ROL_HS32_HPP
16#define ROL_HS32_HPP
17
18#include "ROL_StdVector.hpp"
19#include "ROL_TestProblem.hpp"
20
21namespace ROL {
22namespace ZOO {
23
24template<class Real>
25class Objective_HS32 : public Objective<Real> {
26
27 typedef std::vector<Real> vector;
28 typedef Vector<Real> V;
30
31private:
32
33 Ptr<const vector> getVector( const V& x ) {
34
35 return dynamic_cast<const SV&>(x).getVector();
36 }
37
38 Ptr<vector> getVector( V& x ) {
39
40 return dynamic_cast<SV&>(x).getVector();
41 }
42
43public:
44
45 Real value( const Vector<Real> &x, Real &tol ) {
46 Ptr<const vector> xp = getVector(x);
47
48 Real term1 = (*xp)[0]+3*(*xp)[1]+(*xp)[2];
49 Real term2 = (*xp)[0]-(*xp)[1];
50 return term1*term1 + 4*term2*term2;
51 }
52
53 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
54 Ptr<vector> gp = getVector(g);
55 Ptr<const vector> xp = getVector(x);
56
57 (*gp)[0] = 10*(*xp)[0] - 2*(*xp)[1] + 2*(*xp)[2];
58 (*gp)[1] = -2*(*xp)[0] + 26*(*xp)[1] + 6*(*xp)[2];
59 (*gp)[2] = 2*(*xp)[0] + 6*(*xp)[1] + 2*(*xp)[2];
60 }
61
62 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
63 Ptr<vector> hvp = getVector(hv);
64 Ptr<const vector> vp = getVector(v);
65
66 (*hvp)[0] = 10*(*vp)[0] - 2*(*vp)[1] + 2*(*vp)[2];
67 (*hvp)[1] = -2*(*vp)[0] + 26*(*vp)[1] + 6*(*vp)[2];
68 (*hvp)[2] = 2*(*vp)[0] + 6*(*vp)[1] + 2*(*vp)[2];
69 }
70
71}; // class Objective_HS32
72
73
74template<class Real>
75class EqualityConstraint_HS32 : public Constraint<Real> {
76
77 typedef std::vector<Real> vector;
78 typedef Vector<Real> V;
80
81private:
82
83 Ptr<const vector> getVector( const V& x ) {
84 return dynamic_cast<const SV&>(x).getVector();
85 }
86
87 Ptr<vector> getVector( V& x ) {
88 return dynamic_cast<SV&>(x).getVector();
89 }
90
91public:
93
94 void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
95 const Real one(1);
96 Ptr<vector> cp = getVector(c);
97 Ptr<const vector> xp = getVector(x);
98
99 (*cp)[0] = one - (*xp)[0] - (*xp)[1] - (*xp)[2];
100 }
101
103 const Vector<Real> &x, Real &tol ) {
104 Ptr<vector> jvp = getVector(jv);
105 Ptr<const vector> vp = getVector(v);
106
107 (*jvp)[0] = - (*vp)[0] - (*vp)[1] - (*vp)[2];
108 }
109
111 const Vector<Real> &x, Real &tol ) {
112 Ptr<vector> ajvp = getVector(ajv);
113 Ptr<const vector> vp = getVector(v);
114
115 (*ajvp)[0] = -(*vp)[0];
116 (*ajvp)[1] = -(*vp)[0];
117 (*ajvp)[2] = -(*vp)[0];
118 }
119
121 const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
122 ahuv.zero();
123 }
124
125}; // class EqualityConstraint_HS32
126
127
128template<class Real>
130
131 typedef std::vector<Real> vector;
134
135private:
136 Ptr<const vector> getVector( const V& x ) {
137 return dynamic_cast<const SV&>(x).getVector();
138 }
139
140 Ptr<vector> getVector( V& x ) {
141 return dynamic_cast<SV&>(x).getVector();
142 }
143
144public:
146
147 void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
148 const Real three(3), four(4), six(6);
149 Ptr<vector> cp = getVector(c);
150 Ptr<const vector> xp = getVector(x);
151
152 (*cp)[0] = six*(*xp)[1]+four*(*xp)[2]-std::pow((*xp)[0],three)-three;
153 }
154
156 const Vector<Real> &x, Real &tol ) {
157 const Real three(3), four(4), six(6);
158 Ptr<vector> jvp = getVector(jv);
159 Ptr<const vector> vp = getVector(v);
160 Ptr<const vector> xp = getVector(x);
161
162 (*jvp)[0] = -three*(*xp)[0]*(*xp)[0]*(*vp)[0]+six*(*vp)[1]+four*(*vp)[2];
163 }
164
166 const Vector<Real> &x, Real &tol ) {
167 const Real three(3), four(4), six(6);
168 Ptr<vector> ajvp = getVector(ajv);
169 Ptr<const vector> vp = getVector(v);
170 Ptr<const vector> xp = getVector(x);
171
172 (*ajvp)[0] = -three*(*xp)[0]*(*xp)[0]*(*vp)[0];
173 (*ajvp)[1] = six*(*vp)[0];
174 (*ajvp)[2] = four*(*vp)[0];
175 }
176
178 const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
179 const Real zero(0), six(6);
180 Ptr<vector> ahuvp = getVector(ahuv);
181 Ptr<const vector> up = getVector(u);
182 Ptr<const vector> vp = getVector(v);
183 Ptr<const vector> xp = getVector(x);
184
185 (*ahuvp)[0] = -six*(*up)[0]*(*vp)[0]*(*xp)[0];
186 (*ahuvp)[1] = zero;
187 (*ahuvp)[2] = zero;
188 }
189
190}; // class Constraint_HS32
191
192
193template<class Real>
194class getHS32 : public TestProblem<Real> {
195public:
196 getHS32(void) {}
197
198 Ptr<Objective<Real> > getObjective( void ) const {
199 return makePtr<Objective_HS32<Real>>();
200 }
201
202 Ptr<Constraint<Real> > getEqualityConstraint( void ) const {
203 return makePtr<EqualityConstraint_HS32<Real>>();
204 }
205
206 Ptr<Constraint<Real> > getInequalityConstraint( void ) const {
207 return makePtr<InequalityConstraint_HS32<Real>>();
208 }
209
210 Ptr<BoundConstraint<Real> > getBoundConstraint( void ) const {
211 // Lower bound zero
212 Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(3, static_cast<Real>(0));
213
214 // No upper bound
215 Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(3, ROL_INF<Real>());
216
217 Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
218 Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
219
220 return makePtr<Bounds<Real>>(l,u);
221 }
222
223 Ptr<Vector<Real> > getInitialGuess( void ) const {
224 Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(3);
225 (*x0p)[0] = 0.1;
226 (*x0p)[1] = 0.7;
227 (*x0p)[2] = 0.2;
228
229 return makePtr<StdVector<Real>>(x0p);
230 }
231
232 Ptr<Vector<Real> > getSolution( const int i = 0 ) const {
233 Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(3);
234 (*xp)[0] = 0.0;
235 (*xp)[1] = 0.0;
236 (*xp)[2] = 1.0;
237
238 return makePtr<StdVector<Real>>(xp);
239 }
240
241 Ptr<Vector<Real> > getEqualityMultiplier( void ) const {
242 Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
243 return makePtr<StdVector<Real>>(lp);
244 }
245
246 Ptr<Vector<Real> > getInequalityMultiplier( void ) const {
247 Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
248 return makePtr<StdVector<Real>>(lp);
249 }
250
251 Ptr<BoundConstraint<Real>> getSlackBoundConstraint(void) const {
252 // Lower bound is zero
253 Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
254
255 // No upper bound
256 Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(1,ROL_INF<Real>());
257
258 Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
259 Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
260
261 return makePtr<Bounds<Real>>(l,u);
262 }
263};
264
265}
266} // namespace ROL
267
268
269#endif // ROL_HS32_HPP
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Contains definitions of test objective functions.
Defines the general constraint operator interface.
Provides the interface to evaluate objective functions.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
virtual void zero()
Set to zero vector.
Ptr< const vector > getVector(const V &x)
Definition ROL_HS32.hpp:83
void applyAdjointJacobian(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the adjoint of the the constraint Jacobian at , , to vector .
Definition ROL_HS32.hpp:110
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
Definition ROL_HS32.hpp:94
Ptr< vector > getVector(V &x)
Definition ROL_HS32.hpp:87
void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the constraint Jacobian at , , to vector .
Definition ROL_HS32.hpp:102
void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the derivative of the adjoint of the constraint Jacobian at to vector in direction ,...
Definition ROL_HS32.hpp:120
void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the derivative of the adjoint of the constraint Jacobian at to vector in direction ,...
Definition ROL_HS32.hpp:177
void applyAdjointJacobian(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the adjoint of the the constraint Jacobian at , , to vector .
Definition ROL_HS32.hpp:165
Ptr< const vector > getVector(const V &x)
Definition ROL_HS32.hpp:136
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
Definition ROL_HS32.hpp:147
void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the constraint Jacobian at , , to vector .
Definition ROL_HS32.hpp:155
std::vector< Real > vector
Definition ROL_HS32.hpp:27
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition ROL_HS32.hpp:45
Ptr< const vector > getVector(const V &x)
Definition ROL_HS32.hpp:33
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Definition ROL_HS32.hpp:62
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition ROL_HS32.hpp:53
Ptr< vector > getVector(V &x)
Definition ROL_HS32.hpp:38
StdVector< Real > SV
Definition ROL_HS32.hpp:29
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Definition ROL_HS32.hpp:202
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition ROL_HS32.hpp:210
Ptr< Constraint< Real > > getInequalityConstraint(void) const
Definition ROL_HS32.hpp:206
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition ROL_HS32.hpp:232
Ptr< Objective< Real > > getObjective(void) const
Definition ROL_HS32.hpp:198
Ptr< Vector< Real > > getInitialGuess(void) const
Definition ROL_HS32.hpp:223
Ptr< Vector< Real > > getInequalityMultiplier(void) const
Definition ROL_HS32.hpp:246
Ptr< BoundConstraint< Real > > getSlackBoundConstraint(void) const
Definition ROL_HS32.hpp:251
Ptr< Vector< Real > > getEqualityMultiplier(void) const
Definition ROL_HS32.hpp:241