ROL
example_01b.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
20#include "ROL_StdVector.hpp"
21#include "Sacado.hpp"
22
23using namespace ROL;
24
25template<class ScalarT>
27
28
29
30 public:
31
32 ScalarT eval(const std::vector<ScalarT> &x);
33
34};
35
46template<class ScalarT>
47ScalarT FunctionZakharov<ScalarT>::eval(const std::vector<ScalarT> & x) {
48
49 typedef typename std::vector<ScalarT>::size_type luint;
50
51 ScalarT xdotx = 0;
52 ScalarT kdotx = 0;
53 ScalarT J = 0;
54
55 // Compute dot products
56 for(luint i=0; i<x.size(); ++i) {
57 xdotx += pow(x[i],2); // (k,x)
58 kdotx += ScalarT(i+1)*x[i]; // (x,x)
59 }
60
61 // Sum terms in objective function
62 J = xdotx + pow(kdotx,2)/4.0 + pow(kdotx,4)/16.0;
63
64 return J;
65}
66
67
68template<class Real>
70
71 typedef std::vector<Real> vector;
72 typedef Vector<Real> V;
74
75 typedef Sacado::Fad::DFad<Real> GradType;
76 typedef Sacado::Fad::SFad<Real,1> DirDerivType;
77 typedef Sacado::Fad::DFad<DirDerivType> HessVecType;
78
79 typedef typename vector::size_type luint;
80
81 // In C++11, we could use template typedefs:
82 // template <typename T> using GradTypeT = Sacado::Fad::DFad<T>;
83 // typedef Sacado::Fad::SFad<Real,1> DirDerivType;
84 // typedef GradTypeT<Real> GradType;
85 // typedef GradTypeT<DirDerivType> HessVecType;
86
87 private:
88
92
93 ROL::Ptr<const vector> getVector( const V& x ) {
94
95 return dynamic_cast<const SV&>(x).getVector();
96 }
97
98 ROL::Ptr<vector> getVector( V& x ) {
99
100 return dynamic_cast<SV&>(x).getVector();
101 }
102
103 public:
104
106
107 /* Evaluate the objective function at x */
108 Real value( const Vector<Real> &x, Real &tol ) {
109
110 ROL::Ptr<const vector> xp = getVector(x);
111 return zfunc_.eval(*xp);
112 }
113
114 /* Evaluate the gradient at x */
115 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
116
117 ROL::Ptr<const vector> xp = getVector(x);
118 ROL::Ptr<vector> gp = getVector(g);
119
120 luint n = xp->size();
121
122 std::vector<GradType> x_grad(n);
123
124 for(luint i=0; i<n; ++i) {
125 x_grad[i] = (*xp)[i]; // Set values x(i).
126 x_grad[i].diff(i,n); // Choose canonical directions.
127 }
128
129 GradType J_grad = zfuncGrad_.eval(x_grad);
130
131 for(luint i=0; i<n; ++i) {
132 (*gp)[i] = J_grad.dx(i);
133 }
134
135 }
136
137 /* Compute the action of the Hessian evaluated at x on a vector v */
138 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
139
140
141 ROL::Ptr<vector> hvp = getVector(hv);
142 ROL::Ptr<const vector> vp = getVector(v);
143 ROL::Ptr<const vector> xp = getVector(x);
144
145 luint n = xp->size();
146
147 std::vector<HessVecType> x_hessvec(n);
148
149 for(luint i=0; i<n; ++i) {
150 DirDerivType tmp(1,(*xp)[i]); // Set values x(i).
151 tmp.fastAccessDx(0)= (*vp)[i]; // Set direction values v(i).
152 x_hessvec[i] = tmp; // Use tmp to define hessvec-able x.
153 x_hessvec[i].diff(i,n); // Choose directions.
154 }
155
156 // Compute Hessian-vector product (and other currently irrelevant things).
157 HessVecType J_hessvec = zfuncHessVec_.eval(x_hessvec);
158
159 for(luint i=0; i<n; ++i) {
160 (*hvp)[i] = (J_hessvec.dx(i)).fastAccessDx(0);
161 // hessvec = get gradient get dir deriv
162 }
163 }
164};
165
ScalarT eval(const std::vector< ScalarT > &x)
A Sacado-accessible version of the Zakharov function to differentiate.
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.
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.
std::vector< Real > vector
FunctionZakharov< GradType > zfuncGrad_
FunctionZakharov< Real > zfunc_
ROL::Ptr< vector > getVector(V &x)
Sacado::Fad::DFad< DirDerivType > HessVecType
vector::size_type luint