ROL
ROL_Cubic.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 USE_HESSVEC
50 #define USE_HESSVEC 1
51 #endif
52 
53 #ifndef ROL_CUBIC_HPP
54 #define ROL_CUBIC_HPP
55 
56 #include "ROL_ScaledStdVector.hpp"
57 #include "ROL_StdObjective.hpp"
58 #include "ROL_StdConstraint.hpp"
59 #include "ROL_TestProblem.hpp"
60 
61 namespace ROL {
62 namespace ZOO {
63 
64  template<class Real>
65  class Objective_Cubic : public StdObjective<Real> {
66  public:
68 
69  Real value( const std::vector<Real> &x, Real &tol ) {
70  return std::pow(x[0],3)+std::pow(x[1],3);
71  }
72 
73  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
74  const Real three(3);
75  g[0] = three*std::pow(x[0],2);
76  g[1] = three*std::pow(x[1],2);
77  }
78 #if USE_HESSVEC
79  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
80  const Real six(6);
81  hv[0] = six*x[0]*v[0];
82  hv[1] = six*x[1]*v[1];
83  }
84 #endif
85  void invHessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
86  const Real six(6);
87  hv[0] = v[0]/(six*x[0]);
88  hv[1] = v[1]/(six*x[1]);
89  }
90  };
91 
92  template<class Real>
93  class Constraint_Cubic : public StdConstraint<Real> {
94  public:
96 
97  void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
98  c[0] = std::pow(x[0],3) + x[1];
99  }
100 
101  void applyJacobian( std::vector<Real> &jv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
102  const Real three(3);
103  jv[0] = three*std::pow(x[0],2)*v[0] + v[1];
104  }
105 
106  void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
107  const Real three(3);
108  ajv[0] = three*std::pow(x[0],2)*v[0];
109  ajv[1] = v[0];
110  }
111 #if USE_HESSVEC
112  void applyAdjointHessian( std::vector<Real> &ahuv, const std::vector<Real> &u, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
113  const Real zero(0), six(6);
114  ahuv[0] = six*x[0]*u[0]*v[0];
115  ahuv[1] = zero;
116  }
117 #endif
118  };
119 
120  template<class Real>
121  class getCubic : public TestProblem<Real> {
122  private:
123  const int type_;
124 
125  public:
126  getCubic(int type = 0) : type_(type) {}
127 
128  Ptr<Objective<Real>> getObjective(void) const {
129  return makePtr<Objective_Cubic<Real>>();
130  }
131 
132  Ptr<Vector<Real>> getInitialGuess(void) const {
133  int n = 2;
134  Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(n,static_cast<Real>( 1.0));
135  Ptr<std::vector<Real>> xp = makePtr<std::vector<Real>>(n,static_cast<Real>(-0.9));
136  return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
137  }
138 
139  Ptr<Vector<Real>> getSolution(const int i = 0) const {
140  int n = 2;
141  Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(n,static_cast<Real>( 1.0));
142  Ptr<std::vector<Real>> xp = makePtr<std::vector<Real>>(n,static_cast<Real>(-1.0));
143  if (type_ == 1) {
144  const Real one(1), two(2), three(3), six(6);
145  Real x = -one/std::pow(three,one/six);
146  Real y = -std::pow(x,3);
147  (*xp)[0] = x;
148  (*xp)[1] = y;
149  }
150  if (type_ == 2) {
151  // This solution is only approximate
152  (*xp)[0] = static_cast<Real>(-0.8374930678347255);
153  (*xp)[1] = static_cast<Real>( 0.5774131462277658);
154  }
155  return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
156  }
157 
158  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
159  int n = 2;
160  Ptr<Vector<Real>> l = makePtr<StdVector<Real>>(n,-1.0);
161  Ptr<Vector<Real>> u = makePtr<StdVector<Real>>(n, 1.0);
162  return makePtr<Bounds<Real>>(l,u);
163  }
164 
165  Ptr<Constraint<Real>> getEqualityConstraint(void) const {
166  if (type_ == 1) {
167  return makePtr<Constraint_Cubic<Real>>();
168  }
169  return nullPtr;
170  }
171 
172  Ptr<Vector<Real>> getEqualityMultiplier(void) const {
173  if (type_ == 1) {
174  return makePtr<StdVector<Real>>(1,0.0);
175  }
176  return nullPtr;
177  }
178 
179  Ptr<Constraint<Real>> getInequalityConstraint(void) const {
180  if (type_ == 2) {
181  return makePtr<Constraint_Cubic<Real>>();
182  }
183  return nullPtr;
184  }
185 
186  Ptr<Vector<Real>> getInequalityMultiplier(void) const {
187  if (type_ == 2) {
188  return makePtr<StdVector<Real>>(1,0.0);
189  }
190  return nullPtr;
191  }
192 
193  Ptr<BoundConstraint<Real>> getSlackBoundConstraint(void) const {
194  if (type_ == 2) {
195  Ptr<Vector<Real>> l = makePtr<StdVector<Real>>(1,-0.01);
196  Ptr<Vector<Real>> u = makePtr<StdVector<Real>>(1, 0.01);
197  return makePtr<Bounds<Real>>(l,u);
198  }
199  return nullPtr;
200  }
201  };
202 
203 }// End ZOO Namespace
204 }// End ROL Namespace
205 
206 #endif
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_Cubic.hpp:132
Ptr< Vector< Real > > getInequalityMultiplier(void) const
Definition: ROL_Cubic.hpp:186
Ptr< Vector< Real > > getEqualityMultiplier(void) const
Definition: ROL_Cubic.hpp:172
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_Cubic.hpp:128
virtual void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
void applyJacobian(std::vector< Real > &jv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition: ROL_Cubic.hpp:101
Defines the equality constraint operator interface for StdVectors.
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Definition: ROL_Cubic.hpp:165
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Ptr< BoundConstraint< Real > > getSlackBoundConstraint(void) const
Definition: ROL_Cubic.hpp:193
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
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 ...
void value(std::vector< Real > &c, const std::vector< Real > &x, Real &tol)
Definition: ROL_Cubic.hpp:97
Contains definitions of test objective functions.
getCubic(int type=0)
Definition: ROL_Cubic.hpp:126
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Definition: ROL_Cubic.hpp:73
Ptr< Constraint< Real > > getInequalityConstraint(void) const
Definition: ROL_Cubic.hpp:179
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_Cubic.hpp:139
Real value(const std::vector< Real > &x, Real &tol)
Definition: ROL_Cubic.hpp:69
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_Cubic.hpp:158
void invHessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition: ROL_Cubic.hpp:85
void applyAdjointJacobian(std::vector< Real > &ajv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition: ROL_Cubic.hpp:106