ROL
ROL_Reduced_Objective_SimOpt.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 
45 #ifndef ROL_REDUCED_OBJECTIVE_SIMOPT_H
46 #define ROL_REDUCED_OBJECTIVE_SIMOPT_H
47 
48 #include "ROL_Objective_SimOpt.hpp"
50 #include "ROL_SimController.hpp"
51 
52 namespace ROL {
53 
54 template <class Real>
55 class Reduced_Objective_SimOpt : public Objective<Real> {
56 private:
57  const ROL::Ptr<Objective_SimOpt<Real> > obj_;
58  const ROL::Ptr<Constraint_SimOpt<Real> > con_;
59  ROL::Ptr<SimController<Real> > stateStore_;
60  ROL::Ptr<SimController<Real> > adjointStore_;
61 
62  // Primal vectors
63  ROL::Ptr<Vector<Real> > state_;
64  ROL::Ptr<Vector<Real> > adjoint_;
65  ROL::Ptr<Vector<Real> > state_sens_;
66  ROL::Ptr<Vector<Real> > adjoint_sens_;
67 
68  // Dual vectors
69  ROL::Ptr<Vector<Real> > dualstate_;
70  ROL::Ptr<Vector<Real> > dualstate1_;
71  ROL::Ptr<Vector<Real> > dualadjoint_;
72  ROL::Ptr<Vector<Real> > dualcontrol_;
73 
74  const bool storage_;
75  const bool useFDhessVec_;
76 
79 
80  void solve_state_equation(const Vector<Real> &z, Real &tol) {
81  // Check if state has been computed.
82  bool isComputed = false;
83  if (storage_) {
84  isComputed = stateStore_->get(*state_,Objective<Real>::getParameter());
85  }
86  // Solve state equation if not done already.
87  if (!isComputed || !storage_) {
88  // Update equality constraint with new Opt variable.
89  con_->update_2(z,updateFlag_,updateIter_);
90  // Solve state equation.
91  con_->solve(*dualadjoint_,*state_,z,tol);
92  // Update equality constraint with new Sim variable.
93  con_->update_1(*state_,updateFlag_,updateIter_);
94  // Update full objective function.
95  obj_->update(*state_,z,updateFlag_,updateIter_);
96  // Store state.
97  if (storage_) {
99  }
100  }
101  }
102 
107  void solve_adjoint_equation(const Vector<Real> &z, Real &tol) {
108  // Check if adjoint has been computed.
109  bool isComputed = false;
110  if (storage_) {
112  }
113  // Solve adjoint equation if not done already.
114  if (!isComputed || !storage_) {
115  // Evaluate the full gradient wrt u
116  obj_->gradient_1(*dualstate_,*state_,z,tol);
117  // Solve adjoint equation
118  con_->applyInverseAdjointJacobian_1(*adjoint_,*dualstate_,*state_,z,tol);
119  adjoint_->scale(static_cast<Real>(-1));
120  // Store adjoint
121  if (storage_) {
123  }
124  }
125  }
126 
131  void solve_state_sensitivity(const Vector<Real> &v, const Vector<Real> &z, Real &tol) {
132  // Solve state sensitivity equation
133  con_->applyJacobian_2(*dualadjoint_,v,*state_,z,tol);
134  dualadjoint_->scale(static_cast<Real>(-1));
135  con_->applyInverseJacobian_1(*state_sens_,*dualadjoint_,*state_,z,tol);
136  }
137 
145  void solve_adjoint_sensitivity(const Vector<Real> &v, const Vector<Real> &z, Real &tol) {
146  // Evaluate full hessVec in the direction (s,v)
147  obj_->hessVec_11(*dualstate_,*state_sens_,*state_,z,tol);
148  obj_->hessVec_12(*dualstate1_,v,*state_,z,tol);
149  dualstate_->plus(*dualstate1_);
150  // Apply adjoint Hessian of constraint
151  con_->applyAdjointHessian_11(*dualstate1_,*adjoint_,*state_sens_,*state_,z,tol);
152  dualstate_->plus(*dualstate1_);
153  con_->applyAdjointHessian_21(*dualstate1_,*adjoint_,v,*state_,z,tol);
154  dualstate_->plus(*dualstate1_);
155  // Solve adjoint sensitivity equation
156  dualstate_->scale(static_cast<Real>(-1));
157  con_->applyInverseAdjointJacobian_1(*adjoint_sens_,*dualstate_,*state_,z,tol);
158  }
159 
160 public:
172  const ROL::Ptr<Objective_SimOpt<Real> > &obj,
173  const ROL::Ptr<Constraint_SimOpt<Real> > &con,
174  const ROL::Ptr<Vector<Real> > &state,
175  const ROL::Ptr<Vector<Real> > &control,
176  const ROL::Ptr<Vector<Real> > &adjoint,
177  const bool storage = true,
178  const bool useFDhessVec = false)
179  : obj_(obj), con_(con),
180  storage_(storage), useFDhessVec_(useFDhessVec),
181  updateFlag_(true), updateIter_(0) {
182  stateStore_ = ROL::makePtr<SimController<Real>>();
183  adjointStore_ = ROL::makePtr<SimController<Real>>();
184  state_ = state->clone();
185  adjoint_ = adjoint->clone();
186  state_sens_ = state->clone();
187  adjoint_sens_ = adjoint->clone();
188  dualstate_ = state->dual().clone();
189  dualstate1_ = state->dual().clone();
190  dualadjoint_ = adjoint->dual().clone();
191  dualcontrol_ = control->dual().clone();
192  }
193 
208  const ROL::Ptr<Objective_SimOpt<Real> > &obj,
209  const ROL::Ptr<Constraint_SimOpt<Real> > &con,
210  const ROL::Ptr<Vector<Real> > &state,
211  const ROL::Ptr<Vector<Real> > &control,
212  const ROL::Ptr<Vector<Real> > &adjoint,
213  const ROL::Ptr<Vector<Real> > &dualstate,
214  const ROL::Ptr<Vector<Real> > &dualcontrol,
215  const ROL::Ptr<Vector<Real> > &dualadjoint,
216  const bool storage = true,
217  const bool useFDhessVec = false)
218  : obj_(obj), con_(con),
219  storage_(storage), useFDhessVec_(useFDhessVec),
220  updateFlag_(true), updateIter_(0) {
221  stateStore_ = ROL::makePtr<SimController<Real>>();
222  adjointStore_ = ROL::makePtr<SimController<Real>>();
223  state_ = state->clone();
224  adjoint_ = adjoint->clone();
225  state_sens_ = state->clone();
226  adjoint_sens_ = adjoint->clone();
227  dualstate_ = dualstate->clone();
228  dualstate1_ = dualstate->clone();
229  dualadjoint_ = dualadjoint->clone();
230  dualcontrol_ = dualcontrol->clone();
231  }
232 
245  const ROL::Ptr<Objective_SimOpt<Real> > &obj,
246  const ROL::Ptr<Constraint_SimOpt<Real> > &con,
247  const ROL::Ptr<SimController<Real> > &stateStore,
248  const ROL::Ptr<Vector<Real> > &state,
249  const ROL::Ptr<Vector<Real> > &control,
250  const ROL::Ptr<Vector<Real> > &adjoint,
251  const bool storage = true,
252  const bool useFDhessVec = false)
253  : obj_(obj), con_(con), stateStore_(stateStore),
254  storage_(storage), useFDhessVec_(useFDhessVec),
255  updateFlag_(true), updateIter_(0) {
256  adjointStore_ = ROL::makePtr<SimController<Real>>();
257  state_ = state->clone();
258  adjoint_ = adjoint->clone();
259  state_sens_ = state->clone();
260  adjoint_sens_ = adjoint->clone();
261  dualstate_ = state->dual().clone();
262  dualstate1_ = state->dual().clone();
263  dualadjoint_ = adjoint->dual().clone();
264  dualcontrol_ = control->dual().clone();
265  }
266 
282  const ROL::Ptr<Objective_SimOpt<Real> > &obj,
283  const ROL::Ptr<Constraint_SimOpt<Real> > &con,
284  const ROL::Ptr<SimController<Real> > &stateStore,
285  const ROL::Ptr<Vector<Real> > &state,
286  const ROL::Ptr<Vector<Real> > &control,
287  const ROL::Ptr<Vector<Real> > &adjoint,
288  const ROL::Ptr<Vector<Real> > &dualstate,
289  const ROL::Ptr<Vector<Real> > &dualcontrol,
290  const ROL::Ptr<Vector<Real> > &dualadjoint,
291  const bool storage = true,
292  const bool useFDhessVec = false)
293  : obj_(obj), con_(con), stateStore_(stateStore),
294  storage_(storage), useFDhessVec_(useFDhessVec),
295  updateFlag_(true), updateIter_(0) {
296  adjointStore_ = ROL::makePtr<SimController<Real>>();
297  state_ = state->clone();
298  adjoint_ = adjoint->clone();
299  state_sens_ = state->clone();
300  adjoint_sens_ = adjoint->clone();
301  dualstate_ = dualstate->clone();
302  dualstate1_ = dualstate->clone();
303  dualadjoint_ = dualadjoint->clone();
304  dualcontrol_ = dualcontrol->clone();
305  }
306 
309  void update( const Vector<Real> &z, bool flag = true, int iter = -1 ) {
310  updateFlag_ = flag;
311  updateIter_ = iter;
312  stateStore_->objectiveUpdate(true);
313  adjointStore_->objectiveUpdate(flag);
314  }
315 
320  Real value( const Vector<Real> &z, Real &tol ) {
321  // Solve state equation
322  solve_state_equation(z,tol);
323  // Get objective function value
324  return obj_->value(*state_,z,tol);
325  }
326 
332  void gradient( Vector<Real> &g, const Vector<Real> &z, Real &tol ) {
333  // Solve state equation
334  solve_state_equation(z,tol);
335  // Solve adjoint equation
336  solve_adjoint_equation(z,tol);
337  // Evaluate the full gradient wrt z
338  obj_->gradient_2(*dualcontrol_,*state_,z,tol);
339  // Build gradient
340  con_->applyAdjointJacobian_2(g,*adjoint_,*state_,z,tol);
341  g.plus(*dualcontrol_);
342  }
343 
347  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &z, Real &tol ) {
348  if ( useFDhessVec_ ) {
349  Objective<Real>::hessVec(hv,v,z,tol);
350  }
351  else {
352  // Solve state equation
353  solve_state_equation(z,tol);
354  // Solve adjoint equation
355  solve_adjoint_equation(z,tol);
356  // Solve state sensitivity equation
357  solve_state_sensitivity(v,z,tol);
358  // Solve adjoint sensitivity equation
359  solve_adjoint_sensitivity(v,z,tol);
360  // Build hessVec
361  con_->applyAdjointJacobian_2(hv,*adjoint_sens_,*state_,z,tol);
362  obj_->hessVec_21(*dualcontrol_,*state_sens_,*state_,z,tol);
363  hv.plus(*dualcontrol_);
364  obj_->hessVec_22(*dualcontrol_,v,*state_,z,tol);
365  hv.plus(*dualcontrol_);
366  con_->applyAdjointHessian_12(*dualcontrol_,*adjoint_,*state_sens_,*state_,z,tol);
367  hv.plus(*dualcontrol_);
368  con_->applyAdjointHessian_22(*dualcontrol_,*adjoint_,v,*state_,z,tol);
369  hv.plus(*dualcontrol_);
370  }
371  }
372 
375  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &z, Real &tol ) {
376  Pv.set(v.dual());
377  }
378 
379 // For parametrized (stochastic) objective functions and constraints
380 public:
381  void setParameter(const std::vector<Real> &param) {
383  con_->setParameter(param);
384  obj_->setParameter(param);
385  }
386 }; // class Reduced_Objective_SimOpt
387 
388 } // namespace ROL
389 
390 #endif
Provides the interface to evaluate objective functions.
Provides the interface to evaluate simulation-based objective functions.
const ROL::Ptr< Objective_SimOpt< Real > > obj_
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &z, Real &tol)
Apply a reduced Hessian preconditioner.
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 void plus(const Vector &x)=0
Compute , where .
ROL::Ptr< SimController< Real > > stateStore_
void setParameter(const std::vector< Real > &param)
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
void solve_state_equation(const Vector< Real > &z, Real &tol)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void update(const Vector< Real > &z, bool flag=true, int iter=-1)
Update the SimOpt objective function and equality constraint.
const ROL::Ptr< Constraint_SimOpt< Real > > con_
ROL::Ptr< SimController< Real > > adjointStore_
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &z, Real &tol)
Given , evaluate the Hessian of the objective function in the direction .
void solve_adjoint_sensitivity(const Vector< Real > &v, const Vector< Real > &z, Real &tol)
Given , the adjoint variable , and a direction , solve the adjoint sensitvity equation for ...
void gradient(Vector< Real > &g, const Vector< Real > &z, Real &tol)
Given , evaluate the gradient of the objective function where solves .
Reduced_Objective_SimOpt(const ROL::Ptr< Objective_SimOpt< Real > > &obj, const ROL::Ptr< Constraint_SimOpt< Real > > &con, const ROL::Ptr< SimController< Real > > &stateStore, const ROL::Ptr< Vector< Real > > &state, const ROL::Ptr< Vector< Real > > &control, const ROL::Ptr< Vector< Real > > &adjoint, const bool storage=true, const bool useFDhessVec=false)
Constructor.
Real value(const Vector< Real > &z, Real &tol)
Given , evaluate the objective function where solves .
void solve_adjoint_equation(const Vector< Real > &z, Real &tol)
Given which solves the state equation, solve the adjoint equation for .
virtual void setParameter(const std::vector< Real > &param)
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
Defines the constraint operator interface for simulation-based optimization.
void solve_state_sensitivity(const Vector< Real > &v, const Vector< Real > &z, Real &tol)
Given which solves the state equation and a direction , solve the state senstivity equation for ...
Reduced_Objective_SimOpt(const ROL::Ptr< Objective_SimOpt< Real > > &obj, const ROL::Ptr< Constraint_SimOpt< Real > > &con, const ROL::Ptr< Vector< Real > > &state, const ROL::Ptr< Vector< Real > > &control, const ROL::Ptr< Vector< Real > > &adjoint, const ROL::Ptr< Vector< Real > > &dualstate, const ROL::Ptr< Vector< Real > > &dualcontrol, const ROL::Ptr< Vector< Real > > &dualadjoint, const bool storage=true, const bool useFDhessVec=false)
Secondary, general constructor for use with dual optimization vector spaces where the user does not d...
Reduced_Objective_SimOpt(const ROL::Ptr< Objective_SimOpt< Real > > &obj, const ROL::Ptr< Constraint_SimOpt< Real > > &con, const ROL::Ptr< Vector< Real > > &state, const ROL::Ptr< Vector< Real > > &control, const ROL::Ptr< Vector< Real > > &adjoint, const bool storage=true, const bool useFDhessVec=false)
Constructor.
Reduced_Objective_SimOpt(const ROL::Ptr< Objective_SimOpt< Real > > &obj, const ROL::Ptr< Constraint_SimOpt< Real > > &con, const ROL::Ptr< SimController< Real > > &stateStore, const ROL::Ptr< Vector< Real > > &state, const ROL::Ptr< Vector< Real > > &control, const ROL::Ptr< Vector< Real > > &adjoint, const ROL::Ptr< Vector< Real > > &dualstate, const ROL::Ptr< Vector< Real > > &dualcontrol, const ROL::Ptr< Vector< Real > > &dualadjoint, const bool storage=true, const bool useFDhessVec=false)
Secondary, general constructor for use with dual optimization vector spaces where the user does not d...