ROL
ROL_RiskNeutralObjective.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_RISKNEUTRALOBJECTIVE_HPP
45 #define ROL_RISKNEUTRALOBJECTIVE_HPP
46 
47 #include "ROL_Vector.hpp"
48 #include "ROL_Objective.hpp"
49 #include "ROL_SampleGenerator.hpp"
50 
51 namespace ROL {
52 
53 template<class Real>
54 class RiskNeutralObjective : public Objective<Real> {
55 private:
56  Ptr<Objective<Real>> ParametrizedObjective_;
57  Ptr<SampleGenerator<Real>> ValueSampler_;
58  Ptr<SampleGenerator<Real>> GradientSampler_;
59  Ptr<SampleGenerator<Real>> HessianSampler_;
60 
61  Real value_;
62  Ptr<Vector<Real>> gradient_;
63  Ptr<Vector<Real>> pointDual_;
64  Ptr<Vector<Real>> sumDual_;
65 
67  bool storage_;
68 
69  std::map<std::vector<Real>,Real> value_storage_;
70  std::map<std::vector<Real>,Ptr<Vector<Real>>> gradient_storage_;
71 
72  void initialize(const Vector<Real> &x) {
73  if ( firstUpdate_ ) {
74  gradient_ = (x.dual()).clone();
75  pointDual_ = (x.dual()).clone();
76  sumDual_ = (x.dual()).clone();
77  firstUpdate_ = false;
78  }
79  }
80 
81  void getValue(Real &val, const Vector<Real> &x,
82  const std::vector<Real> &param, Real &tol) {
83  if ( storage_ && value_storage_.count(param) ) {
84  val = value_storage_[param];
85  }
86  else {
87  ParametrizedObjective_->setParameter(param);
88  val = ParametrizedObjective_->value(x,tol);
89  if ( storage_ ) {
90  value_storage_.insert(std::pair<std::vector<Real>,Real>(param,val));
91  }
92  }
93  }
94 
96  const std::vector<Real> &param, Real &tol) {
97  if ( storage_ && gradient_storage_.count(param) ) {
98  g.set(*(gradient_storage_[param]));
99  }
100  else {
101  ParametrizedObjective_->setParameter(param);
102  ParametrizedObjective_->gradient(g,x,tol);
103  if ( storage_ ) {
104  Ptr<Vector<Real>> tmp = g.clone();
105  gradient_storage_.insert(std::pair<std::vector<Real>,Ptr<Vector<Real>>>(param,tmp));
106  gradient_storage_[param]->set(g);
107  }
108  }
109  }
110 
111  void getHessVec(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x,
112  const std::vector<Real> &param, Real &tol) {
113  ParametrizedObjective_->setParameter(param);
114  ParametrizedObjective_->hessVec(hv,v,x,tol);
115  }
116 
117 
118 public:
120  const Ptr<SampleGenerator<Real>> &vsampler,
121  const Ptr<SampleGenerator<Real>> &gsampler,
122  const Ptr<SampleGenerator<Real>> &hsampler,
123  const bool storage = true )
124  : ParametrizedObjective_(pObj),
125  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(hsampler),
126  firstUpdate_(true), storage_(storage) {
127  value_storage_.clear();
128  gradient_storage_.clear();
129  }
130 
132  const Ptr<SampleGenerator<Real>> &vsampler,
133  const Ptr<SampleGenerator<Real>> &gsampler,
134  const bool storage = true )
135  : ParametrizedObjective_(pObj),
136  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(gsampler),
137  firstUpdate_(true), storage_(storage) {
138  value_storage_.clear();
139  gradient_storage_.clear();
140  }
141 
143  const Ptr<SampleGenerator<Real>> &sampler,
144  const bool storage = true )
145  : ParametrizedObjective_(pObj),
146  ValueSampler_(sampler), GradientSampler_(sampler), HessianSampler_(sampler),
147  firstUpdate_(true), storage_(storage) {
148  value_storage_.clear();
149  gradient_storage_.clear();
150  }
151 
152  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
153  initialize(x);
154 // ParametrizedObjective_->update(x,(flag && iter>=0),iter);
155  ParametrizedObjective_->update(x,flag,iter);
156  ValueSampler_->update(x);
157  value_ = static_cast<Real>(0);
158  if ( storage_ ) {
159  value_storage_.clear();
160  }
161  if ( flag ) { //&& iter>=0 ) {
162  GradientSampler_->update(x);
163  HessianSampler_->update(x);
164  gradient_->zero();
165  if ( storage_ ) {
166  gradient_storage_.clear();
167  }
168  }
169  }
170 
171  Real value( const Vector<Real> &x, Real &tol ) {
172  initialize(x);
173  Real myval(0), ptval(0), val(0), one(1), two(2), error(two*tol + one);
174  std::vector<Real> ptvals;
175  while ( error > tol ) {
176  ValueSampler_->refine();
177  for ( int i = ValueSampler_->start(); i < ValueSampler_->numMySamples(); ++i ) {
178  getValue(ptval,x,ValueSampler_->getMyPoint(i),tol);
179  myval += ValueSampler_->getMyWeight(i)*ptval;
180  ptvals.push_back(ptval);
181  }
182  error = ValueSampler_->computeError(ptvals);
183  ptvals.clear();
184  }
185  ValueSampler_->sumAll(&myval,&val,1);
186  value_ += val;
187  ValueSampler_->setSamples();
188  tol = error;
189  return value_;
190  }
191 
192  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
193  initialize(x);
194  g.zero(); pointDual_->zero(); sumDual_->zero();
195  std::vector<Ptr<Vector<Real>>> ptgs;
196  Real one(1), two(2), error(two*tol + one);
197  while ( error > tol ) {
198  GradientSampler_->refine();
199  for ( int i = GradientSampler_->start(); i < GradientSampler_->numMySamples(); ++i ) {
200  getGradient(*pointDual_,x,GradientSampler_->getMyPoint(i),tol);
201  sumDual_->axpy(GradientSampler_->getMyWeight(i),*pointDual_);
202  ptgs.push_back(pointDual_->clone());
203  (ptgs.back())->set(*pointDual_);
204  }
205  error = GradientSampler_->computeError(ptgs,x);
206 //if (GradientSampler_->batchID()==0) {
207 // std::cout << "IN GRADIENT: ERROR = " << error << " TOL = " << tol << std::endl;
208 //}
209  ptgs.clear();
210  }
211  GradientSampler_->sumAll(*sumDual_,g);
212  gradient_->plus(g);
213  g.set(*(gradient_));
214  GradientSampler_->setSamples();
215  tol = error;
216  }
217 
218  void hessVec( Vector<Real> &hv, const Vector<Real> &v,
219  const Vector<Real> &x, Real &tol ) {
220  initialize(x);
221  hv.zero(); pointDual_->zero(); sumDual_->zero();
222  for ( int i = 0; i < HessianSampler_->numMySamples(); ++i ) {
223  getHessVec(*pointDual_,v,x,HessianSampler_->getMyPoint(i),tol);
224  sumDual_->axpy(HessianSampler_->getMyWeight(i),*pointDual_);
225  }
226  HessianSampler_->sumAll(*sumDual_,hv);
227  }
228 
229  void precond( Vector<Real> &Pv, const Vector<Real> &v,
230  const Vector<Real> &x, Real &tol ) {
231  Pv.set(v.dual());
232  }
233 };
234 
235 }
236 
237 #endif
Provides the interface to evaluate objective functions.
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
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) 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)
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 >> &sampler, const bool storage=true)
void initialize(const Vector< Real > &x)
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, bool flag=true, int iter=-1)
Update objective function.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Ptr< SampleGenerator< Real > > ValueSampler_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
RiskNeutralObjective(const Ptr< Objective< Real >> &pObj, const Ptr< SampleGenerator< Real >> &vsampler, const Ptr< SampleGenerator< Real >> &gsampler, const bool storage=true)
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
void getValue(Real &val, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
Ptr< Objective< Real > > ParametrizedObjective_
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Ptr< SampleGenerator< Real > > GradientSampler_
std::map< std::vector< Real >, Ptr< Vector< Real > > > gradient_storage_
std::map< std::vector< Real >, Real > value_storage_
void getGradient(Vector< Real > &g, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
Ptr< SampleGenerator< Real > > HessianSampler_