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"
52 
53 namespace ROL {
54 
55 template<class Real>
56 class RiskAverseObjective : public Objective<Real> {
57 private:
58  // Objective function definition
59  Teuchos::RCP<ParametrizedObjective<Real> > ParametrizedObjective_; // Parametrized objective function
60  Teuchos::RCP<RiskMeasure<Real> > RiskMeasure_; // Risk measure
61 
62  // Sampler generators
63  Teuchos::RCP<SampleGenerator<Real> > ValueSampler_; // Sampler for objective value
64  Teuchos::RCP<SampleGenerator<Real> > GradientSampler_; // Sampler for objective gradient
65  Teuchos::RCP<SampleGenerator<Real> > HessianSampler_; // Sampler for objective Hessian-times-a-vector
66 
67  // Additional storage
69  bool storage_;
70  std::map<std::vector<Real>,Real> value_storage_;
71  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > gradient_storage_;
72  Teuchos::RCP<Vector<Real> > x_;
73  Teuchos::RCP<Vector<Real> > v_;
74  Teuchos::RCP<Vector<Real> > g_;
75  Teuchos::RCP<Vector<Real> > hv_;
76 
77  // Evaluate objective function at current parameter
78  void getValue(Real &val, const Vector<Real> &x,
79  const std::vector<Real> &param, Real &tol) {
80  if ( storage_ && value_storage_.count(param) ) {
81  val = value_storage_[param];
82  }
83  else {
84  ParametrizedObjective_->setParameter(param);
85  val = ParametrizedObjective_->value(x,tol);
86  if ( storage_ ) {
87  value_storage_.insert(std::pair<std::vector<Real>,Real>(param,val));
88  }
89  }
90 //std::cout << "BATCH ID: " << ValueSampler_->batchID() << " "
91 // << "POINT: (" << param[0] << ", " << param[1] << ", " << param[2] << ", " << param[3] << ") "
92 // << "VALUE: " << val << "\n";
93  }
94 
95  // Evaluate gradient of objective function at current parameter
97  const std::vector<Real> &param, Real &tol) {
98  if ( storage_ && gradient_storage_.count(param) ) {
99  g.set(*(gradient_storage_[param]));
100  }
101  else {
102  ParametrizedObjective_->setParameter(param);
103  ParametrizedObjective_->gradient(g,x,tol);
104  if ( storage_ ) {
105  Teuchos::RCP<Vector<Real> > tmp = g.clone();
106  gradient_storage_.insert(std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(param,tmp));
107  gradient_storage_[param]->set(g);
108  }
109  }
110 //std::cout << "BATCH ID: " << GradientSampler_->batchID() << " "
111 // << "POINT: (" << param[0] << ", " << param[1] << ", " << param[2] << ", " << param[3] << ") "
112 // << "GNORM: " << g.norm() << "\n";
113  }
114 
115  // Evaluate Hessian-times-a-vector at current parameter
116  void getHessVec(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x,
117  const std::vector<Real> &param, Real &tol) {
118  ParametrizedObjective_->setParameter(param);
119  ParametrizedObjective_->hessVec(hv,v,x,tol);
120  }
121 
122 public:
123  virtual ~RiskAverseObjective() {}
124 
126  Teuchos::RCP<RiskMeasure<Real> > &rm,
127  Teuchos::RCP<SampleGenerator<Real> > &vsampler,
128  Teuchos::RCP<SampleGenerator<Real> > &gsampler,
129  Teuchos::RCP<SampleGenerator<Real> > &hsampler,
130  bool storage = true )
131  : ParametrizedObjective_(pObj), RiskMeasure_(rm),
132  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(hsampler),
133  firstUpdate_(true), storage_(storage) {
134  value_storage_.clear();
135  gradient_storage_.clear();
136  }
137 
139  Teuchos::RCP<RiskMeasure<Real> > &rm,
140  Teuchos::RCP<SampleGenerator<Real> > &vsampler,
141  Teuchos::RCP<SampleGenerator<Real> > &gsampler,
142  bool storage = true )
143  : ParametrizedObjective_(pObj), RiskMeasure_(rm),
144  ValueSampler_(vsampler), GradientSampler_(gsampler), HessianSampler_(gsampler),
145  firstUpdate_(true), storage_(storage) {
146  value_storage_.clear();
147  gradient_storage_.clear();
148  }
149 
151  Teuchos::RCP<RiskMeasure<Real> > &rm,
152  Teuchos::RCP<SampleGenerator<Real> > &sampler,
153  bool storage = true )
154  : ParametrizedObjective_(pObj), RiskMeasure_(rm),
155  ValueSampler_(sampler), GradientSampler_(sampler), HessianSampler_(sampler),
156  firstUpdate_(true), storage_(storage) {
157  value_storage_.clear();
158  gradient_storage_.clear();
159  }
160 
161  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
162  if ( firstUpdate_ ) {
163  RiskMeasure_->reset(x_,x);
164  g_ = (x_->dual()).clone();
165  hv_ = (x_->dual()).clone();
166  firstUpdate_ = false;
167  }
168  ParametrizedObjective_->update(x,flag,iter);
169  ValueSampler_->update(x);
170  if ( storage_ ) {
171  value_storage_.clear();
172  }
173  if ( flag ) {
174  GradientSampler_->update(x);
175  HessianSampler_->update(x);
176  if ( storage_ ) {
177  gradient_storage_.clear();
178  }
179  }
180  }
181 
182  virtual Real value( const Vector<Real> &x, Real &tol ) {
183  Real val = 0.0;
184  RiskMeasure_->reset(x_,x);
185  for ( int i = 0; i < ValueSampler_->numMySamples(); i++ ) {
186  getValue(val,*x_,ValueSampler_->getMyPoint(i),tol);
187  RiskMeasure_->update(val,ValueSampler_->getMyWeight(i));
188  }
189  return RiskMeasure_->getValue(*ValueSampler_);
190  }
191 
192  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
193  Real val = 0.0;
194  g.zero();
195  RiskMeasure_->reset(x_,x);
196  for ( int i = 0; i < GradientSampler_->numMySamples(); i++ ) {
197  getValue(val,*x_,GradientSampler_->getMyPoint(i),tol);
198  getGradient(*g_,*x_,GradientSampler_->getMyPoint(i),tol);
199  RiskMeasure_->update(val,*g_,GradientSampler_->getMyWeight(i));
200  }
201  RiskMeasure_->getGradient(g,*GradientSampler_);
202  }
203 
204  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v,
205  const Vector<Real> &x, Real &tol ) {
206  Real val = 0.0, gv = 0.0;
207  hv.zero();
208  RiskMeasure_->reset(x_,x,v_,v);
209  for ( int i = 0; i < HessianSampler_->numMySamples(); i++ ) {
210  getValue(val,*x_,HessianSampler_->getMyPoint(i),tol);
211  getGradient(*g_,*x_,HessianSampler_->getMyPoint(i),tol);
212  getHessVec(*hv_,*v_,*x_,HessianSampler_->getMyPoint(i),tol);
213  gv = g_->dot(v_->dual());
214  RiskMeasure_->update(val,*g_,gv,*hv_,HessianSampler_->getMyWeight(i));
215  }
216  RiskMeasure_->getHessVec(hv,*HessianSampler_);
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
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
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
RiskAverseObjective(Teuchos::RCP< ParametrizedObjective< Real > > &pObj, Teuchos::RCP< RiskMeasure< Real > > &rm, Teuchos::RCP< SampleGenerator< Real > > &vsampler, Teuchos::RCP< SampleGenerator< Real > > &gsampler, Teuchos::RCP< SampleGenerator< Real > > &hsampler, bool storage=true)
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(Teuchos::RCP< ParametrizedObjective< Real > > &pObj, Teuchos::RCP< RiskMeasure< Real > > &rm, Teuchos::RCP< SampleGenerator< Real > > &sampler, bool storage=true)
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)
std::map< std::vector< Real >, Real > value_storage_
RiskAverseObjective(Teuchos::RCP< ParametrizedObjective< Real > > &pObj, Teuchos::RCP< RiskMeasure< Real > > &rm, Teuchos::RCP< SampleGenerator< Real > > &vsampler, Teuchos::RCP< SampleGenerator< Real > > &gsampler, 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.
Teuchos::RCP< SampleGenerator< Real > > HessianSampler_
Teuchos::RCP< SampleGenerator< Real > > ValueSampler_
virtual Real value(const Vector< Real > &x, Real &tol)
Compute value.