ROL
ROL_Beale.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_BEALE_HPP
54 #define ROL_BEALE_HPP
55 
56 #include "ROL_ScaledStdVector.hpp"
57 #include "ROL_TestProblem.hpp"
58 
59 namespace ROL {
60 namespace ZOO {
61 
64  template<class Real>
65  class Objective_Beale : public Objective<Real> {
66  private:
67  std::vector<Real> y_;
68 
69  public:
71  y_.clear();
72  y_.push_back(static_cast<Real>(1.5));
73  y_.push_back(static_cast<Real>(2.25));
74  y_.push_back(static_cast<Real>(2.625));
75  }
76 
77  Real value( const Vector<Real> &x, Real &tol ) {
78  Ptr<const std::vector<Real> > ex
79  = dynamic_cast<const StdVector<Real>&>(x).getVector();
80 
81  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
82  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
83  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
84 
85  return pow(f1,2)+pow(f2,2)+pow(f3,2);
86  }
87 
88  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
89  Ptr<std::vector<Real> > eg
90  = dynamic_cast<StdVector<Real>&>(g).getVector();
91  Ptr<const std::vector<Real> > ex
92  = dynamic_cast<const StdVector<Real>&>(x).getVector();
93 
94  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
95  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
96  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
97  Real df1dx = -(static_cast<Real>(1)-(*ex)[1]);
98  Real df1dy = (*ex)[0];
99  Real df2dx = -(static_cast<Real>(1)-pow((*ex)[1],2));
100  Real df2dy = static_cast<Real>(2)*(*ex)[0]*(*ex)[1];
101  Real df3dx = -(static_cast<Real>(1)-pow((*ex)[1],3));
102  Real df3dy = static_cast<Real>(3)*(*ex)[0]*pow((*ex)[1],2);
103 
104  (*eg)[0] = static_cast<Real>(2)*df1dx*f1+static_cast<Real>(2)*df2dx*f2+static_cast<Real>(2)*df3dx*f3;
105  (*eg)[1] = static_cast<Real>(2)*df1dy*f1+static_cast<Real>(2)*df2dy*f2+static_cast<Real>(2)*df3dy*f3;
106  }
107 #if USE_HESSVEC
108  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
109  Ptr<std::vector<Real> > ehv
110  = dynamic_cast<StdVector<Real>&>(hv).getVector();
111  Ptr<const std::vector<Real> > ev
112  = dynamic_cast<const StdVector<Real>&>(v).getVector();
113  Ptr<const std::vector<Real> > ex
114  = dynamic_cast<const StdVector<Real>&>(x).getVector();
115 
116  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
117  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
118  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
119  Real df1dx = -(static_cast<Real>(1)-(*ex)[1]);
120  Real df1dy = (*ex)[0];
121  Real df2dx = -(static_cast<Real>(1)-pow((*ex)[1],2));
122  Real df2dy = static_cast<Real>(2)*(*ex)[0]*(*ex)[1];
123  Real df3dx = -(static_cast<Real>(1)-pow((*ex)[1],3));
124  Real df3dy = static_cast<Real>(3)*(*ex)[0]*pow((*ex)[1],2);
125  Real d2f1dx2 = static_cast<Real>(0);
126  Real d2f1dy2 = static_cast<Real>(0);
127  Real d2f1dxdy = static_cast<Real>(1);
128  Real d2f2dx2 = static_cast<Real>(0);
129  Real d2f2dy2 = static_cast<Real>(2)*(*ex)[0];
130  Real d2f2dxdy = static_cast<Real>(2)*(*ex)[1];
131  Real d2f3dx2 = static_cast<Real>(0);
132  Real d2f3dy2 = static_cast<Real>(6)*(*ex)[0]*(*ex)[1];
133  Real d2f3dxdy = static_cast<Real>(3)*pow((*ex)[1],2);
134 
135  Real H11 = static_cast<Real>(2)*(d2f1dx2*f1+df1dx*df1dx)+static_cast<Real>(2)*(d2f2dx2*f2+df2dx*df2dx)
136  +static_cast<Real>(2)*(d2f3dx2*f3+df3dx*df3dx);
137  Real H22 = static_cast<Real>(2)*(d2f1dy2*f1+df1dy*df1dy)+static_cast<Real>(2)*(d2f2dy2*f2+df2dy*df2dy)
138  +static_cast<Real>(2)*(d2f3dy2*f3+df3dy*df3dy);
139  Real H12 = static_cast<Real>(2)*(d2f1dxdy*f1 + df1dx*df1dy)+static_cast<Real>(2)*(d2f2dxdy*f2 + df2dx*df2dy)
140  +static_cast<Real>(2)*(d2f3dxdy*f3 + df3dx*df3dy);
141 
142  (*ehv)[0] = H11*(*ev)[0]+H12*(*ev)[1];
143  (*ehv)[1] = H12*(*ev)[0]+H22*(*ev)[1];
144  }
145 #endif
146  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
147  Ptr<std::vector<Real> > ehv
148  = dynamic_cast<StdVector<Real>&>(hv).getVector();
149  Ptr<const std::vector<Real> > ev
150  = dynamic_cast<const StdVector<Real>&>(v).getVector();
151  Ptr<const std::vector<Real> > ex
152  = dynamic_cast<const StdVector<Real>&>(x).getVector();
153 
154  Real f1 = static_cast<Real>(1.5)-(*ex)[0]*(static_cast<Real>(1)-(*ex)[1]);
155  Real f2 = static_cast<Real>(2.25)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],2));
156  Real f3 = static_cast<Real>(2.625)-(*ex)[0]*(static_cast<Real>(1)-pow((*ex)[1],3));
157  Real df1dx = -(static_cast<Real>(1)-(*ex)[1]);
158  Real df1dy = (*ex)[0];
159  Real df2dx = -(static_cast<Real>(1)-pow((*ex)[1],2));
160  Real df2dy = static_cast<Real>(2)*(*ex)[0]*(*ex)[1];
161  Real df3dx = -(static_cast<Real>(1)-pow((*ex)[1],3));
162  Real df3dy = static_cast<Real>(3)*(*ex)[0]*pow((*ex)[1],2);
163  Real d2f1dx2 = static_cast<Real>(0);
164  Real d2f1dy2 = static_cast<Real>(0);
165  Real d2f1dxdy = static_cast<Real>(1);
166  Real d2f2dx2 = static_cast<Real>(0);
167  Real d2f2dy2 = static_cast<Real>(2)*(*ex)[0];
168  Real d2f2dxdy = static_cast<Real>(2)*(*ex)[1];
169  Real d2f3dx2 = static_cast<Real>(0);
170  Real d2f3dy2 = static_cast<Real>(6)*(*ex)[0]*(*ex)[1];
171  Real d2f3dxdy = static_cast<Real>(3)*pow((*ex)[1],2);
172 
173  Real H11 = static_cast<Real>(2)*(d2f1dx2*f1+df1dx*df1dx)+static_cast<Real>(2)*(d2f2dx2*f2+df2dx*df2dx)
174  +static_cast<Real>(2)*(d2f3dx2*f3+df3dx*df3dx);
175  Real H22 = static_cast<Real>(2)*(d2f1dy2*f1+df1dy*df1dy)+static_cast<Real>(2)*(d2f2dy2*f2+df2dy*df2dy)
176  +static_cast<Real>(2)*(d2f3dy2*f3+df3dy*df3dy);
177  Real H12 = static_cast<Real>(2)*(d2f1dxdy*f1 + df1dx*df1dy)+static_cast<Real>(2)*(d2f2dxdy*f2 + df2dx*df2dy)
178  +static_cast<Real>(2)*(d2f3dxdy*f3 + df3dx*df3dy);
179 
180  (*ehv)[0] = (static_cast<Real>(1)/(H11*H22-H12*H12))*( H22*(*ev)[0] - H12*(*ev)[1]);
181  (*ehv)[1] = (static_cast<Real>(1)/(H11*H22-H12*H12))*(-H12*(*ev)[0] + H11*(*ev)[1]);
182  }
183  };
184 
185  template<class Real>
186  class getBeale : public TestProblem<Real> {
187  public:
188  getBeale(void) {}
189 
190  Ptr<Objective<Real>> getObjective(void) const {
191  return makePtr<Objective_Beale<Real>>();
192  }
193 
194  Ptr<Vector<Real>> getInitialGuess(void) const {
195  int n = 2;
196  // Build scale
197  Ptr<std::vector<Real> > scale = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
198  (*scale)[0] = static_cast<Real>(1.e-1);
199  (*scale)[1] = static_cast<Real>(1.e1);
200 
201  // Get Initial Guess
202  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
203  (*x0p)[0] = static_cast<Real>(1);
204  (*x0p)[1] = static_cast<Real>(1);
205 
206  return makePtr<PrimalScaledStdVector<Real>>(x0p,scale);
207  }
208 
209  Ptr<Vector<Real>> getSolution(const int i = 0) const {
210  int n = 2;
211  // Build scale
212  Ptr<std::vector<Real> > scale = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
213  (*scale)[0] = static_cast<Real>(1.e-1);
214  (*scale)[1] = static_cast<Real>(1.e1);
215 
216  // Get Solution
217  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,static_cast<Real>(0));
218  (*xp)[0] = static_cast<Real>(3);
219  (*xp)[1] = static_cast<Real>(0.5);
220 
221  return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
222  }
223  };
224 
225 }// End ZOO Namespace
226 }// End ROL Namespace
227 
228 #endif
Provides the interface to evaluate objective functions.
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_Beale.hpp:194
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_Beale.hpp:88
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_Beale.hpp:190
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:80
std::vector< Real > y_
Definition: ROL_Beale.hpp:67
Contains definitions of test objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_Beale.hpp:77
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_Beale.hpp:209
Beale&#39;s function.
Definition: ROL_Beale.hpp:65
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:146