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 
72 namespace ROL {
73 
74 template<class Real>
75 class HMCR : public RiskMeasure<Real> {
76 private:
77  // Plus function (approximation)
78  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
79 
80  // User inputs
81  Real prob_;
82  Real lambda_;
83  unsigned order_;
84 
85  // 1/(1-prob)
86  Real coeff_;
87 
88  // Temporary vector storage
89  Teuchos::RCP<Vector<Real> > mDualVector0_;
90  Teuchos::RCP<Vector<Real> > gDualVector0_;
91  Teuchos::RCP<Vector<Real> > mDualVector1_;
92  Teuchos::RCP<Vector<Real> > gDualVector1_;
93 
94  // Statistic storage
95  Real xvar_;
96  Real vvar_;
97 
98  // Temporary scalar storage
99  Real pnorm_;
100  Real coeff0_;
101  Real coeff1_;
102  Real coeff2_;
103 
104  // Flag to initialized vector storage
106 
107  void checkInputs(void) const {
108  const Real zero(0), one(1);
109  TEUCHOS_TEST_FOR_EXCEPTION((prob_ <= zero) || (prob_ >= one), std::invalid_argument,
110  ">>> ERROR (ROL::HMCR): Confidence level must be between 0 and 1!");
111  TEUCHOS_TEST_FOR_EXCEPTION((lambda_ < zero) || (lambda_ > one), std::invalid_argument,
112  ">>> ERROR (ROL::HMCR): Convex combination parameter must be positive!");
113  TEUCHOS_TEST_FOR_EXCEPTION((order_ < 2), std::invalid_argument,
114  ">>> ERROR (ROL::HMCR): Norm order is less than 2!");
115  TEUCHOS_TEST_FOR_EXCEPTION(plusFunction_ == Teuchos::null, std::invalid_argument,
116  ">>> ERROR (ROL::HMCR): PlusFunction pointer is null!");
117  }
118 
119 public:
129  HMCR( const Real prob, const Real lambda, const unsigned order,
130  const Teuchos::RCP<PlusFunction<Real> > &pf )
131  : RiskMeasure<Real>(),
132  plusFunction_(pf), prob_(prob), lambda_(lambda), order_(order),
133  xvar_(0), vvar_(0), pnorm_(0), coeff0_(0), coeff1_(0), coeff2_(0),
134  HMCR_firstReset_(true) {
135  checkInputs();
136  const Real one(1);
137  coeff_ = one/(one-prob_);
138  }
139 
151  HMCR( Teuchos::ParameterList &parlist )
152  : RiskMeasure<Real>(),
153  xvar_(0), vvar_(0), pnorm_(0), coeff0_(0), coeff1_(0), coeff2_(0),
154  HMCR_firstReset_(true) {
155  Teuchos::ParameterList &list
156  = parlist.sublist("SOL").sublist("Risk Measure").sublist("HMCR");
157  // Check HMCR inputs
158  prob_ = list.get<Real>("Confidence Level");
159  lambda_ = list.get<Real>("Convex Combination Parameter");
160  order_ = (unsigned)list.get<int>("Order",2);
161  // Build (approximate) plus function
162  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
163  // Check inputs
164  checkInputs();
165  const Real one(1);
166  coeff_ = one/(one-prob_);
167  }
168 
169  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
171  xvar_ = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic(0);
172  // Initialize additional vector storage
173  if ( HMCR_firstReset_ ) {
174  mDualVector0_ = (x0->dual()).clone();
175  gDualVector0_ = (x0->dual()).clone();
176  mDualVector1_ = (x0->dual()).clone();
177  gDualVector1_ = (x0->dual()).clone();
178  HMCR_firstReset_ = false;
179  }
180  // Zero temporary storage
181  const Real zero(0);
182  mDualVector0_->zero(); gDualVector0_->zero();
183  pnorm_ = zero; coeff0_ = zero;
184  }
185 
186  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
187  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
188  reset(x0,x);
189  v0 = Teuchos::rcp_const_cast<Vector<Real> >(
190  Teuchos::dyn_cast<const RiskVector<Real> >(v).getVector());
191  vvar_ = Teuchos::dyn_cast<const RiskVector<Real> >(v).getStatistic(0);
192  // Zero temporary storage
193  const Real zero(0);
194  mDualVector1_->zero(); gDualVector1_->zero();
195  coeff1_ = zero; coeff2_ = zero;
196  }
197 
198  void update(const Real val, const Real weight) {
199  const Real rorder = static_cast<Real>(order_);
200  // Expected value
201  RiskMeasure<Real>::val_ += weight*val;
202  // Higher moment
203  Real pf = plusFunction_->evaluate(val-xvar_,0);
204  pnorm_ += weight*std::pow(pf,rorder);
205  }
206 
208  const Real one(1);
209  const Real power = one/static_cast<Real>(order_);
210  std::vector<Real> val_in(2), val_out(2);
211  val_in[0] = RiskMeasure<Real>::val_;
212  val_in[1] = pnorm_;
213  sampler.sumAll(&val_in[0],&val_out[0],2);
214  return (one-lambda_)*val_out[0]
215  + lambda_*(xvar_ + coeff_*std::pow(val_out[1],power));
216  }
217 
218  void update(const Real val, const Vector<Real> &g, const Real weight) {
219  const Real one(1);
220  const Real rorder0 = static_cast<Real>(order_);
221  const Real rorder1 = rorder0 - one;
222  // Expected value
223  RiskMeasure<Real>::g_->axpy(weight,g);
224  // Higher moment
225  Real pf0 = plusFunction_->evaluate(val-xvar_,0);
226  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
227 
228  Real pf0p0 = std::pow(pf0,rorder0);
229  Real pf0p1 = std::pow(pf0,rorder1);
230 
231  pnorm_ += weight*pf0p0;
232  coeff0_ += weight*pf0p1*pf1;
233 
234  mDualVector0_->axpy(weight*pf0p1*pf1,g);
235  }
236 
238  const Real zero(0), one(1);
239  std::vector<Real> val_in(2), val_out(2);
240  val_in[0] = pnorm_; val_in[1] = coeff0_;
241 
242  sampler.sumAll(&val_in[0],&val_out[0],2);
245  Real var = lambda_;
246  // If the higher moment term is positive then compute gradient
247  if ( val_in[0] > zero ) {
248  const Real rorder0 = static_cast<Real>(order_);
249  const Real rorder1 = rorder0 - one;
250  Real denom = std::pow(val_out[0],rorder1/rorder0);
251  // Sum higher moment contribution
254  // Compute statistic gradient
255  var -= lambda_*coeff_*((denom > zero) ? val_out[1]/denom : zero);
256  }
257  // Set gradients
258  (Teuchos::dyn_cast<RiskVector<Real> >(g)).setStatistic(var);
259  (Teuchos::dyn_cast<RiskVector<Real> >(g)).setVector(*(RiskMeasure<Real>::dualVector_));
260  }
261 
262  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
263  const Real weight) {
264  const Real one(1);
265  const Real rorder0 = static_cast<Real>(order_);
266  const Real rorder1 = rorder0-one;
267  const Real rorder2 = rorder1-one;
268  // Expected value
269  RiskMeasure<Real>::hv_->axpy(weight,hv);
270  // Higher moment
271  Real pf0 = plusFunction_->evaluate(val-xvar_,0);
272  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
273  Real pf2 = plusFunction_->evaluate(val-xvar_,2);
274 
275  Real pf0p0 = std::pow(pf0,rorder0);
276  Real pf0p1 = std::pow(pf0,rorder1);
277  Real pf0p2 = std::pow(pf0,rorder2);
278 
279  Real scale0 = (rorder1*pf0p2*pf1*pf1 + pf0p1*pf2)*(gv-vvar_);
280  Real scale1 = pf0p1*pf1;
281 
282  pnorm_ += weight*pf0p0;
283  coeff0_ += weight*scale0;
284  coeff1_ += weight*scale1;
285  coeff2_ += weight*rorder1*scale1*(vvar_-gv);
286 
287  mDualVector0_->axpy(weight*scale0,g);
288  mDualVector0_->axpy(weight*scale1,hv);
289  mDualVector1_->axpy(weight*scale1,g);
290  }
291 
293  const Real zero(0), one(1);
294  std::vector<Real> val_in(4), val_out(4);
295  val_in[0] = pnorm_; val_in[1] = coeff0_;
296  val_in[2] = coeff1_; val_in[3] = coeff2_;
297 
298  sampler.sumAll(&val_in[0],&val_out[0],4);
300 
301  Real var = zero;
303 
304  if ( val_out[0] > zero ) {
305  const Real rorder0 = static_cast<Real>(order_);
306  const Real rorder1 = rorder0-one;
307  const Real rorder2 = rorder0 + rorder1;
308  const Real coeff = lambda_*coeff_;
309 
312 
313  Real denom1 = std::pow(val_out[0],rorder1/rorder0);
314  Real denom2 = std::pow(val_out[0],rorder2/rorder0);
315 
316  var = -coeff*(val_out[1]/denom1 + val_out[3]*val_out[2]/denom2);
317  RiskMeasure<Real>::dualVector_->axpy(coeff/denom1,*gDualVector0_);
318  RiskMeasure<Real>::dualVector_->axpy(coeff*val_out[3]/denom2,*gDualVector1_);
319  }
320 
321  (Teuchos::dyn_cast<RiskVector<Real> >(hv)).setStatistic(var);
322  (Teuchos::dyn_cast<RiskVector<Real> >(hv)).setVector(*(RiskMeasure<Real>::dualVector_));
323  }
324 };
325 
326 }
327 
328 #endif
Real xvar_
Definition: ROL_HMCR.hpp:95
Teuchos::RCP< PlusFunction< Real > > plusFunction_
Definition: ROL_HMCR.hpp:78
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
Definition: ROL_HMCR.hpp:169
bool HMCR_firstReset_
Definition: ROL_HMCR.hpp:105
Teuchos::RCP< Vector< Real > > gDualVector1_
Definition: ROL_HMCR.hpp:92
void sumAll(Real *input, Real *output, int dim) const
HMCR(Teuchos::ParameterList &parlist)
Constructor.
Definition: ROL_HMCR.hpp:151
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
Definition: ROL_HMCR.hpp:237
Real coeff1_
Definition: ROL_HMCR.hpp:101
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real prob_
Definition: ROL_HMCR.hpp:81
Real coeff_
Definition: ROL_HMCR.hpp:86
Teuchos::RCP< Vector< Real > > mDualVector1_
Definition: ROL_HMCR.hpp:91
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.
Definition: ROL_HMCR.hpp:186
Real coeff2_
Definition: ROL_HMCR.hpp:102
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
Definition: ROL_HMCR.hpp:198
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
Definition: ROL_HMCR.hpp:218
Real pnorm_
Definition: ROL_HMCR.hpp:99
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.
Definition: ROL_HMCR.hpp:262
Real lambda_
Definition: ROL_HMCR.hpp:82
unsigned order_
Definition: ROL_HMCR.hpp:83
Real coeff0_
Definition: ROL_HMCR.hpp:100
Real getValue(SampleGenerator< Real > &sampler)
Return risk measure value.
Definition: ROL_HMCR.hpp:207
Real vvar_
Definition: ROL_HMCR.hpp:96
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
Definition: ROL_HMCR.hpp:292
Provides an interface for a convex combination of the expected value and the higher moment coherent r...
Definition: ROL_HMCR.hpp:75
HMCR(const Real prob, const Real lambda, const unsigned order, const Teuchos::RCP< PlusFunction< Real > > &pf)
Constructor.
Definition: ROL_HMCR.hpp:129
Teuchos::RCP< Vector< Real > > mDualVector0_
Definition: ROL_HMCR.hpp:89
void checkInputs(void) const
Definition: ROL_HMCR.hpp:107
Provides the interface to implement risk measures.
Teuchos::RCP< Vector< Real > > gDualVector0_
Definition: ROL_HMCR.hpp:90