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