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_CVaRVector.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  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
76  x0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
77  Teuchos::dyn_cast<const Vector<Real> >(x)).getVector());
78  xvar_ = Teuchos::dyn_cast<const CVaRVector<Real> >(
79  Teuchos::dyn_cast<const Vector<Real> >(x)).getVaR();
80  if ( firstReset_ ) {
81  RiskMeasure<Real>::g_ = (x0->dual()).clone();
82  RiskMeasure<Real>::hv_ = (x0->dual()).clone();
83  dualVector_ = (x0->dual()).clone();
84  firstReset_ = false;
85  }
87  RiskMeasure<Real>::g_->zero();
88  RiskMeasure<Real>::hv_->zero();
89  dualVector_->zero();
90  }
91 
92  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
93  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
94  this->reset(x0,x);
95  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
96  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
97  vvar_ = Teuchos::dyn_cast<const CVaRVector<Real> >(
98  Teuchos::dyn_cast<const Vector<Real> >(v)).getVaR();
99  }
100 
101  void update(const Real val, const Real weight) {
102  Real pf = plusFunction_->evaluate(val-xvar_,0);
103  RiskMeasure<Real>::val_ += weight*((1.0-coeff_)*val + coeff_/(1.0-prob_)*pf);
104  }
105 
106  void update(const Real val, const Vector<Real> &g, const Real weight) {
107  Real pf = plusFunction_->evaluate(val-xvar_,1);
108  RiskMeasure<Real>::val_ += weight*pf;
109  Real c = (1.0-coeff_) + coeff_/(1.0-prob_)*pf;
110  RiskMeasure<Real>::g_->axpy(weight*c,g);
111  }
112 
113  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
114  const Real weight) {
115  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
116  Real pf2 = plusFunction_->evaluate(val-xvar_,2);
117  RiskMeasure<Real>::val_ += weight*pf2*(vvar_-gv);
118  Real c = pf2*coeff_/(1.0-prob_)*(gv-vvar_);
119  RiskMeasure<Real>::hv_->axpy(weight*c,g);
120  c = (1.0-coeff_) + coeff_/(1.0-prob_)*pf1;
121  RiskMeasure<Real>::hv_->axpy(weight*c,hv);
122  }
123 
125  Real val = RiskMeasure<Real>::val_;
126  Real cvar = 0.0;
127  sampler.sumAll(&val,&cvar,1);
128  cvar += coeff_*xvar_;
129  return cvar;
130  }
131 
133  CVaRVector<Real> &gs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(g));
134  Real val = RiskMeasure<Real>::val_;
135  Real var = 0.0;
136  sampler.sumAll(&val,&var,1);
137 
138  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector_);
139  var *= -coeff_/(1.0-prob_);
140  var += coeff_;
141  gs.setVaR(var);
142  gs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
143  }
144 
146  CVaRVector<Real> &hs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
147  Real val = RiskMeasure<Real>::val_;
148  Real var = 0.0;
149  sampler.sumAll(&val,&var,1);
150 
151  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector_);
152  var *= coeff_/(1.0-prob_);
153  hs.setVaR(var);
154  hs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
155  }
156 };
157 
158 }
159 
160 #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:124
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:132
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:145
void update(const Real val, const Real weight)
Definition: ROL_CVaR.hpp:101
void update(const Real val, const Vector< Real > &g, const Real weight)
Definition: ROL_CVaR.hpp:106
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void sumAll(Real *input, Real *output, int dim) const
const Real getVaR() const
Teuchos::RCP< const Vector< Real > > getVector() const
void setVaR(const Real var)
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:113
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Definition: ROL_CVaR.hpp:75
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:92
Real vvar_
Definition: ROL_CVaR.hpp:63
void setVector(const Vector< Real > &vec)