Debian Codegraph @

packages/rol/adapters/sacado/src/function/ROL_Sacado_Objective.hpp

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