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_StdVector.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 
67  typedef std::vector<Real> vector;
68  typedef Vector<Real> V;
69  typedef StdVector<Real> SV;
70 
71  private:
72  vector y_;
73 
74  Teuchos::RCP<const vector> getVector( const V& x ) {
75  using Teuchos::dyn_cast;
76  return dyn_cast<const SV>(x).getVector();
77  }
78 
79  Teuchos::RCP<vector> getVector( V& x ) {
80  using Teuchos::dyn_cast;
81  return dyn_cast<SV>(x).getVector();
82  }
83 
84  public:
86  y_.clear();
87  y_.push_back(1.5);
88  y_.push_back(2.25);
89  y_.push_back(2.625);
90  }
91 
92  Real value( const Vector<Real> &x, Real &tol ) {
93 
94  using Teuchos::RCP;
95 
96  RCP<const vector> ex = getVector(x);
97 
98  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
99  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
100  Real f3 = 2.625-(*ex)[0]*(1.0-pow((*ex)[1],3));
101 
102  return pow(f1,2)+pow(f2,2)+pow(f3,2);
103  }
104 
105  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
106 
107  using Teuchos::RCP;
108 
109  RCP<const vector> ex = getVector(x);
110  RCP<vector> eg = getVector(g);
111 
112  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
113  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
114  Real f3 = 2.625-(*ex)[0]*(1.0-pow((*ex)[1],3));
115  Real df1dx = -(1.0-(*ex)[1]);
116  Real df1dy = (*ex)[0];
117  Real df2dx = -(1.0-pow((*ex)[1],2));
118  Real df2dy = 2.0*(*ex)[0]*(*ex)[1];
119  Real df3dx = -(1.0-pow((*ex)[1],3));
120  Real df3dy = 3.0*(*ex)[0]*pow((*ex)[1],2);
121 
122  (*eg)[0] = 2.0*df1dx*f1+2.0*df2dx*f2+2.0*df3dx*f3;
123  (*eg)[1] = 2.0*df1dy*f1+2.0*df2dy*f2+2.0*df3dy*f3;
124  }
125 #if USE_HESSVEC
126  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
127 
128  using Teuchos::RCP;
129  RCP<const vector> ex = getVector(x);
130  RCP<const vector> ev = getVector(v);
131  RCP<vector> ehv = getVector(hv);
132 
133  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
134  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
135  Real f3 = 2.625-(*ex)[0]*(1.0-pow((*ex)[1],3));
136  Real df1dx = -(1.0-(*ex)[1]);
137  Real df1dy = (*ex)[0];
138  Real df2dx = -(1.0-pow((*ex)[1],2));
139  Real df2dy = 2.0*(*ex)[0]*(*ex)[1];
140  Real df3dx = -(1.0-pow((*ex)[1],3));
141  Real df3dy = 3.0*(*ex)[0]*pow((*ex)[1],2);
142  Real d2f1dx2 = 0.0;
143  Real d2f1dy2 = 0.0;
144  Real d2f1dxdy = 1.0;
145  Real d2f2dx2 = 0.0;
146  Real d2f2dy2 = 2.0*(*ex)[0];
147  Real d2f2dxdy = 2.0*(*ex)[1];
148  Real d2f3dx2 = 0.0;
149  Real d2f3dy2 = 6.0*(*ex)[0]*(*ex)[1];
150  Real d2f3dxdy = 3.0*pow((*ex)[1],2);
151 
152  Real H11 = 2.0*(d2f1dx2*f1+df1dx*df1dx)+2.0*(d2f2dx2*f2+df2dx*df2dx)
153  +2.0*(d2f3dx2*f3+df3dx*df3dx);
154  Real H22 = 2.0*(d2f1dy2*f1+df1dy*df1dy)+2.0*(d2f2dy2*f2+df2dy*df2dy)
155  +2.0*(d2f3dy2*f3+df3dy*df3dy);
156  Real H12 = 2.0*(d2f1dxdy*f1 + df1dx*df1dy)+2.0*(d2f2dxdy*f2 + df2dx*df2dy)
157  +2.0*(d2f3dxdy*f3 + df3dx*df3dy);
158 
159  (*ehv)[0] = H11*(*ev)[0]+H12*(*ev)[1];
160  (*ehv)[1] = H12*(*ev)[0]+H22*(*ev)[1];
161  }
162 #endif
163  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
164 
165  using Teuchos::RCP;
166 
167  RCP<const vector> ex = getVector(x);
168  RCP<const vector> ev = getVector(v);
169  RCP<vector> ehv = getVector(hv);
170 
171  Real f1 = 1.5-(*ex)[0]*(1.0-(*ex)[1]);
172  Real f2 = 2.25-(*ex)[0]*(1.0-pow((*ex)[1],2));
173  Real f3 = 2.625-(*ex)[0]*(1.0-pow((*ex)[1],3));
174  Real df1dx = -(1.0-(*ex)[1]);
175  Real df1dy = (*ex)[0];
176  Real df2dx = -(1.0-pow((*ex)[1],2));
177  Real df2dy = 2.0*(*ex)[0]*(*ex)[1];
178  Real df3dx = -(1.0-pow((*ex)[1],3));
179  Real df3dy = 3.0*(*ex)[0]*pow((*ex)[1],2);
180  Real d2f1dx2 = 0.0;
181  Real d2f1dy2 = 0.0;
182  Real d2f1dxdy = 1.0;
183  Real d2f2dx2 = 0.0;
184  Real d2f2dy2 = 2.0*(*ex)[0];
185  Real d2f2dxdy = 2.0*(*ex)[1];
186  Real d2f3dx2 = 0.0;
187  Real d2f3dy2 = 6.0*(*ex)[0]*(*ex)[1];
188  Real d2f3dxdy = 3.0*pow((*ex)[1],2);
189 
190  Real H11 = 2.0*(d2f1dx2*f1+df1dx*df1dx)+2.0*(d2f2dx2*f2+df2dx*df2dx)
191  +2.0*(d2f3dx2*f3+df3dx*df3dx);
192  Real H22 = 2.0*(d2f1dy2*f1+df1dy*df1dy)+2.0*(d2f2dy2*f2+df2dy*df2dy)
193  +2.0*(d2f3dy2*f3+df3dy*df3dy);
194  Real H12 = 2.0*(d2f1dxdy*f1 + df1dx*df1dy)+2.0*(d2f2dxdy*f2 + df2dx*df2dy)
195  +2.0*(d2f3dxdy*f3 + df3dx*df3dy);
196 
197  (*ehv)[0] = (1.0/(H11*H22-H12*H12))*( H22*(*ev)[0] - H12*(*ev)[1]);
198  (*ehv)[1] = (1.0/(H11*H22-H12*H12))*(-H12*(*ev)[0] + H11*(*ev)[1]);
199  }
200  };
201 
202  template<class Real>
203  void getBeale( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
204 
205  typedef std::vector<Real> vector;
206  typedef StdVector<Real> SV;
207 
208  typedef typename vector::size_type uint;
209 
210  using Teuchos::RCP;
211  using Teuchos::dyn_cast;
212 
213  // Cast Initial Guess and Solution Vectors
214  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
215  RCP<vector> xp = dyn_cast<SV>(x).getVector();
216 
217  uint n = xp->size();
218 
219  // Resize Vectors
220  n = 2;
221  x0p->resize(n);
222  xp->resize(n);
223  // Instantiate Objective Function
224  obj = Teuchos::rcp( new Objective_Beale<Real> );
225  // Get Initial Guess
226  (*x0p)[0] = 1.0;
227  (*x0p)[1] = 1.0;
228  // Get Solution
229  (*xp)[0] = 3.0;
230  (*xp)[1] = 0.5;
231  }
232 
233 
234 }// End ZOO Namespace
235 }// End ROL Namespace
236 
237 #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:105
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
Provides the std::vector implementation of the ROL::Vector interface.
Teuchos::RCP< vector > getVector(V &x)
Definition: ROL_Beale.hpp:79
StdVector< Real > SV
Definition: ROL_Beale.hpp:69
Teuchos::RCP< const vector > getVector(const V &x)
Definition: ROL_Beale.hpp:74
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_Beale.hpp:92
void getBeale(Teuchos::RCP< Objective< Real > > &obj, Vector< Real > &x0, Vector< Real > &x)
Definition: ROL_Beale.hpp:203
std::vector< Real > vector
Definition: ROL_Beale.hpp:67
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:163