ROL
Loading...
Searching...
No Matches
ROL_ParaboloidCircle.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
18
19#ifndef ROL_PARABOLOIDCIRCLE_HPP
20#define ROL_PARABOLOIDCIRCLE_HPP
21
22#include "ROL_TestProblem.hpp"
23#include "ROL_StdVector.hpp"
24#include "ROL_LinearAlgebra.hpp"
25
26namespace ROL {
27namespace ZOO {
28
32 template< class Real, class XPrim=StdVector<Real>, class XDual=StdVector<Real> >
34
35 typedef std::vector<Real> vector;
36 typedef Vector<Real> V;
37
38 typedef typename vector::size_type uint;
39
40
41 private:
42
43 template<class VectorType>
44 ROL::Ptr<const vector> getVector( const V& x ) {
45
46 return dynamic_cast<const VectorType&>(x).getVector();
47 }
48
49 template<class VectorType>
50 ROL::Ptr<vector> getVector( V& x ) {
51
52 return dynamic_cast<VectorType&>(x).getVector();
53 }
54
55 public:
57
58 Real value( const Vector<Real> &x, Real &tol ) {
59
60
61 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
62
63 uint n = xp->size();
64 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, objective value): "
65 "Primal vector x must be of length 2.");
66
67 Real x1 = (*xp)[0];
68 Real x2 = (*xp)[1];
69
70 Real val = x1*x1 + x2*x2;
71
72 return val;
73 }
74
75 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
76
77
78 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
79 ROL::Ptr<vector> gp = getVector<XDual>(g);
80
81 uint n = xp->size();
82 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, objective gradient): "
83 " Primal vector x must be of length 2.");
84
85 n = gp->size();
86 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, objective gradient): "
87 "Gradient vector g must be of length 2.");
88
89 Real x1 = (*xp)[0];
90 Real x2 = (*xp)[1];
91
92 Real two(2);
93
94 (*gp)[0] = two*x1;
95 (*gp)[1] = two*x2;
96 }
97
98 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
99
100
101 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
102 ROL::Ptr<const vector> vp = getVector<XPrim>(v);
103 ROL::Ptr<vector> hvp = getVector<XDual>(hv);
104
105 uint n = xp->size();
106 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, objective hessVec): "
107 "Primal vector x must be of length 2.");
108
109 n = vp->size();
110 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, objective hessVec): "
111 "Input vector v must be of length 2.");
112
113 n = hvp->size();
114 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, objective hessVec): "
115 "Output vector hv must be of length 2.");
116
117 Real v1 = (*vp)[0];
118 Real v2 = (*vp)[1];
119
120 Real two(2);
121
122 (*hvp)[0] = two*v1;
123 (*hvp)[1] = two*v2;
124 }
125
126 };
127
128
131 template<class Real, class XPrim=StdVector<Real>, class XDual=StdVector<Real>, class CPrim=StdVector<Real>, class CDual=StdVector<Real> >
133
134 typedef std::vector<Real> vector;
136
137 typedef typename vector::size_type uint;
138
139 private:
140 template<class VectorType>
141 ROL::Ptr<const vector> getVector( const V& x ) {
142
143 return dynamic_cast<const VectorType&>(x).getVector();
144 }
145
146 template<class VectorType>
147 ROL::Ptr<vector> getVector( V& x ) {
148
149 return dynamic_cast<VectorType&>(x).getVector();
150 }
151
152 public:
154
155 void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
156
157
158 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
159 ROL::Ptr<vector> cp = getVector<CPrim>(c);
160
161 uint n = xp->size();
162 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint value): "
163 "Primal vector x must be of length 2.");
164
165 uint m = cp->size();
166 ROL_TEST_FOR_EXCEPTION( (m != 1), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint value): "
167 "Constraint vector c must be of length 1.");
168
169 Real x1 = (*xp)[0];
170 Real x2 = (*xp)[1];
171
172 Real one(1), two(2);
173
174 (*cp)[0] = (x1-two)*(x1-two) + x2*x2 - one;
175 }
176
177 void applyJacobian( Vector<Real> &jv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
178
179
180 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
181 ROL::Ptr<const vector> vp = getVector<XPrim>(v);
182 ROL::Ptr<vector> jvp = getVector<CPrim>(jv);
183
184 uint n = xp->size();
185 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyJacobian): "
186 "Primal vector x must be of length 2.");
187
188 uint d = vp->size();
189 ROL_TEST_FOR_EXCEPTION( (d != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyJacobian): "
190 "Input vector v must be of length 2.");
191 d = jvp->size();
192 ROL_TEST_FOR_EXCEPTION( (d != 1), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyJacobian): "
193 "Output vector jv must be of length 1.");
194
195 Real x1 = (*xp)[0];
196 Real x2 = (*xp)[1];
197
198 Real v1 = (*vp)[0];
199 Real v2 = (*vp)[1];
200
201 Real two(2);
202
203 (*jvp)[0] = two*(x1-two)*v1 + two*x2*v2;
204 } //applyJacobian
205
206 void applyAdjointJacobian( Vector<Real> &ajv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
207
208
209 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
210 ROL::Ptr<const vector> vp = getVector<CDual>(v);
211 ROL::Ptr<vector> ajvp = getVector<XDual>(ajv);
212
213 uint n = xp->size();
214 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyAdjointJacobian): "
215 "Primal vector x must be of length 2.");
216
217 uint d = vp->size();
218 ROL_TEST_FOR_EXCEPTION( (d != 1), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyAdjointJacobian): "
219 "Input vector v must be of length 1.");
220
221 d = ajvp->size();
222 ROL_TEST_FOR_EXCEPTION( (d != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyAdjointJacobian): "
223 "Output vector ajv must be of length 2.");
224
225 Real x1 = (*xp)[0];
226 Real x2 = (*xp)[1];
227
228 Real v1 = (*vp)[0];
229
230 Real two(2);
231
232 (*ajvp)[0] = two*(x1-two)*v1;
233 (*ajvp)[1] = two*x2*v1;
234
235 } //applyAdjointJacobian
236
237 void applyAdjointHessian( Vector<Real> &ahuv, const Vector<Real> &u, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
238
239 bool useFD = true;
240
241 if (useFD) {
242 Constraint<Real>::applyAdjointHessian( ahuv, u, v, x, tol );
243 }
244 else {
245
246 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
247 ROL::Ptr<const vector> up = getVector<CDual>(u);
248 ROL::Ptr<const vector> vp = getVector<XPrim>(v);
249 ROL::Ptr<vector> ahuvp = getVector<XDual>(ahuv);
250
251 uint n = xp->size();
252 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyAdjointHessian): "
253 "Primal vector x must be of length 2.");
254
255 n = vp->size();
256 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyAdjointHessian): "
257 "Direction vector v must be of length 2.");
258
259 n = ahuvp->size();
260 ROL_TEST_FOR_EXCEPTION( (n != 2), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyAdjointHessian): "
261 "Output vector ahuv must be of length 2.");
262 uint d = up->size();
263 ROL_TEST_FOR_EXCEPTION( (d != 1), std::invalid_argument, ">>> ERROR (ROL_ParaboloidCircle, constraint applyAdjointHessian): "
264 "Dual constraint vector u must be of length 1.");
265
266 Real v1 = (*vp)[0];
267 Real v2 = (*vp)[1];
268
269 Real u1 = (*up)[0];
270
271 Real two(2);
272
273 (*ahuvp)[0] = two*u1*v1;
274 (*ahuvp)[1] = two*u1*v2;
275 }
276 } //applyAdjointHessian
277
278 };
279
280
281 template<class Real, class XPrim=StdVector<Real>, class XDual=StdVector<Real>, class CPrim=StdVector<Real>, class CDual=StdVector<Real> >
282 class getParaboloidCircle : public TestProblem<Real> {
283 typedef std::vector<Real> vector;
284 typedef typename vector::size_type uint;
285 public:
287
288 Ptr<Objective<Real>> getObjective(void) const {
289 // Instantiate objective function.
290 return ROL::makePtr<Objective_ParaboloidCircle<Real,XPrim,XDual>>();
291 }
292
293 Ptr<Vector<Real>> getInitialGuess(void) const {
294 uint n = 2;
295 // Get initial guess.
296 ROL::Ptr<vector> x0p = makePtr<vector>(n,0.0);
297 (*x0p)[0] = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
298 (*x0p)[1] = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
299 return makePtr<XPrim>(x0p);
300 }
301
302 Ptr<Vector<Real>> getSolution(const int i = 0) const {
303 uint n = 2;
304 // Get solution.
305 Real zero(0), one(1);
306 ROL::Ptr<vector> solp = makePtr<vector>(n,0.0);
307 (*solp)[0] = one;
308 (*solp)[1] = zero;
309 return makePtr<XPrim>(solp);
310 }
311
312 Ptr<Constraint<Real>> getEqualityConstraint(void) const {
313 // Instantiate constraints.
314 return ROL::makePtr<Constraint_ParaboloidCircle<Real,XPrim,XDual,CPrim,CDual>>();
315 }
316
317 Ptr<Vector<Real>> getEqualityMultiplier(void) const {
318 ROL::Ptr<vector> lp = makePtr<vector>(1,0.0);
319 return makePtr<CDual>(lp);
320 }
321 };
322
323} // End ZOO Namespace
324} // End ROL Namespace
325
326#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0 zero)()
Contains definitions of test objective functions.
virtual 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 ,...
Defines the linear algebra or vector space interface.
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
ROL::Ptr< const vector > getVector(const V &x)
void applyAdjointJacobian(Vector< Real > &ajv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Real value(const Vector< Real > &x, Real &tol)
ROL::Ptr< const vector > getVector(const V &x)
Ptr< Vector< Real > > getSolution(const int i=0) const
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Ptr< Objective< Real > > getObjective(void) const
Ptr< Vector< Real > > getEqualityMultiplier(void) const
Ptr< Vector< Real > > getInitialGuess(void) const