ROL
ROL_FreudensteinRoth.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_FREUDENSTEINROTH_HPP
54 #define ROL_FREUDENSTEINROTH_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_FreudensteinRoth : public Objective<Real> {
66 
67  typedef std::vector<Real> vector;
68  typedef Vector<Real> V;
70 
71  private:
72 
73  Teuchos::RCP<const vector> getVector( const V& x ) {
74  using Teuchos::dyn_cast;
75  return dyn_cast<const SV>(x).getVector();
76  }
77 
78  Teuchos::RCP<vector> getVector( V& x ) {
79  using Teuchos::dyn_cast;
80  return dyn_cast<SV>(x).getVector();
81  }
82 
83  public:
85 
86  Real value( const Vector<Real> &x, Real &tol ) {
87  using Teuchos::RCP;
88 
89  RCP<const vector> xp = getVector(x);
90 
91  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
92  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
93 
94  return f1*f1+f2*f2;
95  }
96 
97  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
98 
99  using Teuchos::RCP;
100  RCP<const vector> xp = getVector(x);
101  RCP<vector> gp = getVector(g);
102 
103  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
104  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
105 
106  Real f11 = 1.0;
107  Real f12 = 10.0*(*xp)[1] - 3.0*(*xp)[1]*(*xp)[1] - 2.0;
108  Real f21 = 1.0;
109  Real f22 = 3.0*(*xp)[1]*(*xp)[1] + 2.0*(*xp)[1] - 14.0;
110 
111  (*gp)[0] = 2.0*(f11*f1 + f21*f2);
112  (*gp)[1] = 2.0*(f12*f1 + f22*f2);
113  }
114 #if USE_HESSVEC
115  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
116 
117  using Teuchos::RCP;
118  RCP<const vector> xp = getVector(x);
119  RCP<const vector> vp = getVector(v);
120  RCP<vector> hvp = getVector(hv);
121 
122  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
123  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
124 
125  Real f11 = 1.0;
126  Real f12 = 10.0*(*xp)[1] - 3.0*(*xp)[1]*(*xp)[1] - 2.0;
127  Real f21 = 1.0;
128  Real f22 = 3.0*(*xp)[1]*(*xp)[1] + 2.0*(*xp)[1] - 14.0;
129 
130  Real f122 = 10.0 - 6.0*(*xp)[1];
131  Real f222 = 6.0*(*xp)[1] + 2.0;
132 
133  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
134  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
135  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
136 
137  (*hvp)[0] = h11*(*vp)[0] + h12*(*vp)[1];
138  (*hvp)[1] = h12*(*vp)[0] + h22*(*vp)[1];
139  }
140 #endif
141  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
142 
143  using Teuchos::RCP;
144  RCP<const vector> xp = getVector(x);
145  RCP<const vector> vp = getVector(v);
146  RCP<vector> hvp = getVector(hv);
147 
148  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
149  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
150 
151  Real f11 = 1.0;
152  Real f12 = 10.0*(*xp)[1] - 3.0*(*xp)[1]*(*xp)[1] - 2.0;
153  Real f21 = 1.0;
154  Real f22 = 3.0*(*xp)[1]*(*xp)[1] + 2.0*(*xp)[1] - 14.0;
155 
156  Real f122 = 10.0 - 6.0*(*xp)[1];
157  Real f222 = 6.0*(*xp)[1] + 2.0;
158 
159  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
160  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
161  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
162 
163  (*hvp)[0] = (1.0/(h11*h22-h12*h12))*( h22*(*vp)[0] - h12*(*vp)[1]);
164  (*hvp)[1] = (1.0/(h11*h22-h12*h12))*(-h12*(*vp)[0] + h11*(*vp)[1]);
165  }
166  };
167 
168  template<class Real>
169  void getFreudensteinRoth( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
170 
171  typedef std::vector<Real> vector;
172  typedef StdVector<Real> SV;
173  using Teuchos::RCP;
174  using Teuchos::dyn_cast;
175 
176  // Cast Initial Guess and Solution Vectors
177  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
178  RCP<vector> xp = dyn_cast<SV>(x).getVector();
179 
180  int n = xp->size();
181  // Resize Vectors
182  n = 2;
183  x0p->resize(n);
184  xp->resize(n);
185  // Instantiate Objective Function
186  obj = Teuchos::rcp( new Objective_FreudensteinRoth<Real> );
187  // Get Initial Guess
188  (*x0p)[0] = 0.5;
189  (*x0p)[1] = -2.0;
190  // Get Solution
191  (*xp)[0] = 5.0;
192  (*xp)[1] = 4.0;
193  }
194 
195 
196 } // End ZOO Namespace
197 } // End ROL Namespace
198 
199 #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.
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)
Freudenstein and Roth&#39;s function.
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Teuchos::RCP< const vector > getVector(const V &x)
void getFreudensteinRoth(Teuchos::RCP< Objective< Real > > &obj, Vector< Real > &x0, Vector< Real > &x)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.