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_Objective.hpp"
49 #include "ROL_Ptr.hpp"
50 #include "ROL_SampleGenerator.hpp"
51 #include "ROL_ScalarController.hpp"
52 #include "ROL_VectorController.hpp"
53 
78 namespace ROL {
79 
80 template<class Real>
82 private:
83  bool storage_;
85  Ptr<ScalarController<Real>> value_storage_;
86  Ptr<VectorController<Real>> gradient_storage_;
87  Ptr<ScalarController<Real>> gradvec_storage_;
88  Ptr<VectorController<Real>> hessvec_storage_;
89 
90 protected:
91  Real val_;
92  Real gv_;
93  Ptr<Vector<Real> > g_;
94  Ptr<Vector<Real> > hv_;
95  Ptr<Vector<Real> > dualVector_;
97 
98  std::vector<Real> point_;
99  Real weight_;
100 
101  // Evaluate objective function at current parameter
103  Real &tol) {
104  Real val(0);
105  bool isComputed = false;
106  if (storage_) {
107  isComputed = value_storage_->get(val,point_);
108  }
109  if (!isComputed || !storage_) {
110  obj.setParameter(point_);
111  val = obj.value(x,tol);
112  if (storage_) {
113  value_storage_->set(val,point_);
114  }
115  }
116  return val;
117  }
118 
119  // Evaluate gradient of objective function at current parameter
121  const Vector<Real> &x, Real &tol) {
122  bool isComputed = false;
123  if (storage_) {
124  isComputed = gradient_storage_->get(g,point_);
125  }
126  if (!isComputed || !storage_) {
127  obj.setParameter(point_);
128  obj.gradient(g,x,tol);
129  if ( storage_ ) {
130  gradient_storage_->set(g,point_);
131  }
132  }
133  }
134 
135  // Evaluate Gradient-times-a-vector at current parameter
137  const Vector<Real> &v, const Vector<Real> &x,
138  Real &tol) {
139  Real gv(0);
140  computeGradient(g,obj,x,tol);
141  bool isComputed = false;
142  if (storage_hessvec_) {
143  isComputed = gradvec_storage_->get(gv,point_);
144  }
145  if (!isComputed || !storage_hessvec_) {
146  //gv = g.dot(v.dual());
147  gv = g.apply(v);
148  if (storage_hessvec_) {
149  gradvec_storage_->set(gv,point_);
150  }
151  }
152  return gv;
153  }
154 
155  // Evaluate Hessian-times-a-vector at current parameter
157  const Vector<Real> &v, const Vector<Real> &x,
158  Real &tol) {
159  bool isComputed = false;
160  if (storage_hessvec_) {
161  isComputed = hessvec_storage_->get(hv,point_);
162  }
163  if (!isComputed || !storage_hessvec_) {
164  obj.setParameter(point_);
165  obj.hessVec(hv,v,x,tol);
166  if (storage_hessvec_) {
167  hessvec_storage_->set(hv,point_);
168  }
169  }
170  }
171 
172 public:
173  virtual ~RandVarFunctional() {}
174 
176  value_storage_(nullPtr),
177  gradient_storage_(nullPtr),
178  gradvec_storage_(nullPtr),
179  hessvec_storage_(nullPtr),
180  val_(0), gv_(0), firstReset_(true),
181  point_({}), weight_(0) {}
182 
183  void useStorage(bool storage) {
184  storage_ = storage;
185  if (storage) {
186  if (value_storage_ == nullPtr) {
187  value_storage_ = makePtr<ScalarController<Real>>();
188  }
189  if (gradient_storage_ == nullPtr) {
190  gradient_storage_ = makePtr<VectorController<Real>>();
191  }
192  }
193  }
194 
195  void useHessVecStorage(bool storage) {
196  storage_hessvec_ = storage;
197  if (storage) {
198  useStorage(storage);
199  if (gradvec_storage_ == nullPtr) {
200  gradvec_storage_ = makePtr<ScalarController<Real>>();
201  }
202  if (hessvec_storage_ == nullPtr) {
203  hessvec_storage_ = makePtr<VectorController<Real>>();
204  }
205  }
206  }
207 
208  virtual void setStorage(const Ptr<ScalarController<Real>> &value_storage,
209  const Ptr<VectorController<Real>> &gradient_storage) {
210  value_storage_ = value_storage;
211  gradient_storage_ = gradient_storage;
212  useStorage(true);
213  }
214 
215  virtual void setHessVecStorage(const Ptr<ScalarController<Real>> &gradvec_storage,
216  const Ptr<VectorController<Real>> &hessvec_storage) {
217  gradvec_storage_ = gradvec_storage;
218  hessvec_storage_ = hessvec_storage;
219  useHessVecStorage(true);
220  }
221 
226  virtual void resetStorage(bool flag = true) {
227  if (storage_) {
228  value_storage_->objectiveUpdate();
229  if (flag) {
230  gradient_storage_->objectiveUpdate();
231  if (storage_hessvec_) {
232  gradvec_storage_->objectiveUpdate();
233  hessvec_storage_->objectiveUpdate();
234  }
235  }
236  }
237  }
238  virtual void resetStorage(UpdateType type) {
239  if (storage_) {
240  value_storage_->objectiveUpdate(type);
241  gradient_storage_->objectiveUpdate(type);
242  if (storage_hessvec_) {
243  gradvec_storage_->objectiveUpdate(type);
244  hessvec_storage_->objectiveUpdate(type);
245  }
246  }
247  }
248 
253  virtual void initialize(const Vector<Real> &x) {
254  // Create memory for class members
255  if ( firstReset_ ) {
256  g_ = x.dual().clone();
257  hv_ = x.dual().clone();
258  dualVector_ = x.dual().clone();
259  firstReset_ = false;
260  }
261  // Zero member variables
262  const Real zero(0);
263  val_ = zero; gv_ = zero;
264  g_->zero(); hv_->zero(); dualVector_->zero();
265  if (storage_hessvec_) {
266  gradvec_storage_->reset();
267  hessvec_storage_->reset();
268  }
269  }
270 
271  virtual void setSample(const std::vector<Real> &point, const Real weight) {
272  point_.assign(point.begin(),point.end());
273  weight_ = weight;
274  }
275 
281  virtual Real computeStatistic(const Ptr<const std::vector<Real>> &xstat) const {
282  Real stat(0);
283  if (xstat != nullPtr && !xstat->empty()) {
284  stat = (*xstat)[0];
285  }
286  return stat;
287  }
288 
296  virtual void updateValue(Objective<Real> &obj,
297  const Vector<Real> &x,
298  const std::vector<Real> &xstat,
299  Real &tol) {
300  Real val = computeValue(obj,x,tol);
301  val_ += weight_ * val;
302  }
303 
313  virtual void updateGradient(Objective<Real> &obj,
314  const Vector<Real> &x,
315  const std::vector<Real> &xstat,
316  Real &tol) {
317  computeGradient(*dualVector_,obj,x,tol);
318  g_->axpy(weight_,*dualVector_);
319  }
320 
336  virtual void updateHessVec(Objective<Real> &obj,
337  const Vector<Real> &v,
338  const std::vector<Real> &vstat,
339  const Vector<Real> &x,
340  const std::vector<Real> &xstat,
341  Real &tol) {
342  computeHessVec(*dualVector_,obj,v,x,tol);
343  hv_->axpy(weight_,*dualVector_);
344  }
345 
354  virtual Real getValue(const Vector<Real> &x,
355  const std::vector<Real> &xstat,
356  SampleGenerator<Real> &sampler) {
357  Real val(0);
358  sampler.sumAll(&val_,&val,1);
359  return val;
360  }
361 
373  virtual void getGradient(Vector<Real> &g,
374  std::vector<Real> &gstat,
375  const Vector<Real> &x,
376  const std::vector<Real> &xstat,
377  SampleGenerator<Real> &sampler) {
378  sampler.sumAll(*g_,g);
379  }
380 
392  virtual void getHessVec(Vector<Real> &hv,
393  std::vector<Real> &hvstat,
394  const Vector<Real> &v,
395  const std::vector<Real> &vstat,
396  const Vector<Real> &x,
397  const std::vector<Real> &xstat,
398  SampleGenerator<Real> &sampler) {
399  sampler.sumAll(*hv_,hv);
400  }
401 };
402 
403 }
404 
405 #endif
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)
virtual Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
Definition: ROL_Vector.hpp:238
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Ptr< VectorController< Real > > hessvec_storage_
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.
virtual void resetStorage(UpdateType type)
Ptr< Vector< Real > > dualVector_
virtual void setStorage(const Ptr< ScalarController< Real >> &value_storage, const Ptr< VectorController< Real >> &gradient_storage)
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 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.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Ptr< ScalarController< Real > > value_storage_
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.
Ptr< ScalarController< Real > > gradvec_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< VectorController< 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)
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 setHessVecStorage(const Ptr< ScalarController< Real >> &gradvec_storage, const Ptr< VectorController< Real >> &hessvec_storage)
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)