ROL
ROL_HMCR.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_HMCR_HPP
45 #define ROL_HMCR_HPP
46 
47 #include "ROL_RiskMeasure.hpp"
48 #include "ROL_PlusFunction.hpp"
49 #include "ROL_CVaRVector.hpp"
50 
51 namespace ROL {
52 
53 template<class Real>
54 class HMCR : public RiskMeasure<Real> {
55 private:
56  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
57 
58  Real prob_;
59  Real coeff_;
60 
61  unsigned order_;
62 
63  Teuchos::RCP<Vector<Real> > dualVector_;
64  Teuchos::RCP<Vector<Real> > pHMdualVec0_;
65  Teuchos::RCP<Vector<Real> > HMdualVec0_;
66  Teuchos::RCP<Vector<Real> > pHMdualVec1_;
67  Teuchos::RCP<Vector<Real> > HMdualVec1_;
68  Teuchos::RCP<Vector<Real> > pHMdualVec2_;
69  Teuchos::RCP<Vector<Real> > HMdualVec2_;
70  Teuchos::RCP<Vector<Real> > pHMdualVec3_;
71  Teuchos::RCP<Vector<Real> > HMdualVec3_;
72  Real xvar_;
73  Real vvar_;
74 
75  Real pnorm_;
76  Real dpnorm_;
77  Real dpnorm1_;
78  Real pgv_;
79  Real pgv1_;
80 
82 
83 public:
84 
85  HMCR( Real prob, Real coeff, unsigned order, Teuchos::RCP<PlusFunction<Real> > &pf )
86  : RiskMeasure<Real>(), plusFunction_(pf), xvar_(0.0), vvar_(0.0),
87  pnorm_(0.0), dpnorm_(0.0), dpnorm1_(0.0), pgv_(0.0), pgv1_(0.0),
88  firstReset_(true) {
89  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
90  coeff_ = ((coeff >= 0.0) ? ((coeff <= 1.0) ? coeff : 1.0) : 1.0);
91  order_ = ((order < 2) ? 2 : order);
92  }
93 
94  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
95  x0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
96  Teuchos::dyn_cast<const Vector<Real> >(x)).getVector());
97  xvar_ = Teuchos::dyn_cast<const CVaRVector<Real> >(
98  Teuchos::dyn_cast<const Vector<Real> >(x)).getVaR();
99 
100  RiskMeasure<Real>::reset(x0,*x0);
101 
102  if ( firstReset_ ) {
103  dualVector_ = (x0->dual()).clone();
104  pHMdualVec0_ = (x0->dual()).clone();
105  HMdualVec0_ = (x0->dual()).clone();
106  pHMdualVec1_ = (x0->dual()).clone();
107  HMdualVec1_ = (x0->dual()).clone();
108  pHMdualVec2_ = (x0->dual()).clone();
109  HMdualVec2_ = (x0->dual()).clone();
110  pHMdualVec3_ = (x0->dual()).clone();
111  HMdualVec3_ = (x0->dual()).clone();
112  firstReset_ = false;
113  }
114 
115  dualVector_->zero();
116  pHMdualVec0_->zero(); HMdualVec0_->zero();
117 
118  pnorm_ = 0.0;
119  dpnorm_ = 0.0;
120  }
121 
122  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
123  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
124  this->reset(x0,x);
125  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
126  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
127  vvar_ = Teuchos::dyn_cast<const CVaRVector<Real> >(
128  Teuchos::dyn_cast<const Vector<Real> >(v)).getVaR();
129 
130  pHMdualVec1_->zero(); HMdualVec1_->zero();
131  pHMdualVec2_->zero(); HMdualVec2_->zero();
132  pHMdualVec3_->zero(); HMdualVec3_->zero();
133 
134  dpnorm1_ = 0.0;
135  pgv_ = 0.0;
136  pgv1_ = 0.0;
137  }
138 
139  void update(const Real val, const Real weight) {
140  // Expected value
141  RiskMeasure<Real>::update(val,weight);
142  // Higher moment
143  Real pf = plusFunction_->evaluate(val-xvar_,0);
144  pnorm_ += weight*std::pow(pf,(Real)order_);
145  }
146 
147  void update(const Real val, const Vector<Real> &g, const Real weight) {
148  // Expected value
149  RiskMeasure<Real>::update(val,g,weight);
150  // Higher moment
151  Real pf0 = plusFunction_->evaluate(val-xvar_,0);
152  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
153 
154  Real rorder0 = (Real)order_;
155  Real rorder1 = (Real)order_-1.0;
156 
157  Real pf0p0 = std::pow(pf0,rorder0);
158  Real pf0p1 = std::pow(pf0,rorder1);
159 
160  pnorm_ += weight*pf0p0;
161  dpnorm_ += weight*pf0p1*pf1;
162 
163  pHMdualVec0_->axpy(weight*pf0p1*pf1,g);
164  }
165 
166  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
167  const Real weight) {
168  // Expected value
169  RiskMeasure<Real>::update(val,g,gv,hv,weight);
170  // Higher moment
171  Real pf0 = plusFunction_->evaluate(val-xvar_,0);
172  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
173  Real pf2 = plusFunction_->evaluate(val-xvar_,2);
174 
175  Real rorder0 = (Real)order_;
176  Real rorder1 = (Real)order_-1.0;
177  Real rorder2 = (Real)order_-2.0;
178 
179  Real pf0p0 = std::pow(pf0,rorder0);
180  Real pf0p1 = std::pow(pf0,rorder1);
181  Real pf0p2 = std::pow(pf0,rorder2);
182 
183  Real coeff1 = pf0p1*pf1;
184  Real coeff2 = rorder1*pf0p2*pf1*pf1 + pf0p1*pf2;
185 
186  pnorm_ += weight*pf0p0;
187  dpnorm_ += weight*coeff1;
188  dpnorm1_ += weight*coeff2;
189  pgv_ += weight*coeff1*gv;
190  pgv1_ += weight*coeff2*gv;
191 
192  pHMdualVec0_->axpy(weight*coeff1,g);
193  pHMdualVec1_->axpy(weight*coeff2,g);
194  pHMdualVec2_->axpy(weight*coeff1,hv);
195  pHMdualVec3_->axpy(weight*coeff2*gv,g);
196  }
197 
199  std::vector<Real> val_in(2,0.0), val_out(2,0.0);
200  val_in[0] = RiskMeasure<Real>::val_;
201  val_in[1] = pnorm_;
202  sampler.sumAll(&val_in[0],&val_out[0],2);
203  return (1.0-coeff_)*val_out[0]
204  + coeff_*(xvar_ + std::pow(val_out[1],1.0/(Real)order_)/(1.0-prob_));
205  }
206 
208  CVaRVector<Real> &gs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(g));
209 
210  std::vector<Real> val_in(3,0.0), val_out(3,0.0);
211  val_in[0] = RiskMeasure<Real>::val_;
212  val_in[1] = pnorm_; val_in[2] = dpnorm_;
213 
214  sampler.sumAll(&val_in[0],&val_out[0],3);
215  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector_);
216  sampler.sumAll(*pHMdualVec0_,*HMdualVec0_);
217 
218  Real denom = (1.0-prob_)*std::pow(val_out[1],((Real)order_-1.0)/(Real)order_);
219 
220  Real var = coeff_*(1.0 - val_out[2]/denom);
221 
222  dualVector_->scale(1.0-coeff_);
223  dualVector_->axpy(coeff_/denom,*HMdualVec0_);
224 
225  gs.setVaR(var);
226  gs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
227  }
228 
230  CVaRVector<Real> &hs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
231 
232  std::vector<Real> val_in(6,0.0), val_out(6,0.0);
233  val_in[0] = RiskMeasure<Real>::val_;
234  val_in[1] = pnorm_;
235  val_in[2] = dpnorm_; val_in[3] = dpnorm1_;
236  val_in[4] = pgv_; val_in[5] = pgv1_;
237 
238  sampler.sumAll(&val_in[0],&val_out[0],6);
239  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector_);
240  sampler.sumAll(*pHMdualVec0_,*HMdualVec0_); // E[pf^{p-1} pf' g]
241  sampler.sumAll(*pHMdualVec1_,*HMdualVec1_); // E[{(p-1) pf^{p-2} pf' pf' + pf^{p-1} pf''} g]
242  sampler.sumAll(*pHMdualVec2_,*HMdualVec2_); // E[pf^{p-1} pf' hv]
243  sampler.sumAll(*pHMdualVec3_,*HMdualVec3_); // E[{(p-1) pf^{p-2} pf' pf' + pf^{p-1} pf''} g gv]
244 
245  Real rorder0 = (Real)order_;
246  Real rorder1 = (Real)order_-1.0;
247  Real rorder2 = (Real)(2*order_)-1.0;
248 
249  Real denom1 = (1.0-prob_)*std::pow(val_out[1],rorder1/rorder0);
250  Real denom2 = (1.0-prob_)*std::pow(val_out[1],rorder2/rorder0);
251 
252  Real var = coeff_*((val_out[3]/denom1 - rorder1*val_out[2]*val_out[2]/denom2)*vvar_
253  -(val_out[5]/denom1 - rorder1*val_out[4]*val_out[2]/denom2));
254 
255  dualVector_->scale(1.0-coeff_);
256  dualVector_->axpy(coeff_*(-vvar_/denom1),*HMdualVec1_);
257  dualVector_->axpy(coeff_*(vvar_*rorder1*val_out[2]/denom2),*HMdualVec0_);
258  dualVector_->axpy(coeff_/denom1,*HMdualVec3_);
259  dualVector_->axpy(coeff_/denom1,*HMdualVec2_);
260  dualVector_->axpy(coeff_*(-rorder1*val_out[4]/denom2),*HMdualVec0_);
261 
262  hs.setVaR(var);
263  hs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
264  }
265 };
266 
267 }
268 
269 #endif
Real pgv1_
Definition: ROL_HMCR.hpp:79
HMCR(Real prob, Real coeff, unsigned order, Teuchos::RCP< PlusFunction< Real > > &pf)
Definition: ROL_HMCR.hpp:85
bool firstReset_
Definition: ROL_HMCR.hpp:81
Real xvar_
Definition: ROL_HMCR.hpp:72
Teuchos::RCP< PlusFunction< Real > > plusFunction_
Definition: ROL_HMCR.hpp:56
Teuchos::RCP< Vector< Real > > dualVector_
Definition: ROL_HMCR.hpp:63
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Definition: ROL_HMCR.hpp:94
Real dpnorm_
Definition: ROL_HMCR.hpp:76
Real dpnorm1_
Definition: ROL_HMCR.hpp:77
Teuchos::RCP< Vector< Real > > HMdualVec1_
Definition: ROL_HMCR.hpp:67
Teuchos::RCP< Vector< Real > > pHMdualVec2_
Definition: ROL_HMCR.hpp:68
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Definition: ROL_HMCR.hpp:207
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real prob_
Definition: ROL_HMCR.hpp:58
void sumAll(Real *input, Real *output, int dim) const
Real coeff_
Definition: ROL_HMCR.hpp:59
const Real getVaR() const
Teuchos::RCP< Vector< Real > > HMdualVec2_
Definition: ROL_HMCR.hpp:69
Teuchos::RCP< const Vector< Real > > getVector() const
void setVaR(const Real var)
Teuchos::RCP< Vector< Real > > HMdualVec0_
Definition: ROL_HMCR.hpp:65
Teuchos::RCP< Vector< Real > > HMdualVec3_
Definition: ROL_HMCR.hpp:71
Teuchos::RCP< Vector< Real > > pHMdualVec1_
Definition: ROL_HMCR.hpp:66
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
Definition: ROL_HMCR.hpp:122
void update(const Real val, const Real weight)
Definition: ROL_HMCR.hpp:139
void update(const Real val, const Vector< Real > &g, const Real weight)
Definition: ROL_HMCR.hpp:147
Real pnorm_
Definition: ROL_HMCR.hpp:75
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Definition: ROL_HMCR.hpp:166
Teuchos::RCP< Vector< Real > > pHMdualVec0_
Definition: ROL_HMCR.hpp:64
unsigned order_
Definition: ROL_HMCR.hpp:61
virtual void update(const Real val, const Real weight)
Real getValue(SampleGenerator< Real > &sampler)
Definition: ROL_HMCR.hpp:198
void setVector(const Vector< Real > &vec)
Real vvar_
Definition: ROL_HMCR.hpp:73
Real pgv_
Definition: ROL_HMCR.hpp:78
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Definition: ROL_HMCR.hpp:229
Teuchos::RCP< Vector< Real > > pHMdualVec3_
Definition: ROL_HMCR.hpp:70