ROL
ROL_Powell.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_POWELL_HPP
54 #define ROL_POWELL_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_Powell : public Objective<Real> {
66 
67  typedef std::vector<Real> vector;
68  typedef Vector<Real> V;
69  typedef StdVector<Real> SV;
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 
88  using Teuchos::RCP;
89  RCP<const vector> xp = getVector(x);
90 
91  Real f1 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
92  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
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 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
104  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
105 
106  Real f11 = 1.e4*(*xp)[1];
107  Real f12 = 1.e4*(*xp)[0];
108  Real f21 = -std::exp(-(*xp)[0]);
109  Real f22 = -std::exp(-(*xp)[1]);
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 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
123  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
124 
125  Real f11 = 1.e4*(*xp)[1];
126  Real f12 = 1.e4*(*xp)[0];
127  Real f21 = -std::exp(-(*xp)[0]);
128  Real f22 = -std::exp(-(*xp)[1]);
129 
130  Real f111 = 0.0;
131  Real f112 = 1.e4;
132  Real f121 = 1.e4;
133  Real f122 = 0.0;
134  Real f211 = std::exp(-(*xp)[0]);
135  Real f212 = 0.0;
136  Real f221 = 0.0;
137  Real f222 = std::exp(-(*xp)[1]);
138 
139  Real h11 = 2.0*(f111*f1 + f11*f11) + 2.0*(f211*f2 + f21*f21);
140  Real h12 = 2.0*(f112*f1 + f11*f12) + 2.0*(f212*f2 + f21*f22);
141  Real h21 = 2.0*(f121*f1 + f21*f11) + 2.0*(f221*f2 + f22*f21);
142  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
143 
144  (*hvp)[0] = h11*(*vp)[0] + h12*(*vp)[1];
145  (*hvp)[1] = h21*(*vp)[0] + h22*(*vp)[1];
146  }
147 #endif
148  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
149 
150  using Teuchos::RCP;
151  RCP<const vector> xp = getVector(x);
152  RCP<const vector> vp = getVector(v);
153  RCP<vector> hvp = getVector(hv);
154 
155  Real f1 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
156  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
157 
158  Real f11 = 1.e4*(*xp)[1];
159  Real f12 = 1.e4*(*xp)[0];
160  Real f21 = -std::exp(-(*xp)[0]);
161  Real f22 = -std::exp(-(*xp)[1]);
162 
163  Real f111 = 0.0;
164  Real f112 = 1.e4;
165  Real f121 = 1.e4;
166  Real f122 = 0.0;
167  Real f211 = std::exp(-(*xp)[0]);
168  Real f212 = 0.0;
169  Real f221 = 0.0;
170  Real f222 = std::exp(-(*xp)[1]);
171 
172  Real h11 = 2.0*(f111*f1 + f11*f11) + 2.0*(f211*f2 + f21*f21);
173  Real h12 = 2.0*(f112*f1 + f11*f12) + 2.0*(f212*f2 + f21*f22);
174  Real h21 = 2.0*(f121*f1 + f21*f11) + 2.0*(f221*f2 + f22*f21);
175  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
176 
177  (*hvp)[0] = (1.0/(h11*h22-h12*h21))*( h22*(*vp)[0] - h21*(*vp)[1]);
178  (*hvp)[1] = (1.0/(h11*h22-h12*h21))*(-h12*(*vp)[0] + h11*(*vp)[1]);
179  }
180  };
181 
182  template<class Real>
183  void getPowell( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
184 
185  typedef std::vector<Real> vector;
186  typedef StdVector<Real> SV;
187 
188  typedef typename vector::size_type uint;
189 
190  using Teuchos::RCP;
191  using Teuchos::rcp;
192  using Teuchos::dyn_cast;
193 
194  // Cast Initial Guess and Solution Vectors
195  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
196  RCP<vector> xp = dyn_cast<SV>(x).getVector();
197 
198  uint n = xp->size();
199  // Resize Vectors
200  n = 2;
201  x0p->resize(n);
202  xp->resize(n);
203  // Instantiate Objective Function
204  obj = Teuchos::rcp( new Objective_Powell<Real> );
205  // Get Initial Guess
206  (*x0p)[0] = 0.0;
207  (*x0p)[1] = 1.0;
208  // Get Solution
209  //(*xp)[0] = 1.0981770261368074e-05;
210  (*xp)[0] = 1.098e-05;
211  (*xp)[1] = 9.106;
212  }
213 
214 } // End ZOO Namespace
215 } // End ROL Namespace
216 
217 #endif
Provides the interface to evaluate objective functions.
void getPowell(Teuchos::RCP< Objective< Real > > &obj, Vector< Real > &x0, Vector< Real > &x)
Definition: ROL_Powell.hpp:183
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_Powell.hpp:86
Powell&#39;s badly scaled function.
Definition: ROL_Powell.hpp:65
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Teuchos::RCP< vector > getVector(V &x)
Definition: ROL_Powell.hpp:78
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Provides the std::vector implementation of the ROL::Vector interface.
StdVector< Real > SV
Definition: ROL_Powell.hpp:69
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_Powell.hpp:97
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Definition: ROL_Powell.hpp:148
Teuchos::RCP< const vector > getVector(const V &x)
Definition: ROL_Powell.hpp:73
std::vector< Real > vector
Definition: ROL_Powell.hpp:67