ROL
ROL_Reduced_ParametrizedObjective_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_PARAMETRIZEDOBJECTIVE_SIMOPT_H
46 #define ROL_REDUCED_PARAMETRIZEDOBJECTIVE_SIMOPT_H
47 
51 #include "ROL_Vector_SimOpt.hpp"
52 
53 namespace ROL {
54 
55 template <class Real>
57 private:
58  Teuchos::RCP<ParametrizedObjective_SimOpt<Real> > obj_;
59  Teuchos::RCP<ParametrizedEqualityConstraint_SimOpt<Real> > con_;
60 
61  // Primal vectors
62  Teuchos::RCP<Vector<Real> > state_;
63  Teuchos::RCP<Vector<Real> > state_sens_;
64  Teuchos::RCP<Vector<Real> > adjoint_;
65  Teuchos::RCP<Vector<Real> > adjoint_sens_;
66 
67  // Dual vectors
68  Teuchos::RCP<Vector<Real> > dualstate_;
69  Teuchos::RCP<Vector<Real> > dualstate1_;
70  Teuchos::RCP<Vector<Real> > dualadjoint_;
71  Teuchos::RCP<Vector<Real> > dualcontrol_;
72 
73  // Vector storage
74  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > state_storage_;
75  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > adjoint_storage_;
76 
77  bool storage_;
80 
81  void solve_state_equation(const Vector<Real> &x, Real &tol, bool flag = true, int iter = -1) {
82  // Solve state equation if not done already
83  if ( state_storage_.count(this->getParameter()) && storage_ ) {
84  state_->set(*state_storage_[this->getParameter()]);
85  }
86  else {
87  // Update equality constraint
88  con_->update_2(x,flag,iter);
89  // Solve state
90  con_->solve(*dualadjoint_,*state_,x,tol);
91  // Update equality constraint
92  con_->update_1(*state_,flag,iter);
93  // Update full objective function
94  obj_->update(*state_,x,flag,iter);
95  // Store state
96  if ( storage_ ) {
97  state_storage_.insert(
98  std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(
99  this->getParameter(),state_->clone()));
100  state_storage_[this->getParameter()]->set(*state_);
101  }
102  }
103  }
104 
109  void solve_adjoint_equation(const Vector<Real> &x, Real &tol) {
110  // Solve adjoint equation if not done already
111  if ( adjoint_storage_.count(this->getParameter()) && storage_ ) {
112  adjoint_->set(*adjoint_storage_[this->getParameter()]);
113  }
114  else {
115  // Evaluate the full gradient wrt u
116  obj_->gradient_1(*dualstate_,*state_,x,tol);
117  // Solve adjoint equation
118  con_->applyInverseAdjointJacobian_1(*adjoint_,*dualstate_,*state_,x,tol);
119  adjoint_->scale(-1.0);
120  // Store adjoint
121  if ( storage_ ) {
122  adjoint_storage_.insert(
123  std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(
124  this->getParameter(),adjoint_->clone()));
125  adjoint_storage_[this->getParameter()]->set(*adjoint_);
126  }
127  }
128  }
129 
134  void solve_state_sensitivity(const Vector<Real> &v, const Vector<Real> &x, Real &tol) {
135  // Solve state sensitivity equation
136  con_->applyJacobian_2(*dualadjoint_,v,*state_,x,tol);
137  dualadjoint_->scale(-1.0);
138  con_->applyInverseJacobian_1(*state_sens_,*dualadjoint_,*state_,x,tol);
139  }
140 
148  void solve_adjoint_sensitivity(const Vector<Real> &v, const Vector<Real> &x, Real &tol) {
149  // Evaluate full hessVec in the direction (s,v)
150  obj_->hessVec_11(*dualstate_,*state_sens_,*state_,x,tol);
151  obj_->hessVec_12(*dualstate1_,v,*state_,x,tol);
152  dualstate_->plus(*dualstate1_);
153  // Apply adjoint Hessian of constraint
154  con_->applyAdjointHessian_11(*dualstate1_,*adjoint_,*state_sens_,*state_,x,tol);
155  dualstate_->plus(*dualstate1_);
156  con_->applyAdjointHessian_21(*dualstate1_,*adjoint_,v,*state_,x,tol);
157  dualstate_->plus(*dualstate1_);
158  // Solve adjoint sensitivity equation
159  dualstate_->scale(-1.0);
160  con_->applyInverseAdjointJacobian_1(*adjoint_sens_,*dualstate_,*state_,x,tol);
161  }
162 
163 public:
173  Teuchos::RCP<ParametrizedEqualityConstraint_SimOpt<Real> > &con,
174  Teuchos::RCP<Vector<Real> > &state,
175  Teuchos::RCP<Vector<Real> > &adjoint,
176  bool storage = true, bool useFDhessVec = false)
177  : obj_(obj), con_(con),
178  state_(state), state_sens_(state->clone()),
179  adjoint_(adjoint), adjoint_sens_(adjoint->clone()),
180  dualstate_(state->dual().clone()), dualstate1_(state->dual().clone()),
181  dualadjoint_(adjoint->dual().clone()), dualcontrol_(Teuchos::null),
182  storage_(storage), useFDhessVec_(useFDhessVec), is_initialized_(false) {
183  state_storage_.clear();
184  adjoint_storage_.clear();
185  }
186 
201  Teuchos::RCP<Vector<Real> > &state,
202  Teuchos::RCP<Vector<Real> > &adjoint,
203  Teuchos::RCP<Vector<Real> > &dualstate,
204  Teuchos::RCP<Vector<Real> > &dualadjoint,
205  bool storage = true, bool useFDhessVec = false)
206  : obj_(obj), con_(con),
207  state_(state), state_sens_(state->clone()),
208  adjoint_(adjoint), adjoint_sens_(adjoint->clone()),
209  dualstate_(dualstate), dualstate1_(dualstate->clone()),
210  dualadjoint_(dualadjoint->clone()), dualcontrol_(Teuchos::null),
211  storage_(storage), useFDhessVec_(useFDhessVec), is_initialized_(false) {
212  state_storage_.clear();
213  adjoint_storage_.clear();
214  }
215 
216 
217  void setParameter(const std::vector<Real> &param) {
219  con_->setParameter(param);
220  obj_->setParameter(param);
221  }
222 
225  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
226  // Reset storage flags
227  state_storage_.clear();
228  if ( flag ) {
229  adjoint_storage_.clear();
230  }
231  }
232 
237  Real value( const Vector<Real> &x, Real &tol ) {
238  // Solve state equation
239  solve_state_equation(x,tol);
240  // Get objective function value
241  return obj_->value(*state_,x,tol);
242  }
243 
249  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
250  if (!is_initialized_) {
251  dualcontrol_ = g.clone();
252  is_initialized_ = true;
253  }
254  // Solve state equation
255  solve_state_equation(x,tol);
256  // Solve adjoint equation
257  solve_adjoint_equation(x,tol);
258  // Evaluate the full gradient wrt z
259  obj_->gradient_2(*dualcontrol_,*state_,x,tol);
260  // Build gradient
261  con_->applyAdjointJacobian_2(g,*adjoint_,*state_,x,tol);
262  g.plus(*dualcontrol_);
263  }
264 
268  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
269  if ( useFDhessVec_ ) {
271  }
272  else {
273  if (!is_initialized_) {
274  dualcontrol_ = hv.clone();
275  is_initialized_ = true;
276  }
277  // Solve state equation
278  solve_state_equation(x,tol);
279  // Solve adjoint equation
280  solve_adjoint_equation(x,tol);
281  // Solve state sensitivity equation
282  solve_state_sensitivity(v,x,tol);
283  // Solve adjoint sensitivity equation
284  solve_adjoint_sensitivity(v,x,tol);
285  // Build hessVec
286  con_->applyAdjointJacobian_2(hv,*adjoint_sens_,*state_,x,tol);
287  obj_->hessVec_21(*dualcontrol_,*state_sens_,*state_,x,tol);
288  hv.plus(*dualcontrol_);
289  obj_->hessVec_22(*dualcontrol_,v,*state_,x,tol);
290  hv.plus(*dualcontrol_);
291  con_->applyAdjointHessian_12(*dualcontrol_,*adjoint_,*state_sens_,*state_,x,tol);
292  hv.plus(*dualcontrol_);
293  con_->applyAdjointHessian_22(*dualcontrol_,*adjoint_,v,*state_,x,tol);
294  hv.plus(*dualcontrol_);
295  }
296  }
297 
300  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
301  Pv.set(v.dual());
302  }
303 
304 }; // class Reduced_Objective_SimOpt
305 
306 } // namespace ROL
307 
308 #endif
std::map< std::vector< Real >, Teuchos::RCP< Vector< Real > > > state_storage_
Reduced_ParametrizedObjective_SimOpt(Teuchos::RCP< ParametrizedObjective_SimOpt< Real > > &obj, Teuchos::RCP< ParametrizedEqualityConstraint_SimOpt< Real > > &con, Teuchos::RCP< Vector< Real > > &state, Teuchos::RCP< Vector< Real > > &adjoint, bool storage=true, bool useFDhessVec=false)
Constructor.
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:215
virtual void plus(const Vector &x)=0
Compute , where .
void solve_adjoint_equation(const Vector< Real > &x, Real &tol)
Given which solves the state equation, solve the adjoint equation for .
void solve_state_equation(const Vector< Real > &x, Real &tol, bool flag=true, int iter=-1)
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update the SimOpt objective function and equality constraint.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Teuchos::RCP< ParametrizedEqualityConstraint_SimOpt< Real > > con_
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void solve_adjoint_sensitivity(const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Given , the adjoint variable , and a direction , solve the adjoint sensitvity equation for ...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Given , evaluate the Hessian of the objective function in the direction .
Teuchos::RCP< ParametrizedObjective_SimOpt< Real > > obj_
void solve_state_sensitivity(const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Given which solves the state equation and a direction , solve the state senstivity equation for ...
Real value(const Vector< Real > &x, Real &tol)
Given , evaluate the objective function where solves .
std::map< std::vector< Real >, Teuchos::RCP< Vector< Real > > > adjoint_storage_
virtual void setParameter(const std::vector< Real > &param)
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply a reduced Hessian preconditioner.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Given , evaluate the gradient of the objective function where solves .
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:198
const std::vector< Real > getParameter(void) const
Reduced_ParametrizedObjective_SimOpt(Teuchos::RCP< ParametrizedObjective_SimOpt< Real > > &obj, Teuchos::RCP< ParametrizedEqualityConstraint_SimOpt< Real > > &con, Teuchos::RCP< Vector< Real > > &state, Teuchos::RCP< Vector< Real > > &adjoint, Teuchos::RCP< Vector< Real > > &dualstate, Teuchos::RCP< Vector< Real > > &dualadjoint, bool storage=true, bool useFDhessVec=false)
Secondary, general constructor for use with dual optimization vector spaces where the user does not d...