ROL
ROL_HS2.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_HS2_HPP
54 #define ROL_HS2_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_Objective.hpp"
58 #include "ROL_BoundConstraint.hpp"
59 #include "ROL_Types.hpp"
60 
61 namespace ROL {
62 namespace ZOO {
63 
66  template<class Real>
67  class Objective_HS2 : public Objective<Real> {
68 
69  typedef std::vector<Real> vector;
70  typedef Vector<Real> V;
72 
73  private:
74 
75  Teuchos::RCP<const vector> getVector( const V& x ) {
76  using Teuchos::dyn_cast;
77  return dyn_cast<const SV>(x).getVector();
78  }
79 
80  Teuchos::RCP<vector> getVector( V& x ) {
81  using Teuchos::dyn_cast;
82  return dyn_cast<SV>(x).getVector();
83  }
84 
85  public:
86  Objective_HS2(void) {}
87 
88  Real value( const Vector<Real> &x, Real &tol ) {
89 
90  using Teuchos::RCP;
91  RCP<const vector> ex = getVector(x);
92  return 100.0 * std::pow((*ex)[1] - std::pow((*ex)[0],2.0),2.0) + std::pow(1.0-(*ex)[0],2.0);
93  }
94 
95  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
96 
97  using Teuchos::RCP;
98  RCP<const vector> ex = getVector(x);
99  RCP<vector> eg = getVector(g);
100  (*eg)[0] = -400.0 * ((*ex)[1] - std::pow((*ex)[0],2.0)) * (*ex)[0] - 2.0 * (1.0-(*ex)[0]);
101  (*eg)[1] = 200.0 * ((*ex)[1] - std::pow((*ex)[0],2.0));
102  }
103 #if USE_HESSVEC
104  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
105 
106  using Teuchos::RCP;
107  RCP<const vector> ex = getVector(x);
108  RCP<const vector> ev = getVector(v);
109  RCP<vector> ehv = getVector(hv);
110 
111  Real h11 = -400.0 * (*ex)[1] + 1200.0 * std::pow((*ex)[0],2.0) + 2.0;
112  Real h22 = 200.0;
113  Real h12 = -400.0 * (*ex)[0];
114  Real h21 = -400.0 * (*ex)[0];
115 
116  Real alpha = 1.0;
117 
118  (*ehv)[0] = (h11+alpha) * (*ev)[0] + h12 * (*ev)[1];
119  (*ehv)[1] = h21 * (*ev)[0] + (h22+alpha) * (*ev)[1];
120  }
121 #endif
122  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
123 
124  using Teuchos::RCP;
125  RCP<const vector> ex = getVector(x);
126  RCP<const vector> ev = getVector(v);
127  RCP< vector> ehv = getVector(hv);
128 
129  Real h11 = -400.0 * (*ex)[1] + 1200.0 * std::pow((*ex)[0],2.0) + 2.0;
130  Real h22 = 200.0;
131  Real h12 = -400.0 * (*ex)[0];
132  Real h21 = -400.0 * (*ex)[0];
133 
134  (*ehv)[0] = 1.0/(h11*h22 - h12*h21) * (h22 * (*ev)[0] - h12 * (*ev)[1]);
135  (*ehv)[1] = 1.0/(h11*h22 - h12*h21) * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
136  }
137  };
138 
139  template<class Real>
140  void getHS2( Teuchos::RCP<Objective<Real> > &obj, Teuchos::RCP<BoundConstraint<Real> > &con,
141  Vector<Real> &x0, Vector<Real> &x ) {
142 
143  typedef std::vector<Real> vector;
144  typedef Vector<Real> V;
145  typedef StdVector<Real> SV;
146 
147  using Teuchos::RCP;
148  using Teuchos::rcp;
149  using Teuchos::dyn_cast;
150 
151  // Cast Initial Guess and Solution Vectors
152  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
153  RCP<vector> xp = dyn_cast<SV>(x).getVector();
154 
155  int n = xp->size();
156  // Resize Vectors
157  n = 2;
158  x0p->resize(n);
159  xp->resize(n);
160  // Instantiate Objective Function
161  obj = rcp( new Objective_HS2<Real> );
162 
163  // Instantiate BoundConstraint
164  RCP<vector> l_rcp = rcp( new vector(n,0.0) );
165  RCP<vector> u_rcp = rcp( new vector(n,0.0) );
166 
167  (*l_rcp)[0] = -0.1*ROL_OVERFLOW;
168  (*l_rcp)[1] = 1.5;
169  (*u_rcp)[0] = 0.1*ROL_OVERFLOW;
170  (*u_rcp)[1] = 0.1*ROL_OVERFLOW;
171 
172  RCP<V> l = rcp( new SV(l_rcp) );
173  RCP<V> u = rcp( new SV(u_rcp) );
174 
175  con = rcp( new BoundConstraint<Real>(l,u) );
176 
177  // Get Initial Guess
178  (*x0p)[0] = -2.0;
179  (*x0p)[1] = 1.0;
180  // Get Solution
181  Real a = std::sqrt(598.0/1200.0);
182  Real b = 400.0 * std::pow(a,3.0);
183  (*xp)[0] = 2.0*a*std::cos(1.0/3.0 * std::acos(1.0/b));
184  (*xp)[1] = 1.5;
185  }
186 
187 
188 } // End ZOO Namespace
189 } // End ROL Namespace
190 
191 #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.
Contains definitions of custom data types in ROL.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS2.hpp:95
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Vector< Real > V
Definition: ROL_HS2.hpp:70
Provides the std::vector implementation of the ROL::Vector interface.
StdVector< Real > SV
Definition: ROL_HS2.hpp:71
void getHS2(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Vector< Real > &x0, Vector< Real > &x)
Definition: ROL_HS2.hpp:140
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Definition: ROL_HS2.hpp:122
std::vector< Real > vector
Definition: ROL_HS2.hpp:69
Provides the interface to apply upper and lower bound constraints.
Teuchos::RCP< vector > getVector(V &x)
Definition: ROL_HS2.hpp:80
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS2.hpp:88
Teuchos::RCP< const vector > getVector(const V &x)
Definition: ROL_HS2.hpp:75
static const double ROL_OVERFLOW
Platform-dependent maximum double.
Definition: ROL_Types.hpp:126
W. Hock and K. Schittkowski 2nd test function.
Definition: ROL_HS2.hpp:67