ROL
ROL_HS45.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_HS45_HPP
20 #define ROL_HS45_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_HS45 : public Objective<Real> {
34 
35  typedef std::vector<Real> vector;
36  typedef Vector<Real> V;
38 
39  typedef typename vector::size_type uint;
40 
41  private:
43  Real fact_;
44 
45  ROL::Ptr<const vector> getVector( const V& x ) {
46 
47  return dynamic_cast<const SV&>(x).getVector();
48  }
49 
50  ROL::Ptr<vector> getVector( V& x ) {
51 
52  return dynamic_cast<SV&>(x).getVector();
53  }
54 
55  public:
56  Objective_HS45(int dim = 5) : dim_((uint)dim) {
57  fact_ = 1.0;
58  for ( uint i = 0; i < dim_; i++ ) {
59  fact_ *= (Real)(i+1);
60  }
61  }
62 
63  Real value( const Vector<Real> &x, Real &tol ) {
64 
65  ROL::Ptr<const vector> ex = getVector(x);
66  Real prod = 1.0;
67  for ( uint i = 0; i < dim_; i++ ) {
68  prod *= (*ex)[i];
69  }
70  return 2.0 - prod/fact_;
71  }
72 
73  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
74 
75  ROL::Ptr<const vector> ex = getVector(x);
76  ROL::Ptr<vector> eg = getVector(g);
77 
78  Real prod = 1.0;
79  for ( uint j = 0; j < dim_; j++ ) {
80  for ( uint i = 0; i < dim_; i++ ) {
81  if ( j != i ) {
82  prod *= (*ex)[i];
83  }
84  }
85  (*eg)[j] = -prod/fact_;
86  prod = 1.0;
87  }
88  }
89 #if USE_HESSVEC
90  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
91 
92 
93  ROL::Ptr<const vector> ex = getVector(x);
94  ROL::Ptr<const vector> ev = getVector(v);
95  ROL::Ptr<vector> ehv = getVector(hv);
96 
97  hv.zero();
98  Real prod = 1.0;
99  for ( uint l = 0; l < dim_; l++ ) {
100  for ( uint j = 0; j < dim_; j++ ) {
101  if ( l != j ) {
102  for ( uint i = 0; i < dim_; i++ ) {
103  if ( j != i && l != i ) {
104  prod *= (*ex)[i];
105  }
106  }
107  (*ehv)[l] += -prod/fact_*(*ev)[j];
108  }
109  prod = 1.0;
110  }
111  }
112  }
113 #endif
114  };
115 
116 template<class Real>
117 class getHS45 : public TestProblem<Real> {
118 public:
119  getHS45(void) {}
120 
121  Ptr<Objective<Real>> getObjective(void) const {
122  // Problem dimension
123  int n = 10;
124  // Instantiate Objective Function
125  return ROL::makePtr<Objective_HS45<Real>>(n);
126  }
127 
128  Ptr<Vector<Real>> getInitialGuess(void) const {
129  // Problem dimension
130  int n = 10;
131  // Get Initial Guess
132  ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
133  for ( int i = 0; i < n; i++ ) {
134  (*x0p)[i] = 2.0;
135  }
136  return ROL::makePtr<StdVector<Real>>(x0p);
137  }
138 
139  Ptr<Vector<Real>> getSolution(const int i = 0) const {
140  // Problem dimension
141  int n = 10;
142  // Get Solution
143  ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
144  for ( int i = 0; i < n; i++ ) {
145  (*xp)[i] = (Real)(i+1);
146  }
147  return ROL::makePtr<StdVector<Real>>(xp);
148  }
149 
150  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
151  // Problem dimension
152  int n = 10;
153  // Instantiate BoundConstraint
154  ROL::Ptr<std::vector<Real> > lp = ROL::makePtr<std::vector<Real>>(n,0.0);
155  ROL::Ptr<std::vector<Real> > up = ROL::makePtr<std::vector<Real>>(n,0.0);
156  for ( int i = 0; i < n; i++ ) {
157  (*lp)[i] = 0.0;
158  (*up)[i] = static_cast<Real>(i+1);
159  }
160  ROL::Ptr<Vector<Real> > l = ROL::makePtr<StdVector<Real>>(lp);
161  ROL::Ptr<Vector<Real> > u = ROL::makePtr<StdVector<Real>>(up);
162  return ROL::makePtr<Bounds<Real>>(l,u);
163  }
164 };
165 
166 
167 } // End ZOO Namespace
168 } // End ROL Namespace
169 
170 #endif
Provides the interface to evaluate objective functions.
typename PV< Real >::size_type size_type
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS45.hpp:121
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS45.hpp:150
std::vector< Real > vector
Definition: ROL_HS45.hpp:35
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS45.hpp:63
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.
vector::size_type uint
Definition: ROL_HS45.hpp:39
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:133
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
ROL::Ptr< vector > getVector(V &x)
Definition: ROL_HS45.hpp:50
Contains definitions of test objective functions.
W. Hock and K. Schittkowski 45th test function.
Definition: ROL_HS45.hpp:33
StdVector< Real > SV
Definition: ROL_HS45.hpp:37
Vector< Real > V
Definition: ROL_HS45.hpp:36
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS45.hpp:73
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS45.hpp:128
ROL::Ptr< const vector > getVector(const V &x)
Definition: ROL_HS45.hpp:45
constexpr auto dim
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_HS45.hpp:139
Objective_HS45(int dim=5)
Definition: ROL_HS45.hpp:56