ROL
ROL_StochasticObjective.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_STOCHASTICOBJECTIVE_HPP
11 #define ROL_STOCHASTICOBJECTIVE_HPP
12 
14 #include "ROL_Objective.hpp"
15 #include "ROL_RiskVector.hpp"
18 
19 namespace ROL {
20 
21 template<class Real>
22 class StochasticObjective : public Objective<Real> {
23 private:
24  // Objective function definition
25  Ptr<Objective<Real>> obj_; // Uncertain objective function
26  Ptr<RandVarFunctional<Real>> rvf_; // Random variable functional
27 
28  // Sampler generators
29  Ptr<SampleGenerator<Real>> vsampler_; // Sampler for objective value
30  Ptr<SampleGenerator<Real>> gsampler_; // Sampler for objective gradient
31  Ptr<SampleGenerator<Real>> hsampler_; // Sampler for objective Hessian-times-a-vector
32 
33  const int comp_;
34  int index_;
35 
36  Ptr<const Vector<Real>> getConstVector(const Vector<Real> &x) const {
37  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
38  return xrv.getVector();
39  }
40 
41  Ptr<Vector<Real>> getVector(Vector<Real> &x) const {
42  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
43  return xrv.getVector();
44  }
45 
46  Ptr<const std::vector<Real>> getConstStat(const Vector<Real> &x) const {
47  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
48  Ptr<const std::vector<Real>> xstat = xrv.getStatistic(comp_,index_);
49  if (xstat == nullPtr) {
50  xstat = makePtr<const std::vector<Real>>(0);
51  }
52  return xstat;
53  }
54 
55  Ptr<std::vector<Real>> getStat(Vector<Real> &x) const {
56  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
57  Ptr<std::vector<Real>> xstat = xrv.getStatistic(comp_,index_);
58  if (xstat == nullPtr) {
59  xstat = makePtr<std::vector<Real>>(0);
60  }
61  return xstat;
62  }
63 
64 public:
65  virtual ~StochasticObjective() {}
66 
68  const Ptr<RandVarFunctional<Real>> &rvf,
69  const Ptr<SampleGenerator<Real>> &vsampler,
70  const Ptr<SampleGenerator<Real>> &gsampler,
71  const Ptr<SampleGenerator<Real>> &hsampler,
72  const bool storage = true,
73  const int comp = 0, const int index = 0 )
74  : obj_(obj), rvf_(rvf),
75  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
76  comp_(comp), index_(index) {
77  rvf->useStorage(storage);
78  }
79 
81  const Ptr<RandVarFunctional<Real>> &rvf,
82  const Ptr<SampleGenerator<Real>> &vsampler,
83  const Ptr<SampleGenerator<Real>> &gsampler,
84  const bool storage = true,
85  const int comp = 0, const int index = 0 )
86  : StochasticObjective(obj,rvf,vsampler,gsampler,gsampler,storage,comp,index) {}
87 
89  const Ptr<RandVarFunctional<Real>> &rvf,
90  const Ptr<SampleGenerator<Real>> &sampler,
91  const bool storage = true,
92  const int comp = 0, const int index = 0 )
93  : StochasticObjective(obj,rvf,sampler,sampler,sampler,storage,comp,index) {}
94 
96  ParameterList &parlist,
97  const Ptr<SampleGenerator<Real>> &vsampler,
98  const Ptr<SampleGenerator<Real>> &gsampler,
99  const Ptr<SampleGenerator<Real>> &hsampler,
100  const int comp = 0, const int index = 0 )
101  : obj_(obj),
102  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
103  comp_(comp), index_(index) {
104  std::string name, type = parlist.sublist("SOL").get("Type","Risk Averse");
105  if (type == "Risk Averse")
106  name = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
107 
108  if (type == "Risk Averse" && name == "Convex Combination Risk Measure")
109  rvf_ = makePtr<ConvexCombinationRiskMeasure<Real>>(parlist);
110  else
111  rvf_ = RandVarFunctionalFactory<Real>(parlist);
112 
113  bool storage = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
114  rvf_->useStorage(storage);
115  }
116 
118  ROL::ParameterList &parlist,
119  const Ptr<SampleGenerator<Real>> &vsampler,
120  const Ptr<SampleGenerator<Real>> &gsampler,
121  const int comp = 0, const int index = 0 )
122  : StochasticObjective(obj,parlist,vsampler,gsampler,gsampler,comp,index) {}
123 
125  ROL::ParameterList &parlist,
126  const Ptr<SampleGenerator<Real>> &sampler,
127  const int comp = 0, const int index = 0 )
128  : StochasticObjective(obj,parlist,sampler,sampler,sampler,comp,index) {}
129 
130  Real computeStatistic(const Vector<Real> &x) const {
131  Ptr<const std::vector<Real>> xstat = getConstStat(x);
132  return rvf_->computeStatistic(xstat);
133  }
134 
135  void setIndex(int ind) {
136  index_ = ind;
137  }
138 
139  void update( const Vector<Real> &x, UpdateType type, int iter = -1 ) {
140  Ptr<const Vector<Real>> x0 = getConstVector(x);
141  // Update random variable functional
142  rvf_->resetStorage(type);
143  // Update uncertain objective function
144  obj_->update(*x0,type,iter);
145  // Update samplers
146  vsampler_->update(*x0);
147  if ( type != UpdateType::Trial && type != UpdateType::Revert ) {
148  gsampler_->update(*x0);
149  hsampler_->update(*x0);
150  }
151  }
152 
153  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
154  Ptr<const Vector<Real>> x0 = getConstVector(x);
155  // Update random variable functional
156  rvf_->resetStorage(flag);
157  // Update uncertain objective function
158  obj_->update(*x0,flag,iter);
159  // Update samplers
160  vsampler_->update(*x0);
161  if ( flag ) {
162  gsampler_->update(*x0);
163  hsampler_->update(*x0);
164  }
165  }
166 
167  Real value( const Vector<Real> &x, Real &tol ) {
168  Ptr<const Vector<Real>> x0 = getConstVector(x);
169  Ptr<const std::vector<Real>> xstat = getConstStat(x);
170  rvf_->initialize(*x0);
171  Real val(0);
172  for ( int i = 0; i < vsampler_->numMySamples(); i++ ) {
173  rvf_->setSample(vsampler_->getMyPoint(i),vsampler_->getMyWeight(i));
174  rvf_->updateValue(*obj_,*x0,*xstat,tol);
175  }
176  val = rvf_->getValue(*x0,*xstat,*vsampler_);
177  return val;
178  }
179 
180  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
181  g.zero();
182  Ptr<const Vector<Real>> x0 = getConstVector(x);
183  Ptr<const std::vector<Real>> xstat = getConstStat(x);
184  Ptr<Vector<Real>> g0 = getVector(g);
185  Ptr<std::vector<Real>> gstat = getStat(g);
186  rvf_->initialize(*x0);
187  for ( int i = 0; i < gsampler_->numMySamples(); i++ ) {
188  rvf_->setSample(gsampler_->getMyPoint(i),gsampler_->getMyWeight(i));
189  rvf_->updateGradient(*obj_,*x0,*xstat,tol);
190  }
191  rvf_->getGradient(*g0,*gstat,*x0,*xstat,*gsampler_);
192  }
193 
194  void hessVec( Vector<Real> &hv, const Vector<Real> &v,
195  const Vector<Real> &x, Real &tol ) {
196  hv.zero();
197  Ptr<const Vector<Real>> x0 = getConstVector(x);
198  Ptr<const std::vector<Real>> xstat = getConstStat(x);
199  Ptr<const Vector<Real>> v0 = getConstVector(v);
200  Ptr<const std::vector<Real>> vstat = getConstStat(v);
201  Ptr<Vector<Real>> hv0 = getVector(hv);
202  Ptr<std::vector<Real>> hvstat = getStat(hv);
203  rvf_->initialize(*x0);
204  for ( int i = 0; i < hsampler_->numMySamples(); i++ ) {
205  rvf_->setSample(hsampler_->getMyPoint(i),hsampler_->getMyWeight(i));
206  rvf_->updateHessVec(*obj_,*v0,*vstat,*x0,*xstat,tol);
207  }
208  rvf_->getHessVec(*hv0,*hvstat,*v0,*vstat,*x0,*xstat,*hsampler_);
209  }
210 
211  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v,
212  const Vector<Real> &x, Real &tol ) {
213  Pv.set(v.dual());
214  }
215 };
216 
217 }
218 
219 #endif
Provides the interface to evaluate objective functions.
Real computeStatistic(const Vector< Real > &x) const
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
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
StochasticObjective(const Ptr< Objective< Real >> &obj, const Ptr< RandVarFunctional< Real >> &rvf, const Ptr< SampleGenerator< Real >> &vsampler, const Ptr< SampleGenerator< Real >> &gsampler, const bool storage=true, const int comp=0, const int index=0)
Ptr< Objective< Real > > obj_
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
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply preconditioner to vector.
Ptr< std::vector< Real > > getStat(Vector< Real > &x) const
Ptr< const Vector< Real > > getConstVector(const Vector< Real > &x) const
StochasticObjective(const Ptr< Objective< Real >> &obj, ROL::ParameterList &parlist, const Ptr< SampleGenerator< Real >> &sampler, const int comp=0, const int index=0)
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
StochasticObjective(const Ptr< Objective< Real >> &obj, const Ptr< RandVarFunctional< Real >> &rvf, const Ptr< SampleGenerator< Real >> &sampler, const bool storage=true, const int comp=0, const int index=0)
Ptr< std::vector< Real > > getStatistic(const int comp=0, const int index=0)
Ptr< SampleGenerator< Real > > hsampler_
Ptr< SampleGenerator< Real > > vsampler_
Ptr< RandVarFunctional< Real > > rvf_
StochasticObjective(const Ptr< Objective< Real >> &obj, const Ptr< RandVarFunctional< Real >> &rvf, const Ptr< SampleGenerator< Real >> &vsampler, const Ptr< SampleGenerator< Real >> &gsampler, const Ptr< SampleGenerator< Real >> &hsampler, const bool storage=true, const int comp=0, const int index=0)
Ptr< const std::vector< Real > > getConstStat(const Vector< Real > &x) const
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Ptr< const Vector< Real > > getVector(void) const
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
Ptr< Vector< Real > > getVector(Vector< Real > &x) const
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:175
StochasticObjective(const Ptr< Objective< Real >> &obj, ROL::ParameterList &parlist, const Ptr< SampleGenerator< Real >> &vsampler, const Ptr< SampleGenerator< Real >> &gsampler, const int comp=0, const int index=0)
void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
StochasticObjective(const Ptr< Objective< Real >> &obj, ParameterList &parlist, const Ptr< SampleGenerator< Real >> &vsampler, const Ptr< SampleGenerator< Real >> &gsampler, const Ptr< SampleGenerator< Real >> &hsampler, const int comp=0, const int index=0)
Ptr< SampleGenerator< Real > > gsampler_
Real value(const Vector< Real > &x, Real &tol)
Compute value.