ROL
ROL_ParametrizedCompositeObjective_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 
49 
56 namespace ROL {
57 
58 template <class Real>
60 private:
61  const std::vector<Teuchos::RCP<ParametrizedObjective_SimOpt<Real> > > obj_vec_;
62  const Teuchos::RCP<ParametrizedStdObjective<Real> > std_obj_;
63 
64  Teuchos::RCP<std::vector<Real> > obj_value_;
65  Teuchos::RCP<std::vector<Real> > obj_grad_;
66  Teuchos::RCP<std::vector<Real> > obj_gv_;
67  Teuchos::RCP<std::vector<Real> > obj_hess_;
68  Teuchos::RCP<StdVector<Real> > obj_value_vec_;
69  Teuchos::RCP<StdVector<Real> > obj_grad_vec_;
70  Teuchos::RCP<StdVector<Real> > obj_gv_vec_;
71  Teuchos::RCP<StdVector<Real> > obj_hess_vec_;
72  std::vector<Teuchos::RCP<Vector<Real> > > vec_grad1_;
73  std::vector<Teuchos::RCP<Vector<Real> > > vec_grad2_;
74  std::vector<Teuchos::RCP<Vector<Real> > > vec_hess1_;
75  std::vector<Teuchos::RCP<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,Teuchos::null);
84  vec_grad2_.clear(); vec_grad2_.resize(size,Teuchos::null);
85  vec_hess1_.clear(); vec_hess1_.resize(size,Teuchos::null);
86  vec_hess2_.clear(); vec_hess2_.resize(size,Teuchos::null);
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:
182  const std::vector<Teuchos::RCP<ParametrizedObjective_SimOpt<Real> > > &obj_vec,
183  const Teuchos::RCP<ParametrizedStdObjective<Real> > &std_obj)
184  : obj_vec_(obj_vec), std_obj_(std_obj),
185  isInitialized_(false), isValueComputed_(false),
187  obj_value_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
188  obj_value_vec_ = Teuchos::rcp(new StdVector<Real>(obj_value_));
189  obj_grad_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
190  obj_grad_vec_ = Teuchos::rcp(new StdVector<Real>(obj_grad_));
191  obj_gv_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
192  obj_gv_vec_ = Teuchos::rcp(new StdVector<Real>(obj_gv_));
193  obj_hess_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
194  obj_hess_vec_ = Teuchos::rcp(new StdVector<Real>(obj_hess_));
195  }
196 
197  void setParameter(const std::vector<Real> & param) {
199  const int size = obj_vec_.size();
200  for (int i = 0; i < size; ++i) {
201  obj_vec_[i]->setParameter(param);
202  }
203  std_obj_->setParameter(param);
204  isValueComputed_ = false; // Recompute value every time
205  isGradientComputed_ = false; // Recompute gradient every time
206  isGradient1Computed_ = false; // Recompute gradient every time
207  isGradient2Computed_ = false; // Recompute gradient every time
208  }
209 
210  void update( const Vector<Real> &u, const Vector<Real> &z, bool flag = true, int iter = -1 ) {
211  int size = obj_vec_.size();
212  for (int i = 0; i < size; ++i) {
213  obj_vec_[i]->update(u,z,flag,iter);
214  }
215  isValueComputed_ = false;
216  isGradientComputed_ = (flag ? false : isGradientComputed_);
217  isGradient1Computed_ = (flag ? false : isGradient1Computed_);
218  isGradient2Computed_ = (flag ? false : isGradient2Computed_);
219  }
220 
221  Real value( const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
222  computeValue(u,z,tol);
223  return std_obj_->value(*obj_value_vec_,tol);
224  }
225 
226 
227  void gradient_1( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
228  g.zero();
229  computeGradient1(u,z,tol);
230  int size = obj_vec_.size();
231  for (int i = 0; i < size; ++i) {
232  g.axpy((*obj_grad_)[i],*(vec_grad1_[i]));
233  }
234  }
235 
236  void gradient_2( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
237  g.zero();
238  computeGradient2(u,z,tol);
239  int size = obj_vec_.size();
240  for (int i = 0; i < size; ++i) {
241  g.axpy((*obj_grad_)[i],*(vec_grad2_[i]));
242  }
243  }
244 
245  void hessVec_11( Vector<Real> &hv, const Vector<Real> &v,
246  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
247  hv.zero();
248  computeHessVec11(v,u,z,tol);
249  int size = obj_vec_.size();
250  for (int i = 0; i < size; ++i) {
251  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
252  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
253  }
254  }
255 
256  void hessVec_12( Vector<Real> &hv, const Vector<Real> &v,
257  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
258  hv.zero();
259  computeHessVec12(v,u,z,tol);
260  int size = obj_vec_.size();
261  for (int i = 0; i < size; ++i) {
262  hv.axpy((*obj_grad_)[i],*(vec_hess1_[i]));
263  hv.axpy((*obj_hess_)[i],*(vec_grad1_[i]));
264  }
265  }
266 
267  void hessVec_21( Vector<Real> &hv, const Vector<Real> &v,
268  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
269  hv.zero();
270  computeHessVec21(v,u,z,tol);
271  int size = obj_vec_.size();
272  for (int i = 0; i < size; ++i) {
273  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
274  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
275  }
276  }
277 
278  void hessVec_22( Vector<Real> &hv, const Vector<Real> &v,
279  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
280  hv.zero();
281  computeHessVec22(v,u,z,tol);
282  int size = obj_vec_.size();
283  for (int i = 0; i < size; ++i) {
284  hv.axpy((*obj_grad_)[i],*(vec_hess2_[i]));
285  hv.axpy((*obj_hess_)[i],*(vec_grad2_[i]));
286  }
287  }
288 
289 };
290 
291 } // namespace ROL
292 
293 #endif
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector's.
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:215
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...
std::vector< Teuchos::RCP< Vector< Real > > > vec_grad1_
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:145
void computeHessVec22(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
std::vector< Teuchos::RCP< Vector< Real > > > vec_hess1_
void computeHessVec21(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void computeGradient(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void hessVec_21(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
const std::vector< Teuchos::RCP< ParametrizedObjective_SimOpt< Real > > > obj_vec_
void computeHessVec11(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:159
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
Real value(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute value.
void gradient_2(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
void computeGradient2(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
std::vector< Teuchos::RCP< Vector< Real > > > vec_hess2_
void hessVec_12(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void gradient_1(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
const Teuchos::RCP< ParametrizedStdObjective< Real > > std_obj_
virtual void setParameter(const std::vector< Real > &param)
std::vector< Teuchos::RCP< Vector< Real > > > vec_grad2_
void initialize(const Vector< Real > &u, const Vector< Real > &z)
void computeHessVec12(const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
ParametrizedCompositeObjective_SimOpt(const std::vector< Teuchos::RCP< ParametrizedObjective_SimOpt< Real > > > &obj_vec, const Teuchos::RCP< ParametrizedStdObjective< Real > > &std_obj)
void computeValue(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
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.
void computeGradient1(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void hessVec_22(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)