ROL
ROL_CompositeObjective_SimOpt.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_COMPOSITEOBJECTIVE_SIMOPT_H
11 #define ROL_COMPOSITEOBJECTIVE_SIMOPT_H
12 
13 #include "ROL_StdObjective.hpp"
14 #include "ROL_Objective_SimOpt.hpp"
15 
22 namespace ROL {
23 
24 template <class Real>
26 private:
27  const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > obj_vec_;
28  const ROL::Ptr<StdObjective<Real> > std_obj_;
29 
30  ROL::Ptr<std::vector<Real>> obj_value_;
31  ROL::Ptr<std::vector<Real>> obj_grad_;
32  ROL::Ptr<std::vector<Real>> obj_gv_;
33  ROL::Ptr<std::vector<Real>> obj_hess_;
34  ROL::Ptr<StdVector<Real>> obj_value_vec_;
35  ROL::Ptr<StdVector<Real>> obj_grad_vec_;
36  ROL::Ptr<StdVector<Real>> obj_gv_vec_;
37  ROL::Ptr<StdVector<Real>> obj_hess_vec_;
38  std::vector<ROL::Ptr<Vector<Real>>> vec_grad1_;
39  std::vector<ROL::Ptr<Vector<Real>>> vec_grad2_;
40  std::vector<ROL::Ptr<Vector<Real>>> vec_hess1_;
41  std::vector<ROL::Ptr<Vector<Real>>> vec_hess2_;
42 
45 
46  void initialize(const Vector<Real> &u, const Vector<Real> &z) {
47  if (!isInitialized_){
48  int size = obj_vec_.size();
49  vec_grad1_.clear(); vec_grad1_.resize(size,ROL::nullPtr);
50  vec_grad2_.clear(); vec_grad2_.resize(size,ROL::nullPtr);
51  vec_hess1_.clear(); vec_hess1_.resize(size,ROL::nullPtr);
52  vec_hess2_.clear(); vec_hess2_.resize(size,ROL::nullPtr);
53  for (int i = 0; i < size; ++i) {
54  vec_grad1_[i] = u.dual().clone();
55  vec_grad2_[i] = z.dual().clone();
56  vec_hess1_[i] = u.dual().clone();
57  vec_hess2_[i] = z.dual().clone();
58  }
59  isInitialized_ = true;
60  }
61  }
62 
63  void computeValue(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
64  initialize(u,z);
65  if (!isValueComputed_) {
66  int size = obj_vec_.size();
67  for (int i = 0; i < size; ++i) {
68  (*obj_value_)[i] = obj_vec_[i]->value(u,z,tol);
69  }
70  isValueComputed_ = true;
71  }
72  }
73 
74  void computeGradient(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
75  computeValue(u,z,tol);
76  if (!isGradientComputed_) {
77  std_obj_->gradient(*(obj_grad_vec_),*(obj_value_vec_),tol);
78  isGradientComputed_ = true;
79  }
80  }
81 
82  void computeGradient1(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
83  computeGradient(u,z,tol);
84  if (!isGradient1Computed_) {
85  int size = obj_vec_.size();
86  for (int i = 0; i < size; ++i) {
87  obj_vec_[i]->gradient_1(*(vec_grad1_[i]),u,z,tol);
88  }
89  isGradient1Computed_ = true;
90  }
91  }
92 
93  void computeGradient2(const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
94  computeGradient(u,z,tol);
95  if (!isGradient2Computed_) {
96  int size = obj_vec_.size();
97  for (int i = 0; i < size; ++i) {
98  obj_vec_[i]->gradient_2(*(vec_grad2_[i]),u,z,tol);
99  }
100  isGradient2Computed_ = true;
101  }
102  }
103 
104  void computeHessVec11(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
105  computeGradient1(u,z,tol);
106  int size = obj_vec_.size();
107  for (int i = 0; i < size; ++i) {
108  //(*obj_gv_)[i] = vec_grad1_[i]->dot(v.dual());
109  (*obj_gv_)[i] = vec_grad1_[i]->apply(v);
110  obj_vec_[i]->hessVec_11(*(vec_hess1_[i]),v,u,z,tol);
111  }
112  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
113  }
114 
115  void computeHessVec12(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
116  computeGradient1(u,z,tol);
117  computeGradient2(u,z,tol);
118  int size = obj_vec_.size();
119  for (int i = 0; i < size; ++i) {
120  //(*obj_gv_)[i] = vec_grad2_[i]->dot(v.dual());
121  (*obj_gv_)[i] = vec_grad2_[i]->apply(v);
122  obj_vec_[i]->hessVec_12(*(vec_hess1_[i]),v,u,z,tol);
123  }
124  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
125  }
126 
127  void computeHessVec21(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
128  computeGradient1(u,z,tol);
129  computeGradient2(u,z,tol);
130  int size = obj_vec_.size();
131  for (int i = 0; i < size; ++i) {
132  //(*obj_gv_)[i] = vec_grad1_[i]->dot(v.dual());
133  (*obj_gv_)[i] = vec_grad1_[i]->apply(v);
134  obj_vec_[i]->hessVec_21(*(vec_hess2_[i]),v,u,z,tol);
135  }
136  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
137  }
138 
139  void computeHessVec22(const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol) {
140  computeGradient2(u,z,tol);
141  int size = obj_vec_.size();
142  for (int i = 0; i < size; ++i) {
143  //(*obj_gv_)[i] = vec_grad2_[i]->dot(v.dual());
144  (*obj_gv_)[i] = vec_grad2_[i]->apply(v);
145  obj_vec_[i]->hessVec_22(*(vec_hess2_[i]),v,u,z,tol);
146  }
147  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
148  }
149 
150 public:
151  CompositeObjective_SimOpt(const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > &obj_vec,
152  const ROL::Ptr<StdObjective<Real> > &std_obj)
153  : obj_vec_(obj_vec), std_obj_(std_obj),
154  isInitialized_(false), isValueComputed_(false),
156  obj_value_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
157  obj_value_vec_ = ROL::makePtr<StdVector<Real>>(obj_value_);
158  obj_grad_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
159  obj_grad_vec_ = ROL::makePtr<StdVector<Real>>(obj_grad_);
160  obj_gv_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
161  obj_gv_vec_ = ROL::makePtr<StdVector<Real>>(obj_gv_);
162  obj_hess_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
163  obj_hess_vec_ = ROL::makePtr<StdVector<Real>>(obj_hess_);
164  }
165 
166  void update( const Vector<Real> &u, const Vector<Real> &z, bool flag = true, int iter = -1 ) {
167  int size = obj_vec_.size();
168  for (int i = 0; i < size; ++i) {
169  obj_vec_[i]->update(u,z,flag,iter);
170  }
171  isValueComputed_ = false;
172  isGradientComputed_ = (flag ? false : isGradientComputed_);
173  isGradient1Computed_ = (flag ? false : isGradient1Computed_);
174  isGradient2Computed_ = (flag ? false : isGradient2Computed_);
175  }
176 
177  void update( const Vector<Real> &u, const Vector<Real> &z, UpdateType type, int iter = -1 ) {
178  int size = obj_vec_.size();
179  for (int i = 0; i < size; ++i) {
180  obj_vec_[i]->update(u,z,type,iter);
181  }
182  // Do something smarter here
183  isValueComputed_ = false;
184  isGradientComputed_ = false;
185  isGradient1Computed_ = false;
186  isGradient2Computed_ = false;
187  }
188 
189  Real value( const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
190  computeValue(u,z,tol);
191  return std_obj_->value(*obj_value_vec_,tol);
192  }
193 
194 
195  void gradient_1( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
196  g.zero();
197  computeGradient1(u,z,tol);
198  int size = obj_vec_.size();
199  for (int i = 0; i < size; ++i) {
200  g.axpy((*obj_grad_)[i],*(vec_grad1_[i]));
201  }
202  }
203 
204  void gradient_2( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
205  g.zero();
206  computeGradient2(u,z,tol);
207  int size = obj_vec_.size();
208  for (int i = 0; i < size; ++i) {
209  g.axpy((*obj_grad_)[i],*(vec_grad2_[i]));
210  }
211  }
212 
213  void hessVec_11( Vector<Real> &hv, const Vector<Real> &v,
214  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
215  hv.zero();
216  computeHessVec11(v,u,z,tol);
217  int size = obj_vec_.size();
218  for (int i = 0; i < size; ++i) {
219  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
220  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
221  }
222  }
223 
224  void hessVec_12( Vector<Real> &hv, const Vector<Real> &v,
225  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
226  hv.zero();
227  computeHessVec12(v,u,z,tol);
228  int size = obj_vec_.size();
229  for (int i = 0; i < size; ++i) {
230  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
231  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
232  }
233  }
234 
235  void hessVec_21( Vector<Real> &hv, const Vector<Real> &v,
236  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
237  hv.zero();
238  computeHessVec21(v,u,z,tol);
239  int size = obj_vec_.size();
240  for (int i = 0; i < size; ++i) {
241  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
242  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
243  }
244  }
245 
246  void hessVec_22( Vector<Real> &hv, const Vector<Real> &v,
247  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
248  hv.zero();
249  computeHessVec22(v,u,z,tol);
250  int size = obj_vec_.size();
251  for (int i = 0; i < size; ++i) {
252  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
253  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
254  }
255  }
256 
257 // Definitions for parametrized (stochastic) objective functions
258 public:
259  void setParameter(const std::vector<Real> &param) {
261  const int size = obj_vec_.size();
262  for (int i = 0; i < size; ++i) {
263  obj_vec_[i]->setParameter(param);
264  }
265  std_obj_->setParameter(param);
266  isValueComputed_ = false; // Recompute value every time
267  isGradientComputed_ = false; // Recompute gradient every time
268  isGradient1Computed_ = false; // Recompute gradient every time
269  isGradient2Computed_ = false; // Recompute gradient every time
270  }
271 };
272 
273 } // namespace ROL
274 
275 #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:192
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:119
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:133
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:46
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_