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  Ptr<vector> cp = getVector(c);
130  Ptr<const vector> xp = getVector(x);
131 
132  (*cp)[0] = 1.0 - (*xp)[0] - (*xp)[1] - (*xp)[2];
133  }
134 
136  const Vector<Real> &x, Real &tol ) {
137  Ptr<vector> jvp = getVector(jv);
138  Ptr<const vector> vp = getVector(v);
139 
140  (*jvp)[0] = - (*vp)[0] - (*vp)[1] - (*vp)[2];
141  }
142 
144  const Vector<Real> &x, Real &tol ) {
145  Ptr<vector> ajvp = getVector(ajv);
146  Ptr<const vector> vp = getVector(v);
147 
148  (*ajvp)[0] = -(*vp)[0];
149  (*ajvp)[1] = -(*vp)[0];
150  (*ajvp)[2] = -(*vp)[0];
151  }
152 
154  const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
155  ahuv.zero();
156  }
157 
158 }; // class EqualityConstraint_HS32
159 
160 
161 template<class Real>
162 class InequalityConstraint_HS32 : public Constraint<Real> {
163 
164  typedef std::vector<Real> vector;
165  typedef Vector<Real> V;
167 
168 private:
169  Ptr<const vector> getVector( const V& x ) {
170  return dynamic_cast<const SV&>(x).getVector();
171  }
172 
173  Ptr<vector> getVector( V& x ) {
174  return dynamic_cast<SV&>(x).getVector();
175  }
176 
177 public:
179 
180  void value( Vector<Real> &c, const Vector<Real> &x, Real &tol ) {
181  Ptr<vector> cp = getVector(c);
182  Ptr<const vector> xp = getVector(x);
183 
184  (*cp)[0] = 6*(*xp)[1]+4*(*xp)[2]-std::pow((*xp)[0],3)-3.0;
185  }
186 
188  const Vector<Real> &x, Real &tol ) {
189  Ptr<vector> jvp = getVector(jv);
190  Ptr<const vector> vp = getVector(v);
191  Ptr<const vector> xp = getVector(x);
192 
193  (*jvp)[0] = -3*(*xp)[0]*(*xp)[0]*(*vp)[0]+6*(*vp)[1]+4*(*vp)[2];
194  }
195 
197  const Vector<Real> &x, Real &tol ) {
198  Ptr<vector> ajvp = getVector(ajv);
199  Ptr<const vector> vp = getVector(v);
200  Ptr<const vector> xp = getVector(x);
201 
202  (*ajvp)[0] = -3*(*xp)[0]*(*xp)[0]*(*vp)[0];
203  (*ajvp)[1] = 6*(*vp)[0];
204  (*ajvp)[2] = 4*(*vp)[0];
205  }
206 
208  const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
209  Ptr<vector> ahuvp = getVector(ahuv);
210  Ptr<const vector> up = getVector(u);
211  Ptr<const vector> vp = getVector(v);
212  Ptr<const vector> xp = getVector(x);
213 
214  (*ahuvp)[0] = -6*(*up)[0]*(*vp)[0]*(*xp)[0];
215  }
216 
217 }; // class Constraint_HS32
218 
219 
220 template<class Real>
221 class getHS32 : public TestProblem<Real> {
222 public:
223  getHS32(void) {}
224 
225  Ptr<Objective<Real> > getObjective( void ) const {
226  return makePtr<Objective_HS32<Real>>();
227  }
228 
229  Ptr<Constraint<Real> > getEqualityConstraint( void ) const {
230  return makePtr<EqualityConstraint_HS32<Real>>();
231  }
232 
233  Ptr<Constraint<Real> > getInequalityConstraint( void ) const {
234  return makePtr<InequalityConstraint_HS32<Real>>();
235  }
236 
237  Ptr<BoundConstraint<Real> > getBoundConstraint( void ) const {
238  // Lower bound zero
239  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(3, static_cast<Real>(0));
240 
241  // No upper bound
242  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(3, ROL_INF<Real>());
243 
244  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
245  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
246 
247  return makePtr<Bounds<Real>>(l,u);
248  }
249 
250  Ptr<Vector<Real> > getInitialGuess( void ) const {
251  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(3);
252  (*x0p)[0] = 0.1;
253  (*x0p)[1] = 0.7;
254  (*x0p)[2] = 0.2;
255 
256  return makePtr<StdVector<Real>>(x0p);
257  }
258 
259  Ptr<Vector<Real> > getSolution( const int i = 0 ) const {
260  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(3);
261  (*xp)[0] = 0.0;
262  (*xp)[1] = 0.0;
263  (*xp)[2] = 1.0;
264 
265  return makePtr<StdVector<Real>>(xp);
266  }
267 
268  Ptr<Vector<Real> > getEqualityMultiplier( void ) const {
269  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
270  return makePtr<StdVector<Real>>(lp);
271  }
272 
273  Ptr<Vector<Real> > getInequalityMultiplier( void ) const {
274  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
275  return makePtr<StdVector<Real>>(lp);
276  }
277 
278  Ptr<BoundConstraint<Real>> getSlackBoundConstraint(void) const {
279  // Lower bound is zero
280  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(1,0.0);
281 
282  // No upper bound
283  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(1,ROL_INF<Real>());
284 
285  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
286  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
287 
288  return makePtr<Bounds<Real>>(l,u);
289  }
290 };
291 
292 }
293 } // namespace ROL
294 
295 
296 #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:196
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS32.hpp:237
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:187
Ptr< Constraint< Real > > getInequalityConstraint(void) const
Definition: ROL_HS32.hpp:233
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:143
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:259
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS32.hpp:225
void value(Vector< Real > &c, const Vector< Real > &x, Real &tol)
Evaluate the constraint operator at .
Definition: ROL_HS32.hpp:180
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:135
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:153
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:207
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:273
Ptr< const vector > getVector(const V &x)
Definition: ROL_HS32.hpp:67
Ptr< BoundConstraint< Real > > getSlackBoundConstraint(void) const
Definition: ROL_HS32.hpp:278
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Definition: ROL_HS32.hpp:229
Defines the general constraint operator interface.
Ptr< const vector > getVector(const V &x)
Definition: ROL_HS32.hpp:169
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS32.hpp:250
Ptr< Vector< Real > > getEqualityMultiplier(void) const
Definition: ROL_HS32.hpp:268
std::vector< Real > vector
Definition: ROL_HS32.hpp:111