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  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  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 name, type = parlist.sublist("SOL").get("Type","Risk Averse");
139  if (type == "Risk Averse")
140  name = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
141 
142  if (type == "Risk Averse" && name == "Convex Combination Risk Measure")
143  rvf_ = makePtr<ConvexCombinationRiskMeasure<Real>>(parlist);
144  else
145  rvf_ = RandVarFunctionalFactory<Real>(parlist);
146 
147  bool storage = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
148  rvf_->useStorage(storage);
149  }
150 
152  ROL::ParameterList &parlist,
153  const Ptr<SampleGenerator<Real>> &vsampler,
154  const Ptr<SampleGenerator<Real>> &gsampler,
155  const int comp = 0, const int index = 0 )
156  : StochasticObjective(obj,parlist,vsampler,gsampler,gsampler,comp,index) {}
157 
159  ROL::ParameterList &parlist,
160  const Ptr<SampleGenerator<Real>> &sampler,
161  const int comp = 0, const int index = 0 )
162  : StochasticObjective(obj,parlist,sampler,sampler,sampler,comp,index) {}
163 
164  Real computeStatistic(const Vector<Real> &x) const {
165  Ptr<const std::vector<Real>> xstat = getConstStat(x);
166  return rvf_->computeStatistic(xstat);
167  }
168 
169  void setIndex(int ind) {
170  index_ = ind;
171  }
172 
173  void update( const Vector<Real> &x, UpdateType type, int iter = -1 ) {
174  Ptr<const Vector<Real>> x0 = getConstVector(x);
175  // Update random variable functional
176  rvf_->resetStorage(type);
177  // Update uncertain objective function
178  obj_->update(*x0,type,iter);
179  // Update samplers
180  vsampler_->update(*x0);
181  if ( type != UpdateType::Trial && type != UpdateType::Revert ) {
182  gsampler_->update(*x0);
183  hsampler_->update(*x0);
184  }
185  }
186 
187  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
188  Ptr<const Vector<Real>> x0 = getConstVector(x);
189  // Update random variable functional
190  rvf_->resetStorage(flag);
191  // Update uncertain objective function
192  obj_->update(*x0,flag,iter);
193  // Update samplers
194  vsampler_->update(*x0);
195  if ( flag ) {
196  gsampler_->update(*x0);
197  hsampler_->update(*x0);
198  }
199  }
200 
201  Real value( const Vector<Real> &x, Real &tol ) {
202  Ptr<const Vector<Real>> x0 = getConstVector(x);
203  Ptr<const std::vector<Real>> xstat = getConstStat(x);
204  rvf_->initialize(*x0);
205  Real val(0);
206  for ( int i = 0; i < vsampler_->numMySamples(); i++ ) {
207  rvf_->setSample(vsampler_->getMyPoint(i),vsampler_->getMyWeight(i));
208  rvf_->updateValue(*obj_,*x0,*xstat,tol);
209  }
210  val = rvf_->getValue(*x0,*xstat,*vsampler_);
211  return val;
212  }
213 
214  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
215  g.zero();
216  Ptr<const Vector<Real>> x0 = getConstVector(x);
217  Ptr<const std::vector<Real>> xstat = getConstStat(x);
218  Ptr<Vector<Real>> g0 = getVector(g);
219  Ptr<std::vector<Real>> gstat = getStat(g);
220  rvf_->initialize(*x0);
221  for ( int i = 0; i < gsampler_->numMySamples(); i++ ) {
222  rvf_->setSample(gsampler_->getMyPoint(i),gsampler_->getMyWeight(i));
223  rvf_->updateGradient(*obj_,*x0,*xstat,tol);
224  }
225  rvf_->getGradient(*g0,*gstat,*x0,*xstat,*gsampler_);
226  }
227 
228  void hessVec( Vector<Real> &hv, const Vector<Real> &v,
229  const Vector<Real> &x, Real &tol ) {
230  hv.zero();
231  Ptr<const Vector<Real>> x0 = getConstVector(x);
232  Ptr<const std::vector<Real>> xstat = getConstStat(x);
233  Ptr<const Vector<Real>> v0 = getConstVector(v);
234  Ptr<const std::vector<Real>> vstat = getConstStat(v);
235  Ptr<Vector<Real>> hv0 = getVector(hv);
236  Ptr<std::vector<Real>> hvstat = getStat(hv);
237  rvf_->initialize(*x0);
238  for ( int i = 0; i < hsampler_->numMySamples(); i++ ) {
239  rvf_->setSample(hsampler_->getMyPoint(i),hsampler_->getMyWeight(i));
240  rvf_->updateHessVec(*obj_,*v0,*vstat,*x0,*xstat,tol);
241  }
242  rvf_->getHessVec(*hv0,*hvstat,*v0,*vstat,*x0,*xstat,*hsampler_);
243  }
244 
245  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v,
246  const Vector<Real> &x, Real &tol ) {
247  Pv.set(v.dual());
248  }
249 };
250 
251 }
252 
253 #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:226
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: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< 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: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)
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.