ROL
ROL_HS1.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_HS1_HPP
54 #define ROL_HS1_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_TestProblem.hpp"
58 #include "ROL_Bounds.hpp"
59 #include "ROL_Types.hpp"
60 
61 namespace ROL {
62 namespace ZOO {
63 
66 template<class Real>
67 class Objective_HS1 : public Objective<Real> {
68 public:
69  Objective_HS1(void) {}
70 
71  Real value( const Vector<Real> &x, Real &tol ) {
72  Ptr<const std::vector<Real> > ex
73  = dynamic_cast<const StdVector<Real>&>(x).getVector();
74  return 100.0 * std::pow((*ex)[1] - std::pow((*ex)[0],2.0),2.0) + std::pow(1.0-(*ex)[0],2.0);
75  }
76 
77  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
78  Ptr<std::vector<Real> > eg
79  = dynamic_cast<StdVector<Real>&>(g).getVector();
80  Ptr<const std::vector<Real> > ex
81  = dynamic_cast<const StdVector<Real>&>(x).getVector();
82 
83  (*eg)[0] = -4.0 * 100.0 * ((*ex)[1] - std::pow((*ex)[0],2.0)) * (*ex)[0] - 2.0 * (1.0-(*ex)[0]);
84  (*eg)[1] = 2.0 * 100.0 * ((*ex)[1] - std::pow((*ex)[0],2.0));
85  }
86 #if USE_HESSVEC
87  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
88  Ptr<std::vector<Real> > ehv
89  = dynamic_cast<StdVector<Real>&>(hv).getVector();
90  Ptr<const std::vector<Real> > ev
91  = dynamic_cast<const StdVector<Real>&>(v).getVector();
92  Ptr<const std::vector<Real> > ex
93  = dynamic_cast<const StdVector<Real>&>(x).getVector();
94 
95  Real h11 = -4.0 * 100.0 * (*ex)[1] + 12.0 * 100.0 * std::pow((*ex)[0],2.0) + 2.0;
96  Real h22 = 2.0 * 100.0;
97  Real h12 = -4.0 * 100.0 * (*ex)[0];
98  Real h21 = -4.0 * 100.0 * (*ex)[0];
99 
100  (*ehv)[0] = h11 * (*ev)[0] + h12 * (*ev)[1];
101  (*ehv)[1] = h21 * (*ev)[0] + h22 * (*ev)[1];
102  }
103 #endif
104  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
105  Ptr<std::vector<Real> > ehv
106  = dynamic_cast<StdVector<Real>&>(hv).getVector();
107  Ptr<const std::vector<Real> > ev
108  = dynamic_cast<const StdVector<Real>&>(v).getVector();
109  Ptr<const std::vector<Real> > ex
110  = dynamic_cast<const StdVector<Real>&>(x).getVector();
111 
112  Real h11 = -4.0 * 100.0 * (*ex)[1] + 12.0 * 100.0 * std::pow((*ex)[0],2.0) + 2.0;
113  Real h22 = 2.0 * 100.0;
114  Real h12 = -4.0 * 100.0 * (*ex)[0];
115  Real h21 = -4.0 * 100.0 * (*ex)[0];
116 
117  (*ehv)[0] = 1.0/(h11*h22 - h12*h21) * (h22 * (*ev)[0] - h12 * (*ev)[1]);
118  (*ehv)[1] = 1.0/(h11*h22 - h12*h21) * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
119  }
120 };
121 
122 template<class Real>
123 class getHS1 : public TestProblem<Real> {
124 public:
125  getHS1(void) {}
126 
127  Ptr<Objective<Real>> getObjective(void) const {
128  // Instantiate Objective Function
129  return makePtr<Objective_HS1<Real>>();
130  }
131 
132  Ptr<Vector<Real>> getInitialGuess(void) const {
133  // Problem size
134  int n = 2;
135  // Get Initial Guess
136  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,0.0);
137  (*x0p)[0] = -2.0; (*x0p)[1] = 1.0;
138  return makePtr<StdVector<Real>>(x0p);
139  }
140 
141  Ptr<Vector<Real>> getSolution(const int i = 0) const {
142  // Problem size
143  int n = 2;
144  // Get Solution
145  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,0.0);
146  (*xp)[0] = 1.0; (*xp)[1] = 1.0;
147  return makePtr<StdVector<Real>>(xp);
148  }
149 
150  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
151  // Problem size
152  int n = 2;
153  // Build lower bound
154  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(n,0.0);
155  (*lp)[0] = ROL_NINF<Real>(); (*lp)[1] = -1.5;
156  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
157  // Build upper bound
158  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(n,0.0);
159  (*up)[0] = ROL_INF<Real>(); (*up)[1] = ROL_INF<Real>();
160  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
161  // Instantiate BoundConstraint
162  return makePtr<Bounds<Real>>(l,u);
163  }
164 };
165 
166 } // End ZOO Namespace
167 } // End ROL Namespace
168 
169 #endif
Provides the interface to evaluate objective functions.
W. Hock and K. Schittkowski 1st test function.
Definition: ROL_HS1.hpp:67
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.
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_HS1.hpp:141
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS1.hpp:150
Contains definitions of test objective functions.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS1.hpp:77
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Definition: ROL_HS1.hpp:104
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS1.hpp:71
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS1.hpp:132
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS1.hpp:127