ROL
ROL_HS32.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
49 #ifndef ROL_HS32_HPP
50 #define ROL_HS32_HPP
51 
52 #include "ROL_StdVector.hpp"
53 #include "ROL_TestProblem.hpp"
54 
55 namespace ROL {
56 namespace ZOO {
57 
58 template<class Real>
59 class Objective_HS32 : public Objective<Real> {
60 
61  typedef std::vector<Real> vector;
62  typedef Vector<Real> V;
64 
65 private:
66 
67  Ptr<const vector> getVector( const V& x ) {
68 
69  return dynamic_cast<const SV&>(x).getVector();
70  }
71 
72  Ptr<vector> getVector( V& x ) {
73 
74  return dynamic_cast<SV&>(x).getVector();
75  }
76 
77 public:
78 
79  Real value( const Vector<Real> &x, Real &tol ) {
80  Ptr<const vector> xp = getVector(x);
81 
82  Real term1 = (*xp)[0]+3*(*xp)[1]+(*xp)[2];
83  Real term2 = (*xp)[0]-(*xp)[1];
84  return term1*term1 + 4*term2*term2;
85  }
86 
87  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
88  Ptr<vector> gp = getVector(g);
89  Ptr<const vector> xp = getVector(x);
90 
91  (*gp)[0] = 10*(*xp)[0] - 2*(*xp)[1] + 2*(*xp)[2];
92  (*gp)[1] = -2*(*xp)[0] + 26*(*xp)[1] + 6*(*xp)[2];
93  (*gp)[2] = 2*(*xp)[0] + 6*(*xp)[1] + 2*(*xp)[2];
94  }
95 
96  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
97  Ptr<vector> hvp = getVector(hv);
98  Ptr<const vector> vp = getVector(v);
99 
100  (*hvp)[0] = 10*(*vp)[0] - 2*(*vp)[1] + 2*(*vp)[2];
101  (*hvp)[1] = -2*(*vp)[0] + 26*(*vp)[1] + 6*(*vp)[2];
102  (*hvp)[2] = 2*(*vp)[0] + 6*(*vp)[1] + 2*(*vp)[2];
103  }
104 
105 }; // class Objective_HS32
106 
107 
108 template<class Real>
109 class EqualityConstraint_HS32 : public Constraint<Real> {
110 
111  typedef std::vector<Real> vector;
112  typedef Vector<Real> V;
114 
115 private:
116 
117  Ptr<const vector> getVector( const V& x ) {
118  return dynamic_cast<const SV&>(x).getVector();
119  }
120 
121  Ptr<vector> getVector( V& x ) {
122  return dynamic_cast<SV&>(x).getVector();
123  }
124 
125 public:
127 
128  void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
129  const Real one(1);
130  Ptr<vector> cp = getVector(c);
131  Ptr<const vector> xp = getVector(x);
132 
133  (*cp)[0] = one - (*xp)[0] - (*xp)[1] - (*xp)[2];
134  }
135 
137  const Vector<Real> &x, Real &tol ) {
138  Ptr<vector> jvp = getVector(jv);
139  Ptr<const vector> vp = getVector(v);
140 
141  (*jvp)[0] = - (*vp)[0] - (*vp)[1] - (*vp)[2];
142  }
143 
145  const Vector<Real> &x, Real &tol ) {
146  Ptr<vector> ajvp = getVector(ajv);
147  Ptr<const vector> vp = getVector(v);
148 
149  (*ajvp)[0] = -(*vp)[0];
150  (*ajvp)[1] = -(*vp)[0];
151  (*ajvp)[2] = -(*vp)[0];
152  }
153 
155  const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
156  ahuv.zero();
157  }
158 
159 }; // class EqualityConstraint_HS32
160 
161 
162 template<class Real>
163 class InequalityConstraint_HS32 : public Constraint<Real> {
164 
165  typedef std::vector<Real> vector;
166  typedef Vector<Real> V;
168 
169 private:
170  Ptr<const vector> getVector( const V& x ) {
171  return dynamic_cast<const SV&>(x).getVector();
172  }
173 
174  Ptr<vector> getVector( V& x ) {
175  return dynamic_cast<SV&>(x).getVector();
176  }
177 
178 public:
180 
181  void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
182  const Real three(3), four(4), six(6);
183  Ptr<vector> cp = getVector(c);
184  Ptr<const vector> xp = getVector(x);
185 
186  (*cp)[0] = six*(*xp)[1]+four*(*xp)[2]-std::pow((*xp)[0],three)-three;
187  }
188 
190  const Vector<Real> &x, Real &tol ) {
191  const Real three(3), four(4), six(6);
192  Ptr<vector> jvp = getVector(jv);
193  Ptr<const vector> vp = getVector(v);
194  Ptr<const vector> xp = getVector(x);
195 
196  (*jvp)[0] = -three*(*xp)[0]*(*xp)[0]*(*vp)[0]+six*(*vp)[1]+four*(*vp)[2];
197  }
198 
200  const Vector<Real> &x, Real &tol ) {
201  const Real three(3), four(4), six(6);
202  Ptr<vector> ajvp = getVector(ajv);
203  Ptr<const vector> vp = getVector(v);
204  Ptr<const vector> xp = getVector(x);
205 
206  (*ajvp)[0] = -three*(*xp)[0]*(*xp)[0]*(*vp)[0];
207  (*ajvp)[1] = six*(*vp)[0];
208  (*ajvp)[2] = four*(*vp)[0];
209  }
210 
212  const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
213  const Real zero(0), six(6);
214  Ptr<vector> ahuvp = getVector(ahuv);
215  Ptr<const vector> up = getVector(u);
216  Ptr<const vector> vp = getVector(v);
217  Ptr<const vector> xp = getVector(x);
218 
219  (*ahuvp)[0] = -six*(*up)[0]*(*vp)[0]*(*xp)[0];
220  (*ahuvp)[1] = zero;
221  (*ahuvp)[2] = zero;
222  }
223 
224 }; // class Constraint_HS32
225 
226 
227 template<class Real>
228 class getHS32 : public TestProblem<Real> {
229 public:
230  getHS32(void) {}
231 
232  Ptr<Objective<Real> > getObjective( void ) const {
233  return makePtr<Objective_HS32<Real>>();
234  }
235 
236  Ptr<Constraint<Real> > getEqualityConstraint( void ) const {
237  return makePtr<EqualityConstraint_HS32<Real>>();
238  }
239 
240  Ptr<Constraint<Real> > getInequalityConstraint( void ) const {
241  return makePtr<InequalityConstraint_HS32<Real>>();
242  }
243 
244  Ptr<BoundConstraint<Real> > getBoundConstraint( void ) const {
245  // Lower bound zero
246  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(3, static_cast<Real>(0));
247 
248  // No upper bound
249  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(3, ROL_INF<Real>());
250 
251  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
252  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
253 
254  return makePtr<Bounds<Real>>(l,u);
255  }
256 
257  Ptr<Vector<Real> > getInitialGuess( void ) const {
258  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(3);
259  (*x0p)[0] = 0.1;
260  (*x0p)[1] = 0.7;
261  (*x0p)[2] = 0.2;
262 
263  return makePtr<StdVector<Real>>(x0p);
264  }
265 
266  Ptr<Vector<Real> > getSolution( const int i = 0 ) const {
267  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(3);
268  (*xp)[0] = 0.0;
269  (*xp)[1] = 0.0;
270  (*xp)[2] = 1.0;
271 
272  return makePtr<StdVector<Real>>(xp);
273  }
274 
275  Ptr<Vector<Real> > getEqualityMultiplier( void ) const {
276  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
277  return makePtr<StdVector<Real>>(lp);
278  }
279 
280  Ptr<Vector<Real> > getInequalityMultiplier( void ) const {
281  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
282  return makePtr<StdVector<Real>>(lp);
283  }
284 
285  Ptr<BoundConstraint<Real>> getSlackBoundConstraint(void) const {
286  // Lower bound is zero
287  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
288 
289  // No upper bound
290  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(1,ROL_INF<Real>());
291 
292  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
293  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
294 
295  return makePtr<Bounds<Real>>(l,u);
296  }
297 };
298 
299 }
300 } // namespace ROL
301 
302 
303 #endif // ROL_HS32_HPP
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS32.hpp:87
Provides the interface to evaluate objective functions.
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:199
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS32.hpp:244
std::vector< Real > vector
Definition: ROL_HS32.hpp:61
Vector< Real > V
Definition: ROL_HS32.hpp:62
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
Definition: ROL_HS32.hpp:128
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:189
Ptr< Constraint< Real > > getInequalityConstraint(void) const
Definition: ROL_HS32.hpp:240
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Ptr< vector > getVector(V &x)
Definition: ROL_HS32.hpp:72
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Definition: ROL_HS32.hpp:96
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:144
Ptr< const vector > getVector(const V &x)
Definition: ROL_HS32.hpp:117
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_HS32.hpp:266
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS32.hpp:232
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
Definition: ROL_HS32.hpp:181
Contains definitions of test objective functions.
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:136
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:154
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:211
StdVector< Real > SV
Definition: ROL_HS32.hpp:63
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS32.hpp:79
Ptr< vector > getVector(V &x)
Definition: ROL_HS32.hpp:121
Ptr< Vector< Real > > getInequalityMultiplier(void) const
Definition: ROL_HS32.hpp:280
Ptr< const vector > getVector(const V &x)
Definition: ROL_HS32.hpp:67
Ptr< BoundConstraint< Real > > getSlackBoundConstraint(void) const
Definition: ROL_HS32.hpp:285
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Definition: ROL_HS32.hpp:236
Defines the general constraint operator interface.
Ptr< const vector > getVector(const V &x)
Definition: ROL_HS32.hpp:170
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS32.hpp:257
Ptr< Vector< Real > > getEqualityMultiplier(void) const
Definition: ROL_HS32.hpp:275
std::vector< Real > vector
Definition: ROL_HS32.hpp:111