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  Real prob_;
57  Real coeff_;
58  Real xvar_;
59  Real vvar_;
60  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
61 
62 public:
63 
64  CVaR( Real prob, Real coeff, Teuchos::RCP<PlusFunction<Real> > &pf ) : plusFunction_(pf) {
65  prob_ = prob;
66  if ( prob < 0.0 || prob > 1.0 ) {
67  prob_ = 0.5;
68  }
69  coeff_ = coeff;
70  if ( coeff < 0.0 || coeff > 1.0 ) {
71  coeff_ = 1.0;
72  }
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  this->xvar_ = Teuchos::dyn_cast<const CVaRVector<Real> >(Teuchos::dyn_cast<const Vector<Real> >(x)).getVaR();
80  RiskMeasure<Real>::g_ = x0->clone(); RiskMeasure<Real>::g_->zero();
81  RiskMeasure<Real>::hv_ = x0->clone(); RiskMeasure<Real>::hv_->zero();
82  }
83 
84  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
85  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
86  x0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
87  Teuchos::dyn_cast<const Vector<Real> >(x)).getVector());
88  this->xvar_ = Teuchos::dyn_cast<const CVaRVector<Real> >(Teuchos::dyn_cast<const Vector<Real> >(x)).getVaR();
89  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
90  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
91  this->vvar_ = Teuchos::dyn_cast<const CVaRVector<Real> >(Teuchos::dyn_cast<const Vector<Real> >(v)).getVaR();
93  RiskMeasure<Real>::g_ = x0->clone(); RiskMeasure<Real>::g_->zero();
94  RiskMeasure<Real>::hv_ = x0->clone(); RiskMeasure<Real>::hv_->zero();
95  }
96 
97  void update(const Real val, const Real weight) {
98  Real pf = this->plusFunction_->evaluate(val-this->xvar_,0);
99  RiskMeasure<Real>::val_ += weight * ((1.0-this->coeff_) * val + this->coeff_/(1.0-this->prob_) * pf);
100  }
101 
102  void update(const Real val, const Vector<Real> &g, const Real weight) {
103  Real pf = this->plusFunction_->evaluate(val-this->xvar_,1);
104  RiskMeasure<Real>::val_ += weight * pf;
105  Real c = (1.0-this->coeff_) + this->coeff_/(1.0-this->prob_)*pf;
106  RiskMeasure<Real>::g_->axpy(weight*c,g);
107  }
108 
109  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
110  const Real weight) {
111  Real pf1 = this->plusFunction_->evaluate(val-this->xvar_,1);
112  Real pf2 = this->plusFunction_->evaluate(val-this->xvar_,2);
113  RiskMeasure<Real>::val_ += weight * pf2 * (this->vvar_ - gv);
114  Real c = pf2 * this->coeff_/(1.0-this->prob_) * (-this->vvar_ + gv);
115  RiskMeasure<Real>::hv_->axpy(weight*c,g);
116  c = (1.0-this->coeff_) + this->coeff_/(1.0-this->prob_) * pf1;
117  RiskMeasure<Real>::hv_->axpy(weight*c,hv);
118  }
119 
121  Real val = RiskMeasure<Real>::val_;
122  Real cvar = 0.0;
123  sampler.sumAll(&val,&cvar,1);
124  cvar += this->coeff_ * this->xvar_;
125  return cvar;
126  }
127 
129  CVaRVector<Real> &gs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(g));
130  Real val = RiskMeasure<Real>::val_;
131  Real var = 0.0;
132  sampler.sumAll(&val,&var,1);
133 
134  Teuchos::RCP<Vector<Real> > gz = RiskMeasure<Real>::g_->clone();
135  sampler.sumAll(*(RiskMeasure<Real>::g_),*gz);
136  var *= -this->coeff_/(1.0-this->prob_);
137  var += this->coeff_;
138  gs.setVaR(var);
139  gs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(gz)));
140  }
141 
143  CVaRVector<Real> &hs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
144  Real val = RiskMeasure<Real>::val_;
145  Real var = 0.0;
146  sampler.sumAll(&val,&var,1);
147 
148  Teuchos::RCP<Vector<Real> > hz = RiskMeasure<Real>::hv_->clone();
149  sampler.sumAll(*(RiskMeasure<Real>::hv_),*hz);
150  var *= this->coeff_/(1.0-this->prob_);
151  hs.setVaR(var);
152  hs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(hz)));
153  }
154 };
155 
156 }
157 
158 #endif
Real xvar_
Definition: ROL_CVaR.hpp:58
Real getValue(SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:120
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:128
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Definition: ROL_CVaR.hpp:142
void update(const Real val, const Real weight)
Definition: ROL_CVaR.hpp:97
void update(const Real val, const Vector< Real > &g, const Real weight)
Definition: ROL_CVaR.hpp:102
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
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:64
Real coeff_
Definition: ROL_CVaR.hpp:57
Real prob_
Definition: ROL_CVaR.hpp:56
Teuchos::RCP< PlusFunction< Real > > plusFunction_
Definition: ROL_CVaR.hpp:60
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Definition: ROL_CVaR.hpp:109
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Definition: ROL_CVaR.hpp:75
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
Definition: ROL_CVaR.hpp:84
Real vvar_
Definition: ROL_CVaR.hpp:59
void setVector(const Vector< Real > &vec)