ROL
ROL_HS2.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_HS2_HPP
54 #define ROL_HS2_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_TestProblem.hpp"
58 #include "ROL_Bounds.hpp"
59 #include "ROL_Types.hpp"
60 
61 namespace ROL {
62 namespace ZOO {
63 
66  template<class Real>
67  class Objective_HS2 : public Objective<Real> {
68 
69  typedef std::vector<Real> vector;
70  typedef Vector<Real> V;
72 
73  private:
74 
75  Ptr<const vector> getVector( const V& x ) {
76 
77  return dynamic_cast<const SV&>(x).getVector();
78  }
79 
80  Ptr<vector> getVector( V& x ) {
81 
82  return dynamic_cast<SV&>(x).getVector();
83  }
84 
85  public:
86  Objective_HS2(void) {}
87 
88  Real value( const Vector<Real> &x, Real &tol ) {
89 
90 
91  Ptr<const vector> ex = getVector(x);
92  return static_cast<Real>(100) * std::pow((*ex)[1] - std::pow((*ex)[0],2),2)
93  + std::pow(static_cast<Real>(1)-(*ex)[0],2);
94  }
95 
96  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
97 
98 
99  Ptr<const vector> ex = getVector(x);
100  Ptr<vector> eg = getVector(g);
101  (*eg)[0] = static_cast<Real>(-400) * ((*ex)[1] - std::pow((*ex)[0],2))
102  * (*ex)[0] - static_cast<Real>(2) * (static_cast<Real>(1)-(*ex)[0]);
103  (*eg)[1] = static_cast<Real>(200) * ((*ex)[1] - std::pow((*ex)[0],2));
104  }
105 #if USE_HESSVEC
106  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
107 
108 
109  Ptr<const vector> ex = getVector(x);
110  Ptr<const vector> ev = getVector(v);
111  Ptr<vector> ehv = getVector(hv);
112 
113  Real h11 = static_cast<Real>(-400) * (*ex)[1]
114  + static_cast<Real>(1200) * std::pow((*ex)[0],2)
115  + static_cast<Real>(2);
116  Real h22 = static_cast<Real>(200);
117  Real h12 = static_cast<Real>(-400) * (*ex)[0];
118  Real h21 = static_cast<Real>(-400) * (*ex)[0];
119 
120  Real alpha(0);
121 
122  (*ehv)[0] = (h11+alpha) * (*ev)[0] + h12 * (*ev)[1];
123  (*ehv)[1] = h21 * (*ev)[0] + (h22+alpha) * (*ev)[1];
124  }
125 #endif
126  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
127 
128 
129  Ptr<const vector> ex = getVector(x);
130  Ptr<const vector> ev = getVector(v);
131  Ptr< vector> ehv = getVector(hv);
132 
133  Real h11 = static_cast<Real>(-400) * (*ex)[1]
134  + static_cast<Real>(1200) * std::pow((*ex)[0],2)
135  + static_cast<Real>(2);
136  Real h22 = static_cast<Real>(200);
137  Real h12 = static_cast<Real>(-400) * (*ex)[0];
138  Real h21 = static_cast<Real>(-400) * (*ex)[0];
139 
140  (*ehv)[0] = static_cast<Real>(1)/(h11*h22 - h12*h21)
141  * (h22 * (*ev)[0] - h12 * (*ev)[1]);
142  (*ehv)[1] = static_cast<Real>(1)/(h11*h22 - h12*h21)
143  * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
144  }
145  };
146 
147 template<class Real>
148 class getHS2 : public TestProblem<Real> {
149 public:
150  getHS2(void) {}
151 
152  Ptr<Objective<Real>> getObjective(void) const {
153  // Instantiate Objective Function
154  return makePtr<Objective_HS2<Real>>();
155  }
156 
157  Ptr<Vector<Real>> getInitialGuess(void) const {
158  // Problem dimension
159  int n = 2;
160  // Get Initial Guess
161  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,0.0);
162  (*x0p)[0] = -2.0; (*x0p)[1] = 1.0;
163  return makePtr<StdVector<Real>>(x0p);
164  }
165 
166  Ptr<Vector<Real>> getSolution(const int i = 0) const {
167  // Problem dimension
168  int n = 2;
169  // Get Solution
170  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,0.0);
171  Real a = std::sqrt(598.0/1200.0);
172  Real b = 400.0 * std::pow(a,3.0);
173  (*xp)[0] = 2.0*a*std::cos(1.0/3.0 * std::acos(1.0/b));
174  (*xp)[1] = 1.5;
175  return makePtr<StdVector<Real>>(xp);
176  }
177 
178  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
179  // Problem dimension
180  int n = 2;
181  // Instantiate BoundConstraint
182  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(n,0.0);
183  (*lp)[0] = ROL_NINF<Real>(); (*lp)[1] = 1.5;
184  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
185  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(n,0.0);
186  (*up)[0] = ROL_INF<Real>(); (*up)[1] = ROL_INF<Real>();
187  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
188  return makePtr<Bounds<Real>>(l,u);
189  }
190 };
191 
192 } // End ZOO Namespace
193 } // End ROL Namespace
194 
195 #endif
Provides the interface to evaluate objective functions.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Contains definitions of custom data types in ROL.
Ptr< const vector > getVector(const V &x)
Definition: ROL_HS2.hpp:75
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS2.hpp:96
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS2.hpp:157
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS2.hpp:152
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Vector< Real > V
Definition: ROL_HS2.hpp:70
StdVector< Real > SV
Definition: ROL_HS2.hpp:71
Ptr< vector > getVector(V &x)
Definition: ROL_HS2.hpp:80
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Definition: ROL_HS2.hpp:126
Contains definitions of test objective functions.
std::vector< Real > vector
Definition: ROL_HS2.hpp:69
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_HS2.hpp:166
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS2.hpp:88
W. Hock and K. Schittkowski 2nd test function.
Definition: ROL_HS2.hpp:67
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS2.hpp:178