ROL
ROL_PD_HMCR2.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_PD_HMCR2_HPP
45 #define ROL_PD_HMCR2_HPP
46 
48 #include "ROL_Types.hpp"
49 
50 namespace ROL {
51 
52 template<class Real>
53 class PD_HMCR2 : public PD_RandVarFunctional<Real> {
54 private:
55  Real beta_;
57 
58  Ptr<SampledScalar<Real>> values_;
59  Ptr<SampledScalar<Real>> gradvecs_;
60  Ptr<SampledVector<Real>> gradients_;
61  Ptr<SampledVector<Real>> hessvecs_;
62 
68 
71 
76 
81 
82  void initializeStorage(void) {
83  values_ = makePtr<SampledScalar<Real>>();
84  gradvecs_ = makePtr<SampledScalar<Real>>();
85  gradients_ = makePtr<SampledVector<Real>>();
86  hessvecs_ = makePtr<SampledVector<Real>>();
87 
89  RandVarFunctional<Real>::setHessVecStorage(gradvecs_,hessvecs_);
90  }
91 
92  void clear(void) {
93  gradvecs_->update();
94  hessvecs_->update();
95  }
96 
97  void checkInputs(void) {
98  Real zero(0), one(1);
99  ROL_TEST_FOR_EXCEPTION((beta_ < zero) || (beta_ >= one), std::invalid_argument,
100  ">>> ERROR (ROL::PD_HMCR2): Confidence parameter beta is out of range!");
102  }
103 
104 public:
105  PD_HMCR2(const Real beta)
106  : PD_RandVarFunctional<Real>(), beta_(beta) {
107  checkInputs();
108  }
109 
110  void setStorage(const Ptr<SampledScalar<Real>> &value_storage,
111  const Ptr<SampledVector<Real>> &gradient_storage) {
112  values_ = value_storage;
113  gradients_ = gradient_storage;
115  }
116 
117  void setHessVecStorage(const Ptr<SampledScalar<Real>> &gradvec_storage,
118  const Ptr<SampledVector<Real>> &hessvec_storage) {
119  gradvecs_ = gradvec_storage;
120  hessvecs_ = hessvec_storage;
122  }
123 
125  const Real zero(0), two(2);
126  Real val(0), lold(0), lnew(0), mdiff(0), gdiff(0), sum(0), gsum(0);
127  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
128  values_->get(val, sampler.getMyPoint(i));
129  getMultiplier(lold, sampler.getMyPoint(i));
130  lnew = std::max(zero, getPenaltyParameter()*val+lold);
131  sum += sampler.getMyWeight(i) * std::pow(lnew,two);
132  }
133  sampler.sumAll(&sum,&gsum,1);
134  gsum = std::sqrt(gsum);
135  for (int i = sampler.start(); i < sampler.numMySamples(); ++i) {
136  values_->get(val, sampler.getMyPoint(i));
137  getMultiplier(lold, sampler.getMyPoint(i));
138  lnew = std::max(zero, getPenaltyParameter()*val+lold)/gsum;
139  mdiff += sampler.getMyWeight(i) * std::pow(lnew-lold,2);
140  setMultiplier(lnew, sampler.getMyPoint(i));
141  }
142  sampler.sumAll(&mdiff,&gdiff,1);
143  gdiff = std::sqrt(gdiff);
144  return gdiff;
145  }
146 
147  void initialize(const Vector<Real> &x) {
149  mScalar1_ = static_cast<Real>(0);
150  mScalar2_ = static_cast<Real>(0);
151  clear();
152  }
153 
155  const Vector<Real> &x,
156  const std::vector<Real> &xstat,
157  Real &tol) {
158  const Real zero(0), two(2);
159  Real lam(0);
160  getMultiplier(lam, point_);
161  Real val = computeValue(obj, x, tol);
162  Real arg = val - xstat[0];
163  Real pf = std::max(zero, arg + lam/getPenaltyParameter());
164  val_ += weight_ * std::pow(pf,two);
165  setValue(arg, point_);
166  }
167 
168  Real getValue(const Vector<Real> &x,
169  const std::vector<Real> &xstat,
170  SampleGenerator<Real> &sampler) {
171  const Real half(0.5), one(1);
172  Real ev(0);
173  sampler.sumAll(&val_, &ev, 1);
174  Real norm = std::sqrt(ev);
175  Real sig = one/(one-beta_);
176  Real val = (norm <= sig/getPenaltyParameter()
177  ? half * getPenaltyParameter() * ev
178  : sig * (norm - sig*half/getPenaltyParameter()));
179  return xstat[0] + val;
180  }
181 
183  const Vector<Real> &x,
184  const std::vector<Real> &xstat,
185  Real &tol) {
186  const Real zero(0), two(2);
187  Real lam(0);
188  getMultiplier(lam, point_);
189  Real val = computeValue(obj, x, tol);
190  Real arg = val - xstat[0];
191  Real pf = std::max(zero, arg + lam/getPenaltyParameter());
192  if ( pf > zero ) {
193  val_ += weight_ * pf;
194  gv_ += weight_ * std::pow(pf,two);
195  computeGradient(*dualVector_, obj, x, tol);
196  g_->axpy(weight_ * pf, *dualVector_);
197  }
198  }
199 
201  std::vector<Real> &gstat,
202  const Vector<Real> &x,
203  const std::vector<Real> &xstat,
204  SampleGenerator<Real> &sampler) {
205  const Real one(1);
206  std::vector<Real> mv = {val_, gv_};
207  std::vector<Real> ev(2,0);
208  sampler.sumAll(&mv[0], &ev[0], 2);
209  Real norm = std::sqrt(ev[1]);
210  Real sig = one/(one-beta_);
211  Real scal = (norm <= sig/getPenaltyParameter()
213  : sig/norm);
214  gstat[0] = one - scal * ev[0];
215  sampler.sumAll(*g_, g);
216  g.scale(scal);
217  }
218 
220  const Vector<Real> &v,
221  const std::vector<Real> &vstat,
222  const Vector<Real> &x,
223  const std::vector<Real> &xstat,
224  Real &tol) {
225  const Real zero(0), two(2);
226  Real lam(0);
227  getMultiplier(lam, point_);
228  Real val = computeValue(obj, x, tol);
229  Real arg = val - xstat[0];
230  Real pf = std::max(zero, arg + lam/getPenaltyParameter());
231  if ( pf > zero ) {
232  val_ += weight_ * std::pow(pf,two);
233  mScalar1_ += weight_ * pf;
234 
235  Real gv = computeGradVec(*dualVector_, obj, v, x, tol);
236  mScalar2_ += weight_ * pf * gv;
237  gv_ += weight_ * (vstat[0] - gv);
238  g_->axpy(weight_ * pf, *dualVector_);
239  hv_->axpy(weight_ * (gv - vstat[0]), *dualVector_);
240  computeHessVec(*dualVector_, obj, v, x, tol);
241  hv_->axpy(weight_ * pf, *dualVector_);
242  }
243  }
244 
246  std::vector<Real> &hvstat,
247  const Vector<Real> &v,
248  const std::vector<Real> &vstat,
249  const Vector<Real> &x,
250  const std::vector<Real> &xstat,
251  SampleGenerator<Real> &sampler) {
252  const Real one(1);
253  std::vector<Real> mv = {val_, gv_, mScalar1_, mScalar2_};
254  std::vector<Real> ev(4,0);
255  sampler.sumAll(&mv[0],&ev[0],4);
256  Real norm = std::sqrt(ev[0]);
257  Real sig = one/(one-beta_);
258  Real scal = (norm <= sig/getPenaltyParameter()
260  : sig/norm);
261  hvstat[0] = scal * ev[1];
262  sampler.sumAll(*hv_,hv);
263  hv.scale(scal);
264  if (norm > sig/getPenaltyParameter()) {
265  Real norm3 = ev[0]*norm;
266  hvstat[0] += sig/norm3 * (ev[3] - ev[2]*vstat[0]) * ev[2];
267  dualVector_->zero();
268  sampler.sumAll(*g_,*dualVector_);
269  hv.axpy(sig/norm3 * (ev[2]*vstat[0] - ev[3]),*dualVector_);
270  }
271  }
272 };
273 
274 }
275 
276 #endif
virtual void setHessVecStorage(const Ptr< SampledScalar< Real >> &gradvec_storage, const Ptr< SampledVector< Real >> &hessvec_storage)
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)
virtual void scale(const Real alpha)=0
Compute where .
Ptr< Vector< Real > > g_
Real computeValue(Objective< Real > &obj, const Vector< Real > &x, Real &tol)
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
Ptr< Vector< Real > > hv_
Ptr< SampledVector< Real > > gradients_
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.
Contains definitions of custom data types in ROL.
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 setMultiplier(Real &lam, const std::vector< Real > &pt)
Ptr< SampledVector< Real > > hessvecs_
virtual std::vector< Real > getMyPoint(const int i) const
virtual Real getMyWeight(const int i) const
Ptr< Vector< Real > > dualVector_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual int numMySamples(void) const
Ptr< SampledScalar< Real > > gradvecs_
void sumAll(Real *input, Real *output, int dim) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Real computeDual(SampleGenerator< Real > &sampler)
PD_HMCR2(const Real beta)
virtual void setStorage(const Ptr< SampledScalar< Real >> &value_storage, const Ptr< SampledVector< Real >> &gradient_storage)
void checkInputs(void)
void initializeStorage(void)
void initialize(const Vector< Real > &x)
Initialize temporary variables.
void setStorage(const Ptr< SampledScalar< Real >> &value_storage, const Ptr< SampledVector< Real >> &gradient_storage)
virtual void setStorage(const Ptr< SampledScalar< Real >> &value_storage, const Ptr< SampledVector< Real >> &gradient_storage)
Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
void getMultiplier(Real &lam, const std::vector< Real > &pt) const
void computeGradient(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &x, Real &tol)
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 updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
void clear(void)
void setHessVecStorage(const Ptr< SampledScalar< Real >> &gradvec_storage, const Ptr< SampledVector< Real >> &hessvec_storage)
Real computeGradVec(Vector< Real > &g, Objective< Real > &obj, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Ptr< SampledScalar< Real > > values_
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)
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< SampledScalar< Real >> &gradvec_storage, const Ptr< SampledVector< Real >> &hessvec_storage)