ROL
ROL_HS5.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_HS5_HPP
54 #define ROL_HS5_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_HS5 : 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_HS5(void) {}
87 
88  Real value( const Vector<Real> &x, Real &tol ) {
89  using Teuchos::RCP;
90  RCP<const vector> ex = getVector(x);
91 
92  return std::sin((*ex)[0] + (*ex)[1]) + std::pow((*ex)[0]-(*ex)[1],2.0) - 1.5*(*ex)[0] + 2.5*(*ex)[1] + 1.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 
101  (*eg)[0] = std::cos((*ex)[0] + (*ex)[1]) + 2.0*((*ex)[0]-(*ex)[1]) - 1.5;
102  (*eg)[1] = std::cos((*ex)[0] + (*ex)[1]) - 2.0*((*ex)[0]-(*ex)[1]) + 2.5;;
103  }
104 #if USE_HESSVEC
105  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
106 
107  using Teuchos::RCP;
108  RCP<const vector> ex = getVector(x);
109  RCP<const vector> ev = getVector(v);
110  RCP<vector> ehv = getVector(hv);
111 
112  Real h11 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
113  Real h22 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
114  Real h12 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
115  Real h21 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
116 
117  (*ehv)[0] = h11 * (*ev)[0] + h12 * (*ev)[1];
118  (*ehv)[1] = h21 * (*ev)[0] + h22 * (*ev)[1];
119  }
120 #endif
121  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
122 
123  using Teuchos::RCP;
124  RCP<const vector> ex = getVector(x);
125  RCP<const vector> ev = getVector(v);
126  RCP<vector> ehv = getVector(hv);
127 
128  Real h11 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
129  Real h22 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
130  Real h12 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
131  Real h21 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
132 
133  (*ehv)[0] = 1.0/(h11*h22 - h12*h21) * (h22 * (*ev)[0] - h12 * (*ev)[1]);
134  (*ehv)[1] = 1.0/(h11*h22 - h12*h21) * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
135  }
136  };
137 
138 template<class Real>
139 void getHS5( Teuchos::RCP<Objective<Real> > &obj,
140  Teuchos::RCP<BoundConstraint<Real> > &con,
141  Teuchos::RCP<Vector<Real> > &x0,
142  Teuchos::RCP<Vector<Real> > &x ) {
143  // Problem dimension
144  int n = 2;
145 
146  // Get Initial Guess
147  Teuchos::RCP<std::vector<Real> > x0p = Teuchos::rcp(new std::vector<Real>(n,0.0));
148  (*x0p)[0] = 0.0; (*x0p)[1] = 0.0;
149  x0 = Teuchos::rcp(new StdVector<Real>(x0p));
150 
151  // Get Solution
152  Teuchos::RCP<std::vector<Real> > xp = Teuchos::rcp(new std::vector<Real>(n,0.0));
153  (*xp)[0] = -M_PI/3.0 + 1.0/2.0; (*xp)[1] = -M_PI/3.0 - 1.0/2.0;
154  x = Teuchos::rcp(new StdVector<Real>(xp));
155 
156  // Instantiate Objective Function
157  obj = Teuchos::rcp(new Objective_HS5<Real>);
158 
159  // Instantiate BoundConstraint
160  Teuchos::RCP<std::vector<Real> > lp = Teuchos::rcp(new std::vector<Real>(n,0.0));
161  (*lp)[0] = -1.5; (*lp)[1] = -3.0;
162  Teuchos::RCP<Vector<Real> > l = Teuchos::rcp(new StdVector<Real>(lp));
163  Teuchos::RCP<std::vector<Real> > up = Teuchos::rcp(new std::vector<Real>(n,0.0));
164  (*up)[0] = 4.0; (*up)[1] = 3.0;
165  Teuchos::RCP<Vector<Real> > u = Teuchos::rcp(new StdVector<Real>(up));
166  con = Teuchos::rcp(new BoundConstraint<Real>(l,u));
167 }
168 
169 } // End ZOO Namespace
170 } // End ROL Namespace
171 
172 #endif
Provides the interface to evaluate objective functions.
StdVector< Real > SV
Definition: ROL_HS5.hpp:71
Teuchos::RCP< vector > getVector(V &x)
Definition: ROL_HS5.hpp:80
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.
std::vector< Real > vector
Definition: ROL_HS5.hpp:69
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Vector< Real > V
Definition: ROL_HS5.hpp:70
W. Hock and K. Schittkowski 5th test function.
Definition: ROL_HS5.hpp:67
void getHS5(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Teuchos::RCP< Vector< Real > > &x0, Teuchos::RCP< Vector< Real > > &x)
Definition: ROL_HS5.hpp:139
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS5.hpp:95
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Definition: ROL_HS5.hpp:121
Provides the interface to apply upper and lower bound constraints.
Teuchos::RCP< const vector > getVector(const V &x)
Definition: ROL_HS5.hpp:75
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS5.hpp:88