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_RiskVector.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  HMCR( Teuchos::ParameterList &parlist )
95  : RiskMeasure<Real>(), xvar_(0.0), vvar_(0.0),
96  pnorm_(0.0), dpnorm_(0.0), dpnorm1_(0.0), pgv_(0.0), pgv1_(0.0),
97  firstReset_(true) {
98  Teuchos::ParameterList &list
99  = parlist.sublist("SOL").sublist("Risk Measure").sublist("HMCR");
100  // Check HMCR inputs
101  Real prob = list.get("Confidence Level",0.5);
102  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
103  Real coeff = list.get("Convex Combination Parameter",0.5);
104  coeff_ = ((coeff >= 0.0) ? ((coeff <= 1.0) ? coeff : 1.0) : 1.0);
105  unsigned order = list.get("Order",2);
106  order_ = ((order < 2) ? 2 : order);
107  // Build (approximate) plus function
108  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
109  }
110 
111  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
113  xvar_ = Teuchos::dyn_cast<const RiskVector<Real> >(
114  Teuchos::dyn_cast<const Vector<Real> >(x)).getStatistic();
115 
116  if ( firstReset_ ) {
117  dualVector_ = (x0->dual()).clone();
118  pHMdualVec0_ = (x0->dual()).clone();
119  HMdualVec0_ = (x0->dual()).clone();
120  pHMdualVec1_ = (x0->dual()).clone();
121  HMdualVec1_ = (x0->dual()).clone();
122  pHMdualVec2_ = (x0->dual()).clone();
123  HMdualVec2_ = (x0->dual()).clone();
124  pHMdualVec3_ = (x0->dual()).clone();
125  HMdualVec3_ = (x0->dual()).clone();
126  firstReset_ = false;
127  }
128 
129  dualVector_->zero();
130  pHMdualVec0_->zero(); HMdualVec0_->zero();
131 
132  pnorm_ = 0.0;
133  dpnorm_ = 0.0;
134  }
135 
136  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
137  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
138  reset(x0,x);
139  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(
140  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
141  vvar_ = Teuchos::dyn_cast<const RiskVector<Real> >(
142  Teuchos::dyn_cast<const Vector<Real> >(v)).getStatistic();
143 
144  pHMdualVec1_->zero(); HMdualVec1_->zero();
145  pHMdualVec2_->zero(); HMdualVec2_->zero();
146  pHMdualVec3_->zero(); HMdualVec3_->zero();
147 
148  dpnorm1_ = 0.0;
149  pgv_ = 0.0;
150  pgv1_ = 0.0;
151  }
152 
153  void update(const Real val, const Real weight) {
154  // Expected value
155  RiskMeasure<Real>::update(val,weight);
156  // Higher moment
157  Real pf = plusFunction_->evaluate(val-xvar_,0);
158  pnorm_ += weight*std::pow(pf,(Real)order_);
159  }
160 
161  void update(const Real val, const Vector<Real> &g, const Real weight) {
162  // Expected value
163  RiskMeasure<Real>::update(val,g,weight);
164  // Higher moment
165  Real pf0 = plusFunction_->evaluate(val-xvar_,0);
166  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
167 
168  Real rorder0 = (Real)order_;
169  Real rorder1 = (Real)order_-1.0;
170 
171  Real pf0p0 = std::pow(pf0,rorder0);
172  Real pf0p1 = std::pow(pf0,rorder1);
173 
174  pnorm_ += weight*pf0p0;
175  dpnorm_ += weight*pf0p1*pf1;
176 
177  pHMdualVec0_->axpy(weight*pf0p1*pf1,g);
178  }
179 
180  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
181  const Real weight) {
182  // Expected value
183  RiskMeasure<Real>::update(val,g,gv,hv,weight);
184  // Higher moment
185  Real pf0 = plusFunction_->evaluate(val-xvar_,0);
186  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
187  Real pf2 = plusFunction_->evaluate(val-xvar_,2);
188 
189  Real rorder0 = (Real)order_;
190  Real rorder1 = (Real)order_-1.0;
191  Real rorder2 = (Real)order_-2.0;
192 
193  Real pf0p0 = std::pow(pf0,rorder0);
194  Real pf0p1 = std::pow(pf0,rorder1);
195  Real pf0p2 = std::pow(pf0,rorder2);
196 
197  Real coeff1 = pf0p1*pf1;
198  Real coeff2 = rorder1*pf0p2*pf1*pf1 + pf0p1*pf2;
199 
200  pnorm_ += weight*pf0p0;
201  dpnorm_ += weight*coeff1;
202  dpnorm1_ += weight*coeff2;
203  pgv_ += weight*coeff1*gv;
204  pgv1_ += weight*coeff2*gv;
205 
206  pHMdualVec0_->axpy(weight*coeff1,g);
207  pHMdualVec1_->axpy(weight*coeff2,g);
208  pHMdualVec2_->axpy(weight*coeff1,hv);
209  pHMdualVec3_->axpy(weight*coeff2*gv,g);
210  }
211 
213  std::vector<Real> val_in(2,0.0), val_out(2,0.0);
214  val_in[0] = RiskMeasure<Real>::val_;
215  val_in[1] = pnorm_;
216  sampler.sumAll(&val_in[0],&val_out[0],2);
217  return (1.0-coeff_)*val_out[0]
218  + coeff_*(xvar_ + std::pow(val_out[1],1.0/(Real)order_)/(1.0-prob_));
219  }
220 
222  std::vector<Real> val_in(3,0.0), val_out(3,0.0);
223  val_in[0] = RiskMeasure<Real>::val_;
224  val_in[1] = pnorm_; val_in[2] = dpnorm_;
225 
226  sampler.sumAll(&val_in[0],&val_out[0],3);
227  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector_);
228  dualVector_->scale(1.0-coeff_);
229  Real var = coeff_;
230 
231  if ( val_in[1] > 0. ) {
232  sampler.sumAll(*pHMdualVec0_,*HMdualVec0_);
233 
234  Real denom = (1.0-prob_)*std::pow(val_out[1],((Real)order_-1.0)/(Real)order_);
235 
236  var -= coeff_*((denom > 0.) ? val_out[2]/denom : 0.);
237 
238  dualVector_->axpy(coeff_/denom,*HMdualVec0_);
239  }
240 
241  (Teuchos::dyn_cast<RiskVector<Real> >(g)).setStatistic(var);
242  (Teuchos::dyn_cast<RiskVector<Real> >(g)).setVector(*dualVector_);
243  }
244 
246  std::vector<Real> val_in(6,0.0), val_out(6,0.0);
247  val_in[0] = RiskMeasure<Real>::val_; val_in[1] = pnorm_;
248  val_in[2] = dpnorm_; val_in[3] = dpnorm1_;
249  val_in[4] = pgv_; val_in[5] = pgv1_;
250 
251  sampler.sumAll(&val_in[0],&val_out[0],6);
252  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector_);
253 
254  Real var = 0.;
255  dualVector_->scale(1.0-coeff_);
256 
257  if ( val_out[1] > 0. ) {
258  sampler.sumAll(*pHMdualVec0_,*HMdualVec0_); // E[pf^{p-1} pf' g]
259  sampler.sumAll(*pHMdualVec1_,*HMdualVec1_); // E[{(p-1) pf^{p-2} pf' pf' + pf^{p-1} pf''} g]
260  sampler.sumAll(*pHMdualVec2_,*HMdualVec2_); // E[pf^{p-1} pf' hv]
261  sampler.sumAll(*pHMdualVec3_,*HMdualVec3_); // E[{(p-1) pf^{p-2} pf' pf' + pf^{p-1} pf''} g gv]
262 
263  Real rorder0 = (Real)order_;
264  Real rorder1 = (Real)order_-1.0;
265  Real rorder2 = (Real)(2*order_)-1.0;
266 
267  Real denom1 = (1.0-prob_)*std::pow(val_out[1],rorder1/rorder0);
268  Real denom2 = (1.0-prob_)*std::pow(val_out[1],rorder2/rorder0);
269 
270  var = coeff_*((val_out[3]/denom1 - rorder1*val_out[2]*val_out[2]/denom2)*vvar_
271  -(val_out[5]/denom1 - rorder1*val_out[4]*val_out[2]/denom2));
272 
273  dualVector_->axpy(coeff_*(-vvar_/denom1),*HMdualVec1_);
274  dualVector_->axpy(coeff_*(vvar_*rorder1*val_out[2]/denom2),*HMdualVec0_);
275  dualVector_->axpy(coeff_/denom1,*HMdualVec3_);
276  dualVector_->axpy(coeff_/denom1,*HMdualVec2_);
277  dualVector_->axpy(coeff_*(-rorder1*val_out[4]/denom2),*HMdualVec0_);
278  }
279 
280  (Teuchos::dyn_cast<RiskVector<Real> >(hv)).setStatistic(var);
281  (Teuchos::dyn_cast<RiskVector<Real> >(hv)).setVector(*dualVector_);
282  }
283 };
284 
285 }
286 
287 #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:111
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
const Real getStatistic(const int i=0) const
HMCR(Teuchos::ParameterList &parlist)
Definition: ROL_HMCR.hpp:94
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Definition: ROL_HMCR.hpp:221
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
Teuchos::RCP< const Vector< Real > > getVector() const
Teuchos::RCP< Vector< Real > > HMdualVec2_
Definition: ROL_HMCR.hpp:69
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:136
void update(const Real val, const Real weight)
Definition: ROL_HMCR.hpp:153
void update(const Real val, const Vector< Real > &g, const Real weight)
Definition: ROL_HMCR.hpp:161
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:180
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:212
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:245
Teuchos::RCP< Vector< Real > > pHMdualVec3_
Definition: ROL_HMCR.hpp:70