ROL
ROL_RandVarFunctional.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef ROL_RANDVARFUNCTIONAL_HPP
11#define ROL_RANDVARFUNCTIONAL_HPP
12
13#include "ROL_Vector.hpp"
14#include "ROL_Objective.hpp"
15#include "ROL_Ptr.hpp"
19
44namespace ROL {
45
46template<class Real>
48private:
51 Ptr<ScalarController<Real>> value_storage_;
52 Ptr<VectorController<Real>> gradient_storage_;
53 Ptr<ScalarController<Real>> gradvec_storage_;
54 Ptr<VectorController<Real>> hessvec_storage_;
55
56protected:
57 Real val_;
58 Real gv_;
59 Ptr<Vector<Real> > g_;
60 Ptr<Vector<Real> > hv_;
61 Ptr<Vector<Real> > dualVector_;
63
64 std::vector<Real> point_;
65 Real weight_;
66
67 // Evaluate objective function at current parameter
69 Real &tol) {
70 Real val(0);
71 bool isComputed = false;
72 if (storage_) {
73 isComputed = value_storage_->get(val,point_);
74 }
75 if (!isComputed || !storage_) {
77 val = obj.value(x,tol);
78 if (storage_) {
79 value_storage_->set(val,point_);
80 }
81 }
82 return val;
83 }
84
85 // Evaluate gradient of objective function at current parameter
87 const Vector<Real> &x, Real &tol) {
88 bool isComputed = false;
89 if (storage_) {
90 isComputed = gradient_storage_->get(g,point_);
91 }
92 if (!isComputed || !storage_) {
94 obj.gradient(g,x,tol);
95 if ( storage_ ) {
97 }
98 }
99 }
100
101 // Evaluate Gradient-times-a-vector at current parameter
103 const Vector<Real> &v, const Vector<Real> &x,
104 Real &tol) {
105 Real gv(0);
106 computeGradient(g,obj,x,tol);
107 bool isComputed = false;
108 if (storage_hessvec_) {
109 isComputed = gradvec_storage_->get(gv,point_);
110 }
111 if (!isComputed || !storage_hessvec_) {
112 //gv = g.dot(v.dual());
113 gv = g.apply(v);
114 if (storage_hessvec_) {
115 gradvec_storage_->set(gv,point_);
116 }
117 }
118 return gv;
119 }
120
121 // Evaluate Hessian-times-a-vector at current parameter
123 const Vector<Real> &v, const Vector<Real> &x,
124 Real &tol) {
125 bool isComputed = false;
126 if (storage_hessvec_) {
127 isComputed = hessvec_storage_->get(hv,point_);
128 }
129 if (!isComputed || !storage_hessvec_) {
130 obj.setParameter(point_);
131 obj.hessVec(hv,v,x,tol);
132 if (storage_hessvec_) {
133 hessvec_storage_->set(hv,point_);
134 }
135 }
136 }
137
138public:
140
142 value_storage_(nullPtr),
143 gradient_storage_(nullPtr),
144 gradvec_storage_(nullPtr),
145 hessvec_storage_(nullPtr),
146 val_(0), gv_(0), firstReset_(true),
147 point_({}), weight_(0) {}
148
149 void useStorage(bool storage) {
150 storage_ = storage;
151 if (storage) {
152 if (value_storage_ == nullPtr) {
153 value_storage_ = makePtr<ScalarController<Real>>();
154 }
155 if (gradient_storage_ == nullPtr) {
156 gradient_storage_ = makePtr<VectorController<Real>>();
157 }
158 }
159 }
160
161 void useHessVecStorage(bool storage) {
162 storage_hessvec_ = storage;
163 if (storage) {
164 useStorage(storage);
165 if (gradvec_storage_ == nullPtr) {
166 gradvec_storage_ = makePtr<ScalarController<Real>>();
167 }
168 if (hessvec_storage_ == nullPtr) {
169 hessvec_storage_ = makePtr<VectorController<Real>>();
170 }
171 }
172 }
173
174 virtual void setStorage(const Ptr<ScalarController<Real>> &value_storage,
175 const Ptr<VectorController<Real>> &gradient_storage) {
176 value_storage_ = value_storage;
177 gradient_storage_ = gradient_storage;
178 useStorage(true);
179 }
180
181 virtual void setHessVecStorage(const Ptr<ScalarController<Real>> &gradvec_storage,
182 const Ptr<VectorController<Real>> &hessvec_storage) {
183 gradvec_storage_ = gradvec_storage;
184 hessvec_storage_ = hessvec_storage;
185 useHessVecStorage(true);
186 }
187
192 virtual void resetStorage(bool flag = true) {
193 if (storage_) {
194 value_storage_->objectiveUpdate();
195 if (flag) {
196 gradient_storage_->objectiveUpdate();
197 if (storage_hessvec_) {
198 gradvec_storage_->objectiveUpdate();
199 hessvec_storage_->objectiveUpdate();
200 }
201 }
202 }
203 }
204 virtual void resetStorage(UpdateType type) {
205 if (storage_) {
206 value_storage_->objectiveUpdate(type);
207 gradient_storage_->objectiveUpdate(type);
208 if (storage_hessvec_) {
209 gradvec_storage_->objectiveUpdate(type);
210 hessvec_storage_->objectiveUpdate(type);
211 }
212 }
213 }
214
219 virtual void initialize(const Vector<Real> &x) {
220 // Create memory for class members
221 if ( firstReset_ ) {
222 g_ = x.dual().clone();
223 hv_ = x.dual().clone();
224 dualVector_ = x.dual().clone();
225 firstReset_ = false;
226 }
227 // Zero member variables
228 const Real zero(0);
229 val_ = zero; gv_ = zero;
230 g_->zero(); hv_->zero(); dualVector_->zero();
231 if (storage_hessvec_) {
232 gradvec_storage_->reset();
233 hessvec_storage_->reset();
234 }
235 }
236
237 virtual void setSample(const std::vector<Real> &point, const Real weight) {
238 point_.assign(point.begin(),point.end());
239 weight_ = weight;
240 }
241
247 virtual Real computeStatistic(const Ptr<const std::vector<Real>> &xstat) const {
248 Real stat(0);
249 if (xstat != nullPtr && !xstat->empty()) {
250 stat = (*xstat)[0];
251 }
252 return stat;
253 }
254
262 virtual void updateValue(Objective<Real> &obj,
263 const Vector<Real> &x,
264 const std::vector<Real> &xstat,
265 Real &tol) {
266 Real val = computeValue(obj,x,tol);
267 val_ += weight_ * val;
268 }
269
280 const Vector<Real> &x,
281 const std::vector<Real> &xstat,
282 Real &tol) {
283 computeGradient(*dualVector_,obj,x,tol);
284 g_->axpy(weight_,*dualVector_);
285 }
286
303 const Vector<Real> &v,
304 const std::vector<Real> &vstat,
305 const Vector<Real> &x,
306 const std::vector<Real> &xstat,
307 Real &tol) {
308 computeHessVec(*dualVector_,obj,v,x,tol);
309 hv_->axpy(weight_,*dualVector_);
310 }
311
320 virtual Real getValue(const Vector<Real> &x,
321 const std::vector<Real> &xstat,
322 SampleGenerator<Real> &sampler) {
323 Real val(0);
324 sampler.sumAll(&val_,&val,1);
325 return val;
326 }
327
339 virtual void getGradient(Vector<Real> &g,
340 std::vector<Real> &gstat,
341 const Vector<Real> &x,
342 const std::vector<Real> &xstat,
343 SampleGenerator<Real> &sampler) {
344 sampler.sumAll(*g_,g);
345 }
346
358 virtual void getHessVec(Vector<Real> &hv,
359 std::vector<Real> &hvstat,
360 const Vector<Real> &v,
361 const std::vector<Real> &vstat,
362 const Vector<Real> &x,
363 const std::vector<Real> &xstat,
364 SampleGenerator<Real> &sampler) {
365 sampler.sumAll(*hv_,hv);
366 }
367};
368
369}
370
371#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Provides the interface to evaluate objective functions.
virtual void setParameter(const std::vector< Real > &param)
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
Ptr< ScalarController< Real > > gradvec_storage_
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
virtual void resetStorage(UpdateType type)
virtual void setSample(const std::vector< Real > &point, const Real weight)
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
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.
void computeHessVec(Vector< Real > &hv, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
virtual void setStorage(const Ptr< ScalarController< Real > > &value_storage, const Ptr< VectorController< Real > > &gradient_storage)
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Ptr< VectorController< Real > > gradient_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.
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.
Ptr< Vector< Real > > dualVector_
Ptr< VectorController< Real > > hessvec_storage_
void useHessVecStorage(bool storage)
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.
virtual void setHessVecStorage(const Ptr< ScalarController< Real > > &gradvec_storage, const Ptr< VectorController< Real > > &hessvec_storage)
virtual void resetStorage(bool flag=true)
Reset internal storage.
virtual Real computeStatistic(const Ptr< const std::vector< Real > > &xstat) const
Compute statistic.
virtual Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
virtual void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
Ptr< ScalarController< Real > > value_storage_
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
void sumAll(Real *input, Real *output, int dim) const
Defines the linear algebra or vector space interface.
virtual Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.