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 "ROL_RandomVector.hpp"
51 #include <string>
52 
53 // TODO: Add symmetry check for diagonal Hessian blocks and adjoint consistency
54 // for off-diagonal block pairs
55 
56 namespace ROL {
57 
58 template<typename Real>
60 
61  static void check( DynamicObjective<Real>& obj,
62  ValidateFunction<Real>& validator,
63  const Vector<Real>& uo,
64  const Vector<Real>& un,
65  const Vector<Real>& z,
66  const std::vector<std::string>& methods ) {
67 
68  auto gu = uo.dual().clone();
69  auto gz = z.dual().clone();
70  auto vu = uo.clone();
71  auto vz = z.clone();
72 
73  RandomizeVector(*vu);
74  RandomizeVector(*vz);
75 
76  auto obj_check = make_check( obj );
77 
78  //-------------------------------------------------------------------------
79  // Check gradient components
80  if( std::find(methods.begin(),methods.end(),"gradient_uo") != methods.end() ) {
81  auto value = obj_check.value_uo( un, z );
82  auto grad = obj_check.gradient_uo( un, z );
83  auto update = obj_check.update_uo( un, z );
84  validator.derivative_check( value, grad, update, *gu, *vu, uo, "grad_uo'*dir" );
85  }
86  if( std::find(methods.begin(),methods.end(),"gradient_un") != methods.end() ) {
87  auto value = obj_check.value_un( uo, z );
88  auto grad = obj_check.gradient_un( uo, z );
89  auto update = obj_check.update_un( uo, z );
90  validator.derivative_check( value, grad, update, *gu, *vu, un, "grad_un'*dir" );
91  }
92  if( std::find(methods.begin(),methods.end(),"gradient_z") != methods.end() ) {
93  auto value = obj_check.value_z( uo, un );
94  auto grad = obj_check.gradient_z( uo, un );
95  auto update = obj_check.update_z( uo, un );
96  validator.derivative_check( value, grad, update, *gz, *vz, z, "grad_z'*dir" );
97  }
98 
99  //-------------------------------------------------------------------------
100  // Check Hessian components
101  if( std::find(methods.begin(),methods.end(),"hessVec_uo_uo") != methods.end() ) {
102  auto grad = obj_check.gradient_uo_uo( un, z );
103  auto hessVec = obj_check.hessVec_uo_uo( un, z );
104  auto update = obj_check.update_uo( un, z );
105  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_uo_uo*vec)" );
106  }
107 
108  if( std::find(methods.begin(),methods.end(),"hessVec_uo_un") != methods.end() ) {
109  auto grad = obj_check.gradient_uo_un( uo, z );
110  auto hessVec = obj_check.hessVec_uo_un( uo, z );
111  auto update = obj_check.update_un( uo, z );
112  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_uo_un*vec)" );
113  }
114 
115  if( std::find(methods.begin(),methods.end(),"hessVec_uo_z") != methods.end() ) {
116  auto grad = obj_check.gradient_uo_z( uo, un );
117  auto hessVec = obj_check.hessVec_uo_z( uo, un );
118  auto update = obj_check.update_z( uo, un );
119  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_uo_z*vec)" );
120  }
121 
122 
123 
124  if( std::find(methods.begin(),methods.end(),"hessVec_un_uo") != methods.end() ) {
125  auto grad = obj_check.gradient_un_uo( un, z );
126  auto hessVec = obj_check.hessVec_un_uo( un, z );
127  auto update = obj_check.update_uo( un, z );
128  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_un_uo*vec)" );
129  }
130 
131  if( std::find(methods.begin(),methods.end(),"hessVec_un_un") != methods.end() ) {
132  auto grad = obj_check.gradient_un_un( uo, z );
133  auto hessVec = obj_check.hessVec_un_un( uo, z );
134  auto update = obj_check.update_un( uo, z );
135  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_un_un*vec)" );
136  }
137 
138  if( std::find(methods.begin(),methods.end(),"hessVec_un_z") != methods.end() ) {
139  auto grad = obj_check.gradient_un_z( uo, un );
140  auto hessVec = obj_check.hessVec_un_z( uo, un );
141  auto update = obj_check.update_z( uo, un );
142  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_un_z*vec)" );
143  }
144 
145 
146 
147  if( std::find(methods.begin(),methods.end(),"hessVec_z_uo") != methods.end() ) {
148  auto grad = obj_check.gradient_z_uo( un, z );
149  auto hessVec = obj_check.hessVec_z_uo( un, z );
150  auto update = obj_check.update_uo( un, z );
151  validator.derivative_check( grad, hessVec, update, *gz, *vu, uo, "norm(H_z_uo*vec)" );
152  }
153 
154  if( std::find(methods.begin(),methods.end(),"hessVec_z_un") != methods.end() ) {
155  auto grad = obj_check.gradient_z_un( uo, z );
156  auto hessVec = obj_check.hessVec_z_un( uo, z );
157  auto update = obj_check.update_un( uo, z );
158  validator.derivative_check( grad, hessVec, update, *gz, *vu, un, "norm(H_z_un*vec)" );
159  }
160 
161  if( std::find(methods.begin(),methods.end(),"hessVec_z_z") != methods.end() ) {
162  auto grad = obj_check.gradient_z_z( uo, un );
163  auto hessVec = obj_check.hessVec_z_z( uo, un );
164  auto update = obj_check.update_z( uo, un );
165  auto H = obj_check.hessVec_z_z(uo,un);
166  validator.derivative_check( grad, hessVec, update, *gz, *vz, z, "norm(H_z_z*vec)" );
167  }
168  }
169 
170  static void check( DynamicObjective<Real>& obj,
171  ValidateFunction<Real>& validator,
172  const Vector<Real>& uo,
173  const Vector<Real>& un,
174  const Vector<Real>& z ) {
175  std::vector<std::string> methods = {"gradient_uo",
176  "gradient_un",
177  "gradient_z",
178  "hessVec_uo_uo",
179  "hessVec_uo_un",
180  "hessVec_uo_z",
181  "hessVec_un_uo",
182  "hessVec_un_un",
183  "hessVec_un_z",
184  "hessVec_z_uo",
185  "hessVec_z_un",
186  "hessVec_z_z"};
187  check(obj, validator, uo, un, z, methods);
188  }
189 }; // DynamicObjectiveCheck
190 
191 } // namespace ROL
192 
193 #endif // ROL_DYNAMICOBJECTIVECHECK_HPP
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
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
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.
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
DynamicConstraint_CheckInterface< Real > make_check(DynamicConstraint< Real > &con)