ROL
ROL_FreudensteinRoth.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_FREUDENSTEINROTH_HPP
20 #define ROL_FREUDENSTEINROTH_HPP
21 
22 #include "ROL_StdVector.hpp"
23 #include "ROL_TestProblem.hpp"
24 
25 namespace ROL {
26 namespace ZOO {
27 
30 template<class Real>
31 class Objective_FreudensteinRoth : public Objective<Real> {
32 public:
34 
35  Real value( const Vector<Real> &x, Real &tol ) {
36  Ptr<const std::vector<Real> > ex
37  = dynamic_cast<const StdVector<Real>&>(x).getVector();
38 
39  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
40  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
41 
42  return f1*f1+f2*f2;
43  }
44 
45  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
46  Ptr<std::vector<Real> > eg
47  = dynamic_cast<StdVector<Real>&>(g).getVector();
48  Ptr<const std::vector<Real> > ex
49  = dynamic_cast<const StdVector<Real>&>(x).getVector();
50 
51  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
52  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
53 
54  Real f11 = 1.0;
55  Real f12 = 10.0*(*ex)[1] - 3.0*(*ex)[1]*(*ex)[1] - 2.0;
56  Real f21 = 1.0;
57  Real f22 = 3.0*(*ex)[1]*(*ex)[1] + 2.0*(*ex)[1] - 14.0;
58 
59  (*eg)[0] = 2.0*(f11*f1 + f21*f2);
60  (*eg)[1] = 2.0*(f12*f1 + f22*f2);
61  }
62 #if USE_HESSVEC
63  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
64  Ptr<std::vector<Real> > ehv
65  = dynamic_cast<StdVector<Real>&>(hv).getVector();
66  Ptr<const std::vector<Real> > ev
67  = dynamic_cast<const StdVector<Real>&>(v).getVector();
68  Ptr<const std::vector<Real> > ex
69  = dynamic_cast<const StdVector<Real>&>(x).getVector();
70 
71  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
72  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
73 
74  Real f11 = 1.0;
75  Real f12 = 10.0*(*ex)[1] - 3.0*(*ex)[1]*(*ex)[1] - 2.0;
76  Real f21 = 1.0;
77  Real f22 = 3.0*(*ex)[1]*(*ex)[1] + 2.0*(*ex)[1] - 14.0;
78 
79  Real f122 = 10.0 - 6.0*(*ex)[1];
80  Real f222 = 6.0*(*ex)[1] + 2.0;
81 
82  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
83  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
84  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
85 
86  (*ehv)[0] = h11*(*ev)[0] + h12*(*ev)[1];
87  (*ehv)[1] = h12*(*ev)[0] + h22*(*ev)[1];
88  }
89 #endif
90  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
91  Ptr<std::vector<Real> > ehv
92  = dynamic_cast<StdVector<Real>&>(hv).getVector();
93  Ptr<const std::vector<Real> > ev
94  = dynamic_cast<const StdVector<Real>&>(v).getVector();
95  Ptr<const std::vector<Real> > ex
96  = dynamic_cast<const StdVector<Real>&>(x).getVector();
97 
98  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
99  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
100 
101  Real f11 = 1.0;
102  Real f12 = 10.0*(*ex)[1] - 3.0*(*ex)[1]*(*ex)[1] - 2.0;
103  Real f21 = 1.0;
104  Real f22 = 3.0*(*ex)[1]*(*ex)[1] + 2.0*(*ex)[1] - 14.0;
105 
106  Real f122 = 10.0 - 6.0*(*ex)[1];
107  Real f222 = 6.0*(*ex)[1] + 2.0;
108 
109  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
110  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
111  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
112 
113  (*ehv)[0] = (1.0/(h11*h22-h12*h12))*( h22*(*ev)[0] - h12*(*ev)[1]);
114  (*ehv)[1] = (1.0/(h11*h22-h12*h12))*(-h12*(*ev)[0] + h11*(*ev)[1]);
115  }
116 };
117 
118 template<class Real>
119 class getFreudensteinRoth : public TestProblem<Real> {
120 public:
122 
123  Ptr<Objective<Real>> getObjective(void) const {
124  // Instantiate Objective Function
125  return makePtr<Objective_FreudensteinRoth<Real>>();
126  }
127 
128  Ptr<Vector<Real>> getInitialGuess(void) const {
129  // Problem dimension
130  int n = 2;
131  // Get Initial Guess
132  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,0.0);
133  (*x0p)[0] = 0.5; (*x0p)[1] = -2.0;
134  return makePtr<StdVector<Real>>(x0p);
135  }
136 
137  Ptr<Vector<Real>> getSolution(const int i = 0) const {
138  // Problem dimension
139  int n = 2;
140  // Get Solution
141  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,0.0);
142  if (i == 0) {
143  (*xp)[0] = 5.0; (*xp)[1] = 4.0;
144  }
145  else if (i == 1) {
146  (*xp)[0] = 11.412779; (*xp)[1] = -0.896805;
147  }
148  else {
149  throw Exception::NotImplemented(">>> ROL::FreudensteinRoth : The index i must be between 0 and 1!");
150  }
151  return makePtr<StdVector<Real>>(xp);
152  }
153 
154  int getNumSolutions(void) const {
155  return 2;
156  }
157 };
158 
159 
160 } // End ZOO Namespace
161 } // End ROL Namespace
162 
163 #endif
Provides the interface to evaluate objective functions.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Ptr< Vector< Real > > getInitialGuess(void) const
Ptr< Vector< Real > > getSolution(const int i=0) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Contains definitions of test objective functions.
Ptr< Objective< Real > > getObjective(void) const
Freudenstein and Roth&#39;s function.
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.