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 
23 using namespace ROL;
24 
25 template<class ScalarT>
27 
28 
29 
30  public:
31 
32  ScalarT eval(const std::vector<ScalarT> &x);
33 
34 };
35 
46 template<class ScalarT>
47 ScalarT FunctionZakharov<ScalarT>::eval(const std::vector<ScalarT> & x) {
48 
49  typedef typename std::vector<ScalarT>::size_type uint;
50 
51  ScalarT xdotx = 0;
52  ScalarT kdotx = 0;
53  ScalarT J = 0;
54 
55  // Compute dot products
56  for(uint 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 
68 template<class Real>
69 class Zakharov_Sacado_Objective : public Objective<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 uint;
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  uint n = xp->size();
121 
122  std::vector<GradType> x_grad(n);
123 
124  for(uint 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(uint 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  uint n = xp->size();
146 
147  std::vector<HessVecType> x_hessvec(n);
148 
149  for(uint 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(uint 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 
Provides the interface to evaluate objective functions.
StdVector< Real > SV
Definition: example_01b.hpp:73
FunctionZakharov< HessVecType > zfuncHessVec_
Definition: example_01b.hpp:91
ScalarT eval(const std::vector< ScalarT > &x)
A Sacado-accessible version of the Zakharov function to differentiate Where .
Definition: example_01b.hpp:47
typename PV< Real >::size_type size_type
FunctionZakharov< Real > zfunc_
Definition: example_01b.hpp:89
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
FunctionZakharov< GradType > zfuncGrad_
Definition: example_01b.hpp:90
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
ROL::Ptr< vector > getVector(V &x)
Definition: example_01b.hpp:98
Sacado::Fad::SFad< Real, 1 > DirDerivType
Definition: example_01b.hpp:76
vector::size_type uint
Definition: example_01b.hpp:79
ROL::Ptr< const vector > getVector(const V &x)
Definition: example_01b.hpp:93
Sacado::Fad::DFad< Real > GradType
Definition: example_01b.hpp:75
std::vector< Real > vector
Definition: example_01b.hpp:71
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Sacado::Fad::DFad< DirDerivType > HessVecType
Definition: example_01b.hpp:77
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.