ROL
ROL_Beale.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 
15 #ifndef USE_HESSVEC
16 #define USE_HESSVEC 1
17 #endif
18 
19 #ifndef ROL_BEALE_HPP
20 #define ROL_BEALE_HPP
21 
22 #include "ROL_ScaledStdVector.hpp"
23 #include "ROL_TestProblem.hpp"
24 
25 namespace ROL {
26 namespace ZOO {
27 
30  template<class Real>
31  class Objective_Beale : public Objective<Real> {
32  private:
33  std::vector<Real> y_;
34 
35  public:
37  y_.clear();
38  y_.push_back(static_cast<Real>(1.5));
39  y_.push_back(static_cast<Real>(2.25));
40  y_.push_back(static_cast<Real>(2.625));
41  }
42 
43  Real value( const Vector<Real> &x, Real &tol ) {
44  Ptr<const std::vector<Real> > ex
45  = dynamic_cast<const StdVector<Real>&>(x).getVector();
46 
47  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
48  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
49  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
50 
51  return pow(f1,2)+pow(f2,2)+pow(f3,2);
52  }
53 
54  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
55  Ptr<std::vector<Real> > eg
56  = dynamic_cast<StdVector<Real>&>(g).getVector();
57  Ptr<const std::vector<Real> > ex
58  = dynamic_cast<const StdVector<Real>&>(x).getVector();
59 
60  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
61  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
62  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
63  Real df1dx = -(static_cast<Real>(1)-(*ex)[1]);
64  Real df1dy = (*ex)[0];
65  Real df2dx = -(static_cast<Real>(1)-pow((*ex)[1],2));
66  Real df2dy = static_cast<Real>(2)*(*ex)[0]*(*ex)[1];
67  Real df3dx = -(static_cast<Real>(1)-pow((*ex)[1],3));
68  Real df3dy = static_cast<Real>(3)*(*ex)[0]*pow((*ex)[1],2);
69 
70  (*eg)[0] = static_cast<Real>(2)*df1dx*f1+static_cast<Real>(2)*df2dx*f2+static_cast<Real>(2)*df3dx*f3;
71  (*eg)[1] = static_cast<Real>(2)*df1dy*f1+static_cast<Real>(2)*df2dy*f2+static_cast<Real>(2)*df3dy*f3;
72  }
73 #if USE_HESSVEC
74  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
75  Ptr<std::vector<Real> > ehv
76  = dynamic_cast<StdVector<Real>&>(hv).getVector();
77  Ptr<const std::vector<Real> > ev
78  = dynamic_cast<const StdVector<Real>&>(v).getVector();
79  Ptr<const std::vector<Real> > ex
80  = dynamic_cast<const StdVector<Real>&>(x).getVector();
81 
82  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
83  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
84  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
85  Real df1dx = -(static_cast<Real>(1)-(*ex)[1]);
86  Real df1dy = (*ex)[0];
87  Real df2dx = -(static_cast<Real>(1)-pow((*ex)[1],2));
88  Real df2dy = static_cast<Real>(2)*(*ex)[0]*(*ex)[1];
89  Real df3dx = -(static_cast<Real>(1)-pow((*ex)[1],3));
90  Real df3dy = static_cast<Real>(3)*(*ex)[0]*pow((*ex)[1],2);
91  Real d2f1dx2 = static_cast<Real>(0);
92  Real d2f1dy2 = static_cast<Real>(0);
93  Real d2f1dxdy = static_cast<Real>(1);
94  Real d2f2dx2 = static_cast<Real>(0);
95  Real d2f2dy2 = static_cast<Real>(2)*(*ex)[0];
96  Real d2f2dxdy = static_cast<Real>(2)*(*ex)[1];
97  Real d2f3dx2 = static_cast<Real>(0);
98  Real d2f3dy2 = static_cast<Real>(6)*(*ex)[0]*(*ex)[1];
99  Real d2f3dxdy = static_cast<Real>(3)*pow((*ex)[1],2);
100 
101  Real H11 = static_cast<Real>(2)*(d2f1dx2*f1+df1dx*df1dx)+static_cast<Real>(2)*(d2f2dx2*f2+df2dx*df2dx)
102  +static_cast<Real>(2)*(d2f3dx2*f3+df3dx*df3dx);
103  Real H22 = static_cast<Real>(2)*(d2f1dy2*f1+df1dy*df1dy)+static_cast<Real>(2)*(d2f2dy2*f2+df2dy*df2dy)
104  +static_cast<Real>(2)*(d2f3dy2*f3+df3dy*df3dy);
105  Real H12 = static_cast<Real>(2)*(d2f1dxdy*f1 + df1dx*df1dy)+static_cast<Real>(2)*(d2f2dxdy*f2 + df2dx*df2dy)
106  +static_cast<Real>(2)*(d2f3dxdy*f3 + df3dx*df3dy);
107 
108  (*ehv)[0] = H11*(*ev)[0]+H12*(*ev)[1];
109  (*ehv)[1] = H12*(*ev)[0]+H22*(*ev)[1];
110  }
111 #endif
112  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
113  Ptr<std::vector<Real> > ehv
114  = dynamic_cast<StdVector<Real>&>(hv).getVector();
115  Ptr<const std::vector<Real> > ev
116  = dynamic_cast<const StdVector<Real>&>(v).getVector();
117  Ptr<const std::vector<Real> > ex
118  = dynamic_cast<const StdVector<Real>&>(x).getVector();
119 
120  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
121  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
122  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
123  Real df1dx = -(static_cast<Real>(1)-(*ex)[1]);
124  Real df1dy = (*ex)[0];
125  Real df2dx = -(static_cast<Real>(1)-pow((*ex)[1],2));
126  Real df2dy = static_cast<Real>(2)*(*ex)[0]*(*ex)[1];
127  Real df3dx = -(static_cast<Real>(1)-pow((*ex)[1],3));
128  Real df3dy = static_cast<Real>(3)*(*ex)[0]*pow((*ex)[1],2);
129  Real d2f1dx2 = static_cast<Real>(0);
130  Real d2f1dy2 = static_cast<Real>(0);
131  Real d2f1dxdy = static_cast<Real>(1);
132  Real d2f2dx2 = static_cast<Real>(0);
133  Real d2f2dy2 = static_cast<Real>(2)*(*ex)[0];
134  Real d2f2dxdy = static_cast<Real>(2)*(*ex)[1];
135  Real d2f3dx2 = static_cast<Real>(0);
136  Real d2f3dy2 = static_cast<Real>(6)*(*ex)[0]*(*ex)[1];
137  Real d2f3dxdy = static_cast<Real>(3)*pow((*ex)[1],2);
138 
139  Real H11 = static_cast<Real>(2)*(d2f1dx2*f1+df1dx*df1dx)+static_cast<Real>(2)*(d2f2dx2*f2+df2dx*df2dx)
140  +static_cast<Real>(2)*(d2f3dx2*f3+df3dx*df3dx);
141  Real H22 = static_cast<Real>(2)*(d2f1dy2*f1+df1dy*df1dy)+static_cast<Real>(2)*(d2f2dy2*f2+df2dy*df2dy)
142  +static_cast<Real>(2)*(d2f3dy2*f3+df3dy*df3dy);
143  Real H12 = static_cast<Real>(2)*(d2f1dxdy*f1 + df1dx*df1dy)+static_cast<Real>(2)*(d2f2dxdy*f2 + df2dx*df2dy)
144  +static_cast<Real>(2)*(d2f3dxdy*f3 + df3dx*df3dy);
145 
146  (*ehv)[0] = (static_cast<Real>(1)/(H11*H22-H12*H12))*( H22*(*ev)[0] - H12*(*ev)[1]);
147  (*ehv)[1] = (static_cast<Real>(1)/(H11*H22-H12*H12))*(-H12*(*ev)[0] + H11*(*ev)[1]);
148  }
149  };
150 
151  template<class Real>
152  class getBeale : public TestProblem<Real> {
153  public:
154  getBeale(void) {}
155 
156  Ptr<Objective<Real>> getObjective(void) const {
157  return makePtr<Objective_Beale<Real>>();
158  }
159 
160  Ptr<Vector<Real>> getInitialGuess(void) const {
161  int n = 2;
162  // Build scale
163  Ptr<std::vector<Real> > scale = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
164  (*scale)[0] = static_cast<Real>(1.e-1);
165  (*scale)[1] = static_cast<Real>(1.e1);
166 
167  // Get Initial Guess
168  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
169  (*x0p)[0] = static_cast<Real>(1);
170  (*x0p)[1] = static_cast<Real>(1);
171 
172  return makePtr<PrimalScaledStdVector<Real>>(x0p,scale);
173  }
174 
175  Ptr<Vector<Real>> getSolution(const int i = 0) const {
176  int n = 2;
177  // Build scale
178  Ptr<std::vector<Real> > scale = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
179  (*scale)[0] = static_cast<Real>(1.e-1);
180  (*scale)[1] = static_cast<Real>(1.e1);
181 
182  // Get Solution
183  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
184  (*xp)[0] = static_cast<Real>(3);
185  (*xp)[1] = static_cast<Real>(0.5);
186 
187  return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
188  }
189  };
190 
191 }// End ZOO Namespace
192 }// End ROL Namespace
193 
194 #endif
Provides the interface to evaluate objective functions.
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_Beale.hpp:160
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_Beale.hpp:54
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_Beale.hpp:156
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
std::vector< Real > y_
Definition: ROL_Beale.hpp:33
Contains definitions of test objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_Beale.hpp:43
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_Beale.hpp:175
Beale&#39;s function.
Definition: ROL_Beale.hpp:31
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Definition: ROL_Beale.hpp:112