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_gv_)[i] = vec_grad1_[i]->apply(v);
144  obj_vec_[i]->hessVec_11(*(vec_hess1_[i]),v,u,z,tol);
145  }
146  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
147  }
148 
149  void computeHessVec12(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
150  computeGradient1(u,z,tol);
151  computeGradient2(u,z,tol);
152  int size = obj_vec_.size();
153  for (int i = 0; i < size; ++i) {
154  //(*obj_gv_)[i] = vec_grad2_[i]->dot(v.dual());
155  (*obj_gv_)[i] = vec_grad2_[i]->apply(v);
156  obj_vec_[i]->hessVec_12(*(vec_hess1_[i]),v,u,z,tol);
157  }
158  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
159  }
160 
161  void computeHessVec21(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
162  computeGradient1(u,z,tol);
163  computeGradient2(u,z,tol);
164  int size = obj_vec_.size();
165  for (int i = 0; i < size; ++i) {
166  //(*obj_gv_)[i] = vec_grad1_[i]->dot(v.dual());
167  (*obj_gv_)[i] = vec_grad1_[i]->apply(v);
168  obj_vec_[i]->hessVec_21(*(vec_hess2_[i]),v,u,z,tol);
169  }
170  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
171  }
172 
173  void computeHessVec22(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
174  computeGradient2(u,z,tol);
175  int size = obj_vec_.size();
176  for (int i = 0; i < size; ++i) {
177  //(*obj_gv_)[i] = vec_grad2_[i]->dot(v.dual());
178  (*obj_gv_)[i] = vec_grad2_[i]->apply(v);
179  obj_vec_[i]->hessVec_22(*(vec_hess2_[i]),v,u,z,tol);
180  }
181  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
182  }
183 
184 public:
185  CompositeObjective_SimOpt(const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > &obj_vec,
186  const ROL::Ptr<StdObjective<Real> > &std_obj)
187  : obj_vec_(obj_vec), std_obj_(std_obj),
188  isInitialized_(false), isValueComputed_(false),
190  obj_value_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
191  obj_value_vec_ = ROL::makePtr<StdVector<Real>>(obj_value_);
192  obj_grad_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
193  obj_grad_vec_ = ROL::makePtr<StdVector<Real>>(obj_grad_);
194  obj_gv_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
195  obj_gv_vec_ = ROL::makePtr<StdVector<Real>>(obj_gv_);
196  obj_hess_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
197  obj_hess_vec_ = ROL::makePtr<StdVector<Real>>(obj_hess_);
198  }
199 
200  void update( const Vector<Real> &u, const Vector<Real> &z, bool flag = true, int iter = -1 ) {
201  int size = obj_vec_.size();
202  for (int i = 0; i < size; ++i) {
203  obj_vec_[i]->update(u,z,flag,iter);
204  }
205  isValueComputed_ = false;
206  isGradientComputed_ = (flag ? false : isGradientComputed_);
207  isGradient1Computed_ = (flag ? false : isGradient1Computed_);
208  isGradient2Computed_ = (flag ? false : isGradient2Computed_);
209  }
210 
211  void update( const Vector<Real> &u, const Vector<Real> &z, UpdateType type, int iter = -1 ) {
212  int size = obj_vec_.size();
213  for (int i = 0; i < size; ++i) {
214  obj_vec_[i]->update(u,z,type,iter);
215  }
216  // Do something smarter here
217  isValueComputed_ = false;
218  isGradientComputed_ = false;
219  isGradient1Computed_ = false;
220  isGradient2Computed_ = false;
221  }
222 
223  Real value( const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
224  computeValue(u,z,tol);
225  return std_obj_->value(*obj_value_vec_,tol);
226  }
227 
228 
229  void gradient_1( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
230  g.zero();
231  computeGradient1(u,z,tol);
232  int size = obj_vec_.size();
233  for (int i = 0; i < size; ++i) {
234  g.axpy((*obj_grad_)[i],*(vec_grad1_[i]));
235  }
236  }
237 
238  void gradient_2( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
239  g.zero();
240  computeGradient2(u,z,tol);
241  int size = obj_vec_.size();
242  for (int i = 0; i < size; ++i) {
243  g.axpy((*obj_grad_)[i],*(vec_grad2_[i]));
244  }
245  }
246 
247  void hessVec_11( Vector<Real> &hv, const Vector<Real> &v,
248  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
249  hv.zero();
250  computeHessVec11(v,u,z,tol);
251  int size = obj_vec_.size();
252  for (int i = 0; i < size; ++i) {
253  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
254  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
255  }
256  }
257 
258  void hessVec_12( Vector<Real> &hv, const Vector<Real> &v,
259  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
260  hv.zero();
261  computeHessVec12(v,u,z,tol);
262  int size = obj_vec_.size();
263  for (int i = 0; i < size; ++i) {
264  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
265  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
266  }
267  }
268 
269  void hessVec_21( Vector<Real> &hv, const Vector<Real> &v,
270  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
271  hv.zero();
272  computeHessVec21(v,u,z,tol);
273  int size = obj_vec_.size();
274  for (int i = 0; i < size; ++i) {
275  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
276  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
277  }
278  }
279 
280  void hessVec_22( Vector<Real> &hv, const Vector<Real> &v,
281  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
282  hv.zero();
283  computeHessVec22(v,u,z,tol);
284  int size = obj_vec_.size();
285  for (int i = 0; i < size; ++i) {
286  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
287  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
288  }
289  }
290 
291 // Definitions for parametrized (stochastic) objective functions
292 public:
293  void setParameter(const std::vector<Real> &param) {
295  const int size = obj_vec_.size();
296  for (int i = 0; i < size; ++i) {
297  obj_vec_[i]->setParameter(param);
298  }
299  std_obj_->setParameter(param);
300  isValueComputed_ = false; // Recompute value every time
301  isGradientComputed_ = false; // Recompute gradient every time
302  isGradient1Computed_ = false; // Recompute gradient every time
303  isGradient2Computed_ = false; // Recompute gradient every time
304  }
305 };
306 
307 } // namespace ROL
308 
309 #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_
void update(const Vector< Real > &u, const Vector< Real > &z, UpdateType type, int iter=-1)
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_