Debian Codegraph @

packages/rol/adapters/sacado/src/function/ROL_Sacado_Objective_SimOpt.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_SimOpt.hpp"
16
17 using namespace ROL;
18
19 template <class Real, template<class> class Obj>
20 class Sacado_Objective_SimOpt : public Objective_SimOpt<Real> {
21
22 protected:
23 Obj<Real> obj_;
24
25 template<class ScalarT>
26 void gradient_1AD(Vector<ScalarT> &g, const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol);
27
28 template<class ScalarT>
29 void gradient_2AD(Vector<ScalarT> &g, const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol);
30
31 template<class ScalarT>
32 void hessVec_11AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol);
33
34 template<class ScalarT>
35 void hessVec_12AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol);
36
37 template<class ScalarT>
38 void hessVec_21AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol);
39
40 template<class ScalarT>
41 void hessVec_22AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v, const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol);
42
43
44 public:
45
46
47 Sacado_Objective_SimOpt() : obj_(Obj<Real>()) {}
48 Sacado_Objective_SimOpt(const Obj<Real> &obj) : obj_(obj) {}
49
50
51 Real value(const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
52 return obj_.value(u,z,tol);
53 }
54
55 void gradient_1(Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
56 this->gradient_1AD(g,u,z,tol);
57 }
58
59 void gradient_2(Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
60 this->gradient_2AD(g,u,z,tol);
61 }
62
63 void hessVec_11(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
64 this->hessVec_11AD(hv,v,u,z,tol);
65 }
66
67 void hessVec_12(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
68 this->hessVec_12AD(hv,v,u,z,tol);
69 }
70
71 void hessVec_21(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
72 this->hessVec_21AD(hv,v,u,z,tol);
73 }
74
75 void hessVec_22(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
76 this->hessVec_22AD(hv,v,u,z,tol);
77 }
78
79 };
80
81
82 template<class Real, template<class> class Obj>
83 template<class ScalarT>
84 void Sacado_Objective_SimOpt<Real,Obj>::gradient_1AD(Vector<ScalarT> &g, const Vector<ScalarT> &u,
85 const Vector<ScalarT> &z, Real &tol) {
86 typedef Sacado::Fad::DFad<ScalarT> FadType;
87 typedef std::vector<FadType> Fadvector;
88 typedef std::vector<ScalarT> vector;
89 typedef StdVector<ScalarT> SV;
90
91
92
93
94 ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector();
95 ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector();
96
97 // Get a pointer to the gradient vector
98 ROL::Ptr<vector> gp = dynamic_cast<SV&>(g).getVector();
99
100 int m = zp->size();
101 int n = up->size();
102
103 ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>();
104 ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>();
105
106 u_fad_ptr->reserve(n);
107 z_fad_ptr->reserve(m);
108
109 // Initialize constructor for each element of u
110 for(int i=0; i<n; ++i) {
111 u_fad_ptr->push_back(FadType(n,i,(*up)[i]));
112 }
113
114 // Initialize constructor for each element of z
115 for(int j=0; j<m; ++j) {
116 z_fad_ptr->push_back((*zp)[j]);
117 }
118
119 StdVector<FadType> u_fad(u_fad_ptr);
120 StdVector<FadType> z_fad(z_fad_ptr);
121
122 FadType J_fad = obj_.value(u_fad,z_fad,tol);
123
124 // Evaluate gradient
125 for(int i=0; i<n; ++i) {
126 (*gp)[i] = J_fad.dx(i);
127 }
128 }
129
130
131 template<class Real, template<class> class Obj>
132 template<class ScalarT>
133 void Sacado_Objective_SimOpt<Real,Obj>::gradient_2AD(Vector<ScalarT> &g, const Vector<ScalarT> &u,
134 const Vector<ScalarT> &z, Real &tol) {
135 typedef Sacado::Fad::DFad<ScalarT> FadType;
136 typedef std::vector<FadType> Fadvector;
137 typedef std::vector<ScalarT> vector;
138 typedef StdVector<ScalarT> SV;
139
140
141
142
143 ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector();
144 ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector();
145
146 // Get a pointer to the gradient vector
147 ROL::Ptr<vector> gp = dynamic_cast<SV&>(g).getVector();
148
149 int m = zp->size();
150 int n = up->size();
151
152 ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>();
153 ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>();
154
155 u_fad_ptr->reserve(n);
156 z_fad_ptr->reserve(m);
157
158 // Initialize constructor for each element of u
159 for(int i=0; i<n; ++i) {
160 u_fad_ptr->push_back((*up)[i]);
161 }
162
163 // Initialize constructor for each element of z
164 for(int j=0; j<m; ++j) {
165 z_fad_ptr->push_back(FadType(m,j,(*zp)[j]));
166 }
167
168 StdVector<FadType> u_fad(u_fad_ptr);
169 StdVector<FadType> z_fad(z_fad_ptr);
170
171 FadType J_fad = obj_.value(u_fad,z_fad,tol);
172
173 // Evaluate gradient
174 for(int j=0; j<m; ++j) {
175 (*gp)[j] = J_fad.dx(j);
176 }
177 }
178
179
180
181 template <class Real, template<class> class Obj>
182 template <class ScalarT>
183 void Sacado_Objective_SimOpt<Real,Obj>::hessVec_11AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v,
184 const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol) {
185
186 typedef Sacado::Fad::SFad<ScalarT,1> FadType;
187 typedef std::vector<FadType> Fadvector;
188 typedef std::vector<ScalarT> vector;
189 typedef StdVector<ScalarT> SV;
190
191
192
193
194 ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector();
195 ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector();
196 ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector();
197 ROL::Ptr<vector> hvp = dynamic_cast<SV&>(hv).getVector();
198
199 int n = up->size(); // vp and hvp have this size also
200 int m = zp->size();
201
202 ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>();
203 ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>();
204 ROL::Ptr<Fadvector> g_fad_ptr = ROL::makePtr<Fadvector>();
205
206 u_fad_ptr->reserve(n);
207 z_fad_ptr->reserve(m);
208 g_fad_ptr->reserve(n);
209
210 // Initialize constructor for each element of u
211 for(int i=0; i<n; ++i) {
212 u_fad_ptr->push_back(FadType(1,(*up)[i]));
213 (*u_fad_ptr)[i].fastAccessDx(0) = (*vp)[i];
214 g_fad_ptr->push_back(0);
215 }
216
217 // Initialize constructor for each element of z
218 for(int j=0; j<m; ++j) {
219 z_fad_ptr->push_back((*zp)[j]);
220 }
221
222 StdVector<FadType> u_fad(u_fad_ptr);
223 StdVector<FadType> z_fad(z_fad_ptr);
224 StdVector<FadType> g_fad(g_fad_ptr);
225
226 this->gradient_1AD(g_fad,u_fad,z_fad,tol);
227
228 for(int i=0; i<n; ++i) {
229 (*hvp)[i] = (*g_fad_ptr)[i].dx(0);
230 }
231
232 }
233
234
235 template <class Real, template<class> class Obj>
236 template <class ScalarT>
237 void Sacado_Objective_SimOpt<Real,Obj>::hessVec_12AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v,
238 const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol) {
239 typedef Sacado::Fad::DFad<ScalarT> FadType;
240 typedef std::vector<FadType> Fadvector;
241 typedef std::vector<ScalarT> vector;
242 typedef StdVector<ScalarT> SV;
243
244
245
246
247 ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector();
248 ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector();
249 ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector();
250 ROL::Ptr<vector> hvp = dynamic_cast<SV&>(hv).getVector();
251
252 int n = up->size();
253 int m = zp->size();
254
255 ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>();
256 ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>();
257 ROL::Ptr<Fadvector> g_fad_ptr = ROL::makePtr<Fadvector>();
258
259 u_fad_ptr->reserve(n);
260 z_fad_ptr->reserve(m);
261 g_fad_ptr->reserve(n);
262
263 // Initialize constructor for each element of u
264 for(int i=0; i<n; ++i) {
265 u_fad_ptr->push_back((*up)[i]);
266 g_fad_ptr->push_back(0);
267 }
268
269 // Initialize constructor for each element of z
270 for(int j=0; j<m; ++j) {
271 z_fad_ptr->push_back(FadType(m,j,(*zp)[j]));
272 }
273
274 StdVector<FadType> u_fad(u_fad_ptr);
275 StdVector<FadType> z_fad(z_fad_ptr);
276 StdVector<FadType> g_fad(g_fad_ptr);
277
278 this->gradient_1AD(g_fad,u_fad,z_fad,tol);
279
280 FadType vdotg = 0;
281
282 for(int i=0; i<n; ++i) {
283 vdotg += (*vp)[i]*(*g_fad_ptr)[i];
284 }
285
286 for(int j=0; j<m; ++j) {
287 (*hvp)[j] = vdotg.dx(j);
288 }
289 }
290
291
292
293 template <class Real, template<class> class Obj>
294 template <class ScalarT>
295 void Sacado_Objective_SimOpt<Real,Obj>::hessVec_21AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v,
296 const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol) {
297 typedef Sacado::Fad::DFad<ScalarT> FadType;
298 typedef std::vector<FadType> Fadvector;
299 typedef std::vector<ScalarT> vector;
300 typedef StdVector<ScalarT> SV;
301
302
303
304
305 ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector();
306 ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector();
307 ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector();
308 ROL::Ptr<vector> hvp = dynamic_cast<SV&>(hv).getVector();
309
310 int n = up->size();
311 int m = zp->size();
312
313 ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>();
314 ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>();
315 ROL::Ptr<Fadvector> g_fad_ptr = ROL::makePtr<Fadvector>();
316
317 u_fad_ptr->reserve(n);
318 z_fad_ptr->reserve(m);
319 g_fad_ptr->reserve(m);
320
321 // Initialize constructor for each element of u
322 for(int i=0; i<n; ++i) {
323 u_fad_ptr->push_back(FadType(n,i,(*up)[i]));
324 }
325
326 // Initialize constructor for each element of z
327 for(int j=0; j<m; ++j) {
328 z_fad_ptr->push_back((*zp)[j]);
329 g_fad_ptr->push_back(0);
330 }
331
332 StdVector<FadType> u_fad(u_fad_ptr);
333 StdVector<FadType> z_fad(z_fad_ptr);
334 StdVector<FadType> g_fad(g_fad_ptr);
335
336 this->gradient_2AD(g_fad,u_fad,z_fad,tol);
337
338 FadType vdotg = 0;
339
340 for(int j=0; j<m; ++j) {
341 vdotg += (*vp)[j]*(*g_fad_ptr)[j];
342 }
343
344 for(int i=0; i<n; ++i) {
345 (*hvp)[i] = vdotg.dx(i);
346 }
347 }
348
349
350
351
352
353 template <class Real, template<class> class Obj>
354 template <class ScalarT>
355 void Sacado_Objective_SimOpt<Real,Obj>::hessVec_22AD(Vector<ScalarT> &hv, const Vector<ScalarT> &v,
356 const Vector<ScalarT> &u, const Vector<ScalarT> &z, Real &tol) {
357
358 typedef Sacado::Fad::SFad<ScalarT,1> FadType;
359 typedef std::vector<FadType> Fadvector;
360 typedef std::vector<ScalarT> vector;
361 typedef StdVector<ScalarT> SV;
362
363
364
365
366 ROL::Ptr<const vector> up = dynamic_cast<const SV&>(u).getVector();
367 ROL::Ptr<const vector> zp = dynamic_cast<const SV&>(z).getVector();
368 ROL::Ptr<const vector> vp = dynamic_cast<const SV&>(v).getVector();
369 ROL::Ptr<vector> hvp = dynamic_cast<SV&>(hv).getVector();
370
371 int n = up->size();
372 int m = zp->size(); // vp and hvp have this size also
373
374
375 ROL::Ptr<Fadvector> u_fad_ptr = ROL::makePtr<Fadvector>();
376 ROL::Ptr<Fadvector> z_fad_ptr = ROL::makePtr<Fadvector>();
377 ROL::Ptr<Fadvector> g_fad_ptr = ROL::makePtr<Fadvector>();
378
379 u_fad_ptr->reserve(n);
380 z_fad_ptr->reserve(m);
381 g_fad_ptr->reserve(m);
382
383 // Initialize constructor for each element of u
384 for(int i=0; i<n; ++i) {
385 u_fad_ptr->push_back((*up)[i]);
386 }
387
388 // Initialize constructor for each element of z
389 for(int j=0; j<m; ++j) {
390 z_fad_ptr->push_back(FadType(1,(*zp)[j]));
391 (*z_fad_ptr)[j].fastAccessDx(0) = (*vp)[j];
392 g_fad_ptr->push_back(0);
393 }
394
395 StdVector<FadType> u_fad(u_fad_ptr);
396 StdVector<FadType> z_fad(z_fad_ptr);
397 StdVector<FadType> g_fad(g_fad_ptr);
398
399 this->gradient_2AD(g_fad,u_fad,z_fad,tol);
400
401 for(int j=0; j<m; ++j) {
402 (*hvp)[j] = (*g_fad_ptr)[j].dx(0);
403 }
404
405 }
406
407
408
409 #endif