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 
76 namespace ROL {
77 
78 template<class Real>
79 class CVaR : public RiskMeasure<Real> {
80 private:
81  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
82 
83  Real prob_;
84  Real coeff_;
85 
86  Teuchos::RCP<Vector<Real> > dualVector_;
87  Real xvar_;
88  Real vvar_;
89 
91 
92  void checkInputs(void) const {
93  Real zero(0), one(1);
94  TEUCHOS_TEST_FOR_EXCEPTION((prob_ <= zero) || (prob_ >= one), std::invalid_argument,
95  ">>> ERROR (ROL::CVaR): Confidence level must be between 0 and 1!");
96  TEUCHOS_TEST_FOR_EXCEPTION((coeff_ < zero) || (coeff_ > one), std::invalid_argument,
97  ">>> ERROR (ROL::CVaR): Convex combination parameter must be positive!");
98  TEUCHOS_TEST_FOR_EXCEPTION(plusFunction_ == Teuchos::null, std::invalid_argument,
99  ">>> ERROR (ROL::CVaR): PlusFunction pointer is null!");
100  }
101 
102 public:
103 
112  CVaR( const Real prob, const Real coeff,
113  const Teuchos::RCP<PlusFunction<Real> > &pf )
114  : RiskMeasure<Real>(), plusFunction_(pf), prob_(prob), coeff_(coeff),
115  xvar_(0), vvar_(0), firstReset_(true) {
116  checkInputs();
117  }
118 
129  CVaR( Teuchos::ParameterList &parlist )
130  : RiskMeasure<Real>(), xvar_(0), vvar_(0), firstReset_(true) {
131  Teuchos::ParameterList &list
132  = parlist.sublist("SOL").sublist("Risk Measure").sublist("CVaR");
133  // Check CVaR inputs
134  prob_ = list.get<Real>("Confidence Level");
135  coeff_ = list.get<Real>("Convex Combination Parameter");
136  // Build (approximate) plus function
137  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
138  // Check Inputs
139  checkInputs();
140  }
141 
142  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
144  xvar_ = Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic(0);
145  if ( firstReset_ ) {
146  dualVector_ = (x0->dual()).clone();
147  firstReset_ = false;
148  }
149  dualVector_->zero();
150  }
151 
152  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
153  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
154  reset(x0,x);
155  const RiskVector<Real> &vr = Teuchos::dyn_cast<const RiskVector<Real> >(v);
156  v0 = Teuchos::rcp_const_cast<Vector<Real> >(vr.getVector());
157  vvar_ = vr.getStatistic(0);
158  }
159 
160  void update(const Real val, const Real weight) {
161  Real one(1);
162  Real pf = plusFunction_->evaluate(val-xvar_,0);
163  RiskMeasure<Real>::val_ += weight*((one-coeff_)*val + coeff_/(one-prob_)*pf);
164  }
165 
166  void update(const Real val, const Vector<Real> &g, const Real weight) {
167  Real one(1);
168  Real pf = plusFunction_->evaluate(val-xvar_,1);
169  RiskMeasure<Real>::val_ += weight*pf;
170  Real c = (one-coeff_) + coeff_/(one-prob_)*pf;
171  RiskMeasure<Real>::g_->axpy(weight*c,g);
172  }
173 
174  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
175  const Real weight) {
176  Real one(1);
177  Real pf1 = plusFunction_->evaluate(val-xvar_,1);
178  Real pf2 = plusFunction_->evaluate(val-xvar_,2);
179  RiskMeasure<Real>::val_ += weight*pf2*(vvar_-gv);
180  Real c = pf2*coeff_/(one-prob_)*(gv-vvar_);
181  RiskMeasure<Real>::hv_->axpy(weight*c,g);
182  c = (one-coeff_) + coeff_/(one-prob_)*pf1;
183  RiskMeasure<Real>::hv_->axpy(weight*c,hv);
184  }
185 
187  Real val = RiskMeasure<Real>::val_, cvar(0);
188  sampler.sumAll(&val,&cvar,1);
189  cvar += coeff_*xvar_;
190  return cvar;
191  }
192 
194  RiskVector<Real> &gs = Teuchos::dyn_cast<RiskVector<Real> >(g);
195  Real val = RiskMeasure<Real>::val_, var(0), one(1);
196  sampler.sumAll(&val,&var,1);
197 
199  var *= -coeff_/(one-prob_);
200  var += coeff_;
201  gs.setStatistic(var);
202  gs.setVector(*dualVector_);
203  }
204 
206  RiskVector<Real> &hs = Teuchos::dyn_cast<RiskVector<Real> >(hv);
207  Real val = RiskMeasure<Real>::val_, var(0), one(1);
208  sampler.sumAll(&val,&var,1);
209 
211  var *= coeff_/(one-prob_);
212  hs.setStatistic(var);
213  hs.setVector(*dualVector_);
214  }
215 };
216 
217 }
218 
219 #endif
Real xvar_
Definition: ROL_CVaR.hpp:87
Teuchos::RCP< Vector< Real > > dualVector_
Definition: ROL_CVaR.hpp:86
Real getValue(SampleGenerator< Real > &sampler)
Return risk measure value.
Definition: ROL_CVaR.hpp:186
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
Definition: ROL_CVaR.hpp:193
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
Definition: ROL_CVaR.hpp:205
Provides an interface for a convex combination of the expected value and the conditional value-at-ris...
Definition: ROL_CVaR.hpp:79
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
Definition: ROL_CVaR.hpp:160
void sumAll(Real *input, Real *output, int dim) const
CVaR(const Real prob, const Real coeff, const Teuchos::RCP< PlusFunction< Real > > &pf)
Constructor.
Definition: ROL_CVaR.hpp:112
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
Definition: ROL_CVaR.hpp:166
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< const Vector< Real > > getVector(void) const
void setVector(const Vector< Real > &vec)
void setStatistic(const Real stat)
Real coeff_
Definition: ROL_CVaR.hpp:84
Real prob_
Definition: ROL_CVaR.hpp:83
Teuchos::RCP< PlusFunction< Real > > plusFunction_
Definition: ROL_CVaR.hpp:81
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_CVaR.hpp:174
void checkInputs(void) const
Definition: ROL_CVaR.hpp:92
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
Definition: ROL_CVaR.hpp:142
bool firstReset_
Definition: ROL_CVaR.hpp:90
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_CVaR.hpp:152
Real vvar_
Definition: ROL_CVaR.hpp:88
Teuchos::RCP< const StdVector< Real > > getStatistic(void) const
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
Provides the interface to implement risk measures.
CVaR(Teuchos::ParameterList &parlist)
Constructor.
Definition: ROL_CVaR.hpp:129