ROL
ROL_LinearCombinationObjective_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_LINEARCOMBINATIONOBJECTIVE_SIMOPT_H
11 #define ROL_LINEARCOMBINATIONOBJECTIVE_SIMOPT_H
12 
13 #include "ROL_Objective_SimOpt.hpp"
14 #include "ROL_Ptr.hpp"
15 
16 namespace ROL {
17 
18 template <class Real>
20 private:
21  const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > obj_;
22  std::vector<Real> weights_;
23  size_t size_;
24 
25  ROL::Ptr<Vector<Real> > udual_, zdual_;
27 
28 public:
29  LinearCombinationObjective_SimOpt(const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > &obj)
30  : Objective_SimOpt<Real>(), obj_(obj),
31  udual_(ROL::nullPtr), zdual_(ROL::nullPtr),
32  uinitialized_(false), zinitialized_(false) {
33  size_ = obj_.size();
34  weights_.clear(); weights_.assign(size_,static_cast<Real>(1));
35  }
36 
37  LinearCombinationObjective_SimOpt(const std::vector<Real> &weights,
38  const std::vector<ROL::Ptr<Objective_SimOpt<Real> > > &obj)
39  : Objective_SimOpt<Real>(), obj_(obj),
40  weights_(weights), size_(weights.size()),
41  udual_(ROL::nullPtr), zdual_(ROL::nullPtr),
42  uinitialized_(false), zinitialized_(false) {}
43 
44  void update(const Vector<Real> &u, const Vector<Real> &z, UpdateType type, int iter = -1) {
45  for (size_t i=0; i<size_; ++i) {
46  obj_[i]->update(u,z,type,iter);
47  }
48  }
49 
50  void update(const Vector<Real> &u, const Vector<Real> &z, bool flag = true, int iter = -1) {
51  for (size_t i=0; i<size_; ++i) {
52  obj_[i]->update(u,z,flag,iter);
53  }
54  }
55 
56  Real value( const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
57  Real val(0);
58  for (size_t i = 0; i < size_; ++i) {
59  val += weights_[i]*obj_[i]->value(u,z,tol);
60  }
61  return val;
62  }
63 
64  void gradient_1( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
65  if (!uinitialized_) {
66  udual_ = g.clone();
67  uinitialized_ = true;
68  }
69  g.zero();
70  for (size_t i = 0; i < size_; ++i) {
71  obj_[i]->gradient_1(*udual_,u,z,tol);
72  g.axpy(weights_[i],*udual_);
73  }
74  }
75 
76  void gradient_2( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
77  if (!zinitialized_) {
78  zdual_ = g.clone();
79  zinitialized_ = true;
80  }
81  g.zero();
82  for (size_t i = 0; i < size_; ++i) {
83  obj_[i]->gradient_2(*zdual_,u,z,tol);
84  g.axpy(weights_[i],*zdual_);
85  }
86  }
87 
88  void hessVec_11( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
89  if (!uinitialized_) {
90  udual_ = hv.clone();
91  uinitialized_ = true;
92  }
93  hv.zero();
94  for (size_t i = 0; i < size_; ++i) {
95  obj_[i]->hessVec_11(*udual_,v,u,z,tol);
96  hv.axpy(weights_[i],*udual_);
97  }
98  }
99 
100  void hessVec_12( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
101  if (!uinitialized_) {
102  udual_ = hv.clone();
103  uinitialized_ = true;
104  }
105  hv.zero();
106  for (size_t i = 0; i < size_; ++i) {
107  obj_[i]->hessVec_12(*udual_,v,u,z,tol);
108  hv.axpy(weights_[i],*udual_);
109  }
110  }
111 
112  void hessVec_21( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
113  if (!zinitialized_) {
114  zdual_ = hv.clone();
115  zinitialized_ = true;
116  }
117  hv.zero();
118  for (size_t i = 0; i < size_; ++i) {
119  obj_[i]->hessVec_21(*zdual_,v,u,z,tol);
120  hv.axpy(weights_[i],*zdual_);
121  }
122  }
123 
124  void hessVec_22( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
125  if (!zinitialized_) {
126  zdual_ = hv.clone();
127  zinitialized_ = true;
128  }
129  hv.zero();
130  for (size_t i = 0; i < size_; ++i) {
131  obj_[i]->hessVec_22(*zdual_,v,u,z,tol);
132  hv.axpy(weights_[i],*zdual_);
133  }
134  }
135 
136 // Definitions for parametrized (stochastic) objective functions
137 public:
138  void setParameter(const std::vector<Real> &param) {
140  for (size_t i = 0; i < size_; ++i) {
141  obj_[i]->setParameter(param);
142  }
143  }
144 }; // class LinearCombinationObjective
145 
146 } // namespace ROL
147 
148 #endif
void hessVec_12(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Provides the interface to evaluate simulation-based objective functions.
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...
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
LinearCombinationObjective_SimOpt(const std::vector< ROL::Ptr< Objective_SimOpt< Real > > > &obj)
const double weights[4][5]
Definition: ROL_Types.hpp:834
Real value(const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute value.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:119
const std::vector< ROL::Ptr< Objective_SimOpt< Real > > > obj_
void hessVec_22(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void update(const Vector< Real > &u, const Vector< Real > &z, UpdateType type, int iter=-1)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:133
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
void gradient_1(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
LinearCombinationObjective_SimOpt(const std::vector< Real > &weights, const std::vector< ROL::Ptr< Objective_SimOpt< Real > > > &obj)
void hessVec_21(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
void gradient_2(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
virtual void setParameter(const std::vector< Real > &param)
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.