ROL
ROL_MixedCVaR.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_MIXEDQUANTILEQUADRANGLE_HPP
45 #define ROL_MIXEDQUANTILEQUADRANGLE_HPP
46 
48 #include "ROL_PlusFunction.hpp"
49 
50 #include "ROL_ParameterList.hpp"
51 
85 namespace ROL {
86 
87 template<class Real>
88 class MixedCVaR : public RandVarFunctional<Real> {
89 private:
90  ROL::Ptr<PlusFunction<Real> > plusFunction_;
91  std::vector<Real> prob_;
92  std::vector<Real> coeff_;
93  std::vector<Real> vec_;
94  int size_;
95 
101 
104 
109 
110  void initializeMCVAR(void) {
111  size_ = prob_.size();
112  vec_.clear(); vec_.resize(size_,static_cast<Real>(0));
113  }
114 
115  void checkInputs(void) {
116  int pSize = prob_.size(), cSize = coeff_.size();
117  ROL_TEST_FOR_EXCEPTION((pSize!=cSize),std::invalid_argument,
118  ">>> ERROR (ROL::MixedCVaR): Probability and coefficient arrays have different sizes!");
119  Real sum(0), zero(0), one(1);
120  for (int i = 0; i < pSize; i++) {
121  ROL_TEST_FOR_EXCEPTION((prob_[i]>one || prob_[i]<zero), std::invalid_argument,
122  ">>> ERROR (ROL::MixedCVaR): Element of probability array out of range!");
123  ROL_TEST_FOR_EXCEPTION((coeff_[i]>one || coeff_[i]<zero), std::invalid_argument,
124  ">>> ERROR (ROL::MixedCVaR): Element of coefficient array out of range!");
125  sum += coeff_[i];
126  }
127  ROL_TEST_FOR_EXCEPTION((std::abs(sum-one) > std::sqrt(ROL_EPSILON<Real>())),std::invalid_argument,
128  ">>> ERROR (ROL::MixedCVaR): Coefficients do not sum to one!");
129  ROL_TEST_FOR_EXCEPTION(plusFunction_ == ROL::nullPtr, std::invalid_argument,
130  ">>> ERROR (ROL::MixedCVaR): PlusFunction pointer is null!");
131  initializeMCVAR();
132  }
133 
134 public:
135 
136  MixedCVaR( ROL::ParameterList &parlist )
137  : RandVarFunctional<Real>() {
138  ROL::ParameterList &list
139  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mixed CVaR");
140  // Grab probability and coefficient arrays
141  prob_ = ROL::getArrayFromStringParameter<Real>(list,"Probability Array");
142  coeff_ = ROL::getArrayFromStringParameter<Real>(list,"Coefficient Array");
143  plusFunction_ = ROL::makePtr<PlusFunction<Real>>(list);
144  // Check inputs
145  checkInputs();
146  }
147 
148  MixedCVaR(const std::vector<Real> &prob,
149  const std::vector<Real> &coeff,
150  const ROL::Ptr<PlusFunction<Real> > &pf )
151  : RandVarFunctional<Real>(), plusFunction_(pf), prob_(prob), coeff_(coeff) {
152  checkInputs();
153  }
154 
155  void initialize(const Vector<Real> &x) {
157  vec_.assign(size_,static_cast<Real>(0));
158  }
159 
160  Real computeStatistic(const Ptr<std::vector<Real>> &xstat) const {
161  Real stat(0);
162  if (xstat != nullPtr) {
163  for (int i = 0; i < size_; ++i) {
164  stat = coeff_[i]*(*xstat)[i];
165  }
166  }
167  return stat;
168  }
169 
171  const Vector<Real> &x,
172  const std::vector<Real> &xstat,
173  Real &tol) {
174  Real pf(0), one(1);
175  Real val = computeValue(obj,x,tol);
176  for (int i = 0; i < size_; i++) {
177  pf = plusFunction_->evaluate(val-xstat[i],0);
178  val_ += weight_*coeff_[i]/(one-prob_[i])*pf;
179  }
180  }
181 
182  Real getValue(const Vector<Real> &x,
183  const std::vector<Real> &xstat,
184  SampleGenerator<Real> &sampler) {
185  Real cvar(0);
186  sampler.sumAll(&val_,&cvar,1);
187  for (int i = 0; i < size_; i++) {
188  cvar += coeff_[i]*xstat[i];
189  }
190  return cvar;
191  }
192 
194  const Vector<Real> &x,
195  const std::vector<Real> &xstat,
196  Real &tol) {
197  Real pf(0), c(0), one(1);
198  Real val = computeValue(obj,x,tol);
199  for (int i = 0; i < size_; i++) {
200  pf = plusFunction_->evaluate(val-xstat[i],1);
201  c = weight_*coeff_[i]/(one-prob_[i])*pf;
202  if (std::abs(c) >= ROL_EPSILON<Real>()) {
203  vec_[i] -= c;
204  computeGradient(*dualVector_,obj,x,tol);
205  g_->axpy(c,*dualVector_);
206  }
207  }
208  }
209 
211  std::vector<Real> &gstat,
212  const Vector<Real> &x,
213  const std::vector<Real> &xstat,
214  SampleGenerator<Real> &sampler) {
215  sampler.sumAll(&vec_[0],&gstat[0],size_);
216  for (int i = 0; i < size_; i++) {
217  gstat[i] += coeff_[i];
218  }
219  sampler.sumAll(*g_,g);
220  }
221 
223  const Vector<Real> &v,
224  const std::vector<Real> &vstat,
225  const Vector<Real> &x,
226  const std::vector<Real> &xstat,
227  Real &tol) {
228  Real pf1(0), pf2(0), c(0), one(1);
229  Real val = computeValue(obj,x,tol);
230  for (int i = 0; i < size_; i++) {
231  pf1 = plusFunction_->evaluate(val-xstat[i],1);
232  pf2 = plusFunction_->evaluate(val-xstat[i],2);
233  if (std::abs(pf2) >= ROL_EPSILON<Real>()) {
234  Real gv = computeGradVec(*dualVector_,obj,v,x,tol);
235  c = weight_*coeff_[i]/(one-prob_[i])*pf2*(gv-vstat[i]);
236  vec_[i] -= c;
237  hv_->axpy(c,*dualVector_);
238  }
239  if (std::abs(pf1) >= ROL_EPSILON<Real>()) {
240  c = weight_*coeff_[i]/(one-prob_[i])*pf1;
241  computeHessVec(*dualVector_,obj,v,x,tol);
243  }
244  }
245  }
246 
248  std::vector<Real> &hvstat,
249  const Vector<Real> &v,
250  const std::vector<Real> &vstat,
251  const Vector<Real> &x,
252  const std::vector<Real> &xstat,
253  SampleGenerator<Real> &sampler) {
254  sampler.sumAll(&vec_[0],&hvstat[0],size_);
255  sampler.sumAll(*hv_,hv);
256  }
257 };
258 
259 }
260 
261 #endif
Provides the interface to evaluate objective functions.
MixedCVaR(ROL::ParameterList &parlist)
void computeHessVec(Vector< Real > &hv, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Ptr< Vector< Real > > g_
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
std::vector< Real > vec_
Ptr< Vector< Real > > hv_
void updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
ROL::Ptr< PlusFunction< Real > > plusFunction_
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.
Ptr< Vector< Real > > dualVector_
std::vector< Real > coeff_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void sumAll(Real *input, Real *output, int dim) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
Real computeStatistic(const Ptr< std::vector< Real >> &xstat) const
Provides an interface for a convex combination of conditional value-at-risks.
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.
void initialize(const Vector< Real > &x)
Initialize temporary variables.
void checkInputs(void)
MixedCVaR(const std::vector< Real > &prob, const std::vector< Real > &coeff, const ROL::Ptr< PlusFunction< Real > > &pf)
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
void initializeMCVAR(void)
std::vector< Real > prob_
void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
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.
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.