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 #ifndef ROL_REDUCED_OBJECTIVE_SIMOPT_H
45 #define ROL_REDUCED_OBJECTIVE_SIMOPT_H
46 
47 #include "ROL_Objective_SimOpt.hpp"
49 #include "ROL_Vector_SimOpt.hpp"
50 
60 namespace ROL {
61 
62 template <class Real>
63 class Reduced_Objective_SimOpt : public Objective<Real> {
64 private:
65  Teuchos::RCP<Objective_SimOpt<Real> > obj_;
66  Teuchos::RCP<EqualityConstraint_SimOpt<Real> > con_;
67  Teuchos::RCP<Vector<Real> > state_;
68  Teuchos::RCP<Vector<Real> > adjoint_;
69  Teuchos::RCP<const Vector<Real> > dualstate_;
70  Teuchos::RCP<const Vector<Real> > dualadjoint_;
71 
72  bool storage_;
75 
77 
81  void solve_state_equation(const ROL::Vector<Real> &x, Real &tol, bool flag = true, int iter = -1) {
82  // Solve state equation if not done already
83  if (!is_state_computed_ || !storage_) {
84  con_->solve(*state_,x,tol);
85  // Update full objective function
86  obj_->update(*state_,x,flag,iter);
87  // Update equality constraint
88  con_->update(*state_,x,flag,iter);
89  // Reset storage flags
90  is_state_computed_ = true;
91  }
92  }
93 
98  void solve_adjoint_equation(const ROL::Vector<Real> &x, Real &tol) {
99  // Solve state equation if not done already
100  solve_state_equation(x,tol);
101  // Solve adjoint equation if not done already
102  if(!is_adjoint_computed_ || !storage_) {
103  // Evaluate the full gradient wrt u
104  Teuchos::RCP<Vector<Real> > gu = dualstate_->clone();
105  obj_->gradient_1(*gu,*state_,x,tol);
106  // Solve adjoint equation
107  con_->applyInverseAdjointJacobian_1(*adjoint_,*gu,*state_,x,tol);
108  adjoint_->scale(-1.0);
109  // Reset storage flags
110  is_adjoint_computed_ = true;
111  }
112  }
113 
119  const ROL::Vector<Real> &x, Real &tol) {
120  // Solve state equation if not done already
121  solve_state_equation(x,tol);
122  // Solve state sensitivity equation
123  Teuchos::RCP<Vector<Real> > Bv = dualadjoint_->clone();
124  con_->applyJacobian_2(*Bv,v,*state_,x,tol);
125  Bv->scale(-1.0);
126  con_->applyInverseJacobian_1(s,*Bv,*state_,x,tol);
127  }
128 
137  const ROL::Vector<Real> &v, const ROL::Vector<Real> &x, Real &tol) {
138  // Solve state equation if not done already
139  solve_state_equation(x,tol);
140  // Solve adjoint equation if not done already
141  solve_adjoint_equation(x,tol);
142  // Evaluate full hessVec in the direction (s,v)
143  Teuchos::RCP<Vector<Real> > hv11 = dualstate_->clone();
144  obj_->hessVec_11(*hv11,s,*state_,x,tol);
145  Teuchos::RCP<Vector<Real> > hv12 = dualstate_->clone();
146  obj_->hessVec_12(*hv12,v,*state_,x,tol);
147  // Apply adjoint Hessian of constraint
148  Teuchos::RCP<Vector<Real> > hc11 = dualstate_->clone();
149  con_->applyAdjointHessian_11(*hc11,*adjoint_,s,*state_,x,tol);
150  Teuchos::RCP<Vector<Real> > hc21 = dualstate_->clone();
151  con_->applyAdjointHessian_21(*hc21,*adjoint_,v,*state_,x,tol);
152  // Solve adjoint sensitivity equation
153  Teuchos::RCP<Vector<Real> > r = dualstate_->clone();
154  r->set(*hv11);
155  r->plus(*hv12);
156  r->plus(*hc11);
157  r->plus(*hc21);
158  r->scale(-1.0);
159  con_->applyInverseAdjointJacobian_1(p,*r,*state_,x,tol);
160  }
161 
162 public:
163 
174  const Teuchos::RCP<EqualityConstraint_SimOpt<Real> > &con,
175  const Teuchos::RCP<Vector<Real> > &state,
176  const Teuchos::RCP<Vector<Real> > &adjoint,
177  bool storage = true, bool useFDhessVec = false)
178  : obj_(obj), con_(con),
179  state_(state), adjoint_(adjoint),
180  storage_(storage), useFDhessVec_(useFDhessVec) {
181  is_state_computed_ = false;
182  is_adjoint_computed_ = false;
183  dualstate_ = Teuchos::rcpFromRef(state_->dual());
184  dualadjoint_ = Teuchos::rcpFromRef(adjoint_->dual());
185  }
186 
200  Teuchos::RCP<EqualityConstraint_SimOpt<Real> > &con,
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), adjoint_(adjoint), dualstate_(dualstate), dualadjoint_(dualadjoint),
208  storage_(storage), useFDhessVec_(useFDhessVec) {
209  is_state_computed_ = false;
210  is_adjoint_computed_ = false;
211  }
212 
215  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
216  // Reset storage flags
217  is_state_computed_ = false;
218  is_adjoint_computed_ = false;
219  // Solve state equation
220  if ( storage_ ) {
221  Real tol = std::sqrt(ROL_EPSILON);
222  solve_state_equation(x,tol,flag,iter);
223  }
224  }
225 
230  Real value( const Vector<Real> &x, Real &tol ) {
231  // Solve state equation
232  solve_state_equation(x,tol);
233  // Get objective function value
234  return obj_->value(*state_,x,tol);
235  }
236 
242  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
243  // Solve state equation
244  solve_state_equation(x,tol);
245  // Solve adjoint equation
246  solve_adjoint_equation(x,tol);
247  // Evaluate the full gradient wrt z
248  Teuchos::RCP<Vector<Real> > gz = g.clone();
249  obj_->gradient_2(*gz,*state_,x,tol);
250  // Build gradient
251  con_->applyAdjointJacobian_2(g,*adjoint_,*state_,x,tol);
252  g.plus(*gz);
253  }
254 
258  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
259  if ( useFDhessVec_ ) {
260  Objective<Real>::hessVec(hv,v,x,tol);
261  }
262  else {
263  // Solve state equation
264  solve_state_equation(x,tol);
265  // Solve adjoint equation
266  solve_adjoint_equation(x,tol);
267  // Solve state sensitivity equation
268  Teuchos::RCP<Vector<Real> > s = state_->clone();
269  solve_state_sensitivity(*s,v,x,tol);
270  // Solve adjoint sensitivity equation
271  Teuchos::RCP<Vector<Real> > p = adjoint_->clone();
272  solve_adjoint_sensitivity(*p,*s,v,x,tol);
273  // Build hessVec
274  con_->applyAdjointJacobian_2(hv,*p,*state_,x,tol);
275  Teuchos::RCP<Vector<Real> > tmp = hv.clone();
276  obj_->hessVec_21(*tmp,*s,*state_,x,tol);
277  hv.plus(*tmp);
278  obj_->hessVec_22(*tmp,v,*state_,x,tol);
279  hv.plus(*tmp);
280  con_->applyAdjointHessian_12(*tmp,*adjoint_,*s,*state_,x,tol);
281  hv.plus(*tmp);
282  con_->applyAdjointHessian_22(*tmp,*adjoint_,v,*state_,x,tol);
283  hv.plus(*tmp);
284  }
285  }
286 
289  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
290  Pv.set(v.dual());
291  }
292 
293 }; // class ROL::Reduced_Objective_SimOpt
294 
295 } // namespace ROL
296 
297 #endif
Teuchos::RCP< Objective_SimOpt< Real > > obj_
SimOpt objective function.
Provides the interface to evaluate objective functions.
Provides the interface to evaluate simulation-based objective functions.
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:211
virtual void plus(const Vector &x)=0
Compute , where .
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply a reduced Hessian preconditioner.
bool storage_
Flag whether or not to store computed quantities.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Teuchos::RCP< Vector< Real > > state_
Storage for the state variable.
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Reduced_Objective_SimOpt(const Teuchos::RCP< Objective_SimOpt< Real > > &obj, const Teuchos::RCP< EqualityConstraint_SimOpt< Real > > &con, const Teuchos::RCP< Vector< Real > > &state, const Teuchos::RCP< Vector< Real > > &adjoint, bool storage=true, bool useFDhessVec=false)
Primary constructor.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Given , evaluate the gradient of the objective function where solves .
void solve_adjoint_equation(const ROL::Vector< Real > &x, Real &tol)
Given which solves the state equation, solve the adjoint equation for .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Defines the equality constraint operator interface for simulation-based optimization.
Teuchos::RCP< Vector< Real > > adjoint_
Storage for the adjoint variable.
Reduced_Objective_SimOpt(Teuchos::RCP< Objective_SimOpt< Real > > &obj, Teuchos::RCP< EqualityConstraint_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_equation(const ROL::Vector< Real > &x, Real &tol, bool flag=true, int iter=-1)
Given , solve the state equation for .
bool is_state_computed_
Flag whether or not to store the state variable.
Provides the interface to evaluate simulation-based reduced objective functions.
void solve_adjoint_sensitivity(ROL::Vector< Real > &p, const ROL::Vector< Real > &s, const ROL::Vector< Real > &v, const ROL::Vector< Real > &x, Real &tol)
Given , the adjoint variable , and a direction , solve the adjoint sensitvity equation for ...
bool useFDhessVec_
Flag whether or not to use finite difference hessVec.
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< 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 .
Teuchos::RCP< const Vector< Real > > dualadjoint_
Dual adjoint vector, used for cloning only.
void solve_state_sensitivity(ROL::Vector< Real > &s, const ROL::Vector< Real > &v, const ROL::Vector< Real > &x, Real &tol)
Given which solves the state equation and a direction , solve the state senstivity equation for ...
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:194
Teuchos::RCP< EqualityConstraint_SimOpt< Real > > con_
SimOpt equality constraint.
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update the SimOpt objective function and equality constraint.
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:115
bool is_adjoint_computed_
Flag whether or not to store the adjoint variable.