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_RefCountPtr.hpp"
48 #include "ROL_Vector.hpp"
49 #include "ROL_Objective.hpp"
51 #include "ROL_SampleGenerator.hpp"
52 #include "ROL_RiskMeasure.hpp"
53 
54 namespace ROL {
55 
56 template<class Real>
57 class RiskAverseObjective : public Objective<Real> {
58 private:
59  // Problem Data
60  Teuchos::RCP<ParametrizedObjective<Real> > pObj_;
61  Teuchos::RCP<SampleGenerator<Real> > vsampler_;
62  Teuchos::RCP<SampleGenerator<Real> > gsampler_;
63  Teuchos::RCP<RiskMeasure<Real> > rm_;
64 
65  // Storage Information
66  bool storage_;
67  std::map<std::vector<Real>,Real> value_storage_;
68  std::map<std::vector<Real>,Teuchos::RCP<Vector<Real> > > gradient_storage_;
69 
70 public:
71  virtual ~RiskAverseObjective() {}
72 
74  SampleGenerator<Real> &vsampler, SampleGenerator<Real> &gsampler,
75  bool storage = true ) : storage_(storage) {
76  pObj_ = Teuchos::rcp(&pObj,false); // Parametrized Objective Function Object
77  vsampler_ = Teuchos::rcp(&vsampler,false); // Objective Function Value Sampler Object
78  gsampler_ = Teuchos::rcp(&gsampler,false); // Gradient Sampler Object
79  rm_ = Teuchos::rcp(&rm,false); // Risk Measure Object
80 
81  value_storage_.clear();
82  gradient_storage_.clear();
83  }
84 
85  // Delegating constructors require C++11; can't use yet:
86  //RiskAverseObjective( ParametrizedObjective<Real> &pObj, RiskMeasure<Real> &rm,
87  // SampleGenerator<Real> &sampler, bool storage = true )
88  // : RiskAverseObjective(pObj,rm,sampler,sampler,storage) {}
89 
91  SampleGenerator<Real> &sampler, bool storage = true )
92  : storage_(storage) {
93  pObj_ = Teuchos::rcp(&pObj,false); // Parametrized Objective Function Object
94  vsampler_ = Teuchos::rcp(&sampler,false); // Objective Function Value Sampler Object
95  gsampler_ = Teuchos::rcp(&sampler,false); // Gradient Sampler Object
96  rm_ = Teuchos::rcp(&rm,false); // Risk Measure Object
97 
98  value_storage_.clear();
99  gradient_storage_.clear();
100  }
101 
102  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
103  pObj_->update(x,flag,iter);
104  vsampler_->update(x);
105  if ( storage_ ) {
106  value_storage_.clear();
107  }
108  if ( flag ) {
109  gsampler_->update(x);
110  if ( storage_ ) {
111  gradient_storage_.clear();
112  }
113  }
114  }
115 
116  virtual Real value( const Vector<Real> &x, Real &tol ) {
117  Real val = 0.0;
118  Teuchos::RCP<Vector<Real> > x0;
119  rm_->reset(x0,x);
120  for ( int i = 0; i < vsampler_->numMySamples(); i++ ) {
121  pObj_->setParameter(vsampler_->getMyPoint(i));
122  if ( storage_ && value_storage_.count(vsampler_->getMyPoint(i)) ) {
123  val = value_storage_[vsampler_->getMyPoint(i)];
124  }
125  else {
126  val = pObj_->value(*x0,tol);
127  if ( storage_ ) {
128  value_storage_.insert(std::pair<std::vector<Real>,Real>(vsampler_->getMyPoint(i),val));
129  }
130  }
131  rm_->update(val,vsampler_->getMyWeight(i));
132  }
133  return rm_->getValue(*vsampler_);
134  }
135 
136  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
137  //gsampler_->refine(x,tol);
138  g.zero();
139  Teuchos::RCP<Vector<Real> > x0;
140  rm_->reset(x0,x);
141  Teuchos::RCP<Vector<Real> > g0 = x0->clone();
142  Real val = 0.0;
143  for ( int i = 0; i < gsampler_->numMySamples(); i++ ) {
144  pObj_->setParameter(gsampler_->getMyPoint(i));
145  if ( storage_ && value_storage_.count(gsampler_->getMyPoint(i)) ) {
146  val = value_storage_[gsampler_->getMyPoint(i)];
147  }
148  else {
149  val = pObj_->value(*x0,tol);
150  if ( storage_ ) {
151  value_storage_.insert(std::pair<std::vector<Real>,Real>(gsampler_->getMyPoint(i),val));
152  }
153  }
154  if ( storage_ && gradient_storage_.count(gsampler_->getMyPoint(i)) ) {
155  g0->set(*(gradient_storage_[gsampler_->getMyPoint(i)]));
156  }
157  else {
158  pObj_->gradient(*g0,*x0,tol);
159  if ( storage_ ) {
160  Teuchos::RCP<Vector<Real> > tmp = g0->clone();
161  gradient_storage_.insert(std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(gsampler_->getMyPoint(i),tmp));
162  gradient_storage_[gsampler_->getMyPoint(i)]->set(*g0);
163  }
164  }
165  rm_->update(val,*g0,gsampler_->getMyWeight(i));
166  }
167  rm_->getGradient(g,*gsampler_);
168  }
169 
170  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v,
171  const Vector<Real> &x, Real &tol ) {
172  hv.zero();
173  Teuchos::RCP<Vector<Real> > x0;
174  Teuchos::RCP<Vector<Real> > v0;
175  rm_->reset(x0,x,v0,v);
176  Real val = 0.0;
177  Real gv = 0.0;
178  Teuchos::RCP<Vector<Real> > g0 = x0->clone();
179  Teuchos::RCP<Vector<Real> > h0 = x0->clone();
180  for ( int i = 0; i < gsampler_->numMySamples(); i++ ) {
181  pObj_->setParameter(gsampler_->getMyPoint(i));
182  if ( storage_ && value_storage_.count(gsampler_->getMyPoint(i)) ) {
183  val = value_storage_[gsampler_->getMyPoint(i)];
184  }
185  else {
186  val = pObj_->value(*x0,tol);
187  if ( storage_ ) {
188  value_storage_.insert(std::pair<std::vector<Real>,Real>(gsampler_->getMyPoint(i),val));
189  }
190  }
191  if ( storage_ && gradient_storage_.count(gsampler_->getMyPoint(i)) ) {
192  g0->set(*(gradient_storage_[gsampler_->getMyPoint(i)]));
193  }
194  else {
195  pObj_->gradient(*g0,*x0,tol);
196  if ( storage_ ) {
197  Teuchos::RCP<Vector<Real> > tmp = g0->clone();
198  gradient_storage_.insert(std::pair<std::vector<Real>,Teuchos::RCP<Vector<Real> > >(gsampler_->getMyPoint(i),tmp));
199  gradient_storage_[gsampler_->getMyPoint(i)]->set(*g0);
200  }
201  }
202  gv = g0->dot(*v0);
203  pObj_->hessVec(*h0,*v0,*x0,tol);
204  rm_->update(val,*g0,gv,*h0,gsampler_->getMyWeight(i));
205  }
206  rm_->getHessVec(hv,*gsampler_);
207  }
208 
209  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
210  Pv.set(v.dual());
211  }
212 };
213 
214 }
215 
216 #endif
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
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:211
Teuchos::RCP< SampleGenerator< Real > > vsampler_
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Teuchos::RCP< ParametrizedObjective< Real > > pObj_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply preconditioner to vector.
RiskAverseObjective(ParametrizedObjective< Real > &pObj, RiskMeasure< Real > &rm, SampleGenerator< Real > &sampler, bool storage=true)
RiskAverseObjective(ParametrizedObjective< Real > &pObj, RiskMeasure< Real > &rm, SampleGenerator< Real > &vsampler, SampleGenerator< Real > &gsampler, bool storage=true)
std::map< std::vector< Real >, Teuchos::RCP< Vector< Real > > > gradient_storage_
std::map< std::vector< Real >, Real > value_storage_
Teuchos::RCP< SampleGenerator< Real > > gsampler_
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:194
Teuchos::RCP< RiskMeasure< Real > > rm_
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
virtual Real value(const Vector< Real > &x, Real &tol)
Compute value.