ROL
ROL_DynamicObjectiveCheck.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #pragma once
45 #ifndef ROL_DYNAMICOBJECTIVECHECK_HPP
46 #define ROL_DYNAMICOBJECTIVECHECK_HPP
47 
49 #include "ROL_ValidateFunction.hpp"
50 #include <string>
51 
52 // TODO: Add symmetry check for diagonal Hessian blocks and adjoint consistency
53 // for off-diagonal block pairs
54 
55 namespace ROL {
56 
57 template<typename Real>
59 
60  static void check( DynamicObjective<Real>& obj,
61  ValidateFunction<Real>& validator,
62  const Vector<Real>& uo,
63  const Vector<Real>& un,
64  const Vector<Real>& z,
65  const std::vector<std::string>& methods ) {
66 
67  auto gu = uo.dual().clone();
68  auto gz = z.dual().clone();
69  auto vu = uo.clone();
70  auto vz = z.clone();
71 
72  vu->randomize();
73  vz->randomize();
74 
75  auto obj_check = make_check( obj );
76 
77  //-------------------------------------------------------------------------
78  // Check gradient components
79  if( std::find(methods.begin(),methods.end(),"gradient_uo") != methods.end() ) {
80  auto value = obj_check.value_uo( un, z );
81  auto grad = obj_check.gradient_uo( un, z );
82  auto update = obj_check.update_uo( un, z );
83  validator.derivative_check( value, grad, update, *gu, *vu, uo, "grad_uo'*dir" );
84  }
85  if( std::find(methods.begin(),methods.end(),"gradient_un") != methods.end() ) {
86  auto value = obj_check.value_un( uo, z );
87  auto grad = obj_check.gradient_un( uo, z );
88  auto update = obj_check.update_un( uo, z );
89  validator.derivative_check( value, grad, update, *gu, *vu, un, "grad_un'*dir" );
90  }
91  if( std::find(methods.begin(),methods.end(),"gradient_z") != methods.end() ) {
92  auto value = obj_check.value_z( uo, un );
93  auto grad = obj_check.gradient_z( uo, un );
94  auto update = obj_check.update_z( uo, un );
95  validator.derivative_check( value, grad, update, *gz, *vz, z, "grad_z'*dir" );
96  }
97 
98  //-------------------------------------------------------------------------
99  // Check Hessian components
100  if( std::find(methods.begin(),methods.end(),"hessVec_uo_uo") != methods.end() ) {
101  auto grad = obj_check.gradient_uo_uo( un, z );
102  auto hessVec = obj_check.hessVec_uo_uo( un, z );
103  auto update = obj_check.update_uo( un, z );
104  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_uo_uo*vec)" );
105  }
106 
107  if( std::find(methods.begin(),methods.end(),"hessVec_uo_un") != methods.end() ) {
108  auto grad = obj_check.gradient_uo_un( uo, z );
109  auto hessVec = obj_check.hessVec_uo_un( uo, z );
110  auto update = obj_check.update_un( uo, z );
111  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_uo_un*vec)" );
112  }
113 
114  if( std::find(methods.begin(),methods.end(),"hessVec_uo_z") != methods.end() ) {
115  auto grad = obj_check.gradient_uo_z( uo, un );
116  auto hessVec = obj_check.hessVec_uo_z( uo, un );
117  auto update = obj_check.update_z( uo, un );
118  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_uo_z*vec)" );
119  }
120 
121 
122 
123  if( std::find(methods.begin(),methods.end(),"hessVec_un_uo") != methods.end() ) {
124  auto grad = obj_check.gradient_un_uo( un, z );
125  auto hessVec = obj_check.hessVec_un_uo( un, z );
126  auto update = obj_check.update_uo( un, z );
127  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_un_uo*vec)" );
128  }
129 
130  if( std::find(methods.begin(),methods.end(),"hessVec_un_un") != methods.end() ) {
131  auto grad = obj_check.gradient_un_un( uo, z );
132  auto hessVec = obj_check.hessVec_un_un( uo, z );
133  auto update = obj_check.update_un( uo, z );
134  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_un_un*vec)" );
135  }
136 
137  if( std::find(methods.begin(),methods.end(),"hessVec_un_z") != methods.end() ) {
138  auto grad = obj_check.gradient_un_z( uo, un );
139  auto hessVec = obj_check.hessVec_un_z( uo, un );
140  auto update = obj_check.update_z( uo, un );
141  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_un_z*vec)" );
142  }
143 
144 
145 
146  if( std::find(methods.begin(),methods.end(),"hessVec_z_uo") != methods.end() ) {
147  auto grad = obj_check.gradient_z_uo( un, z );
148  auto hessVec = obj_check.hessVec_z_uo( un, z );
149  auto update = obj_check.update_uo( un, z );
150  validator.derivative_check( grad, hessVec, update, *gz, *vu, uo, "norm(H_z_uo*vec)" );
151  }
152 
153  if( std::find(methods.begin(),methods.end(),"hessVec_z_un") != methods.end() ) {
154  auto grad = obj_check.gradient_z_un( uo, z );
155  auto hessVec = obj_check.hessVec_z_un( uo, z );
156  auto update = obj_check.update_un( uo, z );
157  validator.derivative_check( grad, hessVec, update, *gz, *vu, un, "norm(H_z_un*vec)" );
158  }
159 
160  if( std::find(methods.begin(),methods.end(),"hessVec_z_z") != methods.end() ) {
161  auto grad = obj_check.gradient_z_z( uo, un );
162  auto hessVec = obj_check.hessVec_z_z( uo, un );
163  auto update = obj_check.update_z( uo, un );
164  auto H = obj_check.hessVec_z_z(uo,un);
165  validator.derivative_check( grad, hessVec, update, *gz, *vz, z, "norm(H_z_z*vec)" );
166  }
167  }
168 
169  static void check( DynamicObjective<Real>& obj,
170  ValidateFunction<Real>& validator,
171  const Vector<Real>& uo,
172  const Vector<Real>& un,
173  const Vector<Real>& z ) {
174  std::vector<std::string> methods = {"gradient_uo",
175  "gradient_un",
176  "gradient_z",
177  "hessVec_uo_uo",
178  "hessVec_uo_un",
179  "hessVec_uo_z",
180  "hessVec_un_uo",
181  "hessVec_un_un",
182  "hessVec_un_z",
183  "hessVec_z_uo",
184  "hessVec_z_un",
185  "hessVec_z_z"};
186  check(obj, validator, uo, un, z, methods);
187  }
188 }; // DynamicObjectiveCheck
189 
190 } // namespace ROL
191 
192 #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:226
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)
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
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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.
DynamicConstraint_CheckInterface< Real > make_check(DynamicConstraint< Real > &con)