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_SampledScalar.hpp"
50 #include "ROL_SampledVector.hpp"
51 
76 namespace ROL {
77 
78 template<class Real>
80 private:
81  bool storage_;
83  Ptr<SampledScalar<Real>> value_storage_;
84  Ptr<SampledVector<Real>> gradient_storage_;
85  Ptr<SampledScalar<Real>> gradvec_storage_;
86  Ptr<SampledVector<Real>> hessvec_storage_;
87 
88 protected:
89  Real val_;
90  Real gv_;
91  Ptr<Vector<Real> > g_;
92  Ptr<Vector<Real> > hv_;
93  Ptr<Vector<Real> > dualVector_;
95 
96  std::vector<Real> point_;
97  Real weight_;
98 
99  // Evaluate objective function at current parameter
101  Real &tol) {
102  Real val(0);
103  bool isComputed = false;
104  if (storage_) {
105  isComputed = value_storage_->get(val,point_);
106  }
107  if (!isComputed || !storage_) {
108  obj.setParameter(point_);
109  val = obj.value(x,tol);
110  if (storage_) {
111  value_storage_->set(val,point_);
112  }
113  }
114  return val;
115  }
116 
117  // Evaluate gradient of objective function at current parameter
119  const Vector<Real> &x, Real &tol) {
120  bool isComputed = false;
121  if (storage_) {
122  isComputed = gradient_storage_->get(g,point_);
123  }
124  if (!isComputed || !storage_) {
125  obj.setParameter(point_);
126  obj.gradient(g,x,tol);
127  if ( storage_ ) {
128  gradient_storage_->set(g,point_);
129  }
130  }
131  }
132 
133  // Evaluate Gradient-times-a-vector at current parameter
135  const Vector<Real> &v, const Vector<Real> &x,
136  Real &tol) {
137  Real gv(0);
138  computeGradient(g,obj,x,tol);
139  bool isComputed = false;
140  if (storage_hessvec_) {
141  isComputed = gradvec_storage_->get(gv,point_);
142  }
143  if (!isComputed || !storage_hessvec_) {
144  gv = g.dot(v.dual());
145  if (storage_hessvec_) {
146  gradvec_storage_->set(gv,point_);
147  }
148  }
149  return gv;
150  }
151 
152  // Evaluate Hessian-times-a-vector at current parameter
154  const Vector<Real> &v, const Vector<Real> &x,
155  Real &tol) {
156  bool isComputed = false;
157  if (storage_hessvec_) {
158  isComputed = hessvec_storage_->get(hv,point_);
159  }
160  if (!isComputed || !storage_hessvec_) {
161  obj.setParameter(point_);
162  obj.hessVec(hv,v,x,tol);
163  if (storage_hessvec_) {
164  hessvec_storage_->set(hv,point_);
165  }
166  }
167  }
168 
169 public:
170  virtual ~RandVarFunctional() {}
171 
173  value_storage_(nullPtr),
174  gradient_storage_(nullPtr),
175  gradvec_storage_(nullPtr),
176  hessvec_storage_(nullPtr),
177  val_(0), gv_(0), firstReset_(true),
178  point_({}), weight_(0) {}
179 
180  void useStorage(bool storage) {
181  storage_ = storage;
182  if (storage) {
183  if (value_storage_ == nullPtr) {
184  value_storage_ = makePtr<SampledScalar<Real>>();
185  }
186  if (gradient_storage_ == nullPtr) {
187  gradient_storage_ = makePtr<SampledVector<Real>>();
188  }
189  }
190  }
191 
192  void useHessVecStorage(bool storage) {
193  storage_hessvec_ = storage;
194  if (storage) {
195  useStorage(storage);
196  if (gradvec_storage_ == nullPtr) {
197  gradvec_storage_ = makePtr<SampledScalar<Real>>();
198  }
199  if (hessvec_storage_ == nullPtr) {
200  hessvec_storage_ = makePtr<SampledVector<Real>>();
201  }
202  }
203  }
204 
205  virtual void setStorage(const Ptr<SampledScalar<Real>> &value_storage,
206  const Ptr<SampledVector<Real>> &gradient_storage) {
207  value_storage_ = value_storage;
208  gradient_storage_ = gradient_storage;
209  useStorage(true);
210  }
211 
212  virtual void setHessVecStorage(const Ptr<SampledScalar<Real>> &gradvec_storage,
213  const Ptr<SampledVector<Real>> &hessvec_storage) {
214  gradvec_storage_ = gradvec_storage;
215  hessvec_storage_ = hessvec_storage;
216  useHessVecStorage(true);
217  }
218 
223  virtual void resetStorage(bool flag = true) {
224  if (storage_) {
225  value_storage_->update();
226  if (flag) {
227  gradient_storage_->update();
228  if (storage_hessvec_) {
229  gradvec_storage_->update();
230  hessvec_storage_->update();
231  }
232  }
233  }
234  }
235 
240  virtual void initialize(const Vector<Real> &x) {
241  // Create memory for class members
242  if ( firstReset_ ) {
243  g_ = x.dual().clone();
244  hv_ = x.dual().clone();
245  dualVector_ = x.dual().clone();
246  firstReset_ = false;
247  }
248  // Zero member variables
249  const Real zero(0);
250  val_ = zero; gv_ = zero;
251  g_->zero(); hv_->zero(); dualVector_->zero();
252  if (storage_hessvec_) {
253  gradvec_storage_->update();
254  hessvec_storage_->update();
255  }
256  }
257 
258  virtual void setSample(const std::vector<Real> &point, const Real weight) {
259  point_.assign(point.begin(),point.end());
260  weight_ = weight;
261  }
262 
268  virtual Real computeStatistic(const Ptr<const std::vector<Real>> &xstat) const {
269  Real stat(0);
270  if (xstat != nullPtr) {
271  stat = (*xstat)[0];
272  }
273  return stat;
274  }
275 
283  virtual void updateValue(Objective<Real> &obj,
284  const Vector<Real> &x,
285  const std::vector<Real> &xstat,
286  Real &tol) {
287  Real val = computeValue(obj,x,tol);
288  val_ += weight_ * val;
289  }
290 
300  virtual void updateGradient(Objective<Real> &obj,
301  const Vector<Real> &x,
302  const std::vector<Real> &xstat,
303  Real &tol) {
304  computeGradient(*dualVector_,obj,x,tol);
305  g_->axpy(weight_,*dualVector_);
306  }
307 
323  virtual void updateHessVec(Objective<Real> &obj,
324  const Vector<Real> &v,
325  const std::vector<Real> &vstat,
326  const Vector<Real> &x,
327  const std::vector<Real> &xstat,
328  Real &tol) {
329  computeHessVec(*dualVector_,obj,v,x,tol);
330  hv_->axpy(weight_,*dualVector_);
331  }
332 
341  virtual Real getValue(const Vector<Real> &x,
342  const std::vector<Real> &xstat,
343  SampleGenerator<Real> &sampler) {
344  Real val(0);
345  sampler.sumAll(&val_,&val,1);
346  return val;
347  }
348 
360  virtual void getGradient(Vector<Real> &g,
361  std::vector<Real> &gstat,
362  const Vector<Real> &x,
363  const std::vector<Real> &xstat,
364  SampleGenerator<Real> &sampler) {
365  sampler.sumAll(*g_,g);
366  }
367 
379  virtual void getHessVec(Vector<Real> &hv,
380  std::vector<Real> &hvstat,
381  const Vector<Real> &v,
382  const std::vector<Real> &vstat,
383  const Vector<Real> &x,
384  const std::vector<Real> &xstat,
385  SampleGenerator<Real> &sampler) {
386  sampler.sumAll(*hv_,hv);
387  }
388 };
389 
390 }
391 
392 #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)