| 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 |
|
| 10 |
#ifndef ROL_SACADO_STDOBJECTIVE_HPP |
| 11 |
#define ROL_SACADO_STDOBJECTIVE_HPP |
| 12 |
|
| 13 |
#include "Sacado.hpp" |
| 14 |
#include "ROL_Objective.hpp" |
| 15 |
#include "ROL_StdVector.hpp" |
| 16 |
|
| 17 |
namespace ROL { |
| 18 |
|
| 19 |
/** \brief Generic StdObjective wrapper class for class that uses Sacado */ |
| 20 |
template<class Real, template<class> class Obj> |
| 21 |
class Sacado_StdObjective : public Objective<Real> { |
| 22 |
|
| 23 |
template <typename T> using vector = std::vector<T>; |
| 24 |
|
| 25 |
typedef Vector<Real> V; |
| 26 |
typedef StdVector<Real> SV; |
| 27 |
|
| 28 |
protected: |
| 29 |
|
| 30 |
// Template Object should inherit from ROL::StdObjective |
| 31 |
Obj<Real> obj_; |
| 32 |
|
| 33 |
/* Evaluate the gradient at x */ |
| 34 |
template<class ScalarT> |
| 35 |
void gradientAD( vector<ScalarT> &g, const vector<ScalarT> &x, Real &tol ); |
| 36 |
|
| 37 |
/* Compute the action of the Hessian evaluated at x on a vector v */ |
| 38 |
template<class ScalarT> |
| 39 |
void hessVecAD( vector<ScalarT> &hv, const vector<ScalarT> &v, |
| 40 |
const vector<ScalarT> &x, Real &tol ); |
| 41 |
|
| 42 |
public: |
| 43 |
|
| 44 |
/* Evaluate the objective function at x */ |
| 45 |
using Objective<Real>::value; |
| 46 |
Real value( const V &x, Real &tol ) { |
| 47 |
const SV xs = dynamic_cast<const SV&>(x); |
| 48 |
return value(*(xs.getVector()), tol); |
| 49 |
} |
| 50 |
|
| 51 |
Real value( const vector<Real> &x, Real &tol ) { |
| 52 |
return obj_.value(x,tol); |
| 53 |
} |
| 54 |
|
| 55 |
/* Evaluate the gradient at x */ |
| 56 |
using Objective<Real>::gradient; |
| 57 |
void gradient( V &g, const V &x, Real &tol ) { |
| 58 |
SV gs = dynamic_cast<SV&>(g); |
| 59 |
const SV xs = dynamic_cast<const SV&>(x); |
| 60 |
this->gradient(*(gs.getVector()),*(xs.getVector()),tol); |
| 61 |
} |
| 62 |
|
| 63 |
void gradient( vector<Real> &g, const vector<Real> &x, Real &tol ) { |
| 64 |
this->gradientAD(g,x,tol); |
| 65 |
} |
| 66 |
|
| 67 |
/* Compute the action of the Hessian evaluated at x on a vector v */ |
| 68 |
using Objective<Real>::hessVec; |
| 69 |
void hessVec( V &hv, const V &v, const V &x, Real &tol ) { |
| 70 |
SV hvs = dynamic_cast<SV&>(hv); |
| 71 |
const SV vs = dynamic_cast<const SV&>(v); |
| 72 |
const SV xs = dynamic_cast<const SV&>(x); |
| 73 |
hessVec(*(hvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol); |
| 74 |
} |
| 75 |
|
| 76 |
void hessVec( vector<Real> &hv, const vector<Real> &v, const vector<Real> &x, Real &tol ) { |
| 77 |
this->hessVecAD(hv,v,x,tol); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
template<class Real, template<class> class Obj> |
| 84 |
template<class ScalarT> |
| 85 |
void Sacado_StdObjective<Real,Obj>::gradientAD(vector<ScalarT> &g, const vector<ScalarT> &x, Real &tol) { |
| 86 |
|
| 87 |
// Data type which supports automatic differentiation |
| 88 |
typedef Sacado::Fad::DFad<ScalarT> FadType; |
| 89 |
typedef vector<FadType> Fadvector; |
| 90 |
|
| 91 |
// Get a pointer to the gradient vector |
| 92 |
int n = x.size(); |
| 93 |
|
| 94 |
// Create a vector of independent variables |
| 95 |
ROL::Ptr<Fadvector> x_fad = ROL::makePtr<Fadvector>(); |
| 96 |
|
| 97 |
x_fad->reserve(n); |
| 98 |
|
| 99 |
// Initialize constructor for each element |
| 100 |
for(int i=0; i<n; ++i) { |
| 101 |
x_fad->push_back(FadType(n,i,x[i])); |
| 102 |
} |
| 103 |
|
| 104 |
// AD access to objective function |
| 105 |
FadType J_fad = obj_.value(*x_fad,tol); |
| 106 |
|
| 107 |
// Evaluate gradient |
| 108 |
for(int i=0; i<n; ++i) { |
| 109 |
g[i] = J_fad.dx(i); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
template <class Real, template<class> class Obj> |
| 116 |
template <class ScalarT> |
| 117 |
void Sacado_StdObjective<Real,Obj>::hessVecAD( vector<ScalarT> &hv, const vector<ScalarT> &v, |
| 118 |
const vector<ScalarT> &x, Real &tol ) { |
| 119 |
|
| 120 |
// Data type which supports automatic differentiation |
| 121 |
typedef Sacado::Fad::SFad<ScalarT,1> FadType; |
| 122 |
typedef vector<FadType> Fadvector; |
| 123 |
|
| 124 |
int n = x.size(); |
| 125 |
|
| 126 |
// Create a vector of independent variables |
| 127 |
ROL::Ptr<Fadvector> x_fad = ROL::makePtr<Fadvector>(); |
| 128 |
|
| 129 |
x_fad->reserve(n); |
| 130 |
|
| 131 |
// Allocate for gradient |
| 132 |
Fadvector g_fad(n); |
| 133 |
|
| 134 |
for(int i=0; i<n; ++i) { |
| 135 |
x_fad->push_back( FadType(1,x[i]) ); |
| 136 |
} |
| 137 |
|
| 138 |
// Set directional derivative |
| 139 |
for(int i=0; i<n; ++i) { |
| 140 |
(*x_fad)[i].fastAccessDx(0) = v[i]; |
| 141 |
} |
| 142 |
|
| 143 |
this->gradientAD(g_fad,*x_fad,tol); |
| 144 |
|
| 145 |
for(int i=0; i<n; ++i) { |
| 146 |
hv[i] = g_fad[i].dx(0); |
| 147 |
} |
| 148 |
|
| 149 |
} // class Sacado_StdObjective |
| 150 |
|
| 151 |
} // namespace ROL |
| 152 |
|
| 153 |
#endif // ROL_SACADO_STDOBJECTIVE_HPP |