ROL
ROL_Reduced_Constraint_SimOpt.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 #ifndef ROL_REDUCED_CONSTRAINT_SIMOPT_H
11 #define ROL_REDUCED_CONSTRAINT_SIMOPT_H
12 
14 #include "ROL_VectorController.hpp"
15 
16 namespace ROL {
17 
18 template <class Real>
19 class Reduced_Constraint_SimOpt : public Constraint<Real> {
20 private:
21  const ROL::Ptr<Constraint_SimOpt<Real>> conVal_;
22  const ROL::Ptr<Constraint_SimOpt<Real>> conRed_;
23  const ROL::Ptr<VectorController<Real>> stateStore_;
24  ROL::Ptr<VectorController<Real>> adjointStore_;
25 
26  // Primal vectors
27  ROL::Ptr<Vector<Real>> state_;
28  ROL::Ptr<Vector<Real>> adjoint_;
29  ROL::Ptr<Vector<Real>> residual_;
30  ROL::Ptr<Vector<Real>> state_sens_;
31  ROL::Ptr<Vector<Real>> adjoint_sens_;
32 
33  // Dual vectors
34  ROL::Ptr<Vector<Real>> dualstate_;
35  ROL::Ptr<Vector<Real>> dualstate1_;
36  ROL::Ptr<Vector<Real>> dualadjoint_;
37  ROL::Ptr<Vector<Real>> dualcontrol_;
38  ROL::Ptr<Vector<Real>> dualresidual_;
39 
40  const bool storage_;
41  const bool useFDhessVec_;
42 
45 
46  void solve_state_equation(const Vector<Real> &z, Real &tol) {
47  // Check if state has been computed.
48  bool isComputed = false;
49  if (storage_) {
51  }
52  // Solve state equation if not done already.
53  if (!isComputed || !storage_) {
54  // Update equality constraint with new Opt variable.
55  conRed_->update_2(z,updateFlag_,updateIter_);
56  // Solve state equation.
57  conRed_->solve(*dualadjoint_,*state_,z,tol);
58  // Update equality constraint with new Sim variable.
60  // Update full objective function.
62  // Store state.
63  if (storage_) {
65  }
66  }
67  }
68 
73  void solve_adjoint_equation(const Vector<Real> &w, const Vector<Real> &z, Real &tol) {
74  // Check if adjoint has been computed.
75  bool isComputed = false;
76  if (storage_) {
78  }
79  // Solve adjoint equation if not done already.
80  if (!isComputed || !storage_) {
81  // Evaluate the full gradient wrt u
82  conVal_->applyAdjointJacobian_1(*dualstate_,w,*state_,z,tol);
83  // Solve adjoint equation
84  conRed_->applyInverseAdjointJacobian_1(*adjoint_,*dualstate_,*state_,z,tol);
85  adjoint_->scale(static_cast<Real>(-1));
86  // Store adjoint
87  if (storage_) {
89  }
90  }
91  }
92 
97  void solve_state_sensitivity(const Vector<Real> &v, const Vector<Real> &z, Real &tol) {
98  // Solve state sensitivity equation
99  conRed_->applyJacobian_2(*dualadjoint_,v,*state_,z,tol);
100  dualadjoint_->scale(static_cast<Real>(-1));
101  conRed_->applyInverseJacobian_1(*state_sens_,*dualadjoint_,*state_,z,tol);
102  }
103 
111  void solve_adjoint_sensitivity(const Vector<Real> &w, const Vector<Real> &v, const Vector<Real> &z, Real &tol) {
112  // Evaluate full hessVec in the direction (s,v)
113  conVal_->applyAdjointHessian_11(*dualstate_,w,*state_sens_,*state_,z,tol);
114  conVal_->applyAdjointHessian_12(*dualstate1_,w,v,*state_,z,tol);
115  dualstate_->plus(*dualstate1_);
116  // Apply adjoint Hessian of constraint
117  conRed_->applyAdjointHessian_11(*dualstate1_,*adjoint_,*state_sens_,*state_,z,tol);
118  dualstate_->plus(*dualstate1_);
119  conRed_->applyAdjointHessian_21(*dualstate1_,*adjoint_,v,*state_,z,tol);
120  dualstate_->plus(*dualstate1_);
121  // Solve adjoint sensitivity equation
122  dualstate_->scale(static_cast<Real>(-1));
123  conRed_->applyInverseAdjointJacobian_1(*adjoint_sens_,*dualstate_,*state_,z,tol);
124  }
125 
126 public:
139  const ROL::Ptr<Constraint_SimOpt<Real> > &conVal,
140  const ROL::Ptr<Constraint_SimOpt<Real> > &conRed,
141  const ROL::Ptr<VectorController<Real> > &stateStore,
142  const ROL::Ptr<Vector<Real> > &state,
143  const ROL::Ptr<Vector<Real> > &control,
144  const ROL::Ptr<Vector<Real> > &adjoint,
145  const ROL::Ptr<Vector<Real> > &residual,
146  const bool storage = true,
147  const bool useFDhessVec = false)
148  : conVal_(conVal), conRed_(conRed), stateStore_(stateStore),
149  storage_(storage), useFDhessVec_(useFDhessVec),
150  updateFlag_(true), updateIter_(0) {
151  adjointStore_ = ROL::makePtr<VectorController<Real>>();
152  state_ = state->clone();
153  adjoint_ = adjoint->clone();
154  residual_ = residual->clone();
155  state_sens_ = state->clone();
156  adjoint_sens_ = adjoint->clone();
157  dualstate_ = state->dual().clone();
158  dualstate1_ = state->dual().clone();
159  dualadjoint_ = adjoint->dual().clone();
160  dualcontrol_ = control->dual().clone();
161  dualresidual_ = residual->dual().clone();
162  }
163 
179  const ROL::Ptr<Constraint_SimOpt<Real> > &conVal,
180  const ROL::Ptr<Constraint_SimOpt<Real> > &conRed,
181  const ROL::Ptr<VectorController<Real> > &stateStore,
182  const ROL::Ptr<Vector<Real> > &state,
183  const ROL::Ptr<Vector<Real> > &control,
184  const ROL::Ptr<Vector<Real> > &adjoint,
185  const ROL::Ptr<Vector<Real> > &residual,
186  const ROL::Ptr<Vector<Real> > &dualstate,
187  const ROL::Ptr<Vector<Real> > &dualcontrol,
188  const ROL::Ptr<Vector<Real> > &dualadjoint,
189  const ROL::Ptr<Vector<Real> > &dualresidual,
190  const bool storage = true,
191  const bool useFDhessVec = false)
192  : conVal_(conVal), conRed_(conRed), stateStore_(stateStore),
193  storage_(storage), useFDhessVec_(useFDhessVec),
194  updateFlag_(true), updateIter_(0) {
195  adjointStore_ = ROL::makePtr<VectorController<Real>>();
196  state_ = state->clone();
197  adjoint_ = adjoint->clone();
198  residual_ = residual->clone();
199  state_sens_ = state->clone();
200  adjoint_sens_ = adjoint->clone();
201  dualstate_ = dualstate->clone();
202  dualstate1_ = dualstate->clone();
203  dualadjoint_ = dualadjoint->clone();
204  dualcontrol_ = dualcontrol->clone();
205  dualresidual_ = dualresidual->clone();
206  }
207 
210  void update( const Vector<Real> &z, bool flag = true, int iter = -1 ) {
211  updateFlag_ = flag;
212  updateIter_ = iter;
213  stateStore_->constraintUpdate(true);
214  adjointStore_->constraintUpdate(flag);
215  }
216 
221  void value( Vector<Real> &c, const Vector<Real> &z, Real &tol ) {
222  // Solve state equation
223  solve_state_equation(z,tol);
224  // Get constraint value
225  conVal_->value(c,*state_,z,tol);
226  }
227 
234  const Vector<Real> &z, Real &tol ) {
235  // Solve state equation.
236  solve_state_equation(z,tol);
237  // Solve state sensitivity equation.
238  solve_state_sensitivity(v,z,tol);
239  // Apply Sim Jacobian to state sensitivity.
240  conVal_->applyJacobian_1(*residual_,*state_sens_,*state_,z,tol);
241  // Apply Opt Jacobian to vector.
242  conVal_->applyJacobian_2(jv,v,*state_,z,tol);
243  jv.plus(*residual_);
244  }
245 
247  const Vector<Real> &z, Real &tol ) {
248  // Solve state equation
249  solve_state_equation(z,tol);
250  // Solve adjoint equation
251  solve_adjoint_equation(w,z,tol);
252  // Evaluate the full gradient wrt z
253  conVal_->applyAdjointJacobian_2(*dualcontrol_,w,*state_,z,tol);
254  // Build gradient
255  conRed_->applyAdjointJacobian_2(ajw,*adjoint_,*state_,z,tol);
256  ajw.plus(*dualcontrol_);
257  }
258 
263  const Vector<Real> &v, const Vector<Real> &z,
264  Real &tol ) {
265  if ( useFDhessVec_ ) {
267  }
268  else {
269  // Solve state equation
270  solve_state_equation(z,tol);
271  // Solve adjoint equation
272  solve_adjoint_equation(w,z,tol);
273  // Solve state sensitivity equation
274  solve_state_sensitivity(v,z,tol);
275  // Solve adjoint sensitivity equation
276  solve_adjoint_sensitivity(w,v,z,tol);
277  // Build hessVec
278  conRed_->applyAdjointJacobian_2(ahwv,*adjoint_sens_,*state_,z,tol);
279  conVal_->applyAdjointHessian_21(*dualcontrol_,w,*state_sens_,*state_,z,tol);
280  ahwv.plus(*dualcontrol_);
281  conVal_->applyAdjointHessian_22(*dualcontrol_,w,v,*state_,z,tol);
282  ahwv.plus(*dualcontrol_);
283  conRed_->applyAdjointHessian_12(*dualcontrol_,*adjoint_,*state_sens_,*state_,z,tol);
284  ahwv.plus(*dualcontrol_);
285  conRed_->applyAdjointHessian_22(*dualcontrol_,*adjoint_,v,*state_,z,tol);
286  ahwv.plus(*dualcontrol_);
287  }
288  }
289 
290 // For parametrized (stochastic) objective functions and constraints
291 public:
292  void setParameter(const std::vector<Real> &param) {
294  conVal_->setParameter(param);
295  conRed_->setParameter(param);
296  }
297 }; // class Reduced_Constraint_SimOpt
298 
299 } // namespace ROL
300 
301 #endif
virtual void plus(const Vector &x)=0
Compute , where .
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 ...
void applyAdjointHessian(Vector< Real > &ahwv, const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &z, Real &tol)
Given , evaluate the Hessian of the objective function in the direction .
void applyAdjointJacobian(Vector< Real > &ajw, const Vector< Real > &w, const Vector< Real > &z, Real &tol)
Apply the adjoint of the the constraint Jacobian at , , to vector .
ROL::Ptr< VectorController< Real > > adjointStore_
Reduced_Constraint_SimOpt(const ROL::Ptr< Constraint_SimOpt< Real > > &conVal, const ROL::Ptr< Constraint_SimOpt< Real > > &conRed, const ROL::Ptr< VectorController< 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 > > &residual, const ROL::Ptr< Vector< Real > > &dualstate, const ROL::Ptr< Vector< Real > > &dualcontrol, const ROL::Ptr< Vector< Real > > &dualadjoint, const ROL::Ptr< Vector< Real > > &dualresidual, 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_Constraint_SimOpt(const ROL::Ptr< Constraint_SimOpt< Real > > &conVal, const ROL::Ptr< Constraint_SimOpt< Real > > &conRed, const ROL::Ptr< VectorController< 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 > > &residual, const bool storage=true, const bool useFDhessVec=false)
Constructor.
void applyJacobian(Vector< Real > &jv, const Vector< Real > &v, const Vector< Real > &z, Real &tol)
Given , apply the Jacobian to a vector where solves .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
const ROL::Ptr< VectorController< Real > > stateStore_
void solve_state_equation(const Vector< Real > &z, Real &tol)
void value(Vector< Real > &c, const Vector< Real > &z, Real &tol)
Given , evaluate the equality constraint where solves .
void solve_adjoint_equation(const Vector< Real > &w, const Vector< Real > &z, Real &tol)
Given which solves the state equation, solve the adjoint equation for .
virtual void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply the derivative of the adjoint of the constraint Jacobian at to vector in direction ...
void solve_adjoint_sensitivity(const Vector< Real > &w, const Vector< Real > &v, const Vector< Real > &z, Real &tol)
Given , the adjoint variable , and a direction , solve the adjoint sensitvity equation for ...
virtual void setParameter(const std::vector< Real > &param)
const ROL::Ptr< Constraint_SimOpt< Real > > conVal_
void setParameter(const std::vector< Real > &param)
const ROL::Ptr< Constraint_SimOpt< Real > > conRed_
Defines the constraint operator interface for simulation-based optimization.
void update(const Vector< Real > &z, bool flag=true, int iter=-1)
Update the SimOpt objective function and equality constraint.
Defines the general constraint operator interface.