ROL
ROL_HS4.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_HS4_HPP
20 #define ROL_HS4_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_HS4 : public Objective<Real> {
34 
35  typedef std::vector<Real> vector;
36  typedef Vector<Real> V;
38 
39  private:
40 
41  ROL::Ptr<const vector> getVector( const V& x ) {
42 
43  return dynamic_cast<const SV&>(x).getVector();
44  }
45 
46  ROL::Ptr<vector> getVector( V& x ) {
47 
48  return dynamic_cast<SV&>(x).getVector();
49  }
50 
51  public:
52  Objective_HS4(void) {}
53 
54  Real value( const Vector<Real> &x, Real &tol ) {
55 
56  ROL::Ptr<const vector> ex = getVector(x);
57 
58  return 1.0/3.0 * std::pow((*ex)[0] + 1.0,3.0) + (*ex)[1];
59  }
60 
61  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
62 
63  ROL::Ptr<const vector> ex = getVector(x);
64  ROL::Ptr<vector> eg = getVector(g);
65 
66  (*eg)[0] = std::pow((*ex)[0] + 1.0,2.0);
67  (*eg)[1] = 1.0;
68  }
69 #if USE_HESSVEC
70  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
71 
72  ROL::Ptr<const vector> ex = getVector(x);
73  ROL::Ptr<const vector> ev = getVector(v);
74  ROL::Ptr<vector> ehv = getVector(hv);
75 
76  Real alpha = 0.0;
77  (*ehv)[0] = 2.0*((*ex)[0] + 1.0)*(*ev)[0] + alpha*(*ev)[0];
78  (*ehv)[1] = 0.0 + alpha*(*ev)[1];
79  }
80 #endif
81  };
82 
83 template<class Real>
84 class getHS4 : public TestProblem<Real> {
85 public:
86  getHS4(void) {}
87 
88  Ptr<Objective<Real>> getObjective(void) const {
89  // Instantiate Objective Function
90  return ROL::makePtr<Objective_HS4<Real>>();
91  }
92 
93  Ptr<Vector<Real>> getInitialGuess(void) const {
94  // Problem dimension
95  int n = 2;
96  // Get Initial Guess
97  ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
98  (*x0p)[0] = 1.125; (*x0p)[1] = 0.125;
99  return ROL::makePtr<StdVector<Real>>(x0p);
100  }
101 
102  Ptr<Vector<Real>> getSolution(const int i = 0) const {
103  // Problem dimension
104  int n = 2;
105  // Get Solution
106  ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
107  (*xp)[0] = 1.0; (*xp)[1] = 0.0;
108  return ROL::makePtr<StdVector<Real>>(xp);
109  }
110 
111  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
112  // Problem dimension
113  int n = 2;
114  // Instantiate BoundConstraint
115  ROL::Ptr<std::vector<Real> > lp = ROL::makePtr<std::vector<Real>>(n,0.0);
116  (*lp)[0] = 1.0; (*lp)[1] = 0.0;
117  ROL::Ptr<Vector<Real> > l = ROL::makePtr<StdVector<Real>>(lp);
118  ROL::Ptr<std::vector<Real> > up = ROL::makePtr<std::vector<Real>>(n,0.0);
119  (*up)[0] = ROL_INF<Real>(); (*up)[1] = ROL_INF<Real>();
120  ROL::Ptr<Vector<Real> > u = ROL::makePtr<StdVector<Real>>(up);
121  return ROL::makePtr<Bounds<Real>>(l,u);
122  }
123 };
124 
125 } // End ZOO Namespace
126 } // End ROL Namespace
127 
128 #endif
ROL::Ptr< const vector > getVector(const V &x)
Definition: ROL_HS4.hpp:41
Provides the interface to evaluate objective functions.
std::vector< Real > vector
Definition: ROL_HS4.hpp:35
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_HS4.hpp:102
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_HS4.hpp:61
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
StdVector< Real > SV
Definition: ROL_HS4.hpp:37
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS4.hpp:93
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS4.hpp:88
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS4.hpp:54
Contains definitions of test objective functions.
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS4.hpp:111
W. Hock and K. Schittkowski 4th test function.
Definition: ROL_HS4.hpp:33
ROL::Ptr< vector > getVector(V &x)
Definition: ROL_HS4.hpp:46
Vector< Real > V
Definition: ROL_HS4.hpp:36