ROL
ROL_KLDivergence.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_KLDIVERGENCE_HPP
45 #define ROL_KLDIVERGENCE_HPP
46 
47 #include "ROL_RiskMeasure.hpp"
48 
78 namespace ROL {
79 
80 template<class Real>
81 class KLDivergence : public RiskMeasure<Real> {
82 private:
83  Real eps_;
84 
85  Real gval_;
86  Real gvval_;
87  Real hval_;
88  Teuchos::RCP<Vector<Real> > scaledGradient_;
89  Teuchos::RCP<Vector<Real> > scaledHessVec_;
90  Teuchos::RCP<Vector<Real> > dualVector1_;
91  Teuchos::RCP<Vector<Real> > dualVector2_;
92 
93  Real xstat_;
94  Real vstat_;
95 
97 
98  void checkInputs(void) const {
99  Real zero(0);
100  TEUCHOS_TEST_FOR_EXCEPTION((eps_ <= zero), std::invalid_argument,
101  ">>> ERROR (ROL::KLDivergence): Threshold must be positive!");
102  }
103 
104 public:
109  KLDivergence(const Real eps = 1.e-2)
110  : RiskMeasure<Real>(), eps_(eps), firstReset_(true) {
111  checkInputs();
112  }
113 
122  KLDivergence(Teuchos::ParameterList &parlist)
123  : RiskMeasure<Real>(), firstReset_(true) {
124  Teuchos::ParameterList &list
125  = parlist.sublist("SOL").sublist("Risk Measure").sublist("KL Divergence");
126  eps_ = list.get<Real>("Threshold");
127  checkInputs();
128  }
129 
130  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
131  Real zero(0);
133  xstat_ = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic(0);
134  if ( firstReset_ ) {
135  scaledGradient_ = (x0->dual()).clone();
136  scaledHessVec_ = (x0->dual()).clone();
137  dualVector1_ = (x0->dual()).clone();
138  dualVector2_ = (x0->dual()).clone();
139  firstReset_ = false;
140  }
141  gval_ = zero; gvval_ = zero; hval_ = zero;
142  scaledGradient_->zero(); scaledHessVec_->zero();
143  dualVector1_->zero(); dualVector2_->zero();
144  }
145 
146  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
147  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
148  reset(x0,x);
149  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(v).getVector());
150  vstat_ = Teuchos::dyn_cast<const RiskVector<Real> >(v).getStatistic(0);
151  }
152 
153  void update(const Real val, const Real weight) {
154  Real ev = exponential(val,xstat_*eps_);
155  RiskMeasure<Real>::val_ += weight * ev;
156  }
157 
159  if ( xstat_ == static_cast<Real>(0) ) {
160  return ROL_INF<Real>();
161  }
162  Real val = RiskMeasure<Real>::val_, ev(0);
163  sampler.sumAll(&val,&ev,1);
164  return (static_cast<Real>(1) + std::log(ev)/eps_)/xstat_;
165  }
166 
167  void update(const Real val, const Vector<Real> &g, const Real weight) {
168  Real ev = exponential(val,xstat_*eps_);
169  RiskMeasure<Real>::val_ += weight * ev;
170  gval_ += weight * ev * val;
171  RiskMeasure<Real>::g_->axpy(weight*ev,g);
172  }
173 
175  std::vector<Real> local(2), global(2);
176  local[0] = RiskMeasure<Real>::val_;
177  local[1] = gval_;
178  sampler.sumAll(&local[0],&global[0],2);
179  Real ev = global[0], egval = global[1];
180 
182  dualVector1_->scale(static_cast<Real>(1)/ev);
183  Teuchos::dyn_cast<RiskVector<Real> >(g).setVector(*dualVector1_);
184 
185  Real gstat(0);
186  if ( xstat_ == static_cast<Real>(0) ) {
187  gstat = ROL_INF<Real>();
188  }
189  else {
190  gstat = -((static_cast<Real>(1) + std::log(ev)/eps_)/xstat_
191  - egval/ev)/xstat_;
192  }
193  Teuchos::dyn_cast<RiskVector<Real> >(g).setStatistic(gstat);
194  }
195 
196  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
197  const Real weight) {
198  Real ev = exponential(val,xstat_*eps_);
199  RiskMeasure<Real>::val_ += weight * ev;
200  RiskMeasure<Real>::gv_ += weight * ev * gv;
201  gval_ += weight * ev * val;
202  gvval_ += weight * ev * val * gv;
203  hval_ += weight * ev * val * val;
204  RiskMeasure<Real>::g_->axpy(weight*ev,g);
205  RiskMeasure<Real>::hv_->axpy(weight*ev,hv);
206  scaledGradient_->axpy(weight*ev*gv,g);
207  scaledHessVec_->axpy(weight*ev*val,g);
208  }
209 
211  std::vector<Real> local(5), global(5);
212  local[0] = RiskMeasure<Real>::val_;
213  local[1] = RiskMeasure<Real>::gv_;
214  local[2] = gval_;
215  local[3] = gvval_;
216  local[4] = hval_;
217  sampler.sumAll(&local[0],&global[0],5);
218  Real ev = global[0], egv = global[1], egval = global[2];
219  Real egvval = global[3], ehval = global[4];
220  Real c0 = static_cast<Real>(1)/ev, c1 = c0*egval, c2 = c0*egv, c3 = eps_*c0;
221 
225  dualVector1_->scale(c0);
226 
227  dualVector2_->zero();
229  dualVector1_->axpy(-c3*(vstat_*c1 + xstat_*c2),*dualVector2_);
230 
231  dualVector2_->zero();
233  dualVector1_->axpy(vstat_*c3,*dualVector2_);
234 
235  Teuchos::dyn_cast<RiskVector<Real> >(hv).setVector(*dualVector1_);
236 
237  Real hstat(0);
238  if ( xstat_ == static_cast<Real>(0) ) {
239  hstat = ROL_INF<Real>();
240  }
241  else {
242  Real xstat2 = static_cast<Real>(2)/(xstat_*xstat_);
243  Real h11 = xstat2*((static_cast<Real>(1) + std::log(ev)/eps_)/xstat_ - c1)
244  + (c3*ehval - eps_*c1*c1)/xstat_;
245  hstat = vstat_ * h11 + (c3*egvval - eps_*c1*c2);
246  }
247 
248  Teuchos::dyn_cast<RiskVector<Real> >(hv).setStatistic(hstat);
249  }
250 
251 private:
252  Real exponential(const Real arg1, const Real arg2) const {
253  if ( arg1 < arg2 ) {
254  return power(exponential(arg1),arg2);
255  }
256  else {
257  return power(exponential(arg2),arg1);
258  }
259  }
260 
261  Real exponential(const Real arg) const {
262  if ( arg >= std::log(ROL_INF<Real>()) ) {
263  return ROL_INF<Real>();
264  }
265  else {
266  return std::exp(arg);
267  }
268  }
269 
270  Real power(const Real arg, const Real pow) const {
271  if ( arg >= std::pow(ROL_INF<Real>(),static_cast<Real>(1)/pow) ) {
272  return ROL_INF<Real>();
273  }
274  else {
275  return std::pow(arg,pow);
276  }
277  }
278 };
279 
280 }
281 
282 #endif
Real getValue(SampleGenerator< Real > &sampler)
Return risk measure value.
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
Provides an interface for the Kullback-Leibler distributionally robust expectation.
Teuchos::RCP< Vector< Real > > dualVector1_
void sumAll(Real *input, Real *output, int dim) const
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
Real exponential(const Real arg1, const Real arg2) const
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
KLDivergence(Teuchos::ParameterList &parlist)
Constructor.
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
Reset internal risk measure storage. Called for Hessian-times-a-vector computation.
Real power(const Real arg, const Real pow) const
Teuchos::RCP< Vector< Real > > scaledGradient_
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
Teuchos::RCP< Vector< Real > > scaledHessVec_
Real exponential(const Real arg) const
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
void checkInputs(void) const
KLDivergence(const Real eps=1.e-2)
Constructor.
Provides the interface to implement risk measures.
Teuchos::RCP< Vector< Real > > dualVector2_
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Update internal risk measure storage for Hessian-time-a-vector computation.