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"
49 #include "ROL_Objective.hpp"
50 #include "ROL_SampleGenerator.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<Objective<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 
126  RiskAverseObjective( const Teuchos::RCP<Objective<Real> > &pObj,
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 )
133  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(hsampler),
134  firstUpdate_(true), storage_(storage) {
135  value_storage_.clear();
136  gradient_storage_.clear();
137  }
138 
139  RiskAverseObjective( const Teuchos::RCP<Objective<Real> > &pObj,
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 )
145  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(gsampler),
146  firstUpdate_(true), storage_(storage) {
147  value_storage_.clear();
148  gradient_storage_.clear();
149  }
150 
151  RiskAverseObjective( const Teuchos::RCP<Objective<Real> > &pObj,
152  const Teuchos::RCP<RiskMeasure<Real> > &rm,
153  const Teuchos::RCP<SampleGenerator<Real> > &sampler,
154  const bool storage = true )
156  ValueSampler_(sampler), GradientSampler_(sampler), HessianSampler_(sampler),
157  firstUpdate_(true), storage_(storage) {
158  value_storage_.clear();
159  gradient_storage_.clear();
160  }
161 
162  RiskAverseObjective( const Teuchos::RCP<Objective<Real> > &pObj,
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  std::string name = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
171  if (name != "Convex Combination Risk Measure") {
172  RiskMeasure_ = RiskMeasureFactory<Real>(parlist);
173  }
174  else {
175  RiskMeasure_ = Teuchos::rcp(new ConvexCombinationRiskMeasure<Real>(parlist));
176  }
177  storage_ = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
178  value_storage_.clear();
179  gradient_storage_.clear();
180  }
181 
182  RiskAverseObjective( const Teuchos::RCP<Objective<Real> > &pObj,
183  Teuchos::ParameterList &parlist,
184  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
185  const Teuchos::RCP<SampleGenerator<Real> > &gsampler )
186  : ParametrizedObjective_(pObj),
187  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(gsampler),
188  firstUpdate_(true) {
189  std::string name = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
190  if (name != "Convex Combination Risk Measure") {
191  RiskMeasure_ = RiskMeasureFactory<Real>(parlist);
192  }
193  else {
195  }
196  storage_ = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
197  value_storage_.clear();
198  gradient_storage_.clear();
199  }
200 
201  RiskAverseObjective( const Teuchos::RCP<Objective<Real> > &pObj,
202  Teuchos::ParameterList &parlist,
203  const Teuchos::RCP<SampleGenerator<Real> > &sampler )
204  : ParametrizedObjective_(pObj),
205  ValueSampler_(sampler), GradientSampler_(sampler), HessianSampler_(sampler),
206  firstUpdate_(true) {
207  std::string name = parlist.sublist("SOL").sublist("Risk Measure").get("Name","CVaR");
208  if (name != "Convex Combination Risk Measure") {
209  RiskMeasure_ = RiskMeasureFactory<Real>(parlist);
210  }
211  else {
213  }
214  storage_ = parlist.sublist("SOL").get("Store Sampled Value and Gradient",true);
215  value_storage_.clear();
216  gradient_storage_.clear();
217  }
218 
219  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
220  if ( firstUpdate_ ) {
221  RiskMeasure_->reset(x_,x);
222  g_ = (x_->dual()).clone();
223  hv_ = (x_->dual()).clone();
224  firstUpdate_ = false;
225  }
226  ParametrizedObjective_->update(x,flag,iter);
227  ValueSampler_->update(x);
228  if ( storage_ ) {
229  value_storage_.clear();
230  }
231  if ( flag ) {
232  GradientSampler_->update(x);
233  HessianSampler_->update(x);
234  if ( storage_ ) {
235  gradient_storage_.clear();
236  }
237  }
238  }
239 
240  virtual Real value( const Vector<Real> &x, Real &tol ) {
241  Real val = 0.0;
242  RiskMeasure_->reset(x_,x);
243  for ( int i = 0; i < ValueSampler_->numMySamples(); i++ ) {
244  getValue(val,*x_,ValueSampler_->getMyPoint(i),tol);
245  RiskMeasure_->update(val,ValueSampler_->getMyWeight(i));
246  }
247  return RiskMeasure_->getValue(*ValueSampler_);
248  }
249 
250  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
251  Real val = 0.0;
252  g.zero();
253  RiskMeasure_->reset(x_,x);
254  for ( int i = 0; i < GradientSampler_->numMySamples(); i++ ) {
255  getValue(val,*x_,GradientSampler_->getMyPoint(i),tol);
256  getGradient(*g_,*x_,GradientSampler_->getMyPoint(i),tol);
257  RiskMeasure_->update(val,*g_,GradientSampler_->getMyWeight(i));
258  }
259  RiskMeasure_->getGradient(g,*GradientSampler_);
260  }
261 
262  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v,
263  const Vector<Real> &x, Real &tol ) {
264  Real val = 0.0, gv = 0.0;
265  hv.zero();
266  RiskMeasure_->reset(x_,x,v_,v);
267  for ( int i = 0; i < HessianSampler_->numMySamples(); i++ ) {
268  getValue(val,*x_,HessianSampler_->getMyPoint(i),tol);
269  getGradient(*g_,*x_,HessianSampler_->getMyPoint(i),tol);
270  getHessVec(*hv_,*v_,*x_,HessianSampler_->getMyPoint(i),tol);
271  gv = g_->dot(v_->dual());
272  RiskMeasure_->update(val,*g_,gv,*hv_,HessianSampler_->getMyWeight(i));
273  }
274  RiskMeasure_->getHessVec(hv,*HessianSampler_);
275  }
276 
277  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v,
278  const Vector<Real> &x, Real &tol ) {
279  Pv.set(v.dual());
280  }
281 };
282 
283 }
284 
285 #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.
RiskAverseObjective(const Teuchos::RCP< Objective< 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.
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.
RiskAverseObjective(const Teuchos::RCP< Objective< Real > > &pObj, Teuchos::ParameterList &parlist, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler)
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< Objective< Real > > &pObj, const Teuchos::RCP< RiskMeasure< Real > > &rm, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const bool storage=true)
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< Objective< Real > > &pObj, Teuchos::ParameterList &parlist, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< SampleGenerator< Real > > &hsampler)
Teuchos::RCP< Vector< Real > > x_
void getHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, const std::vector< Real > &param, Real &tol)
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)
RiskAverseObjective(const Teuchos::RCP< Objective< Real > > &pObj, Teuchos::ParameterList &parlist, const Teuchos::RCP< SampleGenerator< Real > > &sampler)
std::map< std::vector< Real >, Real > value_storage_
Teuchos::RCP< Objective< Real > > ParametrizedObjective_
RiskAverseObjective(const Teuchos::RCP< Objective< Real > > &pObj, const Teuchos::RCP< RiskMeasure< Real > > &rm, const Teuchos::RCP< SampleGenerator< Real > > &sampler, const bool storage=true)
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.
Provides the interface to implement risk measures.
Teuchos::RCP< SampleGenerator< Real > > HessianSampler_
Teuchos::RCP< SampleGenerator< Real > > ValueSampler_
Provides an interface for a convex combination of risk measures.
virtual Real value(const Vector< Real > &x, Real &tol)
Compute value.