ROL
ROL_CVaR.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_CVAR_HPP
45 #define ROL_CVAR_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 CVaR : public RiskMeasure<Real> {
55 private:
56  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
57 
58  Real prob_;
59  Real coeff_;
60 
61  Teuchos::RCP<Vector<Real> > dualVector_;
62  Real xvar_;
63  Real vvar_;
64 
66 
67 public:
68 
69  CVaR( Real prob, Real coeff, Teuchos::RCP<PlusFunction<Real> > &pf )
70  : RiskMeasure<Real>(), plusFunction_(pf), xvar_(0.0), vvar_(0.0), firstReset_(true) {
71  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
72  coeff_ = ((coeff >= 0.0) ? ((coeff <= 1.0) ? coeff : 1.0) : 1.0);
73  }
74 
75  CVaR( Teuchos::ParameterList &parlist )
76  : RiskMeasure<Real>(), xvar_(0.0), vvar_(0.0), firstReset_(true) {
77  Teuchos::ParameterList &list
78  = parlist.sublist("SOL").sublist("Risk Measure").sublist("CVaR");
79  // Check CVaR inputs
80  Real prob = list.get("Confidence Level",0.5);
81  prob_ = ((prob >= 0.0) ? ((prob <= 1.0) ? prob : 0.5) : 0.5);
82  Real coeff = list.get("Convex Combination Parameter", 1.0);
83  coeff_ = ((coeff >= 0.0) ? ((coeff <= 1.0) ? coeff : 1.0) : 1.0);
84  // Build (approximate) plus function
85  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
86  }
87 
88  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
90  xvar_ = Teuchos::dyn_cast<const RiskVector<Real> >(
91  Teuchos::dyn_cast<const Vector<Real> >(x)).getStatistic();
92  if ( firstReset_ ) {
93  dualVector_ = (x0->dual()).clone();
94  firstReset_ = false;
95  }
96  dualVector_->zero();
97  }
98 
99  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
100  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
101  reset(x0,x);
102  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(
103  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
104  vvar_ = Teuchos::dyn_cast<const RiskVector<Real> >(
105  Teuchos::dyn_cast<const Vector<Real> >(v)).getStatistic();
106  }
107 
108  void update(const Real val, const Real weight) {
109  Real pf = plusFunction_->evaluate(val-xvar_,0);
110  RiskMeasure<Real>::val_ += weight*((1.0-coeff_)*val + coeff_/(1.0-prob_)*pf);
111  }
112 
113  void update(const Real val, const Vector<Real> &g, const Real weight) {
114  Real pf = plusFunction_->evaluate(val-xvar_,1);
115  RiskMeasure<Real>::val_ += weight*pf;
116  Real c = (1.0-coeff_) + coeff_/(1.0-prob_)*pf;
117  RiskMeasure<Real>::g_->axpy(weight*c,g);
118  }
119 
120  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
121  const Real weight) {
122  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
123  Real pf2 = plusFunction_->evaluate(val-xvar_,2);
124  RiskMeasure<Real>::val_ += weight*pf2*(vvar_-gv);
125  Real c = pf2*coeff_/(1.0-prob_)*(gv-vvar_);
126  RiskMeasure<Real>::hv_->axpy(weight*c,g);
127  c = (1.0-coeff_) + coeff_/(1.0-prob_)*pf1;
128  RiskMeasure<Real>::hv_->axpy(weight*c,hv);
129  }
130 
132  Real val = RiskMeasure<Real>::val_;
133  Real cvar = 0.0;
134  sampler.sumAll(&val,&cvar,1);
135  cvar += coeff_*xvar_;
136  return cvar;
137  }
138 
140  RiskVector<Real> &gs = Teuchos::dyn_cast<RiskVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(g));
141  Real val = RiskMeasure<Real>::val_;
142  Real var = 0.0;
143  sampler.sumAll(&val,&var,1);
144 
145  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector_);
146  var *= -coeff_/(1.0-prob_);
147  var += coeff_;
148  gs.setStatistic(var);
149  gs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
150  }
151 
153  RiskVector<Real> &hs = Teuchos::dyn_cast<RiskVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
154  Real val = RiskMeasure<Real>::val_;
155  Real var = 0.0;
156  sampler.sumAll(&val,&var,1);
157 
158  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector_);
159  var *= coeff_/(1.0-prob_);
160  hs.setStatistic(var);
161  hs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
162  }
163 };
164 
165 }
166 
167 #endif
Real xvar_
Definition: ROL_CVaR.hpp:62
Teuchos::RCP< Vector< Real > > dualVector_
Definition: ROL_CVaR.hpp:61
Real getValue(SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:131
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:139
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:152
void update(const Real val, const Real weight)
Definition: ROL_CVaR.hpp:108
const Real getStatistic(const int i=0) const
void update(const Real val, const Vector< Real > &g, const Real weight)
Definition: ROL_CVaR.hpp:113
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void sumAll(Real *input, Real *output, int dim) const
void setVector(const Vector< Real > &vec)
Teuchos::RCP< const Vector< Real > > getVector() const
void setStatistic(const Real stat)
CVaR(Real prob, Real coeff, Teuchos::RCP< PlusFunction< Real > > &pf)
Definition: ROL_CVaR.hpp:69
Real coeff_
Definition: ROL_CVaR.hpp:59
Real prob_
Definition: ROL_CVaR.hpp:58
Teuchos::RCP< PlusFunction< Real > > plusFunction_
Definition: ROL_CVaR.hpp:56
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Definition: ROL_CVaR.hpp:120
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Definition: ROL_CVaR.hpp:88
bool firstReset_
Definition: ROL_CVaR.hpp:65
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
Definition: ROL_CVaR.hpp:99
Real vvar_
Definition: ROL_CVaR.hpp:63
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
CVaR(Teuchos::ParameterList &parlist)
Definition: ROL_CVaR.hpp:75