ROL
ROL_MeanSemiDeviation.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_MEANSEMIDEVIATION_HPP
45 #define ROL_MEANSEMIDEVIATION_HPP
46 
48 #include "ROL_PlusFunction.hpp"
49 
69 namespace ROL {
70 
71 template<class Real>
72 class MeanSemiDeviation : public RandVarFunctional<Real> {
73 private:
74  Ptr<PlusFunction<Real> > plusFunction_;
75  Real coeff_;
76 
77  Ptr<SampledScalar<Real>> values_;
78  Ptr<SampledScalar<Real>> gradvecs_;
79  Ptr<SampledVector<Real>> gradients_;
80  Ptr<SampledVector<Real>> hessvecs_;
81 
87 
90 
95 
96  void initializeStorage(void) {
97  values_ = makePtr<SampledScalar<Real>>();
98  gradvecs_ = makePtr<SampledScalar<Real>>();
99  gradients_ = makePtr<SampledVector<Real>>();
100  hessvecs_ = makePtr<SampledVector<Real>>();
101 
104  }
105 
106  void clear(void) {
107  gradvecs_->update();
108  hessvecs_->update();
109  }
110 
111  void checkInputs(void) {
112  const Real zero(0);
113  ROL_TEST_FOR_EXCEPTION((coeff_ < zero), std::invalid_argument,
114  ">>> ERROR (ROL::MeanSemiDeviation): Coefficient must be positive!");
115  ROL_TEST_FOR_EXCEPTION(plusFunction_ == nullPtr, std::invalid_argument,
116  ">>> ERROR (ROL::MeanSemiDeviation): PlusFunction pointer is null!");
118  }
119 
120 public:
121 
127  MeanSemiDeviation( const Real coeff, const Ptr<PlusFunction<Real> > &pf )
128  : RandVarFunctional<Real>(), plusFunction_(pf), coeff_(coeff) {
129  checkInputs();
130  }
131 
141  MeanSemiDeviation( ROL::ParameterList &parlist )
142  : RandVarFunctional<Real>() {
143  ROL::ParameterList &list
144  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mean Plus Semi-Deviation");
145  // Check CVaR inputs
146  coeff_ = list.get<Real>("Coefficient");
147  // Build (approximate) plus function
148  plusFunction_ = makePtr<PlusFunction<Real>>(list);
149  // Check Inputs
150  checkInputs();
151  }
152 
153  void setStorage(const Ptr<SampledScalar<Real>> &value_storage,
154  const Ptr<SampledVector<Real>> &gradient_storage) {
155  values_ = value_storage;
156  gradients_ = gradient_storage;
158  }
159 
160  void setHessVecStorage(const Ptr<SampledScalar<Real>> &gradvec_storage,
161  const Ptr<SampledVector<Real>> &hessvec_storage) {
162  gradvecs_ = gradvec_storage;
163  hessvecs_ = hessvec_storage;
165  }
166 
167  void initialize(const Vector<Real> &x) {
169  clear();
170  }
171 
173  const Vector<Real> &x,
174  const std::vector<Real> &xstat,
175  Real &tol) {
176  Real val = computeValue(obj,x,tol);
177  val_ += weight_ * val;
178  }
179 
180  Real getValue(const Vector<Real> &x,
181  const std::vector<Real> &xstat,
182  SampleGenerator<Real> &sampler) {
183  // Compute expected value
184  Real ev(0);
185  sampler.sumAll(&val_,&ev,1);
186  // Compute deviation
187  Real diff(0), pf(0), dev(0), weight(0);
188  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
189  values_->get(diff,sampler.getMyPoint(i));
190  weight = sampler.getMyWeight(i);
191  diff -= ev;
192  pf += weight * plusFunction_->evaluate(diff,0);
193  }
194  sampler.sumAll(&pf,&dev,1);
195  // Return mean plus deviation
196  return ev + coeff_ * dev;
197  }
198 
200  const Vector<Real> &x,
201  const std::vector<Real> &xstat,
202  Real &tol) {
203  Real val = computeValue(obj,x,tol);
204  val_ += weight_ * val;
205  computeGradient(*dualVector_,obj,x,tol);
206  }
207 
209  std::vector<Real> &gstat,
210  const Vector<Real> &x,
211  const std::vector<Real> &xstat,
212  SampleGenerator<Real> &sampler) {
213  // Compute expected value
214  Real ev(0);
215  sampler.sumAll(&val_,&ev,1);
216  // Compute deviation
217  Real diff(0), dev(0), pf (0), c(0), one(1), weight(0);
218  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
219  values_->get(diff,sampler.getMyPoint(i));
220  weight = sampler.getMyWeight(i);
221  diff -= ev;
222  pf += weight * plusFunction_->evaluate(diff,1);
223  }
224  sampler.sumAll(&pf,&dev,1);
225  // Compute derivative
226  g_->zero(); dualVector_->zero();
227  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
228  values_->get(diff,sampler.getMyPoint(i));
229  weight = sampler.getMyWeight(i);
230  diff -= ev;
231  pf = plusFunction_->evaluate(diff,1);
232  c = one + coeff_ * (pf - dev);
233  gradients_->get(*dualVector_, sampler.getMyPoint(i));
234  g_->axpy(weight * c, *dualVector_);
235  }
236  sampler.sumAll(*g_, g);
237  }
238 
240  const Vector<Real> &v,
241  const std::vector<Real> &vstat,
242  const Vector<Real> &x,
243  const std::vector<Real> &xstat,
244  Real &tol) {
245  Real val = computeValue(obj,x,tol);
246  val_ += weight_ * val;
247  Real gv = computeGradVec(*dualVector_,obj,v,x,tol);
248  gv_ += weight_ * gv;
249  computeHessVec(*dualVector_,obj,v,x,tol);
250  }
251 
253  std::vector<Real> &hvstat,
254  const Vector<Real> &v,
255  const std::vector<Real> &vstat,
256  const Vector<Real> &x,
257  const std::vector<Real> &xstat,
258  SampleGenerator<Real> &sampler) {
259  const Real one(1);
260  // Compute expected value
261  std::vector<Real> mval = {val_, gv_};
262  std::vector<Real> gval(2,0);
263  sampler.sumAll(&mval[0],&gval[0],2);
264  Real ev = gval[0], egv = gval[1];
265  // Compute deviation
266  Real diff(0), pf1(0), pf2(0), weight(0), gv(0), c(0);
267  std::vector<Real> pf(2,0), dev(2,0);
268  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
269  values_->get(diff, sampler.getMyPoint(i));
270  gradvecs_->get(gv, sampler.getMyPoint(i));
271  weight = sampler.getMyWeight(i);
272  diff -= ev;
273  pf[0] += weight * plusFunction_->evaluate(diff,1);
274  pf[1] += weight * plusFunction_->evaluate(diff,2) * (gv - egv);
275  }
276  sampler.sumAll(&pf[0],&dev[0],2);
277  hv_->zero(); dualVector_->zero();
278  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
279  values_->get(diff, sampler.getMyPoint(i));
280  gradvecs_->get(gv, sampler.getMyPoint(i));
281  weight = sampler.getMyWeight(i);
282  diff -= ev;
283  pf1 = plusFunction_->evaluate(diff,1);
284  c = one + coeff_ * (pf1 - dev[0]);
285  hessvecs_->get(*dualVector_, sampler.getMyPoint(i));
286  hv_->axpy(weight * c, *dualVector_);
287  pf2 = plusFunction_->evaluate(diff,2) * (gv - egv);
288  c = coeff_ * (pf2 - dev[1]);
289  gradients_->get(*dualVector_, sampler.getMyPoint(i));
290  hv_->axpy(weight * c, *dualVector_);
291  }
292  sampler.sumAll(*hv_, hv);
293  }
294 };
295 
296 }
297 
298 #endif
ROL::MeanSemiDeviation::setStorage
void setStorage(const Ptr< SampledScalar< Real >> &value_storage, const Ptr< SampledVector< Real >> &gradient_storage)
Definition: ROL_MeanSemiDeviation.hpp:153
ROL::MeanSemiDeviation::setHessVecStorage
void setHessVecStorage(const Ptr< SampledScalar< Real >> &gradvec_storage, const Ptr< SampledVector< Real >> &hessvec_storage)
Definition: ROL_MeanSemiDeviation.hpp:160
ROL::RandVarFunctional::hv_
Ptr< Vector< Real > > hv_
Definition: ROL_RandVarFunctional.hpp:93
ROL::MeanSemiDeviation::getGradient
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_MeanSemiDeviation.hpp:208
zero
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Definition: ROL_Objective_SerialSimOpt.hpp:112
ROL::MeanSemiDeviation::coeff_
Real coeff_
Definition: ROL_MeanSemiDeviation.hpp:75
ROL::SampleGenerator
Definition: ROL_SampleGenerator.hpp:54
ROL::MeanSemiDeviation::plusFunction_
Ptr< PlusFunction< Real > > plusFunction_
Definition: ROL_MeanSemiDeviation.hpp:74
ROL::PlusFunction
Definition: ROL_PlusFunction.hpp:55
ROL::MeanSemiDeviation
Provides an interface for the mean plus upper semideviation of order 1.
Definition: ROL_MeanSemiDeviation.hpp:72
ROL::MeanSemiDeviation::MeanSemiDeviation
MeanSemiDeviation(const Real coeff, const Ptr< PlusFunction< Real > > &pf)
Constructor.
Definition: ROL_MeanSemiDeviation.hpp:127
ROL::RandVarFunctional::g_
Ptr< Vector< Real > > g_
Definition: ROL_RandVarFunctional.hpp:92
ROL::MeanSemiDeviation::MeanSemiDeviation
MeanSemiDeviation(ROL::ParameterList &parlist)
Constructor.
Definition: ROL_MeanSemiDeviation.hpp:141
ROL::MeanSemiDeviation::gradvecs_
Ptr< SampledScalar< Real > > gradvecs_
Definition: ROL_MeanSemiDeviation.hpp:78
ROL::RandVarFunctional::computeValue
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Definition: ROL_RandVarFunctional.hpp:101
ROL::RandVarFunctional::computeHessVec
void computeHessVec(Vector< Real > &hv, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Definition: ROL_RandVarFunctional.hpp:154
ROL::MeanSemiDeviation::gradients_
Ptr< SampledVector< Real > > gradients_
Definition: ROL_MeanSemiDeviation.hpp:79
ROL::RandVarFunctional::gv_
Real gv_
Definition: ROL_RandVarFunctional.hpp:91
ROL_RandVarFunctional.hpp
ROL::MeanSemiDeviation::getHessVec
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_MeanSemiDeviation.hpp:252
ROL::RandVarFunctional::dualVector_
Ptr< Vector< Real > > dualVector_
Definition: ROL_RandVarFunctional.hpp:94
ROL::MeanSemiDeviation::values_
Ptr< SampledScalar< Real > > values_
Definition: ROL_MeanSemiDeviation.hpp:77
ROL::SampledVector
Definition: ROL_SampledVector.hpp:51
ROL::MeanSemiDeviation::clear
void clear(void)
Definition: ROL_MeanSemiDeviation.hpp:106
ROL::MeanSemiDeviation::checkInputs
void checkInputs(void)
Definition: ROL_MeanSemiDeviation.hpp:111
ROL::Vector
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84
ROL::MeanSemiDeviation::initializeStorage
void initializeStorage(void)
Definition: ROL_MeanSemiDeviation.hpp:96
ROL::MeanSemiDeviation::updateHessVec
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_MeanSemiDeviation.hpp:239
ROL::RandVarFunctional::val_
Real val_
Definition: ROL_RandVarFunctional.hpp:90
ROL::MeanSemiDeviation::getValue
Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
Definition: ROL_MeanSemiDeviation.hpp:180
ROL::SampleGenerator::getMyPoint
virtual std::vector< Real > getMyPoint(const int i) const
Definition: ROL_SampleGenerator.hpp:112
ROL::SampledScalar
Definition: ROL_SampledScalar.hpp:51
ROL
Definition: ROL_ElementwiseVector.hpp:61
ROL::RandVarFunctional::setStorage
virtual void setStorage(const Ptr< SampledScalar< Real >> &value_storage, const Ptr< SampledVector< Real >> &gradient_storage)
Definition: ROL_RandVarFunctional.hpp:206
ROL::SampleGenerator::numMySamples
virtual int numMySamples(void) const
Definition: ROL_SampleGenerator.hpp:108
ROL::RandVarFunctional::weight_
Real weight_
Definition: ROL_RandVarFunctional.hpp:98
ROL::MeanSemiDeviation::updateValue
void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
Definition: ROL_MeanSemiDeviation.hpp:172
ROL_PlusFunction.hpp
ROL::RandVarFunctional::initialize
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
Definition: ROL_RandVarFunctional.hpp:241
ROL::SampleGenerator::getMyWeight
virtual Real getMyWeight(const int i) const
Definition: ROL_SampleGenerator.hpp:116
ROL::RandVarFunctional::computeGradient
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
Definition: ROL_RandVarFunctional.hpp:119
ROL::RandVarFunctional::setHessVecStorage
virtual void setHessVecStorage(const Ptr< SampledScalar< Real >> &gradvec_storage, const Ptr< SampledVector< Real >> &hessvec_storage)
Definition: ROL_RandVarFunctional.hpp:213
ROL::RandVarFunctional
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
Definition: ROL_RandVarFunctional.hpp:80
ROL::MeanSemiDeviation::updateGradient
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_MeanSemiDeviation.hpp:199
ROL::MeanSemiDeviation::hessvecs_
Ptr< SampledVector< Real > > hessvecs_
Definition: ROL_MeanSemiDeviation.hpp:80
ROL::MeanSemiDeviation::initialize
void initialize(const Vector< Real > &x)
Initialize temporary variables.
Definition: ROL_MeanSemiDeviation.hpp:167
ROL::Objective
Provides the interface to evaluate objective functions.
Definition: ROL_Objective.hpp:77
ROL::SampleGenerator::start
virtual int start(void)
Definition: ROL_SampleGenerator.hpp:83
ROL::SampleGenerator::sumAll
void sumAll(Real *input, Real *output, int dim) const
Definition: ROL_SampleGenerator.hpp:128
ROL::RandVarFunctional::computeGradVec
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Definition: ROL_RandVarFunctional.hpp:135