ROL
ROL_DynamicObjectiveCheck.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 
10 #pragma once
11 #ifndef ROL_DYNAMICOBJECTIVECHECK_HPP
12 #define ROL_DYNAMICOBJECTIVECHECK_HPP
13 
15 #include "ROL_ValidateFunction.hpp"
16 #include <string>
17 
18 // TODO: Add symmetry check for diagonal Hessian blocks and adjoint consistency
19 // for off-diagonal block pairs
20 
21 namespace ROL {
22 
23 template<typename Real>
25 
26  static void check( DynamicObjective<Real>& obj,
27  ValidateFunction<Real>& validator,
28  const Vector<Real>& uo,
29  const Vector<Real>& un,
30  const Vector<Real>& z,
31  const std::vector<std::string>& methods ) {
32 
33  auto obj_check = make_check( obj );
34  check( obj_check, validator, uo, un, z, methods );
35  }
36 
37  static void check( DynamicObjective<Real>& obj,
38  ValidateFunction<Real>& validator,
39  const Vector<Real>& uo,
40  const Vector<Real>& un,
41  const Vector<Real>& z,
42  TimeStamp<Real>& timeStamp,
43  const std::vector<std::string>& methods ) {
44 
45  auto obj_check = make_check( obj, timeStamp );
46  check( obj_check, validator, uo, un, z, methods );
47  }
48 
49  static void check( DynamicObjective_CheckInterface<Real>& obj_check,
50  ValidateFunction<Real>& validator,
51  const Vector<Real>& uo,
52  const Vector<Real>& un,
53  const Vector<Real>& z,
54  const std::vector<std::string>& methods ) {
55 
56  auto gu = uo.dual().clone();
57  auto gz = z.dual().clone();
58  auto vu = uo.clone();
59  auto vz = z.clone();
60 
61  vu->randomize();
62  vz->randomize();
63 
64 
65  //-------------------------------------------------------------------------
66  // Check gradient components
67  if( std::find(methods.begin(),methods.end(),"gradient_uo") != methods.end() ) {
68  auto value = obj_check.value_uo( un, z );
69  auto grad = obj_check.gradient_uo( un, z );
70  auto update = obj_check.update_uo( un, z );
71  validator.derivative_check( value, grad, update, *gu, *vu, uo, "grad_uo'*dir" );
72  }
73  if( std::find(methods.begin(),methods.end(),"gradient_un") != methods.end() ) {
74  auto value = obj_check.value_un( uo, z );
75  auto grad = obj_check.gradient_un( uo, z );
76  auto update = obj_check.update_un( uo, z );
77  validator.derivative_check( value, grad, update, *gu, *vu, un, "grad_un'*dir" );
78  }
79  if( std::find(methods.begin(),methods.end(),"gradient_z") != methods.end() ) {
80  auto value = obj_check.value_z( uo, un );
81  auto grad = obj_check.gradient_z( uo, un );
82  auto update = obj_check.update_z( uo, un );
83  validator.derivative_check( value, grad, update, *gz, *vz, z, "grad_z'*dir" );
84  }
85 
86  //-------------------------------------------------------------------------
87  // Check Hessian components
88  if( std::find(methods.begin(),methods.end(),"hessVec_uo_uo") != methods.end() ) {
89  auto grad = obj_check.gradient_uo_uo( un, z );
90  auto hessVec = obj_check.hessVec_uo_uo( un, z );
91  auto update = obj_check.update_uo( un, z );
92  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_uo_uo*vec)" );
93  }
94 
95  if( std::find(methods.begin(),methods.end(),"hessVec_uo_un") != methods.end() ) {
96  auto grad = obj_check.gradient_uo_un( uo, z );
97  auto hessVec = obj_check.hessVec_uo_un( uo, z );
98  auto update = obj_check.update_un( uo, z );
99  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_uo_un*vec)" );
100  }
101 
102  if( std::find(methods.begin(),methods.end(),"hessVec_uo_z") != methods.end() ) {
103  auto grad = obj_check.gradient_uo_z( uo, un );
104  auto hessVec = obj_check.hessVec_uo_z( uo, un );
105  auto update = obj_check.update_z( uo, un );
106  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_uo_z*vec)" );
107  }
108 
109 
110 
111  if( std::find(methods.begin(),methods.end(),"hessVec_un_uo") != methods.end() ) {
112  auto grad = obj_check.gradient_un_uo( un, z );
113  auto hessVec = obj_check.hessVec_un_uo( un, z );
114  auto update = obj_check.update_uo( un, z );
115  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_un_uo*vec)" );
116  }
117 
118  if( std::find(methods.begin(),methods.end(),"hessVec_un_un") != methods.end() ) {
119  auto grad = obj_check.gradient_un_un( uo, z );
120  auto hessVec = obj_check.hessVec_un_un( uo, z );
121  auto update = obj_check.update_un( uo, z );
122  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_un_un*vec)" );
123  }
124 
125  if( std::find(methods.begin(),methods.end(),"hessVec_un_z") != methods.end() ) {
126  auto grad = obj_check.gradient_un_z( uo, un );
127  auto hessVec = obj_check.hessVec_un_z( uo, un );
128  auto update = obj_check.update_z( uo, un );
129  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_un_z*vec)" );
130  }
131 
132 
133 
134  if( std::find(methods.begin(),methods.end(),"hessVec_z_uo") != methods.end() ) {
135  auto grad = obj_check.gradient_z_uo( un, z );
136  auto hessVec = obj_check.hessVec_z_uo( un, z );
137  auto update = obj_check.update_uo( un, z );
138  validator.derivative_check( grad, hessVec, update, *gz, *vu, uo, "norm(H_z_uo*vec)" );
139  }
140 
141  if( std::find(methods.begin(),methods.end(),"hessVec_z_un") != methods.end() ) {
142  auto grad = obj_check.gradient_z_un( uo, z );
143  auto hessVec = obj_check.hessVec_z_un( uo, z );
144  auto update = obj_check.update_un( uo, z );
145  validator.derivative_check( grad, hessVec, update, *gz, *vu, un, "norm(H_z_un*vec)" );
146  }
147 
148  if( std::find(methods.begin(),methods.end(),"hessVec_z_z") != methods.end() ) {
149  auto grad = obj_check.gradient_z_z( uo, un );
150  auto hessVec = obj_check.hessVec_z_z( uo, un );
151  auto update = obj_check.update_z( uo, un );
152  auto H = obj_check.hessVec_z_z(uo,un);
153  validator.derivative_check( grad, hessVec, update, *gz, *vz, z, "norm(H_z_z*vec)" );
154  }
155  }
156 
157  static void check( DynamicObjective<Real>& obj,
158  ValidateFunction<Real>& validator,
159  const Vector<Real>& uo,
160  const Vector<Real>& un,
161  const Vector<Real>& z ) {
162  std::vector<std::string> methods = {"gradient_uo",
163  "gradient_un",
164  "gradient_z",
165  "hessVec_uo_uo",
166  "hessVec_uo_un",
167  "hessVec_uo_z",
168  "hessVec_un_uo",
169  "hessVec_un_un",
170  "hessVec_un_z",
171  "hessVec_z_uo",
172  "hessVec_z_un",
173  "hessVec_z_z"};
174  check(obj, validator, uo, un, z, methods);
175  }
176 
177  static void check( DynamicObjective<Real>& obj,
178  ValidateFunction<Real>& validator,
179  const Vector<Real>& uo,
180  const Vector<Real>& un,
181  const Vector<Real>& z,
182  TimeStamp<Real> &ts ) {
183  std::vector<std::string> methods = {"gradient_uo",
184  "gradient_un",
185  "gradient_z",
186  "hessVec_uo_uo",
187  "hessVec_uo_un",
188  "hessVec_uo_z",
189  "hessVec_un_uo",
190  "hessVec_un_un",
191  "hessVec_un_z",
192  "hessVec_z_uo",
193  "hessVec_z_un",
194  "hessVec_z_z"};
195  check(obj, validator, uo, un, z, ts, methods);
196  }
197 }; // DynamicObjectiveCheck
198 
199 } // namespace ROL
200 
201 #endif // ROL_DYNAMICOBJECTIVECHECK_HPP
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:192
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
static void check(DynamicObjective< Real > &obj, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, const std::vector< std::string > &methods)
static void check(DynamicObjective< Real > &obj, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, TimeStamp< Real > &ts)
virtual void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1) override
static void check(DynamicObjective< Real > &obj, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z)
ROL::Objective_SimOpt value
static void check(DynamicObjective< Real > &obj, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, TimeStamp< Real > &timeStamp, const std::vector< std::string > &methods)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Defines the time-dependent objective function interface for simulation-based optimization. Computes time-local contributions of value, gradient, Hessian-vector product etc to a larger composite objective defined over the simulation time. In contrast to other objective classes Objective_TimeSimOpt has a default implementation of value which returns zero, as time-dependent simulation based optimization problems may have an objective value which depends only on the final state of the system.
static void check(DynamicObjective_CheckInterface< Real > &obj_check, ValidateFunction< Real > &validator, const Vector< Real > &uo, const Vector< Real > &un, const Vector< Real > &z, const std::vector< std::string > &methods)
DynamicConstraint_CheckInterface< Real > make_check(DynamicConstraint< Real > &con)