ROL
ROL_CompositeObjective_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_COMPOSITEOBJECTIVE_SIMOPT_H
45 #define ROL_COMPOSITEOBJECTIVE_SIMOPT_H
46 
47 #include "ROL_StdObjective.hpp"
48 #include "ROL_Objective_SimOpt.hpp"
49 
56 namespace ROL {
57 
58 template <class Real>
60 private:
61  const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > obj_vec_;
62  const ROL::Ptr<StdObjective<Real> > std_obj_;
63 
64  ROL::Ptr<std::vector<Real>> obj_value_;
65  ROL::Ptr<std::vector<Real>> obj_grad_;
66  ROL::Ptr<std::vector<Real>> obj_gv_;
67  ROL::Ptr<std::vector<Real>> obj_hess_;
68  ROL::Ptr<StdVector<Real>> obj_value_vec_;
69  ROL::Ptr<StdVector<Real>> obj_grad_vec_;
70  ROL::Ptr<StdVector<Real>> obj_gv_vec_;
71  ROL::Ptr<StdVector<Real>> obj_hess_vec_;
72  std::vector<ROL::Ptr<Vector<Real>>> vec_grad1_;
73  std::vector<ROL::Ptr<Vector<Real>>> vec_grad2_;
74  std::vector<ROL::Ptr<Vector<Real>>> vec_hess1_;
75  std::vector<ROL::Ptr<Vector<Real>>> vec_hess2_;
76 
79 
80  void initialize(const Vector<Real> &u, const Vector<Real> &z) {
81  if (!isInitialized_){
82  int size = obj_vec_.size();
83  vec_grad1_.clear(); vec_grad1_.resize(size,ROL::nullPtr);
84  vec_grad2_.clear(); vec_grad2_.resize(size,ROL::nullPtr);
85  vec_hess1_.clear(); vec_hess1_.resize(size,ROL::nullPtr);
86  vec_hess2_.clear(); vec_hess2_.resize(size,ROL::nullPtr);
87  for (int i = 0; i < size; ++i) {
88  vec_grad1_[i] = u.dual().clone();
89  vec_grad2_[i] = z.dual().clone();
90  vec_hess1_[i] = u.dual().clone();
91  vec_hess2_[i] = z.dual().clone();
92  }
93  isInitialized_ = true;
94  }
95  }
96 
97  void computeValue(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
98  initialize(u,z);
99  if (!isValueComputed_) {
100  int size = obj_vec_.size();
101  for (int i = 0; i < size; ++i) {
102  (*obj_value_)[i] = obj_vec_[i]->value(u,z,tol);
103  }
104  isValueComputed_ = true;
105  }
106  }
107 
108  void computeGradient(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
109  computeValue(u,z,tol);
110  if (!isGradientComputed_) {
111  std_obj_->gradient(*(obj_grad_vec_),*(obj_value_vec_),tol);
112  isGradientComputed_ = true;
113  }
114  }
115 
116  void computeGradient1(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
117  computeGradient(u,z,tol);
118  if (!isGradient1Computed_) {
119  int size = obj_vec_.size();
120  for (int i = 0; i < size; ++i) {
121  obj_vec_[i]->gradient_1(*(vec_grad1_[i]),u,z,tol);
122  }
123  isGradient1Computed_ = true;
124  }
125  }
126 
127  void computeGradient2(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
128  computeGradient(u,z,tol);
129  if (!isGradient2Computed_) {
130  int size = obj_vec_.size();
131  for (int i = 0; i < size; ++i) {
132  obj_vec_[i]->gradient_2(*(vec_grad2_[i]),u,z,tol);
133  }
134  isGradient2Computed_ = true;
135  }
136  }
137 
138  void computeHessVec11(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
139  computeGradient1(u,z,tol);
140  int size = obj_vec_.size();
141  for (int i = 0; i < size; ++i) {
142  (*obj_gv_)[i] = vec_grad1_[i]->dot(v.dual());
143  obj_vec_[i]->hessVec_11(*(vec_hess1_[i]),v,u,z,tol);
144  }
145  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
146  }
147 
148  void computeHessVec12(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
149  computeGradient1(u,z,tol);
150  computeGradient2(u,z,tol);
151  int size = obj_vec_.size();
152  for (int i = 0; i < size; ++i) {
153  (*obj_gv_)[i] = vec_grad2_[i]->dot(v.dual());
154  obj_vec_[i]->hessVec_12(*(vec_hess1_[i]),v,u,z,tol);
155  }
156  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
157  }
158 
159  void computeHessVec21(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
160  computeGradient1(u,z,tol);
161  computeGradient2(u,z,tol);
162  int size = obj_vec_.size();
163  for (int i = 0; i < size; ++i) {
164  (*obj_gv_)[i] = vec_grad1_[i]->dot(v.dual());
165  obj_vec_[i]->hessVec_21(*(vec_hess2_[i]),v,u,z,tol);
166  }
167  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
168  }
169 
170  void computeHessVec22(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
171  computeGradient2(u,z,tol);
172  int size = obj_vec_.size();
173  for (int i = 0; i < size; ++i) {
174  (*obj_gv_)[i] = vec_grad2_[i]->dot(v.dual());
175  obj_vec_[i]->hessVec_22(*(vec_hess2_[i]),v,u,z,tol);
176  }
177  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
178  }
179 
180 public:
181  CompositeObjective_SimOpt(const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > &obj_vec,
182  const ROL::Ptr<StdObjective<Real> > &std_obj)
183  : obj_vec_(obj_vec), std_obj_(std_obj),
184  isInitialized_(false), isValueComputed_(false),
186  obj_value_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
187  obj_value_vec_ = ROL::makePtr<StdVector<Real>>(obj_value_);
188  obj_grad_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
189  obj_grad_vec_ = ROL::makePtr<StdVector<Real>>(obj_grad_);
190  obj_gv_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
191  obj_gv_vec_ = ROL::makePtr<StdVector<Real>>(obj_gv_);
192  obj_hess_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
193  obj_hess_vec_ = ROL::makePtr<StdVector<Real>>(obj_hess_);
194  }
195 
196  void update( const Vector<Real> &u, const Vector<Real> &z, bool flag = true, int iter = -1 ) {
197  int size = obj_vec_.size();
198  for (int i = 0; i < size; ++i) {
199  obj_vec_[i]->update(u,z,flag,iter);
200  }
201  isValueComputed_ = false;
202  isGradientComputed_ = (flag ? false : isGradientComputed_);
203  isGradient1Computed_ = (flag ? false : isGradient1Computed_);
204  isGradient2Computed_ = (flag ? false : isGradient2Computed_);
205  }
206 
207  Real value( const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
208  computeValue(u,z,tol);
209  return std_obj_->value(*obj_value_vec_,tol);
210  }
211 
212 
213  void gradient_1( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
214  g.zero();
215  computeGradient1(u,z,tol);
216  int size = obj_vec_.size();
217  for (int i = 0; i < size; ++i) {
218  g.axpy((*obj_grad_)[i],*(vec_grad1_[i]));
219  }
220  }
221 
222  void gradient_2( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
223  g.zero();
224  computeGradient2(u,z,tol);
225  int size = obj_vec_.size();
226  for (int i = 0; i < size; ++i) {
227  g.axpy((*obj_grad_)[i],*(vec_grad2_[i]));
228  }
229  }
230 
231  void hessVec_11( Vector<Real> &hv, const Vector<Real> &v,
232  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
233  hv.zero();
234  computeHessVec11(v,u,z,tol);
235  int size = obj_vec_.size();
236  for (int i = 0; i < size; ++i) {
237  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
238  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
239  }
240  }
241 
242  void hessVec_12( Vector<Real> &hv, const Vector<Real> &v,
243  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
244  hv.zero();
245  computeHessVec12(v,u,z,tol);
246  int size = obj_vec_.size();
247  for (int i = 0; i < size; ++i) {
248  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
249  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
250  }
251  }
252 
253  void hessVec_21( Vector<Real> &hv, const Vector<Real> &v,
254  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
255  hv.zero();
256  computeHessVec21(v,u,z,tol);
257  int size = obj_vec_.size();
258  for (int i = 0; i < size; ++i) {
259  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
260  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
261  }
262  }
263 
264  void hessVec_22( Vector<Real> &hv, const Vector<Real> &v,
265  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
266  hv.zero();
267  computeHessVec22(v,u,z,tol);
268  int size = obj_vec_.size();
269  for (int i = 0; i < size; ++i) {
270  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
271  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
272  }
273  }
274 
275 // Definitions for parametrized (stochastic) objective functions
276 public:
277  void setParameter(const std::vector<Real> &param) {
279  const int size = obj_vec_.size();
280  for (int i = 0; i < size; ++i) {
281  obj_vec_[i]->setParameter(param);
282  }
283  std_obj_->setParameter(param);
284  isValueComputed_ = false; // Recompute value every time
285  isGradientComputed_ = false; // Recompute gradient every time
286  isGradient1Computed_ = false; // Recompute gradient every time
287  isGradient2Computed_ = false; // Recompute gradient every time
288  }
289 };
290 
291 } // namespace ROL
292 
293 #endif
Provides the interface to evaluate simulation-based objective functions.
ROL::Ptr< StdVector< Real > > obj_gv_vec_
const std::vector< ROL::Ptr< Objective_SimOpt< Real > > > obj_vec_
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:226
const ROL::Ptr< StdObjective< Real > > std_obj_
ROL::Ptr< std::vector< Real > > obj_grad_
std::vector< ROL::Ptr< Vector< Real > > > vec_grad1_
void computeGradient(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
void hessVec_11(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
Provides the interface to evaluate simulation-based composite objective functions.
CompositeObjective_SimOpt(const std::vector< ROL::Ptr< Objective_SimOpt< Real > > > &obj_vec, const ROL::Ptr< StdObjective< Real > > &std_obj)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1)
Update objective function. u is an iterate, z is an iterate, flag = true if the iterate has changed...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void computeGradient1(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
ROL::Ptr< std::vector< Real > > obj_value_
std::vector< ROL::Ptr< Vector< Real > > > vec_hess1_
void computeGradient2(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
void hessVec_21(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void computeHessVec21(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
ROL::Ptr< std::vector< Real > > obj_hess_
void computeHessVec12(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void setParameter(const std::vector< Real > &param)
void gradient_1(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
std::vector< ROL::Ptr< Vector< Real > > > vec_hess2_
void gradient_2(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
void hessVec_12(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void initialize(const Vector< Real > &u, const Vector< Real > &z)
Real value(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute value.
std::vector< ROL::Ptr< Vector< Real > > > vec_grad2_
ROL::Ptr< std::vector< Real > > obj_gv_
virtual void setParameter(const std::vector< Real > &param)
void hessVec_22(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void computeValue(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void computeHessVec11(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
ROL::Ptr< StdVector< Real > > obj_value_vec_
ROL::Ptr< StdVector< Real > > obj_hess_vec_
void computeHessVec22(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
ROL::Ptr< StdVector< Real > > obj_grad_vec_