ROL
ROL_RiskAverseObjective.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_RISKAVERSEOBJECTIVE_HPP
45 #define ROL_RISKAVERSEOBJECTIVE_HPP
46 
47 #include "Teuchos_RCP.hpp"
48 #include "ROL_Vector.hpp"
50 #include "ROL_SampleGenerator.hpp"
51 #include "ROL_RiskMeasure.hpp"
53 
54 namespace ROL {
55 
56 template<class Real>
57 class RiskAverseObjective : public Objective<Real> {
58 private:
59  // Objective function definition
60  Teuchos::RCP<ParametrizedObjective<Real> > ParametrizedObjective_; // Parametrized objective function
61  Teuchos::RCP<RiskMeasure<Real> > RiskMeasure_; // Risk measure
62 
63  // Sampler generators
64  Teuchos::RCP<SampleGenerator<Real> > ValueSampler_; // Sampler for objective value
65  Teuchos::RCP<SampleGenerator<Real> > GradientSampler_; // Sampler for objective gradient
66  Teuchos::RCP<SampleGenerator<Real> > HessianSampler_; // Sampler for objective Hessian-times-a-vector
67 
68  // Additional storage
70  bool storage_;
71  std::map<std::vector<Real>,Real> value_storage_;
72  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > gradient_storage_;
73  Teuchos::RCP<Vector<Real> > x_;
74  Teuchos::RCP<Vector<Real> > v_;
75  Teuchos::RCP<Vector<Real> > g_;
76  Teuchos::RCP<Vector<Real> > hv_;
77 
78  // Evaluate objective function at current parameter
79  void getValue(Real &val, const Vector<Real> &x,
80  const std::vector<Real> &param, Real &tol) {
81  if ( storage_ && value_storage_.count(param) ) {
82  val = value_storage_[param];
83  }
84  else {
85  ParametrizedObjective_->setParameter(param);
86  val = ParametrizedObjective_->value(x,tol);
87  if ( storage_ ) {
88  value_storage_.insert(std::pair<std::vector<Real>,Real>(param,val));
89  }
90  }
91 //std::cout << "BATCH ID: " << ValueSampler_->batchID() << " "
92 // << "POINT: (" << param[0] << ", " << param[1] << ", " << param[2] << ", " << param[3] << ") "
93 // << "VALUE: " << val << "\n";
94  }
95 
96  // Evaluate gradient of objective function at current parameter
98  const std::vector<Real> &param, Real &tol) {
99  if ( storage_ && gradient_storage_.count(param) ) {
100  g.set(*(gradient_storage_[param]));
101  }
102  else {
103  ParametrizedObjective_->setParameter(param);
104  ParametrizedObjective_->gradient(g,x,tol);
105  if ( storage_ ) {
106  Teuchos::RCP<Vector<Real> > tmp = g.clone();
107  gradient_storage_.insert(std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(param,tmp));
108  gradient_storage_[param]->set(g);
109  }
110  }
111 //std::cout << "BATCH ID: " << GradientSampler_->batchID() << " "
112 // << "POINT: (" << param[0] << ", " << param[1] << ", " << param[2] << ", " << param[3] << ") "
113 // << "GNORM: " << g.norm() << "\n";
114  }
115 
116  // Evaluate Hessian-times-a-vector at current parameter
117  void getHessVec(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x,
118  const std::vector<Real> &param, Real &tol) {
119  ParametrizedObjective_->setParameter(param);
120  ParametrizedObjective_->hessVec(hv,v,x,tol);
121  }
122 
123 public:
124  virtual ~RiskAverseObjective() {}
125 
127  const Teuchos::RCP<RiskMeasure<Real> > &rm,
128  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
129  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
130  const Teuchos::RCP<SampleGenerator<Real> > &hsampler,
131  const bool storage = true )
132  : ParametrizedObjective_(pObj), RiskMeasure_(rm),
133  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(hsampler),
134  firstUpdate_(true), storage_(storage) {
135  value_storage_.clear();
136  gradient_storage_.clear();
137  }
138 
140  const Teuchos::RCP<RiskMeasure<Real> > &rm,
141  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
142  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
143  const bool storage = true )
144  : ParametrizedObjective_(pObj), RiskMeasure_(rm),
145  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(gsampler),
146  firstUpdate_(true), storage_(storage) {
147  value_storage_.clear();
148  gradient_storage_.clear();
149  }
150 
152  const Teuchos::RCP<RiskMeasure<Real> > &rm,
153  const Teuchos::RCP<SampleGenerator<Real> > &sampler,
154  const bool storage = true )
155  : ParametrizedObjective_(pObj), RiskMeasure_(rm),
156  ValueSampler_(sampler), GradientSampler_(sampler), HessianSampler_(sampler),
157  firstUpdate_(true), storage_(storage) {
158  value_storage_.clear();
159  gradient_storage_.clear();
160  }
161 
163  Teuchos::ParameterList &parlist,
164  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
165  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
166  const Teuchos::RCP<SampleGenerator<Real> > &hsampler )
167  : ParametrizedObjective_(pObj),
168  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(hsampler),
169  firstUpdate_(true) {
170  RiskMeasure_ = RiskMeasureFactory<Real>(parlist);
171  storage_ = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
172  value_storage_.clear();
173  gradient_storage_.clear();
174  }
175 
177  Teuchos::ParameterList &parlist,
178  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
179  const Teuchos::RCP<SampleGenerator<Real> > &gsampler )
180  : ParametrizedObjective_(pObj),
181  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(gsampler),
182  firstUpdate_(true) {
183  RiskMeasure_ = RiskMeasureFactory<Real>(parlist);
184  storage_ = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
185  value_storage_.clear();
186  gradient_storage_.clear();
187  }
188 
190  Teuchos::ParameterList &parlist,
191  const Teuchos::RCP<SampleGenerator<Real> > &sampler )
192  : ParametrizedObjective_(pObj),
193  ValueSampler_(sampler), GradientSampler_(sampler), HessianSampler_(sampler),
194  firstUpdate_(true) {
195  RiskMeasure_ = RiskMeasureFactory<Real>(parlist);
196  storage_ = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
197  value_storage_.clear();
198  gradient_storage_.clear();
199  }
200 
201  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
202  if ( firstUpdate_ ) {
203  RiskMeasure_->reset(x_,x);
204  g_ = (x_->dual()).clone();
205  hv_ = (x_->dual()).clone();
206  firstUpdate_ = false;
207  }
208  ParametrizedObjective_->update(x,flag,iter);
209  ValueSampler_->update(x);
210  if ( storage_ ) {
211  value_storage_.clear();
212  }
213  if ( flag ) {
214  GradientSampler_->update(x);
215  HessianSampler_->update(x);
216  if ( storage_ ) {
217  gradient_storage_.clear();
218  }
219  }
220  }
221 
222  virtual Real value( const Vector<Real> &x, Real &tol ) {
223  Real val = 0.0;
224  RiskMeasure_->reset(x_,x);
225  for ( int i = 0; i < ValueSampler_->numMySamples(); i++ ) {
226  getValue(val,*x_,ValueSampler_->getMyPoint(i),tol);
227  RiskMeasure_->update(val,ValueSampler_->getMyWeight(i));
228  }
229  return RiskMeasure_->getValue(*ValueSampler_);
230  }
231 
232  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
233  Real val = 0.0;
234  g.zero();
235  RiskMeasure_->reset(x_,x);
236  for ( int i = 0; i < GradientSampler_->numMySamples(); i++ ) {
237  getValue(val,*x_,GradientSampler_->getMyPoint(i),tol);
238  getGradient(*g_,*x_,GradientSampler_->getMyPoint(i),tol);
239  RiskMeasure_->update(val,*g_,GradientSampler_->getMyWeight(i));
240  }
241  RiskMeasure_->getGradient(g,*GradientSampler_);
242  }
243 
244  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v,
245  const Vector<Real> &x, Real &tol ) {
246  Real val = 0.0, gv = 0.0;
247  hv.zero();
248  RiskMeasure_->reset(x_,x,v_,v);
249  for ( int i = 0; i < HessianSampler_->numMySamples(); i++ ) {
250  getValue(val,*x_,HessianSampler_->getMyPoint(i),tol);
251  getGradient(*g_,*x_,HessianSampler_->getMyPoint(i),tol);
252  getHessVec(*hv_,*v_,*x_,HessianSampler_->getMyPoint(i),tol);
253  gv = g_->dot(v_->dual());
254  RiskMeasure_->update(val,*g_,gv,*hv_,HessianSampler_->getMyWeight(i));
255  }
256  RiskMeasure_->getHessVec(hv,*HessianSampler_);
257  }
258 
259  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v,
260  const Vector<Real> &x, Real &tol ) {
261  Pv.set(v.dual());
262  }
263 };
264 
265 }
266 
267 #endif
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Teuchos::RCP< Vector< Real > > g_
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
RiskAverseObjective(const Teuchos::RCP< ParametrizedObjective< Real > > &pObj, const Teuchos::RCP< RiskMeasure< Real > > &rm, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< SampleGenerator< Real > > &hsampler, const bool storage=true)
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Teuchos::RCP< ParametrizedObjective< Real > > ParametrizedObjective_
void getValue(Real &val, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Teuchos::RCP< SampleGenerator< Real > > GradientSampler_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< Vector< Real > > hv_
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply preconditioner to vector.
RiskAverseObjective(const Teuchos::RCP< ParametrizedObjective< Real > > &pObj, const Teuchos::RCP< RiskMeasure< Real > > &rm, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const bool storage=true)
Teuchos::RCP< Vector< Real > > x_
RiskAverseObjective(const Teuchos::RCP< ParametrizedObjective< Real > > &pObj, Teuchos::ParameterList &parlist, const Teuchos::RCP< SampleGenerator< Real > > &sampler)
void getHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
RiskAverseObjective(const Teuchos::RCP< ParametrizedObjective< Real > > &pObj, Teuchos::ParameterList &parlist, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< SampleGenerator< Real > > &hsampler)
RiskAverseObjective(const Teuchos::RCP< ParametrizedObjective< Real > > &pObj, Teuchos::ParameterList &parlist, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler)
std::map< std::vector< Real >, Teuchos::RCP< Vector< Real > > > gradient_storage_
void getGradient(Vector< Real > &g, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
std::map< std::vector< Real >, Real > value_storage_
Teuchos::RCP< Vector< Real > > v_
Teuchos::RCP< RiskMeasure< Real > > RiskMeasure_
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:196
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
RiskAverseObjective(const Teuchos::RCP< ParametrizedObjective< Real > > &pObj, const Teuchos::RCP< RiskMeasure< Real > > &rm, const Teuchos::RCP< SampleGenerator< Real > > &sampler, const bool storage=true)
Teuchos::RCP< SampleGenerator< Real > > HessianSampler_
Teuchos::RCP< SampleGenerator< Real > > ValueSampler_
virtual Real value(const Vector< Real > &x, Real &tol)
Compute value.