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 
50 #include "ROL_Vector_SimOpt.hpp"
51 
52 namespace ROL {
53 
54 template <class Real>
56 private:
57  Teuchos::RCP<ParametrizedObjective_SimOpt<Real> > obj_;
58  Teuchos::RCP<ParametrizedEqualityConstraint_SimOpt<Real> > con_;
59 
60  // Primal vectors
61  Teuchos::RCP<Vector<Real> > state_;
62  Teuchos::RCP<Vector<Real> > state_sens_;
63  Teuchos::RCP<Vector<Real> > adjoint_;
64  Teuchos::RCP<Vector<Real> > adjoint_sens_;
65 
66  // Dual vectors
67  Teuchos::RCP<Vector<Real> > dualstate_;
68  Teuchos::RCP<Vector<Real> > dualstate1_;
69  Teuchos::RCP<Vector<Real> > dualadjoint_;
70  Teuchos::RCP<Vector<Real> > dualcontrol_;
71 
72  // Vector storage
73  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > state_storage_;
74  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > adjoint_storage_;
75 
76  bool storage_;
79 
80  void solve_state_equation(const Vector<Real> &x, Real &tol, bool flag = true, int iter = -1) {
81  // Solve state equation if not done already
82  if ( state_storage_.count(this->getParameter()) && storage_ ) {
83  state_->set(*state_storage_[this->getParameter()]);
84  }
85  else {
86  con_->solve(*dualadjoint_,*state_,x,tol);
87  // Update full objective function
88  obj_->update(*state_,x,flag,iter);
89  // Update equality constraint
90  con_->update(*state_,x,flag,iter);
91  // Store state
92  if ( storage_ ) {
93  state_storage_.insert(
94  std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(
95  this->getParameter(),state_->clone()));
96  state_storage_[this->getParameter()]->set(*state_);
97  }
98  }
99  }
100 
105  void solve_adjoint_equation(const Vector<Real> &x, Real &tol) {
106  // Solve adjoint equation if not done already
107  if ( adjoint_storage_.count(this->getParameter()) && storage_ ) {
108  adjoint_->set(*adjoint_storage_[this->getParameter()]);
109  }
110  else {
111  // Evaluate the full gradient wrt u
112  obj_->gradient_1(*dualstate_,*state_,x,tol);
113  // Solve adjoint equation
114  con_->applyInverseAdjointJacobian_1(*adjoint_,*dualstate_,*state_,x,tol);
115  adjoint_->scale(-1.0);
116  // Store adjoint
117  if ( storage_ ) {
118  adjoint_storage_.insert(
119  std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(
120  this->getParameter(),adjoint_->clone()));
121  adjoint_storage_[this->getParameter()]->set(*adjoint_);
122  }
123  }
124  }
125 
130  void solve_state_sensitivity(const Vector<Real> &v, const Vector<Real> &x, Real &tol) {
131  // Solve state sensitivity equation
132  con_->applyJacobian_2(*dualadjoint_,v,*state_,x,tol);
133  dualadjoint_->scale(-1.0);
134  con_->applyInverseJacobian_1(*state_sens_,*dualadjoint_,*state_,x,tol);
135  }
136 
144  void solve_adjoint_sensitivity(const Vector<Real> &v, const Vector<Real> &x, Real &tol) {
145  // Evaluate full hessVec in the direction (s,v)
146  obj_->hessVec_11(*dualstate_,*state_sens_,*state_,x,tol);
147  obj_->hessVec_12(*dualstate1_,v,*state_,x,tol);
148  dualstate_->plus(*dualstate1_);
149  // Apply adjoint Hessian of constraint
150  con_->applyAdjointHessian_11(*dualstate1_,*adjoint_,*state_sens_,*state_,x,tol);
151  dualstate_->plus(*dualstate1_);
152  con_->applyAdjointHessian_21(*dualstate1_,*adjoint_,v,*state_,x,tol);
153  dualstate_->plus(*dualstate1_);
154  // Solve adjoint sensitivity equation
155  dualstate_->scale(-1.0);
156  con_->applyInverseAdjointJacobian_1(*adjoint_sens_,*dualstate_,*state_,x,tol);
157  }
158 
159 public:
169  Teuchos::RCP<ParametrizedEqualityConstraint_SimOpt<Real> > &con,
170  Teuchos::RCP<Vector<Real> > &state,
171  Teuchos::RCP<Vector<Real> > &adjoint,
172  bool storage = true, bool useFDhessVec = false)
173  : obj_(obj), con_(con),
174  state_(state), state_sens_(state->clone()),
175  adjoint_(adjoint), adjoint_sens_(adjoint->clone()),
176  dualstate_(state->dual().clone()), dualstate1_(state->dual().clone()),
177  dualadjoint_(adjoint->dual().clone()), dualcontrol_(Teuchos::null),
178  storage_(storage), useFDhessVec_(useFDhessVec), is_initialized_(false) {
179  state_storage_.clear();
180  adjoint_storage_.clear();
181  }
182 
197  Teuchos::RCP<Vector<Real> > &state,
198  Teuchos::RCP<Vector<Real> > &adjoint,
199  Teuchos::RCP<Vector<Real> > &dualstate,
200  Teuchos::RCP<Vector<Real> > &dualadjoint,
201  bool storage = true, bool useFDhessVec = false)
202  : obj_(obj), con_(con),
203  state_(state), state_sens_(state->clone()),
204  adjoint_(adjoint), adjoint_sens_(adjoint->clone()),
205  dualstate_(dualstate), dualstate1_(dualstate->clone()),
206  dualadjoint_(dualadjoint->clone()), dualcontrol_(Teuchos::null),
207  storage_(storage), useFDhessVec_(useFDhessVec), is_initialized_(false) {
208  state_storage_.clear();
209  adjoint_storage_.clear();
210  }
211 
212 
213  void setParameter(const std::vector<Real> &param) {
215  con_->setParameter(param);
216  obj_->setParameter(param);
217  }
218 
221  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
222  // Reset storage flags
223  state_storage_.clear();
224  if ( flag ) {
225  adjoint_storage_.clear();
226  }
227  }
228 
233  Real value( const Vector<Real> &x, Real &tol ) {
234  // Solve state equation
235  solve_state_equation(x,tol);
236  // Get objective function value
237  return obj_->value(*state_,x,tol);
238  }
239 
245  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
246  if (!is_initialized_) {
247  dualcontrol_ = g.clone();
248  is_initialized_ = true;
249  }
250  // Solve state equation
251  solve_state_equation(x,tol);
252  // Solve adjoint equation
253  solve_adjoint_equation(x,tol);
254  // Evaluate the full gradient wrt z
255  obj_->gradient_2(*dualcontrol_,*state_,x,tol);
256  // Build gradient
257  con_->applyAdjointJacobian_2(g,*adjoint_,*state_,x,tol);
258  g.plus(*dualcontrol_);
259  }
260 
264  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
265  if ( useFDhessVec_ ) {
267  }
268  else {
269  if (!is_initialized_) {
270  dualcontrol_ = hv.clone();
271  is_initialized_ = true;
272  }
273  // Solve state equation
274  solve_state_equation(x,tol);
275  // Solve adjoint equation
276  solve_adjoint_equation(x,tol);
277  // Solve state sensitivity equation
278  solve_state_sensitivity(v,x,tol);
279  // Solve adjoint sensitivity equation
280  solve_adjoint_sensitivity(v,x,tol);
281  // Build hessVec
282  con_->applyAdjointJacobian_2(hv,*adjoint_sens_,*state_,x,tol);
283  obj_->hessVec_21(*dualcontrol_,*state_sens_,*state_,x,tol);
284  hv.plus(*dualcontrol_);
285  obj_->hessVec_22(*dualcontrol_,v,*state_,x,tol);
286  hv.plus(*dualcontrol_);
287  con_->applyAdjointHessian_12(*dualcontrol_,*adjoint_,*state_sens_,*state_,x,tol);
288  hv.plus(*dualcontrol_);
289  con_->applyAdjointHessian_22(*dualcontrol_,*adjoint_,v,*state_,x,tol);
290  hv.plus(*dualcontrol_);
291  }
292  }
293 
296  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
297  Pv.set(v.dual());
298  }
299 
300 }; // class Reduced_Objective_SimOpt
301 
302 } // namespace ROL
303 
304 #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:213
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 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.
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:74
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 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:196
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...