44#ifndef ROL_RANDVARFUNCTIONAL_HPP
45#define ROL_RANDVARFUNCTIONAL_HPP
83 bool storage_hessvec_;
84 Ptr<ScalarController<Real>> value_storage_;
85 Ptr<VectorController<Real>> gradient_storage_;
86 Ptr<ScalarController<Real>> gradvec_storage_;
87 Ptr<VectorController<Real>> hessvec_storage_;
92 Ptr<Vector<Real> > g_;
93 Ptr<Vector<Real> > hv_;
94 Ptr<Vector<Real> > dualVector_;
97 std::vector<Real> point_;
101 Real computeValue(Objective<Real> &obj,
const Vector<Real> &x,
104 bool isComputed =
false;
106 isComputed = value_storage_->get(val,point_);
108 if (!isComputed || !storage_) {
109 obj.setParameter(point_);
110 val = obj.value(x,tol);
112 value_storage_->set(val,point_);
119 void computeGradient(Vector<Real> &g, Objective<Real> &obj,
120 const Vector<Real> &x, Real &tol) {
121 bool isComputed =
false;
123 isComputed = gradient_storage_->get(g,point_);
125 if (!isComputed || !storage_) {
126 obj.setParameter(point_);
127 obj.gradient(g,x,tol);
129 gradient_storage_->set(g,point_);
135 Real computeGradVec(Vector<Real> &g, Objective<Real> &obj,
136 const Vector<Real> &v,
const Vector<Real> &x,
139 computeGradient(g,obj,x,tol);
140 bool isComputed =
false;
141 if (storage_hessvec_) {
142 isComputed = gradvec_storage_->get(gv,point_);
144 if (!isComputed || !storage_hessvec_) {
147 if (storage_hessvec_) {
148 gradvec_storage_->set(gv,point_);
155 void computeHessVec(Vector<Real> &hv, Objective<Real> &obj,
156 const Vector<Real> &v,
const Vector<Real> &x,
158 bool isComputed =
false;
159 if (storage_hessvec_) {
160 isComputed = hessvec_storage_->get(hv,point_);
162 if (!isComputed || !storage_hessvec_) {
163 obj.setParameter(point_);
164 obj.hessVec(hv,v,x,tol);
165 if (storage_hessvec_) {
166 hessvec_storage_->set(hv,point_);
175 value_storage_(nullPtr),
176 gradient_storage_(nullPtr),
177 gradvec_storage_(nullPtr),
178 hessvec_storage_(nullPtr),
179 val_(0), gv_(0), firstReset_(
true),
180 point_({}), weight_(0) {}
182 void useStorage(
bool storage) {
185 if (value_storage_ == nullPtr) {
186 value_storage_ = makePtr<ScalarController<Real>>();
188 if (gradient_storage_ == nullPtr) {
189 gradient_storage_ = makePtr<VectorController<Real>>();
194 void useHessVecStorage(
bool storage) {
195 storage_hessvec_ = storage;
198 if (gradvec_storage_ == nullPtr) {
199 gradvec_storage_ = makePtr<ScalarController<Real>>();
201 if (hessvec_storage_ == nullPtr) {
202 hessvec_storage_ = makePtr<VectorController<Real>>();
207 virtual void setStorage(
const Ptr<ScalarController<Real>> &value_storage,
208 const Ptr<VectorController<Real>> &gradient_storage) {
209 value_storage_ = value_storage;
210 gradient_storage_ = gradient_storage;
214 virtual void setHessVecStorage(
const Ptr<ScalarController<Real>> &gradvec_storage,
215 const Ptr<VectorController<Real>> &hessvec_storage) {
216 gradvec_storage_ = gradvec_storage;
217 hessvec_storage_ = hessvec_storage;
218 useHessVecStorage(
true);
227 value_storage_->objectiveUpdate();
229 gradient_storage_->objectiveUpdate();
230 if (storage_hessvec_) {
231 gradvec_storage_->objectiveUpdate();
232 hessvec_storage_->objectiveUpdate();
239 value_storage_->objectiveUpdate(type);
240 gradient_storage_->objectiveUpdate(type);
241 if (storage_hessvec_) {
242 gradvec_storage_->objectiveUpdate(type);
243 hessvec_storage_->objectiveUpdate(type);
255 g_ = x.dual().clone();
256 hv_ = x.dual().clone();
257 dualVector_ = x.dual().clone();
263 g_->zero(); hv_->zero(); dualVector_->zero();
264 if (storage_hessvec_) {
265 gradvec_storage_->reset();
266 hessvec_storage_->reset();
270 virtual void setSample(
const std::vector<Real> &point,
const Real weight) {
271 point_.assign(point.begin(),point.end());
282 if (xstat != nullPtr && !xstat->empty()) {
296 const Vector<Real> &x,
297 const std::vector<Real> &xstat,
299 Real val = computeValue(obj,x,tol);
300 val_ += weight_ * val;
313 const Vector<Real> &x,
314 const std::vector<Real> &xstat,
316 computeGradient(*dualVector_,obj,x,tol);
317 g_->axpy(weight_,*dualVector_);
336 const Vector<Real> &v,
337 const std::vector<Real> &vstat,
338 const Vector<Real> &x,
339 const std::vector<Real> &xstat,
341 computeHessVec(*dualVector_,obj,v,x,tol);
342 hv_->axpy(weight_,*dualVector_);
354 const std::vector<Real> &xstat,
355 SampleGenerator<Real> &sampler) {
357 sampler.sumAll(&val_,&val,1);
373 std::vector<Real> &gstat,
374 const Vector<Real> &x,
375 const std::vector<Real> &xstat,
376 SampleGenerator<Real> &sampler) {
377 sampler.sumAll(*g_,g);
392 std::vector<Real> &hvstat,
393 const Vector<Real> &v,
394 const std::vector<Real> &vstat,
395 const Vector<Real> &x,
396 const std::vector<Real> &xstat,
397 SampleGenerator<Real> &sampler) {
398 sampler.sumAll(*hv_,hv);
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0 zero)()
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 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 initialize(const Vector< Real > &x)
Initialize temporary variables.
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 Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
virtual Real computeStatistic(const Ptr< const std::vector< Real > > &xstat) const
Compute statistic.
virtual void setSample(const std::vector< Real > &point, const Real weight)
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 void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
virtual void resetStorage(bool flag=true)
Provides the interface to implement any functional that maps a random variable to a (extended) real n...