ROL
ROL_StochasticObjective.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_STOCHASTICOBJECTIVE_HPP
45 #define ROL_STOCHASTICOBJECTIVE_HPP
46 
48 #include "ROL_Objective.hpp"
49 #include "ROL_RiskVector.hpp"
52 
53 namespace ROL {
54 
55 template<class Real>
56 class StochasticObjective : public Objective<Real> {
57 private:
58  // Objective function definition
59  Ptr<Objective<Real>> obj_; // Uncertain objective function
60  Ptr<RandVarFunctional<Real>> rvf_; // Random variable functional
61 
62  // Sampler generators
63  Ptr<SampleGenerator<Real>> vsampler_; // Sampler for objective value
64  Ptr<SampleGenerator<Real>> gsampler_; // Sampler for objective gradient
65  Ptr<SampleGenerator<Real>> hsampler_; // Sampler for objective Hessian-times-a-vector
66 
67  const int comp_;
68  const int index_;
69 
70  Ptr<const Vector<Real>> getConstVector(const Vector<Real> &x) const {
71  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
72  return xrv.getVector();
73  }
74 
75  Ptr<Vector<Real>> getVector(Vector<Real> &x) const {
76  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
77  return xrv.getVector();
78  }
79 
80  Ptr<const std::vector<Real>> getConstStat(const Vector<Real> &x) const {
81  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
82  Ptr<const std::vector<Real>> xstat = xrv.getStatistic(comp_,index_);
83  if (xstat == nullPtr) {
84  xstat = makePtr<const std::vector<Real>>(0);
85  }
86  return xstat;
87  }
88 
89  Ptr<std::vector<Real>> getStat(Vector<Real> &x) const {
90  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
91  Ptr<std::vector<Real>> xstat = xrv.getStatistic(comp_,index_);
92  if (xstat == nullPtr) {
93  xstat = makePtr<std::vector<Real>>(0);
94  }
95  return xstat;
96  }
97 
98 public:
99  virtual ~StochasticObjective() {}
100 
102  const Ptr<RandVarFunctional<Real>> &rvf,
103  const Ptr<SampleGenerator<Real>> &vsampler,
104  const Ptr<SampleGenerator<Real>> &gsampler,
105  const Ptr<SampleGenerator<Real>> &hsampler,
106  const bool storage = true,
107  const int comp = 0, const int index = 0 )
108  : obj_(obj), rvf_(rvf),
109  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
110  comp_(comp), index_(index) {
111  rvf->useStorage(storage);
112  }
113 
115  const Ptr<RandVarFunctional<Real>> &rvf,
116  const Ptr<SampleGenerator<Real>> &vsampler,
117  const Ptr<SampleGenerator<Real>> &gsampler,
118  const bool storage = true,
119  const int comp = 0, const int index = 0 )
120  : StochasticObjective(obj,rvf,vsampler,gsampler,gsampler,storage,comp,index) {}
121 
123  const Ptr<RandVarFunctional<Real>> &rvf,
124  const Ptr<SampleGenerator<Real>> &sampler,
125  const bool storage = true,
126  const int comp = 0, const int index = 0 )
127  : StochasticObjective(obj,rvf,sampler,sampler,sampler,storage,comp,index) {}
128 
130  ROL::ParameterList &parlist,
131  const Ptr<SampleGenerator<Real>> &vsampler,
132  const Ptr<SampleGenerator<Real>> &gsampler,
133  const Ptr<SampleGenerator<Real>> &hsampler,
134  const int comp = 0, const int index = 0 )
135  : obj_(obj),
136  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
137  comp_(comp), index_(index) {
138  std::string type =parlist.sublist("SOL").get("Stochastic Component Type","Risk Averse");
139  std::string name;
140  if (type == "Risk Averse") {
141  name = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
142  }
143 
144  if (type == "Risk Averse" && name == "Convex Combination Risk Measure") {
145  rvf_ = makePtr<ConvexCombinationRiskMeasure<Real>>(parlist);
146  }
147  else {
148  rvf_ = RandVarFunctionalFactory<Real>(parlist);
149  }
150  bool storage = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
151  rvf_->useStorage(storage);
152  }
153 
155  ROL::ParameterList &parlist,
156  const Ptr<SampleGenerator<Real>> &vsampler,
157  const Ptr<SampleGenerator<Real>> &gsampler,
158  const int comp = 0, const int index = 0 )
159  : StochasticObjective(obj,parlist,vsampler,gsampler,gsampler,comp,index) {}
160 
162  ROL::ParameterList &parlist,
163  const Ptr<SampleGenerator<Real>> &sampler,
164  const int comp = 0, const int index = 0 )
165  : StochasticObjective(obj,parlist,sampler,sampler,sampler,comp,index) {}
166 
167  Real computeStatistic(const Vector<Real> &x) const {
168  Ptr<const std::vector<Real>> xstat = getConstStat(x);
169  return rvf_->computeStatistic(xstat);
170  }
171 
172  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
173  Ptr<const Vector<Real>> x0 = getConstVector(x);
174  // Update random variable functional
175  rvf_->resetStorage(flag);
176  // Update uncertain objective function
177  obj_->update(*x0,flag,iter);
178  // Update samplers
179  vsampler_->update(*x0);
180  if ( flag ) {
181  gsampler_->update(*x0);
182  hsampler_->update(*x0);
183  }
184  }
185 
186  Real value( const Vector<Real> &x, Real &tol ) {
187  Ptr<const Vector<Real>> x0 = getConstVector(x);
188  Ptr<const std::vector<Real>> xstat = getConstStat(x);
189  rvf_->initialize(*x0);
190  Real val(0);
191  for ( int i = 0; i < vsampler_->numMySamples(); i++ ) {
192  rvf_->setSample(vsampler_->getMyPoint(i),vsampler_->getMyWeight(i));
193  rvf_->updateValue(*obj_,*x0,*xstat,tol);
194  }
195  val = rvf_->getValue(*x0,*xstat,*vsampler_);
196  return val;
197  }
198 
199  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
200  g.zero();
201  Ptr<const Vector<Real>> x0 = getConstVector(x);
202  Ptr<const std::vector<Real>> xstat = getConstStat(x);
203  Ptr<Vector<Real>> g0 = getVector(g);
204  Ptr<std::vector<Real>> gstat = getStat(g);
205  rvf_->initialize(*x0);
206  for ( int i = 0; i < gsampler_->numMySamples(); i++ ) {
207  rvf_->setSample(gsampler_->getMyPoint(i),gsampler_->getMyWeight(i));
208  rvf_->updateGradient(*obj_,*x0,*xstat,tol);
209  }
210  rvf_->getGradient(*g0,*gstat,*x0,*xstat,*gsampler_);
211  }
212 
213  void hessVec( Vector<Real> &hv, const Vector<Real> &v,
214  const Vector<Real> &x, Real &tol ) {
215  hv.zero();
216  Ptr<const Vector<Real>> x0 = getConstVector(x);
217  Ptr<const std::vector<Real>> xstat = getConstStat(x);
218  Ptr<const Vector<Real>> v0 = getConstVector(v);
219  Ptr<const std::vector<Real>> vstat = getConstStat(v);
220  Ptr<Vector<Real>> hv0 = getVector(hv);
221  Ptr<std::vector<Real>> hvstat = getStat(hv);
222  rvf_->initialize(*x0);
223  for ( int i = 0; i < hsampler_->numMySamples(); i++ ) {
224  rvf_->setSample(hsampler_->getMyPoint(i),hsampler_->getMyWeight(i));
225  rvf_->updateHessVec(*obj_,*v0,*vstat,*x0,*xstat,tol);
226  }
227  rvf_->getHessVec(*hv0,*hvstat,*v0,*vstat,*x0,*xstat,*hsampler_);
228  }
229 
230  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v,
231  const Vector<Real> &x, Real &tol ) {
232  Pv.set(v.dual());
233  }
234 };
235 
236 }
237 
238 #endif
Provides the interface to evaluate objective functions.
ROL::Ptr< std::vector< Real > > getStatistic(const int comp=0, const int index=0)
StochasticObjective(const Ptr< Objective< Real >> &obj, ROL::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)
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:226
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
ROL::Ptr< const Vector< Real > > getVector(void) const
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:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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< 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.
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:209
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)
Ptr< SampleGenerator< Real > > gsampler_
Real value(const Vector< Real > &x, Real &tol)
Compute value.