ROL
ROL_HS1.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #ifndef USE_HESSVEC
16 #define USE_HESSVEC 1
17 #endif
18 
19 #ifndef ROL_HS1_HPP
20 #define ROL_HS1_HPP
21 
22 #include "ROL_StdVector.hpp"
23 #include "ROL_TestProblem.hpp"
24 #include "ROL_Bounds.hpp"
25 #include "ROL_Types.hpp"
26 
27 namespace ROL {
28 namespace ZOO {
29 
32 template<class Real>
33 class Objective_HS1 : public Objective<Real> {
34 public:
35  Objective_HS1(void) {}
36 
37  Real value( const Vector<Real> &x, Real &tol ) {
38  Ptr<const std::vector<Real> > ex
39  = dynamic_cast<const StdVector<Real>&>(x).getVector();
40  return 100.0 * std::pow((*ex)[1] - std::pow((*ex)[0],2.0),2.0) + std::pow(1.0-(*ex)[0],2.0);
41  }
42 
43  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
44  Ptr<std::vector<Real> > eg
45  = dynamic_cast<StdVector<Real>&>(g).getVector();
46  Ptr<const std::vector<Real> > ex
47  = dynamic_cast<const StdVector<Real>&>(x).getVector();
48 
49  (*eg)[0] = -4.0 * 100.0 * ((*ex)[1] - std::pow((*ex)[0],2.0)) * (*ex)[0] - 2.0 * (1.0-(*ex)[0]);
50  (*eg)[1] = 2.0 * 100.0 * ((*ex)[1] - std::pow((*ex)[0],2.0));
51  }
52 #if USE_HESSVEC
53  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
54  Ptr<std::vector<Real> > ehv
55  = dynamic_cast<StdVector<Real>&>(hv).getVector();
56  Ptr<const std::vector<Real> > ev
57  = dynamic_cast<const StdVector<Real>&>(v).getVector();
58  Ptr<const std::vector<Real> > ex
59  = dynamic_cast<const StdVector<Real>&>(x).getVector();
60 
61  Real h11 = -4.0 * 100.0 * (*ex)[1] + 12.0 * 100.0 * std::pow((*ex)[0],2.0) + 2.0;
62  Real h22 = 2.0 * 100.0;
63  Real h12 = -4.0 * 100.0 * (*ex)[0];
64  Real h21 = -4.0 * 100.0 * (*ex)[0];
65 
66  (*ehv)[0] = h11 * (*ev)[0] + h12 * (*ev)[1];
67  (*ehv)[1] = h21 * (*ev)[0] + h22 * (*ev)[1];
68  }
69 #endif
70  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
71  Ptr<std::vector<Real> > ehv
72  = dynamic_cast<StdVector<Real>&>(hv).getVector();
73  Ptr<const std::vector<Real> > ev
74  = dynamic_cast<const StdVector<Real>&>(v).getVector();
75  Ptr<const std::vector<Real> > ex
76  = dynamic_cast<const StdVector<Real>&>(x).getVector();
77 
78  Real h11 = -4.0 * 100.0 * (*ex)[1] + 12.0 * 100.0 * std::pow((*ex)[0],2.0) + 2.0;
79  Real h22 = 2.0 * 100.0;
80  Real h12 = -4.0 * 100.0 * (*ex)[0];
81  Real h21 = -4.0 * 100.0 * (*ex)[0];
82 
83  (*ehv)[0] = 1.0/(h11*h22 - h12*h21) * (h22 * (*ev)[0] - h12 * (*ev)[1]);
84  (*ehv)[1] = 1.0/(h11*h22 - h12*h21) * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
85  }
86 };
87 
88 template<class Real>
89 class getHS1 : public TestProblem<Real> {
90 public:
91  getHS1(void) {}
92 
93  Ptr<Objective<Real>> getObjective(void) const {
94  // Instantiate Objective Function
95  return makePtr<Objective_HS1<Real>>();
96  }
97 
98  Ptr<Vector<Real>> getInitialGuess(void) const {
99  // Problem size
100  int n = 2;
101  // Get Initial Guess
102  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,0.0);
103  (*x0p)[0] = -2.0; (*x0p)[1] = 1.0;
104  return makePtr<StdVector<Real>>(x0p);
105  }
106 
107  Ptr<Vector<Real>> getSolution(const int i = 0) const {
108  // Problem size
109  int n = 2;
110  // Get Solution
111  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,0.0);
112  (*xp)[0] = 1.0; (*xp)[1] = 1.0;
113  return makePtr<StdVector<Real>>(xp);
114  }
115 
116  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
117  // Problem size
118  int n = 2;
119  // Build lower bound
120  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(n,0.0);
121  (*lp)[0] = ROL_NINF<Real>(); (*lp)[1] = -1.5;
122  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
123  // Build upper bound
124  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(n,0.0);
125  (*up)[0] = ROL_INF<Real>(); (*up)[1] = ROL_INF<Real>();
126  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
127  // Instantiate BoundConstraint
128  return makePtr<Bounds<Real>>(l,u);
129  }
130 };
131 
132 } // End ZOO Namespace
133 } // End ROL Namespace
134 
135 #endif
Provides the interface to evaluate objective functions.
W. Hock and K. Schittkowski 1st test function.
Definition: ROL_HS1.hpp:33
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:107
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS1.hpp:116
Contains definitions of test objective functions.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS1.hpp:43
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:70
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS1.hpp:37
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS1.hpp:98
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS1.hpp:93