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