10#ifndef ROL_RANDVARFUNCTIONAL_HPP
11#define ROL_RANDVARFUNCTIONAL_HPP
50 bool storage_hessvec_;
51 Ptr<ScalarController<Real>> value_storage_;
52 Ptr<VectorController<Real>> gradient_storage_;
53 Ptr<ScalarController<Real>> gradvec_storage_;
54 Ptr<VectorController<Real>> hessvec_storage_;
59 Ptr<Vector<Real> > g_;
60 Ptr<Vector<Real> > hv_;
61 Ptr<Vector<Real> > dualVector_;
64 std::vector<Real> point_;
68 Real computeValue(Objective<Real> &obj,
const Vector<Real> &x,
71 bool isComputed =
false;
73 isComputed = value_storage_->get(val,point_);
75 if (!isComputed || !storage_) {
76 obj.setParameter(point_);
77 val = obj.value(x,tol);
79 value_storage_->set(val,point_);
86 void computeGradient(Vector<Real> &g, Objective<Real> &obj,
87 const Vector<Real> &x, Real &tol) {
88 bool isComputed =
false;
90 isComputed = gradient_storage_->get(g,point_);
92 if (!isComputed || !storage_) {
93 obj.setParameter(point_);
94 obj.gradient(g,x,tol);
96 gradient_storage_->set(g,point_);
102 Real computeGradVec(Vector<Real> &g, Objective<Real> &obj,
103 const Vector<Real> &v,
const Vector<Real> &x,
106 computeGradient(g,obj,x,tol);
107 bool isComputed =
false;
108 if (storage_hessvec_) {
109 isComputed = gradvec_storage_->get(gv,point_);
111 if (!isComputed || !storage_hessvec_) {
114 if (storage_hessvec_) {
115 gradvec_storage_->set(gv,point_);
122 void computeHessVec(Vector<Real> &hv, Objective<Real> &obj,
123 const Vector<Real> &v,
const Vector<Real> &x,
125 bool isComputed =
false;
126 if (storage_hessvec_) {
127 isComputed = hessvec_storage_->get(hv,point_);
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_);
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) {}
149 void useStorage(
bool storage) {
152 if (value_storage_ == nullPtr) {
153 value_storage_ = makePtr<ScalarController<Real>>();
155 if (gradient_storage_ == nullPtr) {
156 gradient_storage_ = makePtr<VectorController<Real>>();
161 void useHessVecStorage(
bool storage) {
162 storage_hessvec_ = storage;
165 if (gradvec_storage_ == nullPtr) {
166 gradvec_storage_ = makePtr<ScalarController<Real>>();
168 if (hessvec_storage_ == nullPtr) {
169 hessvec_storage_ = makePtr<VectorController<Real>>();
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;
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);
194 value_storage_->objectiveUpdate();
196 gradient_storage_->objectiveUpdate();
197 if (storage_hessvec_) {
198 gradvec_storage_->objectiveUpdate();
199 hessvec_storage_->objectiveUpdate();
206 value_storage_->objectiveUpdate(type);
207 gradient_storage_->objectiveUpdate(type);
208 if (storage_hessvec_) {
209 gradvec_storage_->objectiveUpdate(type);
210 hessvec_storage_->objectiveUpdate(type);
222 g_ = x.dual().clone();
223 hv_ = x.dual().clone();
224 dualVector_ = x.dual().clone();
230 g_->zero(); hv_->zero(); dualVector_->zero();
231 if (storage_hessvec_) {
232 gradvec_storage_->reset();
233 hessvec_storage_->reset();
237 virtual void setSample(
const std::vector<Real> &point,
const Real weight) {
238 point_.assign(point.begin(),point.end());
249 if (xstat != nullPtr && !xstat->empty()) {
263 const Vector<Real> &x,
264 const std::vector<Real> &xstat,
266 Real val = computeValue(obj,x,tol);
267 val_ += weight_ * val;
280 const Vector<Real> &x,
281 const std::vector<Real> &xstat,
283 computeGradient(*dualVector_,obj,x,tol);
284 g_->axpy(weight_,*dualVector_);
303 const Vector<Real> &v,
304 const std::vector<Real> &vstat,
305 const Vector<Real> &x,
306 const std::vector<Real> &xstat,
308 computeHessVec(*dualVector_,obj,v,x,tol);
309 hv_->axpy(weight_,*dualVector_);
321 const std::vector<Real> &xstat,
322 SampleGenerator<Real> &sampler) {
324 sampler.sumAll(&val_,&val,1);
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);
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);
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...