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  public:
68 
69  Real value( const Vector<Real> &x, Real &tol ) {
70  StdVector<Real> & ex =
71  Teuchos::dyn_cast<StdVector<Real> >(const_cast <Vector<Real> &>(x));
72  Teuchos::RCP<const std::vector<Real> > xp = ex.getVector();
73 
74  Real f1 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
75  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
76 
77  return f1*f1+f2*f2;
78  }
79 
80  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
81  Teuchos::RCP<const std::vector<Real> > xp =
82  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
83  Teuchos::RCP<std::vector<Real> > gp =
84  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(g)).getVector());
85 
86  Real f1 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
87  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
88 
89  Real f11 = 1.e4*(*xp)[1];
90  Real f12 = 1.e4*(*xp)[0];
91  Real f21 = -std::exp(-(*xp)[0]);
92  Real f22 = -std::exp(-(*xp)[1]);
93 
94  (*gp)[0] = 2.0*(f11*f1 + f21*f2);
95  (*gp)[1] = 2.0*(f12*f1 + f22*f2);
96  }
97 #if USE_HESSVEC
98  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
99  Teuchos::RCP<const std::vector<Real> > xp =
100  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
101  Teuchos::RCP<const std::vector<Real> > vp =
102  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
103  Teuchos::RCP<std::vector<Real> > hvp =
104  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
105 
106  Real f1 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
107  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
108 
109  Real f11 = 1.e4*(*xp)[1];
110  Real f12 = 1.e4*(*xp)[0];
111  Real f21 = -std::exp(-(*xp)[0]);
112  Real f22 = -std::exp(-(*xp)[1]);
113 
114  Real f111 = 0.0;
115  Real f112 = 1.e4;
116  Real f121 = 1.e4;
117  Real f122 = 0.0;
118  Real f211 = std::exp(-(*xp)[0]);
119  Real f212 = 0.0;
120  Real f221 = 0.0;
121  Real f222 = std::exp(-(*xp)[1]);
122 
123  Real h11 = 2.0*(f111*f1 + f11*f11) + 2.0*(f211*f2 + f21*f21);
124  Real h12 = 2.0*(f112*f1 + f11*f12) + 2.0*(f212*f2 + f21*f22);
125  Real h21 = 2.0*(f121*f1 + f21*f11) + 2.0*(f221*f2 + f22*f21);
126  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
127 
128  (*hvp)[0] = h11*(*vp)[0] + h12*(*vp)[1];
129  (*hvp)[1] = h21*(*vp)[0] + h22*(*vp)[1];
130  }
131 #endif
132  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
133  Teuchos::RCP<const std::vector<Real> > xp =
134  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
135  Teuchos::RCP<const std::vector<Real> > vp =
136  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
137  Teuchos::RCP<std::vector<Real> > hvp =
138  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
139 
140  Real f1 = 1.e4*(*xp)[0]*(*xp)[1] - 1.0;
141  Real f2 = std::exp(-(*xp)[0]) + std::exp(-(*xp)[1]) - 1.0001;
142 
143  Real f11 = 1.e4*(*xp)[1];
144  Real f12 = 1.e4*(*xp)[0];
145  Real f21 = -std::exp(-(*xp)[0]);
146  Real f22 = -std::exp(-(*xp)[1]);
147 
148  Real f111 = 0.0;
149  Real f112 = 1.e4;
150  Real f121 = 1.e4;
151  Real f122 = 0.0;
152  Real f211 = std::exp(-(*xp)[0]);
153  Real f212 = 0.0;
154  Real f221 = 0.0;
155  Real f222 = std::exp(-(*xp)[1]);
156 
157  Real h11 = 2.0*(f111*f1 + f11*f11) + 2.0*(f211*f2 + f21*f21);
158  Real h12 = 2.0*(f112*f1 + f11*f12) + 2.0*(f212*f2 + f21*f22);
159  Real h21 = 2.0*(f121*f1 + f21*f11) + 2.0*(f221*f2 + f22*f21);
160  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
161 
162  (*hvp)[0] = (1.0/(h11*h22-h12*h21))*( h22*(*vp)[0] - h21*(*vp)[1]);
163  (*hvp)[1] = (1.0/(h11*h22-h12*h21))*(-h12*(*vp)[0] + h11*(*vp)[1]);
164  }
165  };
166 
167  template<class Real>
168  void getPowell( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
169  // Cast Initial Guess and Solution Vectors
170  Teuchos::RCP<std::vector<Real> > x0p =
171  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x0)).getVector());
172  Teuchos::RCP<std::vector<Real> > xp =
173  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x)).getVector());
174  int n = xp->size();
175  // Resize Vectors
176  n = 2;
177  x0p->resize(n);
178  xp->resize(n);
179  // Instantiate Objective Function
180  obj = Teuchos::rcp( new Objective_Powell<Real> );
181  // Get Initial Guess
182  (*x0p)[0] = 0.0;
183  (*x0p)[1] = 1.0;
184  // Get Solution
185  //(*xp)[0] = 1.0981770261368074e-05;
186  (*xp)[0] = 1.098e-05;
187  (*xp)[1] = 9.106;
188  }
189 
190 } // End ZOO Namespace
191 } // End ROL Namespace
192 
193 #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:168
Teuchos::RCP< const std::vector< Element > > getVector() const
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_Powell.hpp:69
Powell'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.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Provides the std::vector implementation of the ROL::Vector interface.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_Powell.hpp:80
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:132