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_Objective.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(1.5);
73  y_.push_back(2.25);
74  y_.push_back(2.625);
75  }
76 
77  Real value( const Vector<Real> &x, Real &tol ) {
78  Teuchos::RCP<const std::vector<Real> > ex
79  = Teuchos::dyn_cast<const StdVector<Real> >(x).getVector();
80 
81  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
82  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
83  Real f3 = 2.625-(*ex)[0]*(1.0-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  Teuchos::RCP<std::vector<Real> > eg
90  = Teuchos::dyn_cast<StdVector<Real> >(g).getVector();
91  Teuchos::RCP<const std::vector<Real> > ex
92  = Teuchos::dyn_cast<const StdVector<Real> >(x).getVector();
93 
94  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
95  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
96  Real f3 = 2.625-(*ex)[0]*(1.0-pow((*ex)[1],3));
97  Real df1dx = -(1.0-(*ex)[1]);
98  Real df1dy = (*ex)[0];
99  Real df2dx = -(1.0-pow((*ex)[1],2));
100  Real df2dy = 2.0*(*ex)[0]*(*ex)[1];
101  Real df3dx = -(1.0-pow((*ex)[1],3));
102  Real df3dy = 3.0*(*ex)[0]*pow((*ex)[1],2);
103 
104  (*eg)[0] = 2.0*df1dx*f1+2.0*df2dx*f2+2.0*df3dx*f3;
105  (*eg)[1] = 2.0*df1dy*f1+2.0*df2dy*f2+2.0*df3dy*f3;
106  }
107 #if USE_HESSVEC
108  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
109  Teuchos::RCP<std::vector<Real> > ehv
110  = Teuchos::dyn_cast<StdVector<Real> >(hv).getVector();
111  Teuchos::RCP<const std::vector<Real> > ev
112  = Teuchos::dyn_cast<const StdVector<Real> >(v).getVector();
113  Teuchos::RCP<const std::vector<Real> > ex
114  = Teuchos::dyn_cast<const StdVector<Real> >(x).getVector();
115 
116  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
117  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
118  Real f3 = 2.625-(*ex)[0]*(1.0-pow((*ex)[1],3));
119  Real df1dx = -(1.0-(*ex)[1]);
120  Real df1dy = (*ex)[0];
121  Real df2dx = -(1.0-pow((*ex)[1],2));
122  Real df2dy = 2.0*(*ex)[0]*(*ex)[1];
123  Real df3dx = -(1.0-pow((*ex)[1],3));
124  Real df3dy = 3.0*(*ex)[0]*pow((*ex)[1],2);
125  Real d2f1dx2 = 0.0;
126  Real d2f1dy2 = 0.0;
127  Real d2f1dxdy = 1.0;
128  Real d2f2dx2 = 0.0;
129  Real d2f2dy2 = 2.0*(*ex)[0];
130  Real d2f2dxdy = 2.0*(*ex)[1];
131  Real d2f3dx2 = 0.0;
132  Real d2f3dy2 = 6.0*(*ex)[0]*(*ex)[1];
133  Real d2f3dxdy = 3.0*pow((*ex)[1],2);
134 
135  Real H11 = 2.0*(d2f1dx2*f1+df1dx*df1dx)+2.0*(d2f2dx2*f2+df2dx*df2dx)
136  +2.0*(d2f3dx2*f3+df3dx*df3dx);
137  Real H22 = 2.0*(d2f1dy2*f1+df1dy*df1dy)+2.0*(d2f2dy2*f2+df2dy*df2dy)
138  +2.0*(d2f3dy2*f3+df3dy*df3dy);
139  Real H12 = 2.0*(d2f1dxdy*f1 + df1dx*df1dy)+2.0*(d2f2dxdy*f2 + df2dx*df2dy)
140  +2.0*(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  Teuchos::RCP<std::vector<Real> > ehv
148  = Teuchos::dyn_cast<StdVector<Real> >(hv).getVector();
149  Teuchos::RCP<const std::vector<Real> > ev
150  = Teuchos::dyn_cast<const StdVector<Real> >(v).getVector();
151  Teuchos::RCP<const std::vector<Real> > ex
152  = Teuchos::dyn_cast<const StdVector<Real> >(x).getVector();
153 
154  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
155  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
156  Real f3 = 2.625-(*ex)[0]*(1.0-pow((*ex)[1],3));
157  Real df1dx = -(1.0-(*ex)[1]);
158  Real df1dy = (*ex)[0];
159  Real df2dx = -(1.0-pow((*ex)[1],2));
160  Real df2dy = 2.0*(*ex)[0]*(*ex)[1];
161  Real df3dx = -(1.0-pow((*ex)[1],3));
162  Real df3dy = 3.0*(*ex)[0]*pow((*ex)[1],2);
163  Real d2f1dx2 = 0.0;
164  Real d2f1dy2 = 0.0;
165  Real d2f1dxdy = 1.0;
166  Real d2f2dx2 = 0.0;
167  Real d2f2dy2 = 2.0*(*ex)[0];
168  Real d2f2dxdy = 2.0*(*ex)[1];
169  Real d2f3dx2 = 0.0;
170  Real d2f3dy2 = 6.0*(*ex)[0]*(*ex)[1];
171  Real d2f3dxdy = 3.0*pow((*ex)[1],2);
172 
173  Real H11 = 2.0*(d2f1dx2*f1+df1dx*df1dx)+2.0*(d2f2dx2*f2+df2dx*df2dx)
174  +2.0*(d2f3dx2*f3+df3dx*df3dx);
175  Real H22 = 2.0*(d2f1dy2*f1+df1dy*df1dy)+2.0*(d2f2dy2*f2+df2dy*df2dy)
176  +2.0*(d2f3dy2*f3+df3dy*df3dy);
177  Real H12 = 2.0*(d2f1dxdy*f1 + df1dx*df1dy)+2.0*(d2f2dxdy*f2 + df2dx*df2dy)
178  +2.0*(d2f3dxdy*f3 + df3dx*df3dy);
179 
180  (*ehv)[0] = (1.0/(H11*H22-H12*H12))*( H22*(*ev)[0] - H12*(*ev)[1]);
181  (*ehv)[1] = (1.0/(H11*H22-H12*H12))*(-H12*(*ev)[0] + H11*(*ev)[1]);
182  }
183  };
184 
185  template<class Real>
186  void getBeale( Teuchos::RCP<Objective<Real> > &obj,
187  Teuchos::RCP<Vector<Real> > &x0,
188  Teuchos::RCP<Vector<Real> > &x ) {
189  // Problem dimension
190  int n = 2;
191 
192  // Build scale
193  Teuchos::RCP<std::vector<Real> > scale = Teuchos::rcp(new std::vector<Real>(n,0.0));
194  (*scale)[0] = 1.e-1; (*scale)[1] = 1.e1;
195 
196  // Get Initial Guess
197  Teuchos::RCP<std::vector<Real> > x0p = Teuchos::rcp(new std::vector<Real>(n,0.0));
198  (*x0p)[0] = 1.0; (*x0p)[1] = 1.0;
199  x0 = Teuchos::rcp(new PrimalScaledStdVector<Real>(x0p,scale));
200 
201  // Get Solution
202  Teuchos::RCP<std::vector<Real> > xp = Teuchos::rcp(new std::vector<Real>(n,0.0));
203  (*xp)[0] = 3.0; (*xp)[1] = 0.5;
204  x = Teuchos::rcp(new PrimalScaledStdVector<Real>(xp,scale));
205 
206  // Instantiate Objective Function
207  obj = Teuchos::rcp(new Objective_Beale<Real>);
208  }
209 
210 }// End ZOO Namespace
211 }// End ROL Namespace
212 
213 #endif
Provides the interface to evaluate objective functions.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_Beale.hpp:88
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:74
std::vector< Real > y_
Definition: ROL_Beale.hpp:67
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_Beale.hpp:77
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
void getBeale(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< Vector< Real > > &x0, Teuchos::RCP< Vector< Real > > &x)
Definition: ROL_Beale.hpp:186
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