ROL
ROL_HS38.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_HS38_HPP
54 #define ROL_HS38_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_HS38 : public Objective<Real> {
68 
69  typedef std::vector<Real> vector;
70  typedef Vector<Real> V;
71  typedef StdVector<Real> SV;
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_HS38(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  90.0 * std::pow((*ex)[3] - std::pow((*ex)[2],2.0),2.0) + std::pow(1.0-(*ex)[2],2.0) +
94  10.1 * (std::pow((*ex)[1] - 1.0,2.0) + std::pow((*ex)[3]-1.0,2.0)) +
95  19.8 * ((*ex)[1] - 1.0) * ((*ex)[3] - 1.0);
96  }
97 
98  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
99 
100  using Teuchos::RCP;
101  RCP<const vector> ex = getVector(x);
102  RCP<vector> eg = getVector(g);
103 
104  (*eg)[0] = -4.0 * 100.0 * ((*ex)[1] - std::pow((*ex)[0],2.0)) * (*ex)[0] - 2.0 * (1.0-(*ex)[0]);
105  (*eg)[1] = 2.0 * 100.0 * ((*ex)[1] - std::pow((*ex)[0],2.0)) +
106  2.0 * 10.1 * ((*ex)[1] - 1.0) + 19.8*((*ex)[3] - 1.0);
107  (*eg)[2] = -4.0 * 90.0 * ((*ex)[3] - std::pow((*ex)[2],2.0)) * (*ex)[2] - 2.0 * (1.0-(*ex)[2]);
108  (*eg)[3] = 2.0 * 90.0 * ((*ex)[3] - std::pow((*ex)[2],2.0)) +
109  2.0 * 10.1 * ((*ex)[3] - 1.0) + 19.8*((*ex)[1] - 1.0);
110  }
111 #if USE_HESSVEC
112  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
113 
114  using Teuchos::RCP;
115  RCP<const vector> ex = getVector(x);
116  RCP<const vector> ev = getVector(v);
117  RCP<vector> ehv = getVector(hv);
118 
119  Real h11 = -4.0 * 100.0 * (*ex)[1] + 12.0 * 100.0 * std::pow((*ex)[0],2.0) + 2.0;
120  Real h12 = -4.0 * 100.0 * (*ex)[0];
121  Real h13 = 0.0;
122  Real h14 = 0.0;
123  Real h21 = -4.0 * 100.0 * (*ex)[0];
124  Real h22 = 2.0 * 100.0 + 2.0 * 10.1;
125  Real h23 = 0.0;
126  Real h24 = 19.8;
127  Real h31 = 0.0;
128  Real h32 = 0.0;
129  Real h33 = -4.0 * 90.0 * (*ex)[3] + 12.0 * 90.0 * std::pow((*ex)[2],2.0) + 2.0;
130  Real h34 = -4.0 * 90.0 * (*ex)[2];
131  Real h41 = 0.0;
132  Real h42 = 19.8;
133  Real h43 = -4.0 * 90.0 * (*ex)[2];
134  Real h44 = 2.0 * 90.0 + 2.0 * 10.1;
135 
136  (*ehv)[0] = h11 * (*ev)[0] + h12 * (*ev)[1] + h13 * (*ev)[2] + h14 * (*ev)[3];
137  (*ehv)[1] = h21 * (*ev)[0] + h22 * (*ev)[1] + h23 * (*ev)[2] + h24 * (*ev)[3];
138  (*ehv)[2] = h31 * (*ev)[0] + h32 * (*ev)[1] + h33 * (*ev)[2] + h34 * (*ev)[3];
139  (*ehv)[3] = h41 * (*ev)[0] + h42 * (*ev)[1] + h43 * (*ev)[2] + h44 * (*ev)[3];
140  }
141 #endif
142  };
143 
144 template<class Real>
145 void getHS38( Teuchos::RCP<Objective<Real> > &obj,
146  Teuchos::RCP<BoundConstraint<Real> > &con,
147  Teuchos::RCP<Vector<Real> > &x0,
148  Teuchos::RCP<Vector<Real> > &x ) {
149  // Problem dimension
150  int n = 4;
151 
152  // Get Initial Guess
153  Teuchos::RCP<std::vector<Real> > x0p = Teuchos::rcp(new std::vector<Real>(n,0.0));
154  (*x0p)[0] = -3.0; (*x0p)[1] = -1.0;
155  (*x0p)[2] = -3.0; (*x0p)[3] = -1.0;
156  x0 = Teuchos::rcp(new StdVector<Real>(x0p));
157 
158  // Get Solution
159  Teuchos::RCP<std::vector<Real> > xp = Teuchos::rcp(new std::vector<Real>(n,0.0));
160  (*xp)[0] = 1.0; (*xp)[1] = 1.0;
161  (*xp)[2] = 1.0; (*xp)[3] = 1.0;
162  x = Teuchos::rcp(new StdVector<Real>(xp));
163 
164  // Instantiate Objective Function
165  obj = Teuchos::rcp(new Objective_HS38<Real>);
166 
167  // Instantiate BoundConstraint
168  Teuchos::RCP<std::vector<Real> > lp = Teuchos::rcp(new std::vector<Real>(n,-10.0));
169  Teuchos::RCP<Vector<Real> > l = Teuchos::rcp(new StdVector<Real>(lp));
170  Teuchos::RCP<std::vector<Real> > up = Teuchos::rcp(new std::vector<Real>(n, 10.0));
171  Teuchos::RCP<Vector<Real> > u = Teuchos::rcp(new StdVector<Real>(up));
172  con = Teuchos::rcp(new BoundConstraint<Real>(l,u));
173 
174 }
175 
176 
177 } // End ZOO Namespace
178 } // End ROL Namespace
179 
180 #endif
Provides the interface to evaluate objective functions.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS38.hpp:98
StdVector< Real > SV
Definition: ROL_HS38.hpp:71
Teuchos::RCP< const vector > getVector(const V &x)
Definition: ROL_HS38.hpp:75
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_HS38.hpp:69
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS38.hpp:88
W. Hock and K. Schittkowski 38th test function.
Definition: ROL_HS38.hpp:67
Teuchos::RCP< vector > getVector(V &x)
Definition: ROL_HS38.hpp:80
Provides the interface to apply upper and lower bound constraints.
Vector< Real > V
Definition: ROL_HS38.hpp:70
void getHS38(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Teuchos::RCP< Vector< Real > > &x0, Teuchos::RCP< Vector< Real > > &x)
Definition: ROL_HS38.hpp:145