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 
49 namespace ROL {
50 
51 template<class Real>
52 class KLDivergence : public RiskMeasure<Real> {
53 private:
54  Real eps_;
55 
56  Real gval_;
57  Real gvval_;
58  Real hval_;
59  Teuchos::RCP<Vector<Real> > scaledGradient_;
60  Teuchos::RCP<Vector<Real> > scaledHessVec_;
61  Teuchos::RCP<Vector<Real> > dualVector1_;
62  Teuchos::RCP<Vector<Real> > dualVector2_;
63 
64  Real xstat_;
65  Real vstat_;
66 
68 
69 public:
70  KLDivergence(const Real eps = 1.e-2)
71  : RiskMeasure<Real>(), eps_(eps > 0 ? eps : 1.e-2), firstReset_(true) {}
72 
73  KLDivergence(Teuchos::ParameterList &parlist)
74  : RiskMeasure<Real>(), firstReset_(true) {
75  Teuchos::ParameterList &list
76  = parlist.sublist("SOL").sublist("Risk Measure").sublist("KL Divergence");
77  Real eps = list.get("Threshold",1.e-2);
78  eps_ = eps > 0 ? eps : 1.e-2;
79  }
80 
81  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
83  xstat_ = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic();
84  if ( firstReset_ ) {
85  scaledGradient_ = (x0->dual()).clone();
86  scaledHessVec_ = (x0->dual()).clone();
87  dualVector1_ = (x0->dual()).clone();
88  dualVector2_ = (x0->dual()).clone();
89  firstReset_ = false;
90  }
91  gval_ = 0.0; gvval_ = 0.0; hval_ = 0.0;
92  scaledGradient_->zero(); scaledHessVec_->zero();
93  dualVector1_->zero(); dualVector2_->zero();
94  }
95 
96  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
97  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
98  reset(x0,x);
99  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(v).getVector());
100  vstat_ = Teuchos::dyn_cast<const RiskVector<Real> >(v).getStatistic();
101  }
102 
103  void update(const Real val, const Real weight) {
104  RiskMeasure<Real>::val_ += weight * std::exp(val/xstat_);
105  }
106 
107  void update(const Real val, const Vector<Real> &g, const Real weight) {
108  Real ev = std::exp(val/xstat_);
109  RiskMeasure<Real>::val_ += weight * ev;
110  RiskMeasure<Real>::g_->axpy(weight*ev,g);
111  gval_ += weight*ev*val;
112  }
113 
114  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
115  const Real weight) {
116  Real ev = std::exp(val/xstat_);
117  RiskMeasure<Real>::val_ += weight * ev;
118  RiskMeasure<Real>::gv_ += weight * ev * gv;
119  RiskMeasure<Real>::g_->axpy(weight*ev,g);
120  RiskMeasure<Real>::hv_->axpy(weight*ev,hv);
121  scaledGradient_->axpy(weight*ev*gv,g);
122  scaledHessVec_->axpy(weight*ev*val,g);
123  gval_ += weight*ev*val;
124  gvval_ += weight*ev*val*gv;
125  hval_ += weight*ev*val*val;
126  }
127 
129  Real val = RiskMeasure<Real>::val_;
130  Real ev = 0.0;
131  sampler.sumAll(&val,&ev,1);
132  return xstat_*(eps_ + std::log(ev));
133  }
134 
136  std::vector<Real> local(2,0.0), global(2,0.0);
137  local[0] = RiskMeasure<Real>::val_;
138  local[1] = gval_;
139  sampler.sumAll(&local[0],&global[0],2);
140  Real ev = global[0];
141  Real egval = global[1];
142 
143  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector1_);
144  dualVector1_->scale(1.0/ev);
145 
146  Real gstat = eps_ + std::log(ev) - egval/(ev*xstat_);
147 
148  (Teuchos::dyn_cast<RiskVector<Real> >(g)).setVector(*dualVector1_);
149  (Teuchos::dyn_cast<RiskVector<Real> >(g)).setStatistic(gstat);
150  }
151 
153  std::vector<Real> local(5,0.0), global(5,0.0);
154  local[0] = RiskMeasure<Real>::val_;
155  local[1] = RiskMeasure<Real>::gv_;
156  local[2] = gval_;
157  local[3] = gvval_;
158  local[4] = hval_;
159  sampler.sumAll(&local[0],&global[0],5);
160  Real ev = global[0];
161  Real egv = global[1];
162  Real egval = global[2];
163  Real egvval = global[3];
164  Real ehval = global[4];
165 
166  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector1_);
167 
168  sampler.sumAll(*scaledGradient_,*dualVector2_);
169  dualVector1_->axpy(1.0/xstat_,*dualVector2_);
170  dualVector1_->scale(1.0/ev);
171 
172  dualVector2_->zero();
173  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector2_);
174  dualVector1_->axpy((vstat_*egval/xstat_ - egv)/(xstat_*ev*ev),*dualVector2_);
175 
176  dualVector2_->zero();
177  sampler.sumAll(*scaledHessVec_,*dualVector2_);
178  dualVector1_->axpy(-vstat_/(xstat_*xstat_*ev),*dualVector2_);
179 
180  (Teuchos::dyn_cast<RiskVector<Real> >(hv)).setVector(*dualVector1_);
181 
182  Real hstat = vstat_/(xstat_*xstat_*xstat_*ev) * (ehval - egval*egval/ev)
183  + (egv*egval/ev - egvval)/(ev*xstat_*xstat_);
184  (Teuchos::dyn_cast<RiskVector<Real> >(hv)).setStatistic(hstat);
185  }
186 };
187 
188 }
189 
190 #endif
Real getValue(SampleGenerator< Real > &sampler)
void update(const Real val, const Real weight)
Teuchos::RCP< Vector< Real > > dualVector1_
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
KLDivergence(Teuchos::ParameterList &parlist)
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
void sumAll(Real *input, Real *output, int dim) const
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
Teuchos::RCP< Vector< Real > > scaledGradient_
void update(const Real val, const Vector< Real > &g, const Real weight)
Teuchos::RCP< Vector< Real > > scaledHessVec_
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
KLDivergence(const Real eps=1.e-2)
Teuchos::RCP< Vector< Real > > dualVector2_
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)