ROL
ROL_PD_MeanSemiDeviation.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_PD_MEANSEMIDEVIATION_HPP
11 #define ROL_PD_MEANSEMIDEVIATION_HPP
12 
14 
15 namespace ROL {
16 
17 template<class Real>
19 private:
20  Real coeff_;
21 
22  Ptr<ScalarController<Real>> values_;
23  Ptr<ScalarController<Real>> gradvecs_;
24  Ptr<VectorController<Real>> gradients_;
25  Ptr<VectorController<Real>> hessvecs_;
26 
32 
35 
40 
45 
46  void initializeStorage(void) {
47  values_ = makePtr<ScalarController<Real>>();
48  gradvecs_ = makePtr<ScalarController<Real>>();
49  gradients_ = makePtr<VectorController<Real>>();
50  hessvecs_ = makePtr<VectorController<Real>>();
51 
53  RandVarFunctional<Real>::setHessVecStorage(gradvecs_,hessvecs_);
54  }
55 
56  void clear(void) {
57  gradvecs_->reset();
58  hessvecs_->reset();
59  }
60 
61  void checkInputs(void) {
62  Real zero(0);
63  ROL_TEST_FOR_EXCEPTION((coeff_ < zero), std::invalid_argument,
64  ">>> ERROR (ROL::PD_MeanSemiDeviation): Element of coefficient array out of range!");
66  }
67 
68 public:
69  PD_MeanSemiDeviation(const Real coeff)
70  : PD_RandVarFunctional<Real>(), coeff_(coeff) {
71  checkInputs();
72  }
73 
74  void setStorage(const Ptr<ScalarController<Real>> &value_storage,
75  const Ptr<VectorController<Real>> &gradient_storage) {
76  values_ = value_storage;
77  gradients_ = gradient_storage;
79  }
80 
81  void setHessVecStorage(const Ptr<ScalarController<Real>> &gradvec_storage,
82  const Ptr<VectorController<Real>> &hessvec_storage) {
83  gradvecs_ = gradvec_storage;
84  hessvecs_ = hessvec_storage;
86  }
87 
88  void initialize(const Vector<Real> &x) {
90  clear();
91  }
92 
94  const Vector<Real> &x,
95  const std::vector<Real> &xstat,
96  Real &tol) {
97  Real val = computeValue(obj,x,tol);
98  val_ += weight_ * val;
99  }
100 
101  Real getValue(const Vector<Real> &x,
102  const std::vector<Real> &xstat,
103  SampleGenerator<Real> &sampler) {
104  // Compute expected value
105  Real ev(0);
106  sampler.sumAll(&val_,&ev,1);
107  // Compute deviation
108  Real diff(0), pf0(0), dev(0), weight(0), lam(0);
109  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
110  values_->get(diff,sampler.getMyPoint(i));
111  diff -= ev;
112  setValue(diff, sampler.getMyPoint(i));
113  getMultiplier(lam,sampler.getMyPoint(i));
114  weight = sampler.getMyWeight(i);
115  pf0 += weight * ppf(diff, lam, getPenaltyParameter(), 0);
116  }
117  sampler.sumAll(&pf0,&dev,1);
118  dev *= coeff_;
119  // Return mean plus deviation
120  return ev + dev;
121  }
122 
124  const Vector<Real> &x,
125  const std::vector<Real> &xstat,
126  Real &tol) {
127  Real val = computeValue(obj,x,tol);
128  val_ += weight_ * val;
129  computeGradient(*dualVector_,obj,x,tol);
130  g_->axpy(weight_,*dualVector_);
131  }
132 
134  std::vector<Real> &gstat,
135  const Vector<Real> &x,
136  const std::vector<Real> &xstat,
137  SampleGenerator<Real> &sampler) {
138  // Compute expected value
139  Real ev(0);
140  sampler.sumAll(&val_,&ev,1);
141  // Compute deviation
142  hv_->zero(); dualVector_->zero();
143  Real diff(0), pf(0), pf1(0), dev(0), one(1), weight(0), lam(0);
144  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
145  values_->get(diff,sampler.getMyPoint(i));
146  diff -= ev;
147  getMultiplier(lam,sampler.getMyPoint(i));
148  weight = sampler.getMyWeight(i);
149  pf1 = weight * ppf(diff, lam, getPenaltyParameter(), 1);
150  pf += pf1;
151  gradients_->get(*hv_, sampler.getMyPoint(i));
152  dualVector_->axpy(coeff_ * pf1, *hv_);
153  }
154  sampler.sumAll(&pf,&dev,1);
155  g_->scale(one - coeff_ * dev);
156  g_->plus(*dualVector_);
157  sampler.sumAll(*g_,g);
158  }
159 
161  const Vector<Real> &v,
162  const std::vector<Real> &vstat,
163  const Vector<Real> &x,
164  const std::vector<Real> &xstat,
165  Real &tol) {
166  Real val = computeValue(obj,x,tol);
167  val_ += weight_ * val;
168  Real gv = computeGradVec(*dualVector_,obj,v,x,tol);
169  gv_ += weight_ * gv;
170  g_->axpy(weight_, *dualVector_);
171  computeHessVec(*dualVector_,obj,v,x,tol);
172  hv_->axpy(weight_, *dualVector_);
173  }
174 
176  std::vector<Real> &hvstat,
177  const Vector<Real> &v,
178  const std::vector<Real> &vstat,
179  const Vector<Real> &x,
180  const std::vector<Real> &xstat,
181  SampleGenerator<Real> &sampler) {
182  const Real one(1);
183  // Compute expected value
184  std::vector<Real> mval(2), gval(2);
185  mval[0] = val_; mval[1] = gv_;
186  sampler.sumAll(&mval[0],&gval[0],2);
187  Real ev = gval[0], egv = gval[1];
188  // Compute deviation
189  std::vector<Real> mvec(3), gvec(3);
190  Real diff(0), gv(0), pf1(0), pf2(0), weight(0), lam(0);
191  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
192  values_->get(diff,sampler.getMyPoint(i));
193  gradvecs_->get(gv,sampler.getMyPoint(i));
194  getMultiplier(lam,sampler.getMyPoint(i));
195  weight = sampler.getMyWeight(i);
196  diff -= ev;
197  pf1 = ppf(diff, lam, getPenaltyParameter(), 1);
198  pf2 = ppf(diff, lam, getPenaltyParameter(), 2);
199  mvec[0] += weight * pf1;
200  mvec[1] += weight * pf2;
201  mvec[2] += weight * pf2 * gv;
202  }
203  sampler.sumAll(&mvec[0],&gvec[0],3);
204  Real c1 = one - coeff_ * gvec[0];
205  Real c2 = coeff_ * (gvec[1]*egv - gvec[2]);
206  hv_->scale(c1);
207  hv_->axpy(c2, *g_);
208  sampler.sumAll(*hv_,hv);
209 
210  dualVector_->zero(); hv_->zero(); g_->zero();
211  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
212  values_->get(diff,sampler.getMyPoint(i));
213  gradients_->get(*g_,sampler.getMyPoint(i));
214  gradvecs_->get(gv,sampler.getMyPoint(i));
215  hessvecs_->get(*dualVector_,sampler.getMyPoint(i));
216  getMultiplier(lam,sampler.getMyPoint(i));
217  weight = sampler.getMyWeight(i);
218  diff -= ev;
219  pf1 = ppf(diff, lam, getPenaltyParameter(), 1);
220  pf2 = ppf(diff, lam, getPenaltyParameter(), 2);
221  hv_->axpy(weight * coeff_ * pf2 * (gv-egv), *g_);
222  hv_->axpy(weight * coeff_ *pf1, *dualVector_);
223  }
224  sampler.sumAll(*hv_, *dualVector_);
225  hv.plus(*dualVector_);
226  }
227 };
228 
229 }
230 
231 #endif
Provides the interface to evaluate objective functions.
void computeHessVec(Vector< Real > &hv, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Ptr< Vector< Real > > g_
virtual void setHessVecStorage(const Ptr< ScalarController< Real >> &gradvec_storage, const Ptr< VectorController< Real >> &hessvec_storage)
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
virtual void plus(const Vector &x)=0
Compute , where .
Ptr< Vector< Real > > hv_
Ptr< ScalarController< Real > > values_
Real ppf(const Real x, const Real t, const Real r, const int deriv=0) const
Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
virtual std::vector< Real > getMyPoint(const int i) const
virtual Real getMyWeight(const int i) const
Ptr< Vector< Real > > dualVector_
void setHessVecStorage(const Ptr< ScalarController< Real >> &gradvec_storage, const Ptr< VectorController< Real >> &hessvec_storage)
virtual void setStorage(const Ptr< ScalarController< Real >> &value_storage, const Ptr< VectorController< Real >> &gradient_storage)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual int numMySamples(void) const
void updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
void sumAll(Real *input, Real *output, int dim) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void setStorage(const Ptr< ScalarController< Real >> &value_storage, const Ptr< VectorController< Real >> &gradient_storage)
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.
virtual void setStorage(const Ptr< ScalarController< Real >> &value_storage, const Ptr< VectorController< Real >> &gradient_storage)
void getMultiplier(Real &lam, const std::vector< Real > &pt) const
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
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 setValue(const Real val, const std::vector< Real > &pt)
Ptr< VectorController< Real > > hessvecs_
void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
virtual void setHessVecStorage(const Ptr< ScalarController< Real >> &gradvec_storage, const Ptr< VectorController< Real >> &hessvec_storage)
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.
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.
void initialize(const Vector< Real > &x)
Initialize temporary variables.
Ptr< VectorController< Real > > gradients_
Ptr< ScalarController< Real > > gradvecs_