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 obj_check = make_check( obj );
68  check( obj_check, validator, uo, un, z, methods );
69  }
70 
71  static void check( DynamicObjective<Real>& obj,
72  ValidateFunction<Real>& validator,
73  const Vector<Real>& uo,
74  const Vector<Real>& un,
75  const Vector<Real>& z,
76  TimeStamp<Real>& timeStamp,
77  const std::vector<std::string>& methods ) {
78 
79  auto obj_check = make_check( obj, timeStamp );
80  check( obj_check, validator, uo, un, z, methods );
81  }
82 
83  static void check( DynamicObjective_CheckInterface<Real>& obj_check,
84  ValidateFunction<Real>& validator,
85  const Vector<Real>& uo,
86  const Vector<Real>& un,
87  const Vector<Real>& z,
88  const std::vector<std::string>& methods ) {
89 
90  auto gu = uo.dual().clone();
91  auto gz = z.dual().clone();
92  auto vu = uo.clone();
93  auto vz = z.clone();
94 
95  vu->randomize();
96  vz->randomize();
97 
98 
99  //-------------------------------------------------------------------------
100  // Check gradient components
101  if( std::find(methods.begin(),methods.end(),"gradient_uo") != methods.end() ) {
102  auto value = obj_check.value_uo( un, z );
103  auto grad = obj_check.gradient_uo( un, z );
104  auto update = obj_check.update_uo( un, z );
105  validator.derivative_check( value, grad, update, *gu, *vu, uo, "grad_uo'*dir" );
106  }
107  if( std::find(methods.begin(),methods.end(),"gradient_un") != methods.end() ) {
108  auto value = obj_check.value_un( uo, z );
109  auto grad = obj_check.gradient_un( uo, z );
110  auto update = obj_check.update_un( uo, z );
111  validator.derivative_check( value, grad, update, *gu, *vu, un, "grad_un'*dir" );
112  }
113  if( std::find(methods.begin(),methods.end(),"gradient_z") != methods.end() ) {
114  auto value = obj_check.value_z( uo, un );
115  auto grad = obj_check.gradient_z( uo, un );
116  auto update = obj_check.update_z( uo, un );
117  validator.derivative_check( value, grad, update, *gz, *vz, z, "grad_z'*dir" );
118  }
119 
120  //-------------------------------------------------------------------------
121  // Check Hessian components
122  if( std::find(methods.begin(),methods.end(),"hessVec_uo_uo") != methods.end() ) {
123  auto grad = obj_check.gradient_uo_uo( un, z );
124  auto hessVec = obj_check.hessVec_uo_uo( un, z );
125  auto update = obj_check.update_uo( un, z );
126  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_uo_uo*vec)" );
127  }
128 
129  if( std::find(methods.begin(),methods.end(),"hessVec_uo_un") != methods.end() ) {
130  auto grad = obj_check.gradient_uo_un( uo, z );
131  auto hessVec = obj_check.hessVec_uo_un( uo, z );
132  auto update = obj_check.update_un( uo, z );
133  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_uo_un*vec)" );
134  }
135 
136  if( std::find(methods.begin(),methods.end(),"hessVec_uo_z") != methods.end() ) {
137  auto grad = obj_check.gradient_uo_z( uo, un );
138  auto hessVec = obj_check.hessVec_uo_z( uo, un );
139  auto update = obj_check.update_z( uo, un );
140  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_uo_z*vec)" );
141  }
142 
143 
144 
145  if( std::find(methods.begin(),methods.end(),"hessVec_un_uo") != methods.end() ) {
146  auto grad = obj_check.gradient_un_uo( un, z );
147  auto hessVec = obj_check.hessVec_un_uo( un, z );
148  auto update = obj_check.update_uo( un, z );
149  validator.derivative_check( grad, hessVec, update, *gu, *vu, uo, "norm(H_un_uo*vec)" );
150  }
151 
152  if( std::find(methods.begin(),methods.end(),"hessVec_un_un") != methods.end() ) {
153  auto grad = obj_check.gradient_un_un( uo, z );
154  auto hessVec = obj_check.hessVec_un_un( uo, z );
155  auto update = obj_check.update_un( uo, z );
156  validator.derivative_check( grad, hessVec, update, *gu, *vu, un, "norm(H_un_un*vec)" );
157  }
158 
159  if( std::find(methods.begin(),methods.end(),"hessVec_un_z") != methods.end() ) {
160  auto grad = obj_check.gradient_un_z( uo, un );
161  auto hessVec = obj_check.hessVec_un_z( uo, un );
162  auto update = obj_check.update_z( uo, un );
163  validator.derivative_check( grad, hessVec, update, *gu, *vz, z, "norm(H_un_z*vec)" );
164  }
165 
166 
167 
168  if( std::find(methods.begin(),methods.end(),"hessVec_z_uo") != methods.end() ) {
169  auto grad = obj_check.gradient_z_uo( un, z );
170  auto hessVec = obj_check.hessVec_z_uo( un, z );
171  auto update = obj_check.update_uo( un, z );
172  validator.derivative_check( grad, hessVec, update, *gz, *vu, uo, "norm(H_z_uo*vec)" );
173  }
174 
175  if( std::find(methods.begin(),methods.end(),"hessVec_z_un") != methods.end() ) {
176  auto grad = obj_check.gradient_z_un( uo, z );
177  auto hessVec = obj_check.hessVec_z_un( uo, z );
178  auto update = obj_check.update_un( uo, z );
179  validator.derivative_check( grad, hessVec, update, *gz, *vu, un, "norm(H_z_un*vec)" );
180  }
181 
182  if( std::find(methods.begin(),methods.end(),"hessVec_z_z") != methods.end() ) {
183  auto grad = obj_check.gradient_z_z( uo, un );
184  auto hessVec = obj_check.hessVec_z_z( uo, un );
185  auto update = obj_check.update_z( uo, un );
186  auto H = obj_check.hessVec_z_z(uo,un);
187  validator.derivative_check( grad, hessVec, update, *gz, *vz, z, "norm(H_z_z*vec)" );
188  }
189  }
190 
191  static void check( DynamicObjective<Real>& obj,
192  ValidateFunction<Real>& validator,
193  const Vector<Real>& uo,
194  const Vector<Real>& un,
195  const Vector<Real>& z ) {
196  std::vector<std::string> methods = {"gradient_uo",
197  "gradient_un",
198  "gradient_z",
199  "hessVec_uo_uo",
200  "hessVec_uo_un",
201  "hessVec_uo_z",
202  "hessVec_un_uo",
203  "hessVec_un_un",
204  "hessVec_un_z",
205  "hessVec_z_uo",
206  "hessVec_z_un",
207  "hessVec_z_z"};
208  check(obj, validator, uo, un, z, methods);
209  }
210 }; // DynamicObjectiveCheck
211 
212 } // namespace ROL
213 
214 #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
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: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.
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)