ROL
example_01b.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
54#include "ROL_StdVector.hpp"
55#include "Sacado.hpp"
56
57using namespace ROL;
58
59template<class ScalarT>
61
62
63
64 public:
65
66 ScalarT eval(const std::vector<ScalarT> &x);
67
68};
69
80template<class ScalarT>
81ScalarT FunctionZakharov<ScalarT>::eval(const std::vector<ScalarT> & x) {
82
83 typedef typename std::vector<ScalarT>::size_type uint;
84
85 ScalarT xdotx = 0;
86 ScalarT kdotx = 0;
87 ScalarT J = 0;
88
89 // Compute dot products
90 for(uint i=0; i<x.size(); ++i) {
91 xdotx += pow(x[i],2); // (k,x)
92 kdotx += ScalarT(i+1)*x[i]; // (x,x)
93 }
94
95 // Sum terms in objective function
96 J = xdotx + pow(kdotx,2)/4.0 + pow(kdotx,4)/16.0;
97
98 return J;
99}
100
101
102template<class Real>
104
105 typedef std::vector<Real> vector;
108
109 typedef Sacado::Fad::DFad<Real> GradType;
110 typedef Sacado::Fad::SFad<Real,1> DirDerivType;
111 typedef Sacado::Fad::DFad<DirDerivType> HessVecType;
112
113 typedef typename vector::size_type uint;
114
115 // In C++11, we could use template typedefs:
116 // template <typename T> using GradTypeT = Sacado::Fad::DFad<T>;
117 // typedef Sacado::Fad::SFad<Real,1> DirDerivType;
118 // typedef GradTypeT<Real> GradType;
119 // typedef GradTypeT<DirDerivType> HessVecType;
120
121 private:
122
126
127 ROL::Ptr<const vector> getVector( const V& x ) {
128
129 return dynamic_cast<const SV&>(x).getVector();
130 }
131
132 ROL::Ptr<vector> getVector( V& x ) {
133
134 return dynamic_cast<SV&>(x).getVector();
135 }
136
137 public:
138
140
141 /* Evaluate the objective function at x */
142 Real value( const Vector<Real> &x, Real &tol ) {
143
144 ROL::Ptr<const vector> xp = getVector(x);
145 return zfunc_.eval(*xp);
146 }
147
148 /* Evaluate the gradient at x */
149 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
150
151 ROL::Ptr<const vector> xp = getVector(x);
152 ROL::Ptr<vector> gp = getVector(g);
153
154 uint n = xp->size();
155
156 std::vector<GradType> x_grad(n);
157
158 for(uint i=0; i<n; ++i) {
159 x_grad[i] = (*xp)[i]; // Set values x(i).
160 x_grad[i].diff(i,n); // Choose canonical directions.
161 }
162
163 GradType J_grad = zfuncGrad_.eval(x_grad);
164
165 for(uint i=0; i<n; ++i) {
166 (*gp)[i] = J_grad.dx(i);
167 }
168
169 }
170
171 /* Compute the action of the Hessian evaluated at x on a vector v */
172 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
173
174
175 ROL::Ptr<vector> hvp = getVector(hv);
176 ROL::Ptr<const vector> vp = getVector(v);
177 ROL::Ptr<const vector> xp = getVector(x);
178
179 uint n = xp->size();
180
181 std::vector<HessVecType> x_hessvec(n);
182
183 for(uint i=0; i<n; ++i) {
184 DirDerivType tmp(1,(*xp)[i]); // Set values x(i).
185 tmp.fastAccessDx(0)= (*vp)[i]; // Set direction values v(i).
186 x_hessvec[i] = tmp; // Use tmp to define hessvec-able x.
187 x_hessvec[i].diff(i,n); // Choose directions.
188 }
189
190 // Compute Hessian-vector product (and other currently irrelevant things).
191 HessVecType J_hessvec = zfuncHessVec_.eval(x_hessvec);
192
193 for(uint i=0; i<n; ++i) {
194 (*hvp)[i] = (J_hessvec.dx(i)).fastAccessDx(0);
195 // hessvec = get gradient get dir deriv
196 }
197 }
198};
199
ScalarT eval(const std::vector< ScalarT > &x)
A Sacado-accessible version of the Zakharov function to differentiate.
Definition: example_01b.hpp:81
Provides the interface to evaluate objective functions.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84
ROL::Ptr< const vector > getVector(const V &x)
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
FunctionZakharov< HessVecType > zfuncHessVec_
Sacado::Fad::DFad< Real > GradType
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Sacado::Fad::SFad< Real, 1 > DirDerivType
Real value(const Vector< Real > &x, Real &tol)
Compute value.
StdVector< Real > SV
std::vector< Real > vector
FunctionZakharov< GradType > zfuncGrad_
FunctionZakharov< Real > zfunc_
ROL::Ptr< vector > getVector(V &x)
vector::size_type uint
Sacado::Fad::DFad< DirDerivType > HessVecType