ROL
ROL_HS25.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_HS25_HPP
20 #define ROL_HS25_HPP
21 
22 #include "ROL_ScaledStdVector.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_HS25 : public Objective<Real> {
34 
36 
37 private:
38  std::vector<Real> u_vec_;
40 
41 public:
43  u_size_ = 99;
44  for ( uint i = 0; i < u_size_; i++ ) {
45  u_vec_.push_back(static_cast<Real>(25)
46  + std::pow((static_cast<Real>(-50)
47  *std::log(static_cast<Real>(0.01)*static_cast<Real>(i+1))),
48  static_cast<Real>(2)/static_cast<Real>(3)));
49  }
50  }
51 
52  Real value( const Vector<Real> &x, Real &tol ) {
53  Ptr<const std::vector<Real> > ex
54  = dynamic_cast<const PrimalScaledStdVector<Real>&>(x).getVector();
55 
56  Real val(0), f(0), u(0);
57  Real x1 = (*ex)[0], x2 = (*ex)[1], x3 = (*ex)[2];
58  for ( uint i = 0; i < u_size_; i++ ) {
59  u = u_vec_[i];
60  f = -static_cast<Real>(0.01)*static_cast<Real>(i+1)
61  + std::exp(-std::pow(u-x2,x3)/x1);
62  val += f*f;
63  }
64  return val;
65  }
66 
67  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
68  Ptr<std::vector<Real> > eg
69  = dynamic_cast<DualScaledStdVector<Real>&>(g).getVector();
70  Ptr<const std::vector<Real> > ex
71  = dynamic_cast<const PrimalScaledStdVector<Real>&>(x).getVector();
72  g.zero();
73 
74  Real f(0), df1(0), df2(0), df3(0);
75  Real u(0), tmp(0), tmp0(0), tmp1(0);
76  Real x1 = (*ex)[0], x2 = (*ex)[1], x3 = (*ex)[2];
77  Real x1sqr = x1*x1;
78  for ( uint i = 0; i < u_size_; i++ ) {
79  u = u_vec_[i];
80  tmp0 = std::pow(u-x2,x3);
81  tmp1 = std::pow(u-x2,x3-static_cast<Real>(1));
82  tmp = std::exp(-tmp0/x1);
83 
84  f = -static_cast<Real>(0.01)*static_cast<Real>(i+1) + tmp;
85 
86  df1 = tmp*tmp0/x1sqr;
87  df2 = tmp*x3*tmp1/x1;
88  df3 = tmp*tmp0*std::log(u-x2)/x1;
89 
90  (*eg)[0] += static_cast<Real>(2)*f*df1;
91  (*eg)[1] += static_cast<Real>(2)*f*df2;
92  (*eg)[2] += static_cast<Real>(2)*f*df3;
93  }
94  }
95 #if USE_HESSVEC
96  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
97  Ptr<std::vector<Real> > ehv
98  = dynamic_cast<DualScaledStdVector<Real>&>(hv).getVector();
99  Ptr<const std::vector<Real> > ev
100  = dynamic_cast<const PrimalScaledStdVector<Real>&>(v).getVector();
101  Ptr<const std::vector<Real> > ex
102  = dynamic_cast<const PrimalScaledStdVector<Real>&>(x).getVector();
103  hv.zero();
104 
105  Real f(0);
106  Real df1(0), df2(0), df3(0);
107  Real df11(0), df12(0), df13(0);
108  Real df21(0), df22(0), df23(0);
109  Real df31(0), df32(0), df33(0);
110  Real u(0), tmp(0), tmp0(0), tmp1(0), tmp2(0), tmp3(0), tmp4(0);
111  Real x1 = (*ex)[0], x2 = (*ex)[1], x3 = (*ex)[2];
112  Real v1 = (*ev)[0], v2 = (*ev)[1], v3 = (*ev)[2];
113  Real x1sqr = x1*x1, x1cub = x1sqr*x1, x1quar = x1cub*x1;
114  for ( uint i = 0; i < u_size_; i++ ) {
115  u = u_vec_[i];
116  tmp0 = std::pow(u-x2,x3);
117  tmp1 = std::pow(u-x2,x3-static_cast<Real>(1));
118  tmp2 = std::pow(u-x2,static_cast<Real>(2)*(x3-static_cast<Real>(1)));
119  tmp3 = std::pow(u-x2,x3-static_cast<Real>(2));
120  tmp4 = std::pow(u-x2,static_cast<Real>(2)*x3-static_cast<Real>(1));
121  tmp = std::exp(-tmp0/x1);
122 
123  f = -static_cast<Real>(0.01)*static_cast<Real>(i+1) + tmp;
124 
125  df1 = tmp*tmp0/x1sqr;
126  df2 = tmp*x3*tmp1/x1;
127  df3 = tmp*tmp0*std::log(u-x2)/x1;
128 
129  df11 = tmp0*tmp*(tmp0-static_cast<Real>(2)*x1)/x1quar;
130  df12 = x3*tmp1*tmp*(tmp0-x1)/x1cub;
131  df13 = tmp0*std::log(u-x2)*tmp*(x1-tmp0)/x1cub;
132 
133  df21 = df12;
134  df22 = x3*x3*tmp2*tmp/(x1*x1)
135  -(x3-static_cast<Real>(1))*x3*tmp3*tmp/x1;
136  df23 = -x3*tmp4*std::log(u-x2)*tmp/x1sqr
137  +tmp1*tmp/x1 + x3*tmp1*std::log(u-x2)*tmp/x1;
138 
139  df31 = df13;
140  df32 = df23;
141  df33 = tmp0*std::pow(std::log(u-x2),2)*tmp*(tmp0-x1)/x1sqr;
142 
143  (*ehv)[0] += static_cast<Real>(2)*(f*(df11*v1 + df12*v2 + df13*v3)
144  + df1*(df1*v1 + df2*v2 + df3*v3));
145  (*ehv)[1] += static_cast<Real>(2)*(f*(df21*v1 + df22*v2 + df23*v3)
146  + df2*(df1*v1 + df2*v2 + df3*v3));
147  (*ehv)[2] += static_cast<Real>(2)*(f*(df31*v1 + df32*v2 + df33*v3)
148  + df3*(df1*v1 + df2*v2 + df3*v3));
149  }
150  }
151 #endif
152 };
153 
154 template<class Real>
155 class getHS25 : public TestProblem<Real> {
156 private:
157  int n_;
158  Ptr<std::vector<Real> > scale_;
159 
160 public:
161  getHS25(void) {
162  // Problem dimension
163  n_ = 3;
164  // Set up vector scaling
165  scale_ = makePtr<std::vector<Real>>(n_,0);
166  (*scale_)[0] = static_cast<Real>(1.e-4);
167  (*scale_)[1] = static_cast<Real>(1.e-3);
168  (*scale_)[2] = static_cast<Real>(0.5);
169  }
170 
171  Ptr<Objective<Real>> getObjective(void) const {
172  // Instantiate Objective Function
173  return makePtr<Objective_HS25<Real>>();
174  }
175 
176  Ptr<Vector<Real>> getInitialGuess(void) const {
177  // Get Initial Guess
178  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n_,0);
179  (*x0p)[0] = static_cast<Real>(100);
180  (*x0p)[1] = static_cast<Real>(12.5);
181  (*x0p)[2] = static_cast<Real>(3);
182  return makePtr<PrimalScaledStdVector<Real>>(x0p,scale_);
183  }
184 
185  Ptr<Vector<Real>> getSolution(const int i = 0) const {
186  // Get Solution
187  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n_,0);
188  (*xp)[0] = static_cast<Real>(50);
189  (*xp)[1] = static_cast<Real>(25);
190  (*xp)[2] = static_cast<Real>(1.5);
191  return makePtr<PrimalScaledStdVector<Real>>(xp,scale_);
192  }
193 
194  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
195  // Instantiate BoundConstraint
196  Ptr<std::vector<Real> > lp = makePtr<std::vector<Real>>(n_,0);
197  (*lp)[0] = static_cast<Real>(0.1);
198  (*lp)[1] = static_cast<Real>(0);
199  (*lp)[2] = static_cast<Real>(0);
200  Ptr<Vector<Real> > l = makePtr<StdVector<Real>>(lp);
201  Ptr<std::vector<Real> > up = makePtr<std::vector<Real>>(n_,0);
202  (*up)[0] = static_cast<Real>(100);
203  (*up)[1] = static_cast<Real>(25.6);
204  (*up)[2] = static_cast<Real>(5);
205  Ptr<Vector<Real> > u = makePtr<StdVector<Real>>(up);
206  return makePtr<Bounds<Real>>(l,u);
207  }
208 };
209 
210 } // End ZOO Namespace
211 } // End ROL Namespace
212 
213 #endif
Provides the interface to evaluate objective functions.
typename PV< Real >::size_type size_type
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS25.hpp:176
std::vector< Real > u_vec_
Definition: ROL_HS25.hpp:38
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< Objective< Real > > getObjective(void) const
Definition: ROL_HS25.hpp:171
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
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS25.hpp:52
std::vector< Real >::size_type uint
Definition: ROL_HS25.hpp:35
Contains definitions of test objective functions.
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_HS25.hpp:185
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS25.hpp:67
W. Hock and K. Schittkowski 25th test function.
Definition: ROL_HS25.hpp:33
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS25.hpp:194
Ptr< std::vector< Real > > scale_
Definition: ROL_HS25.hpp:158