ROL
ROL_CVaR.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef ROL_CVAR_HPP
11#define ROL_CVAR_HPP
12
14#include "ROL_PlusFunction.hpp"
15
41namespace ROL {
42
43template<class Real>
44class CVaR : public RandVarFunctional<Real> {
45private:
46 Ptr<PlusFunction<Real> > plusFunction_;
47 Real prob_;
48 Real coeff_;
49
50 using RandVarFunctional<Real>::val_;
51 using RandVarFunctional<Real>::gv_;
52 using RandVarFunctional<Real>::g_;
53 using RandVarFunctional<Real>::hv_;
55
56 using RandVarFunctional<Real>::point_;
58
63
64 void checkInputs(void) const {
65 Real zero(0), one(1);
66 ROL_TEST_FOR_EXCEPTION((prob_ <= zero) || (prob_ >= one), std::invalid_argument,
67 ">>> ERROR (ROL::CVaR): Confidence level must be between 0 and 1!");
68 ROL_TEST_FOR_EXCEPTION((coeff_ < zero) || (coeff_ > one), std::invalid_argument,
69 ">>> ERROR (ROL::CVaR): Convex combination parameter must be positive!");
70 ROL_TEST_FOR_EXCEPTION(plusFunction_ == nullPtr, std::invalid_argument,
71 ">>> ERROR (ROL::CVaR): PlusFunction pointer is null!");
72 }
73
74public:
75
84 CVaR( const Real prob, const Real coeff,
85 const Ptr<PlusFunction<Real> > &pf )
86 : RandVarFunctional<Real>(), plusFunction_(pf), prob_(prob), coeff_(coeff) {
88 }
89
100 CVaR( ROL::ParameterList &parlist )
101 : RandVarFunctional<Real>() {
102 ROL::ParameterList &list
103 = parlist.sublist("SOL").sublist("Risk Measure").sublist("CVaR");
104 // Check CVaR inputs
105 prob_ = list.get<Real>("Confidence Level");
106 coeff_ = list.get<Real>("Convex Combination Parameter");
107 // Build (approximate) plus function
108 plusFunction_ = makePtr<PlusFunction<Real>>(list);
109 // Check Inputs
110 checkInputs();
111 }
112
114 const Vector<Real> &x,
115 const std::vector<Real> &xstat,
116 Real &tol) {
117 Real one(1);
118 Real val = computeValue(obj,x,tol);
119 Real pf = plusFunction_->evaluate(val-xstat[0],0);
120 val_ += weight_*((one-coeff_)*val + coeff_/(one-prob_)*pf);
121 }
122
124 const Vector<Real> &x,
125 const std::vector<Real> &xstat,
126 Real &tol) {
127 Real one(1);
128 Real val = computeValue(obj,x,tol);
129 Real pf = plusFunction_->evaluate(val-xstat[0],1);
130 val_ += weight_*pf;
131 Real c = (one-coeff_) + coeff_/(one-prob_)*pf;
132 if (std::abs(c) >= ROL_EPSILON<Real>()) {
133 computeGradient(*dualVector_,obj,x,tol);
134 g_->axpy(weight_*c,*dualVector_);
135 }
136 }
137
139 const Vector<Real> &v,
140 const std::vector<Real> &vstat,
141 const Vector<Real> &x,
142 const std::vector<Real> &xstat,
143 Real &tol) {
144 Real one(1);
145 Real val = computeValue(obj,x,tol);
146 Real pf1 = plusFunction_->evaluate(val-xstat[0],1);
147 Real pf2 = plusFunction_->evaluate(val-xstat[0],2);
148 Real c(0);
149 if (std::abs(pf2) >= ROL_EPSILON<Real>()) {
150 Real gv = computeGradVec(*dualVector_,obj,v,x,tol);
151 val_ += weight_*pf2*(vstat[0]-gv);
152 c = pf2*coeff_/(one-prob_)*(gv-vstat[0]);
153 hv_->axpy(weight_*c,*dualVector_);
154 }
155 c = (one-coeff_) + coeff_/(one-prob_)*pf1;
156 if (std::abs(c) >= ROL_EPSILON<Real>()) {
157 computeHessVec(*dualVector_,obj,v,x,tol);
158 hv_->axpy(weight_*c,*dualVector_);
159 }
160 }
161
162 Real getValue(const Vector<Real> &x,
163 const std::vector<Real> &xstat,
164 SampleGenerator<Real> &sampler) {
165 Real cvar(0);
166 sampler.sumAll(&val_,&cvar,1);
167 cvar += coeff_*xstat[0];
168 return cvar;
169 }
170
172 std::vector<Real> &gstat,
173 const Vector<Real> &x,
174 const std::vector<Real> &xstat,
175 SampleGenerator<Real> &sampler) {
176 Real var(0), one(1);
177 sampler.sumAll(&val_,&var,1);
178 var *= -coeff_/(one-prob_);
179 var += coeff_;
180 gstat[0] = var;
181 sampler.sumAll(*g_,g);
182 }
183
185 std::vector<Real> &hvstat,
186 const Vector<Real> &v,
187 const std::vector<Real> &vstat,
188 const Vector<Real> &x,
189 const std::vector<Real> &xstat,
190 SampleGenerator<Real> &sampler) {
191 Real var(0), one(1);
192 sampler.sumAll(&val_,&var,1);
193 var *= coeff_/(one-prob_);
194 hvstat[0] = var;
195 sampler.sumAll(*hv_,hv);
196 }
197};
198
199}
200
201#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Provides an interface for a convex combination of the expected value and the conditional value-at-ris...
Definition ROL_CVaR.hpp:44
Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
Definition ROL_CVaR.hpp:162
CVaR(ROL::ParameterList &parlist)
Constructor.
Definition ROL_CVaR.hpp:100
Real coeff_
Definition ROL_CVaR.hpp:48
void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
Definition ROL_CVaR.hpp:113
void updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
Definition ROL_CVaR.hpp:123
Real prob_
Definition ROL_CVaR.hpp:47
Ptr< PlusFunction< Real > > plusFunction_
Definition ROL_CVaR.hpp:46
void getGradient(Vector< Real > &g, std::vector< Real > &gstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
Definition ROL_CVaR.hpp:171
void updateHessVec(Objective< Real > &obj, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for Hessian-time-a-vector computation.
Definition ROL_CVaR.hpp:138
CVaR(const Real prob, const Real coeff, const Ptr< PlusFunction< Real > > &pf)
Constructor.
Definition ROL_CVaR.hpp:84
void checkInputs(void) const
Definition ROL_CVaR.hpp:64
void getHessVec(Vector< Real > &hv, std::vector< Real > &hvstat, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
Definition ROL_CVaR.hpp:184
Provides the interface to evaluate objective functions.
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
void computeHessVec(Vector< Real > &hv, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Ptr< Vector< Real > > dualVector_
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
void sumAll(Real *input, Real *output, int dim) const
Defines the linear algebra or vector space interface.