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_Objective.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  StdVector<Real> & ex =
71  Teuchos::dyn_cast<StdVector<Real> >(const_cast <Vector<Real> &>(x));
72  Teuchos::RCP<const std::vector<Real> > xp = ex.getVector();
73 
74  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
75  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
76 
77  return f1*f1+f2*f2;
78  }
79 
80  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
81  Teuchos::RCP<const std::vector<Real> > xp =
82  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
83  Teuchos::RCP<std::vector<Real> > gp =
84  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(g)).getVector());
85 
86  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
87  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
88 
89  Real f11 = 1.0;
90  Real f12 = 10.0*(*xp)[1] - 3.0*(*xp)[1]*(*xp)[1] - 2.0;
91  Real f21 = 1.0;
92  Real f22 = 3.0*(*xp)[1]*(*xp)[1] + 2.0*(*xp)[1] - 14.0;
93 
94  (*gp)[0] = 2.0*(f11*f1 + f21*f2);
95  (*gp)[1] = 2.0*(f12*f1 + f22*f2);
96  }
97 #if USE_HESSVEC
98  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
99  Teuchos::RCP<const std::vector<Real> > xp =
100  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
101  Teuchos::RCP<const std::vector<Real> > vp =
102  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
103  Teuchos::RCP<std::vector<Real> > hvp =
104  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
105 
106  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
107  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
108 
109  Real f11 = 1.0;
110  Real f12 = 10.0*(*xp)[1] - 3.0*(*xp)[1]*(*xp)[1] - 2.0;
111  Real f21 = 1.0;
112  Real f22 = 3.0*(*xp)[1]*(*xp)[1] + 2.0*(*xp)[1] - 14.0;
113 
114  Real f122 = 10.0 - 6.0*(*xp)[1];
115  Real f222 = 6.0*(*xp)[1] + 2.0;
116 
117  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
118  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
119  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
120 
121  (*hvp)[0] = h11*(*vp)[0] + h12*(*vp)[1];
122  (*hvp)[1] = h12*(*vp)[0] + h22*(*vp)[1];
123  }
124 #endif
125  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
126  Teuchos::RCP<const std::vector<Real> > xp =
127  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
128  Teuchos::RCP<const std::vector<Real> > vp =
129  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
130  Teuchos::RCP<std::vector<Real> > hvp =
131  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
132 
133  Real f1 = -13.0 + (*xp)[0] + ((5.0-(*xp)[1])*(*xp)[1] - 2.0)*(*xp)[1];
134  Real f2 = -29.0 + (*xp)[0] + (((*xp)[1]+1.0)*(*xp)[1] - 14.0)*(*xp)[1];
135 
136  Real f11 = 1.0;
137  Real f12 = 10.0*(*xp)[1] - 3.0*(*xp)[1]*(*xp)[1] - 2.0;
138  Real f21 = 1.0;
139  Real f22 = 3.0*(*xp)[1]*(*xp)[1] + 2.0*(*xp)[1] - 14.0;
140 
141  Real f122 = 10.0 - 6.0*(*xp)[1];
142  Real f222 = 6.0*(*xp)[1] + 2.0;
143 
144  Real h11 = 2.0*(f11*f11) + 2.0*(f21*f21);
145  Real h12 = 2.0*(f12*f11) + 2.0*(f22*f21);
146  Real h22 = 2.0*(f122*f1 + f12*f12) + 2.0*(f222*f2 + f22*f22);
147 
148  (*hvp)[0] = (1.0/(h11*h22-h12*h12))*( h22*(*vp)[0] - h12*(*vp)[1]);
149  (*hvp)[1] = (1.0/(h11*h22-h12*h12))*(-h12*(*vp)[0] + h11*(*vp)[1]);
150  }
151  };
152 
153  template<class Real>
154  void getFreudensteinRoth( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
155  // Cast Initial Guess and Solution Vectors
156  Teuchos::RCP<std::vector<Real> > x0p =
157  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x0)).getVector());
158  Teuchos::RCP<std::vector<Real> > xp =
159  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x)).getVector());
160  int n = xp->size();
161  // Resize Vectors
162  n = 2;
163  x0p->resize(n);
164  xp->resize(n);
165  // Instantiate Objective Function
166  obj = Teuchos::rcp( new Objective_FreudensteinRoth<Real> );
167  // Get Initial Guess
168  (*x0p)[0] = 0.5;
169  (*x0p)[1] = -2.0;
170  // Get Solution
171  (*xp)[0] = 5.0;
172  (*xp)[1] = 4.0;
173  }
174 
175 
176 } // End ZOO Namespace
177 } // End ROL Namespace
178 
179 #endif
Provides the interface to evaluate objective functions.
Teuchos::RCP< const std::vector< Element > > getVector() const
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Provides the std::vector implementation of the ROL::Vector interface.
Freudenstein and Roth'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 getFreudensteinRoth(Teuchos::RCP< Objective< Real > > &obj, Vector< Real > &x0, Vector< Real > &x)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.