ROL
ROL_RiskNeutralObjective.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_RISKNEUTRALOBJECTIVE_HPP
11#define ROL_RISKNEUTRALOBJECTIVE_HPP
12
13#include "ROL_Vector.hpp"
14#include "ROL_Objective.hpp"
18
19namespace ROL {
20
21template<class Real>
22class RiskNeutralObjective : public Objective<Real> {
23private:
24 Ptr<Objective<Real>> ParametrizedObjective_;
25 Ptr<SampleGenerator<Real>> ValueSampler_;
26 Ptr<SampleGenerator<Real>> GradientSampler_;
27 Ptr<SampleGenerator<Real>> HessianSampler_;
28
29 Real value_;
30 Ptr<Vector<Real>> gradient_;
31 Ptr<Vector<Real>> pointDual_;
32 Ptr<Vector<Real>> sumDual_;
33
36
37 //std::map<std::vector<Real>,Real> value_storage_;
38 //std::map<std::vector<Real>,Ptr<Vector<Real>>> gradient_storage_;
39 Ptr<ScalarController<Real>> value_storage_;
40 Ptr<VectorController<Real>> gradient_storage_;
41
42 void initialize(const Vector<Real> &x) {
43 if ( firstUpdate_ ) {
44 gradient_ = (x.dual()).clone();
45 pointDual_ = (x.dual()).clone();
46 sumDual_ = (x.dual()).clone();
47 firstUpdate_ = false;
48 }
49 }
50
51 void getValue(Real &val, const Vector<Real> &x,
52 const std::vector<Real> &param, Real &tol) {
53 bool isComputed = false;
54 if ( storage_) {
55 isComputed = value_storage_->get(val,param);
56 }
57 if (!isComputed || !storage_) {
58 ParametrizedObjective_->setParameter(param);
59 val = ParametrizedObjective_->value(x,tol);
60 if ( storage_ ) {
61 value_storage_->set(val,param);
62 }
63 }
64 }
65
67 const std::vector<Real> &param, Real &tol) {
68 bool isComputed = false;
69 if ( storage_) {
70 isComputed = gradient_storage_->get(g,param);
71 }
72 if (!isComputed || !storage_) {
73 ParametrizedObjective_->setParameter(param);
74 ParametrizedObjective_->gradient(g,x,tol);
75 if ( storage_ ) {
76 gradient_storage_->set(g,param);
77 }
78 }
79 }
80
81 void getHessVec(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x,
82 const std::vector<Real> &param, Real &tol) {
83 ParametrizedObjective_->setParameter(param);
84 ParametrizedObjective_->hessVec(hv,v,x,tol);
85 }
86
87
88public:
90 const Ptr<SampleGenerator<Real>> &vsampler,
91 const Ptr<SampleGenerator<Real>> &gsampler,
92 const Ptr<SampleGenerator<Real>> &hsampler,
93 const bool storage = true )
95 ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(hsampler),
96 firstUpdate_(true), storage_(storage) {
97 value_storage_ = makePtr<ScalarController<Real>>();
98 gradient_storage_ = makePtr<VectorController<Real>>();
99 }
100
102 const Ptr<SampleGenerator<Real>> &vsampler,
103 const Ptr<SampleGenerator<Real>> &gsampler,
104 const bool storage = true )
106 ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(gsampler),
107 firstUpdate_(true), storage_(storage) {
108 value_storage_ = makePtr<ScalarController<Real>>();
109 gradient_storage_ = makePtr<VectorController<Real>>();
110 }
111
113 const Ptr<SampleGenerator<Real>> &sampler,
114 const bool storage = true )
116 ValueSampler_(sampler), GradientSampler_(sampler), HessianSampler_(sampler),
117 firstUpdate_(true), storage_(storage) {
118 value_storage_ = makePtr<ScalarController<Real>>();
119 gradient_storage_ = makePtr<VectorController<Real>>();
120 }
121
122 void update( const Vector<Real> &x, UpdateType type, int iter = -1 ) {
123 initialize(x);
124// ParametrizedObjective_->update(x,(flag && iter>=0),iter);
125 ParametrizedObjective_->update(x,type,iter);
126 ValueSampler_->update(x);
127 value_ = static_cast<Real>(0);
128 if ( storage_ ) {
129 value_storage_->objectiveUpdate(type);
130 gradient_storage_->objectiveUpdate(type);
131 }
132 if ( type != UpdateType::Trial && type != UpdateType::Revert ) { //&& iter>=0 ) {
133 GradientSampler_->update(x);
134 HessianSampler_->update(x);
135 gradient_->zero();
136 }
137 }
138
139 void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
140 initialize(x);
141// ParametrizedObjective_->update(x,(flag && iter>=0),iter);
142 ParametrizedObjective_->update(x,flag,iter);
143 ValueSampler_->update(x);
144 value_ = static_cast<Real>(0);
145 if ( storage_ ) {
146 value_storage_->objectiveUpdate(true);
147 }
148 //if ( flag ) { //&& iter>=0 ) {
149 GradientSampler_->update(x);
150 HessianSampler_->update(x);
151 gradient_->zero();
152 if ( storage_ ) {
153 gradient_storage_->objectiveUpdate(true);
154 }
155 //}
156 }
157
158 Real value( const Vector<Real> &x, Real &tol ) {
159 initialize(x);
160 Real myval(0), ptval(0), val(0), one(1), two(2), error(two*tol + one);
161 std::vector<Real> ptvals;
162 while ( error > tol ) {
163 ValueSampler_->refine();
164 for ( int i = ValueSampler_->start(); i < ValueSampler_->numMySamples(); ++i ) {
165 getValue(ptval,x,ValueSampler_->getMyPoint(i),tol);
166 myval += ValueSampler_->getMyWeight(i)*ptval;
167 ptvals.push_back(ptval);
168 }
169 error = ValueSampler_->computeError(ptvals);
170 ptvals.clear();
171 }
172 ValueSampler_->sumAll(&myval,&val,1);
173 value_ += val;
174 ValueSampler_->setSamples();
175 tol = error;
176 return value_;
177 }
178
179 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
180 initialize(x);
181 g.zero(); pointDual_->zero(); sumDual_->zero();
182 std::vector<Ptr<Vector<Real>>> ptgs;
183 Real one(1), two(2), error(two*tol + one);
184 while ( error > tol ) {
185 GradientSampler_->refine();
186 for ( int i = GradientSampler_->start(); i < GradientSampler_->numMySamples(); ++i ) {
187 getGradient(*pointDual_,x,GradientSampler_->getMyPoint(i),tol);
188 sumDual_->axpy(GradientSampler_->getMyWeight(i),*pointDual_);
189 ptgs.push_back(pointDual_->clone());
190 (ptgs.back())->set(*pointDual_);
191 }
192 error = GradientSampler_->computeError(ptgs,x);
193//if (GradientSampler_->batchID()==0) {
194// std::cout << "IN GRADIENT: ERROR = " << error << " TOL = " << tol << std::endl;
195//}
196 ptgs.clear();
197 }
198 GradientSampler_->sumAll(*sumDual_,g);
199 gradient_->plus(g);
200 g.set(*(gradient_));
201 GradientSampler_->setSamples();
202 tol = error;
203 }
204
205 void hessVec( Vector<Real> &hv, const Vector<Real> &v,
206 const Vector<Real> &x, Real &tol ) {
207 initialize(x);
208 hv.zero(); pointDual_->zero(); sumDual_->zero();
209 for ( int i = 0; i < HessianSampler_->numMySamples(); ++i ) {
210 getHessVec(*pointDual_,v,x,HessianSampler_->getMyPoint(i),tol);
211 sumDual_->axpy(HessianSampler_->getMyWeight(i),*pointDual_);
212 }
213 HessianSampler_->sumAll(*sumDual_,hv);
214 }
215
216 void precond( Vector<Real> &Pv, const Vector<Real> &v,
217 const Vector<Real> &x, Real &tol ) {
218 Pv.set(v.dual());
219 }
220};
221
222}
223
224#endif
Provides the interface to evaluate objective functions.
RiskNeutralObjective(const Ptr< Objective< Real > > &pObj, const Ptr< SampleGenerator< Real > > &sampler, const bool storage=true)
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Ptr< Objective< Real > > ParametrizedObjective_
Ptr< SampleGenerator< Real > > HessianSampler_
RiskNeutralObjective(const Ptr< Objective< Real > > &pObj, const Ptr< SampleGenerator< Real > > &vsampler, const Ptr< SampleGenerator< Real > > &gsampler, const bool storage=true)
void getHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
Ptr< SampleGenerator< Real > > GradientSampler_
void getValue(Real &val, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply preconditioner to vector.
RiskNeutralObjective(const Ptr< Objective< Real > > &pObj, const Ptr< SampleGenerator< Real > > &vsampler, const Ptr< SampleGenerator< Real > > &gsampler, const Ptr< SampleGenerator< Real > > &hsampler, const bool storage=true)
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Ptr< SampleGenerator< Real > > ValueSampler_
Ptr< VectorController< Real > > gradient_storage_
Ptr< ScalarController< Real > > value_storage_
void initialize(const Vector< Real > &x)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
void getGradient(Vector< Real > &g, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
Defines the linear algebra or vector space interface.
virtual void set(const Vector &x)
Set where .
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis,...
virtual void zero()
Set to zero vector.