ROL
ROL_RandVarFunctional.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_RANDVARFUNCTIONAL_HPP
45 #define ROL_RANDVARFUNCTIONAL_HPP
46 
47 #include "ROL_Vector.hpp"
48 #include "ROL_Ptr.hpp"
49 #include "ROL_SampleGenerator.hpp"
50 #include "ROL_SampledScalar.hpp"
51 #include "ROL_SampledVector.hpp"
52 
77 namespace ROL {
78 
79 template<class Real>
81 private:
82  bool storage_;
84  Ptr<SampledScalar<Real>> value_storage_;
85  Ptr<SampledVector<Real>> gradient_storage_;
86  Ptr<SampledScalar<Real>> gradvec_storage_;
87  Ptr<SampledVector<Real>> hessvec_storage_;
88 
89 protected:
90  Real val_;
91  Real gv_;
92  Ptr<Vector<Real> > g_;
93  Ptr<Vector<Real> > hv_;
94  Ptr<Vector<Real> > dualVector_;
96 
97  std::vector<Real> point_;
98  Real weight_;
99 
100  // Evaluate objective function at current parameter
102  Real &tol) {
103  Real val(0);
104  bool isComputed = false;
105  if (storage_) {
106  isComputed = value_storage_->get(val,point_);
107  }
108  if (!isComputed || !storage_) {
109  obj.setParameter(point_);
110  val = obj.value(x,tol);
111  if (storage_) {
112  value_storage_->set(val,point_);
113  }
114  }
115  return val;
116  }
117 
118  // Evaluate gradient of objective function at current parameter
120  const Vector<Real> &x, Real &tol) {
121  bool isComputed = false;
122  if (storage_) {
123  isComputed = gradient_storage_->get(g,point_);
124  }
125  if (!isComputed || !storage_) {
126  obj.setParameter(point_);
127  obj.gradient(g,x,tol);
128  if ( storage_ ) {
129  gradient_storage_->set(g,point_);
130  }
131  }
132  }
133 
134  // Evaluate Gradient-times-a-vector at current parameter
136  const Vector<Real> &v, const Vector<Real> &x,
137  Real &tol) {
138  Real gv(0);
139  computeGradient(g,obj,x,tol);
140  bool isComputed = false;
141  if (storage_hessvec_) {
142  isComputed = gradvec_storage_->get(gv,point_);
143  }
144  if (!isComputed || !storage_hessvec_) {
145  gv = g.dot(v.dual());
146  if (storage_hessvec_) {
147  gradvec_storage_->set(gv,point_);
148  }
149  }
150  return gv;
151  }
152 
153  // Evaluate Hessian-times-a-vector at current parameter
155  const Vector<Real> &v, const Vector<Real> &x,
156  Real &tol) {
157  bool isComputed = false;
158  if (storage_hessvec_) {
159  isComputed = hessvec_storage_->get(hv,point_);
160  }
161  if (!isComputed || !storage_hessvec_) {
162  obj.setParameter(point_);
163  obj.hessVec(hv,v,x,tol);
164  if (storage_hessvec_) {
165  hessvec_storage_->set(hv,point_);
166  }
167  }
168  }
169 
170 public:
171  virtual ~RandVarFunctional() {}
172 
174  value_storage_(nullPtr),
175  gradient_storage_(nullPtr),
176  gradvec_storage_(nullPtr),
177  hessvec_storage_(nullPtr),
178  val_(0), gv_(0), firstReset_(true),
179  point_({}), weight_(0) {}
180 
181  void useStorage(bool storage) {
182  storage_ = storage;
183  if (storage) {
184  if (value_storage_ == nullPtr) {
185  value_storage_ = makePtr<SampledScalar<Real>>();
186  }
187  if (gradient_storage_ == nullPtr) {
188  gradient_storage_ = makePtr<SampledVector<Real>>();
189  }
190  }
191  }
192 
193  void useHessVecStorage(bool storage) {
194  storage_hessvec_ = storage;
195  if (storage) {
196  useStorage(storage);
197  if (gradvec_storage_ == nullPtr) {
198  gradvec_storage_ = makePtr<SampledScalar<Real>>();
199  }
200  if (hessvec_storage_ == nullPtr) {
201  hessvec_storage_ = makePtr<SampledVector<Real>>();
202  }
203  }
204  }
205 
206  virtual void setStorage(const Ptr<SampledScalar<Real>> &value_storage,
207  const Ptr<SampledVector<Real>> &gradient_storage) {
208  value_storage_ = value_storage;
209  gradient_storage_ = gradient_storage;
210  useStorage(true);
211  }
212 
213  virtual void setHessVecStorage(const Ptr<SampledScalar<Real>> &gradvec_storage,
214  const Ptr<SampledVector<Real>> &hessvec_storage) {
215  gradvec_storage_ = gradvec_storage;
216  hessvec_storage_ = hessvec_storage;
217  useHessVecStorage(true);
218  }
219 
224  virtual void resetStorage(bool flag = true) {
225  if (storage_) {
226  value_storage_->update();
227  if (flag) {
228  gradient_storage_->update();
229  if (storage_hessvec_) {
230  gradvec_storage_->update();
231  hessvec_storage_->update();
232  }
233  }
234  }
235  }
236 
241  virtual void initialize(const Vector<Real> &x) {
242  // Create memory for class members
243  if ( firstReset_ ) {
244  g_ = x.dual().clone();
245  hv_ = x.dual().clone();
246  dualVector_ = x.dual().clone();
247  firstReset_ = false;
248  }
249  // Zero member variables
250  const Real zero(0);
251  val_ = zero; gv_ = zero;
252  g_->zero(); hv_->zero(); dualVector_->zero();
253  if (storage_hessvec_) {
254  gradvec_storage_->update();
255  hessvec_storage_->update();
256  }
257  }
258 
259  virtual void setSample(const std::vector<Real> &point, const Real weight) {
260  point_.assign(point.begin(),point.end());
261  weight_ = weight;
262  }
263 
269  virtual Real computeStatistic(const Ptr<const std::vector<Real>> &xstat) const {
270  Real stat(0);
271  if (xstat != nullPtr) {
272  stat = (*xstat)[0];
273  }
274  return stat;
275  }
276 
284  virtual void updateValue(Objective<Real> &obj,
285  const Vector<Real> &x,
286  const std::vector<Real> &xstat,
287  Real &tol) {
288  Real val = computeValue(obj,x,tol);
289  val_ += weight_ * val;
290  }
291 
301  virtual void updateGradient(Objective<Real> &obj,
302  const Vector<Real> &x,
303  const std::vector<Real> &xstat,
304  Real &tol) {
305  computeGradient(*dualVector_,obj,x,tol);
306  g_->axpy(weight_,*dualVector_);
307  }
308 
324  virtual void updateHessVec(Objective<Real> &obj,
325  const Vector<Real> &v,
326  const std::vector<Real> &vstat,
327  const Vector<Real> &x,
328  const std::vector<Real> &xstat,
329  Real &tol) {
330  computeHessVec(*dualVector_,obj,v,x,tol);
331  hv_->axpy(weight_,*dualVector_);
332  }
333 
342  virtual Real getValue(const Vector<Real> &x,
343  const std::vector<Real> &xstat,
344  SampleGenerator<Real> &sampler) {
345  Real val(0);
346  sampler.sumAll(&val_,&val,1);
347  return val;
348  }
349 
361  virtual void getGradient(Vector<Real> &g,
362  std::vector<Real> &gstat,
363  const Vector<Real> &x,
364  const std::vector<Real> &xstat,
365  SampleGenerator<Real> &sampler) {
366  sampler.sumAll(*g_,g);
367  }
368 
380  virtual void getHessVec(Vector<Real> &hv,
381  std::vector<Real> &hvstat,
382  const Vector<Real> &v,
383  const std::vector<Real> &vstat,
384  const Vector<Real> &x,
385  const std::vector<Real> &xstat,
386  SampleGenerator<Real> &sampler) {
387  sampler.sumAll(*hv_,hv);
388  }
389 };
390 
391 }
392 
393 #endif
virtual void setHessVecStorage(const Ptr< SampledScalar< Real >> &gradvec_storage, const Ptr< SampledVector< Real >> &hessvec_storage)
Provides the interface to evaluate objective functions.
virtual void updateHessVec(Objective< Real > &obj, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for Hessian-time-a-vector computation.
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 computeHessVec(Vector< Real > &hv, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Ptr< Vector< Real > > g_
virtual void setSample(const std::vector< Real > &point, const Real weight)
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Ptr< Vector< Real > > hv_
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
virtual void getHessVec(Vector< Real > &hv, std::vector< Real > &hvstat, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Ptr< Vector< Real > > dualVector_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
virtual Real dot(const Vector &x) const =0
Compute where .
virtual void getGradient(Vector< Real > &g, std::vector< Real > &gstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
void sumAll(Real *input, Real *output, int dim) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
virtual void resetStorage(bool flag=true)
Reset internal storage.
Ptr< SampledVector< Real > > hessvec_storage_
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual void updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
virtual void setStorage(const Ptr< SampledScalar< Real >> &value_storage, const Ptr< SampledVector< Real >> &gradient_storage)
Ptr< SampledScalar< Real > > value_storage_
virtual void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
Ptr< SampledVector< Real > > gradient_storage_
virtual void setParameter(const std::vector< Real > &param)
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Ptr< SampledScalar< Real > > gradvec_storage_
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
virtual Real computeStatistic(const Ptr< const std::vector< Real >> &xstat) const
Compute statistic.
void useHessVecStorage(bool storage)