ROL
ROL_FreudensteinRoth.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
49 #ifndef USE_HESSVEC
50 #define USE_HESSVEC 1
51 #endif
52 
53 #ifndef ROL_FREUDENSTEINROTH_HPP
54 #define ROL_FREUDENSTEINROTH_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_TestProblem.hpp"
58 
59 namespace ROL {
60 namespace ZOO {
61 
64 template<class Real>
65 class Objective_FreudensteinRoth : public Objective<Real> {
66 public:
68 
69  Real value( const Vector<Real> &x, Real &tol ) {
70  Ptr<const std::vector<Real> > ex
71  = dynamic_cast<const StdVector<Real>&>(x).getVector();
72 
73  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
74  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
75 
76  return f1*f1+f2*f2;
77  }
78 
79  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
80  Ptr<std::vector<Real> > eg
81  = dynamic_cast<StdVector<Real>&>(g).getVector();
82  Ptr<const std::vector<Real> > ex
83  = dynamic_cast<const StdVector<Real>&>(x).getVector();
84 
85  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
86  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
87 
88  Real f11 = 1.0;
89  Real f12 = 10.0*(*ex)[1] - 3.0*(*ex)[1]*(*ex)[1] - 2.0;
90  Real f21 = 1.0;
91  Real f22 = 3.0*(*ex)[1]*(*ex)[1] + 2.0*(*ex)[1] - 14.0;
92 
93  (*eg)[0] = 2.0*(f11*f1 + f21*f2);
94  (*eg)[1] = 2.0*(f12*f1 + f22*f2);
95  }
96 #if USE_HESSVEC
97  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
98  Ptr<std::vector<Real> > ehv
99  = dynamic_cast<StdVector<Real>&>(hv).getVector();
100  Ptr<const std::vector<Real> > ev
101  = dynamic_cast<const StdVector<Real>&>(v).getVector();
102  Ptr<const std::vector<Real> > ex
103  = dynamic_cast<const StdVector<Real>&>(x).getVector();
104 
105  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
106  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
107 
108  Real f11 = 1.0;
109  Real f12 = 10.0*(*ex)[1] - 3.0*(*ex)[1]*(*ex)[1] - 2.0;
110  Real f21 = 1.0;
111  Real f22 = 3.0*(*ex)[1]*(*ex)[1] + 2.0*(*ex)[1] - 14.0;
112 
113  Real f122 = 10.0 - 6.0*(*ex)[1];
114  Real f222 = 6.0*(*ex)[1] + 2.0;
115 
116  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
117  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
118  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
119 
120  (*ehv)[0] = h11*(*ev)[0] + h12*(*ev)[1];
121  (*ehv)[1] = h12*(*ev)[0] + h22*(*ev)[1];
122  }
123 #endif
124  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
125  Ptr<std::vector<Real> > ehv
126  = dynamic_cast<StdVector<Real>&>(hv).getVector();
127  Ptr<const std::vector<Real> > ev
128  = dynamic_cast<const StdVector<Real>&>(v).getVector();
129  Ptr<const std::vector<Real> > ex
130  = dynamic_cast<const StdVector<Real>&>(x).getVector();
131 
132  Real f1 = -13.0 + (*ex)[0] + ((5.0-(*ex)[1])*(*ex)[1] - 2.0)*(*ex)[1];
133  Real f2 = -29.0 + (*ex)[0] + (((*ex)[1]+1.0)*(*ex)[1] - 14.0)*(*ex)[1];
134 
135  Real f11 = 1.0;
136  Real f12 = 10.0*(*ex)[1] - 3.0*(*ex)[1]*(*ex)[1] - 2.0;
137  Real f21 = 1.0;
138  Real f22 = 3.0*(*ex)[1]*(*ex)[1] + 2.0*(*ex)[1] - 14.0;
139 
140  Real f122 = 10.0 - 6.0*(*ex)[1];
141  Real f222 = 6.0*(*ex)[1] + 2.0;
142 
143  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
144  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
145  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
146 
147  (*ehv)[0] = (1.0/(h11*h22-h12*h12))*( h22*(*ev)[0] - h12*(*ev)[1]);
148  (*ehv)[1] = (1.0/(h11*h22-h12*h12))*(-h12*(*ev)[0] + h11*(*ev)[1]);
149  }
150 };
151 
152 template<class Real>
153 class getFreudensteinRoth : public TestProblem<Real> {
154 public:
156 
157  Ptr<Objective<Real>> getObjective(void) const {
158  // Instantiate Objective Function
159  return makePtr<Objective_FreudensteinRoth<Real>>();
160  }
161 
162  Ptr<Vector<Real>> getInitialGuess(void) const {
163  // Problem dimension
164  int n = 2;
165  // Get Initial Guess
166  Ptr<std::vector<Real> > x0p = makePtr<std::vector<Real>>(n,0.0);
167  (*x0p)[0] = 0.5; (*x0p)[1] = -2.0;
168  return makePtr<StdVector<Real>>(x0p);
169  }
170 
171  Ptr<Vector<Real>> getSolution(const int i = 0) const {
172  // Problem dimension
173  int n = 2;
174  // Get Solution
175  Ptr<std::vector<Real> > xp = makePtr<std::vector<Real>>(n,0.0);
176  if (i == 0) {
177  (*xp)[0] = 5.0; (*xp)[1] = 4.0;
178  }
179  else if (i == 1) {
180  (*xp)[0] = 11.412779; (*xp)[1] = -0.896805;
181  }
182  else {
183  throw Exception::NotImplemented(">>> ROL::FreudensteinRoth : The index i must be between 0 and 1!");
184  }
185  return makePtr<StdVector<Real>>(xp);
186  }
187 
188  int getNumSolutions(void) const {
189  return 2;
190  }
191 };
192 
193 
194 } // End ZOO Namespace
195 } // End ROL Namespace
196 
197 #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:80
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.