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  Teuchos::RCP<Vector<Real> > state_;
60  Teuchos::RCP<Vector<Real> > adjoint_;
61  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > state_storage_;
62  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > adjoint_storage_;
63  Teuchos::RCP<const Vector<Real> > dualstate_;
64  Teuchos::RCP<const Vector<Real> > dualadjoint_;
65 
66  bool storage_;
67 
69 
70  void solve_state_equation(const Vector<Real> &x, Real &tol, bool flag = true, int iter = -1) {
71  // Solve state equation if not done already
72  if ( state_storage_.count(this->getParameter()) ) {
73  state_->set(*state_storage_[this->getParameter()]);
74  }
75  else {
76  con_->solve(*state_,x,tol);
77  // Update full objective function
78  obj_->update(*state_,x,flag,iter);
79  // Update equality constraint
80  con_->update(*state_,x,flag,iter);
81  // Store state
82  Teuchos::RCP<Vector<Real> > tmp = state_->clone();
83  state_storage_.insert(std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(this->getParameter(),tmp));
84  state_storage_[this->getParameter()]->set(*state_);
85  }
86  }
87 
92  void solve_adjoint_equation(const Vector<Real> &x, Real &tol) {
93  // Solve state equation if not done already
94  solve_state_equation(x,tol);
95  // Solve adjoint equation if not done already
96  if ( adjoint_storage_.count(this->getParameter()) ) {
97  adjoint_->set(*adjoint_storage_[this->getParameter()]);
98  }
99  else {
100  // Evaluate the full gradient wrt u
101  Teuchos::RCP<Vector<Real> > gu = dualstate_->clone();
102  obj_->gradient_1(*gu,*state_,x,tol);
103  // Solve adjoint equation
104  con_->applyInverseAdjointJacobian_1(*adjoint_,*gu,*state_,x,tol);
105  adjoint_->scale(-1.0);
106  // Store adjoint
107  Teuchos::RCP<Vector<Real> > tmp = adjoint_->clone();
108  adjoint_storage_.insert(std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(this->getParameter(),tmp));
109  adjoint_storage_[this->getParameter()]->set(*adjoint_);
110  }
111  }
112 
118  const Vector<Real> &x, Real &tol) {
119  // Solve state equation if not done already
120  solve_state_equation(x,tol);
121  // Solve state sensitivity equation
122  Teuchos::RCP<Vector<Real> > Bv = dualadjoint_->clone();
123  con_->applyJacobian_2(*Bv,v,*state_,x,tol);
124  Bv->scale(-1.0);
125  con_->applyInverseJacobian_1(s,*Bv,*state_,x,tol);
126  }
127 
136  const Vector<Real> &v, const Vector<Real> &x, Real &tol) {
137  // Solve state equation if not done already
138  solve_state_equation(x,tol);
139  // Solve adjoint equation if not done already
140  solve_adjoint_equation(x,tol);
141  // Evaluate full hessVec in the direction (s,v)
142  Teuchos::RCP<Vector<Real> > hv11 = dualstate_->clone();
143  obj_->hessVec_11(*hv11,s,*state_,x,tol);
144  Teuchos::RCP<Vector<Real> > hv12 = dualstate_->clone();
145  obj_->hessVec_12(*hv12,v,*state_,x,tol);
146  // Apply adjoint Hessian of constraint
147  Teuchos::RCP<Vector<Real> > hc11 = dualstate_->clone();
148  con_->applyAdjointHessian_11(*hc11,*adjoint_,s,*state_,x,tol);
149  Teuchos::RCP<Vector<Real> > hc21 = dualstate_->clone();
150  con_->applyAdjointHessian_21(*hc21,*adjoint_,v,*state_,x,tol);
151  // Solve adjoint sensitivity equation
152  Teuchos::RCP<Vector<Real> > r = dualstate_->clone();
153  r->set(*hv11);
154  r->plus(*hv12);
155  r->plus(*hc11);
156  r->plus(*hc21);
157  r->scale(-1.0);
158  con_->applyInverseAdjointJacobian_1(p,*r,*state_,x,tol);
159  }
160 
161 public:
171  Teuchos::RCP<ParametrizedEqualityConstraint_SimOpt<Real> > &con,
172  Teuchos::RCP<Vector<Real> > &state,
173  Teuchos::RCP<Vector<Real> > &adjoint,
174  bool storage = true, bool useFDhessVec = false)
175  : obj_(obj), con_(con), state_(state), adjoint_(adjoint), storage_(storage), useFDhessVec_(useFDhessVec) {
176  state_storage_.clear();
177  adjoint_storage_.clear();
178  dualstate_ = Teuchos::rcpFromRef(state_->dual());
179  dualadjoint_ = Teuchos::rcpFromRef(adjoint_->dual());
180  }
181 
196  Teuchos::RCP<Vector<Real> > &state,
197  Teuchos::RCP<Vector<Real> > &adjoint,
198  Teuchos::RCP<Vector<Real> > &dualstate,
199  Teuchos::RCP<Vector<Real> > &dualadjoint,
200  bool storage = true, bool useFDhessVec = false)
201  : obj_(obj), con_(con),
202  state_(state), adjoint_(adjoint), dualstate_(dualstate), dualadjoint_(dualadjoint),
203  storage_(storage), useFDhessVec_(useFDhessVec) {
204  state_storage_.clear();
205  adjoint_storage_.clear();
206  }
207 
208 
209  void setParameter(const std::vector<Real> &param) {
211  con_->setParameter(param);
212  obj_->setParameter(param);
213  }
214 
217  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
218  // Reset storage flags
219  state_storage_.clear();
220  if ( flag ) {
221  adjoint_storage_.clear();
222  }
223  }
224 
229  Real value( const Vector<Real> &x, Real &tol ) {
230  // Solve state equation
231  solve_state_equation(x,tol);
232  // Get objective function value
233  return obj_->value(*state_,x,tol);
234  }
235 
241  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
242  // Solve state equation
243  solve_state_equation(x,tol);
244  // Solve adjoint equation
245  solve_adjoint_equation(x,tol);
246  // Evaluate the full gradient wrt z
247  Teuchos::RCP<Vector<Real> > gz = x.clone();
248  obj_->gradient_2(*gz,*state_,x,tol);
249  // Build gradient
250  con_->applyAdjointJacobian_2(g,*adjoint_,*state_,x,tol);
251  g.plus(*gz);
252  }
253 
257  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
258  if ( useFDhessVec_ ) {
260  }
261  else {
262  // Solve state equation
263  solve_state_equation(x,tol);
264  // Solve adjoint equation
265  solve_adjoint_equation(x,tol);
266  // Solve state sensitivity equation
267  Teuchos::RCP<Vector<Real> > s = state_->clone();
268  solve_state_sensitivity(*s,v,x,tol);
269  // Solve adjoint sensitivity equation
270  Teuchos::RCP<Vector<Real> > p = adjoint_->clone();
271  solve_adjoint_sensitivity(*p,*s,v,x,tol);
272  // Build hessVec
273  con_->applyAdjointJacobian_2(hv,*p,*state_,x,tol);
274  Teuchos::RCP<Vector<Real> > tmp = x.clone();
275  obj_->hessVec_21(*tmp,*s,*state_,x,tol);
276  hv.plus(*tmp);
277  obj_->hessVec_22(*tmp,v,*state_,x,tol);
278  hv.plus(*tmp);
279  con_->applyAdjointHessian_12(*tmp,*adjoint_,*s,*state_,x,tol);
280  hv.plus(*tmp);
281  con_->applyAdjointHessian_22(*tmp,*adjoint_,v,*state_,x,tol);
282  hv.plus(*tmp);
283  }
284  }
285 
288  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
289  Pv.set(v);
290  }
291 
292 }; // class Reduced_Objective_SimOpt
293 
294 } // namespace ROL
295 
296 #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 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.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
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_
Teuchos::RCP< const Vector< Real > > dualadjoint_
Dual adjoint vector, used for cloning only.
Teuchos::RCP< const Vector< Real > > dualstate_
Dual state vector, used for cloning only.
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:194
void solve_adjoint_sensitivity(Vector< Real > &p, const Vector< Real > &s, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Given , the adjoint variable , and a direction , solve the adjoint sensitvity equation for ...
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...
void solve_state_sensitivity(Vector< Real > &s, 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 ...