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