ROL
ROL_PointwiseCDFObjective.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_POINTWISECDFOBJECTIVE_H
11 #define ROL_POINTWISECDFOBJECTIVE_H
12 
13 #include "ROL_Objective.hpp"
14 #include "ROL_BatchManager.hpp"
15 #include "ROL_Vector.hpp"
16 #include "ROL_Distribution.hpp"
17 #include "ROL_Ptr.hpp"
18 #include <math.h>
19 
20 namespace ROL {
21 
22 template <class Real>
23 class PointwiseCDFObjective : public Objective<Real> {
24 private:
25  std::vector<ROL::Ptr<Distribution<Real> > > dist_;
26  ROL::Ptr<BatchManager<Real> > bman_;
27  const Real scale_;
28  const Real sqrt2_;
29  const Real sqrtpi_;
30 
31  Real valueCDF(const int dim, const Real loc, const SROMVector<Real> &x) const {
32  const int numSamples = x.getNumSamples();
33  Real val = 0., hs = 0., xpt = 0., xwt = 0.;
34  for (int k = 0; k < numSamples; k++) {
35  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
36  hs = 0.5 * (1. + erf((loc-xpt)/(sqrt2_*scale_)));
37  val += xwt * hs;
38  }
39  return val;
40  }
41 
42  Real gradientCDF(std::vector<Real> &gradx, std::vector<Real> &gradp,
43  const int dim, const Real loc, const SROMVector<Real> &x) const {
44  const int numSamples = x.getNumSamples();
45  gradx.resize(numSamples,0.); gradp.resize(numSamples,0.);
46  Real val = 0., hs = 0., xpt = 0., xwt = 0.;
47  for (int k = 0; k < numSamples; k++) {
48  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
49  hs = 0.5 * (1. + erf((loc-xpt)/(sqrt2_*scale_)));
50  val += xwt * hs;
51  gradx[k] = -(xwt/(sqrt2_*sqrtpi_*scale_))
52  * std::exp(-std::pow((loc-xpt)/(sqrt2_*scale_),2));
53  gradp[k] = hs;
54  }
55  return val;
56  }
57 
58  Real hessVecCDF(std::vector<Real> &hvx,
59  const int dim, const Real loc, const SROMVector<Real> &x, const SROMVector<Real> &v) const {
60  const int numSamples = x.getNumSamples();
61  hvx.resize(numSamples,0.);
62  Real val = 0., hs = 0., xpt = 0., xwt = 0., scale3 = std::pow(scale_,3);
63  for (int k = 0; k < numSamples; k++) {
64  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
65  hs = 0.5 * (1. + erf((loc-xpt)/(sqrt2_*scale_)));
66  val += xwt * hs;
67  hvx[k] = -(xwt/(sqrt2_*sqrtpi_*scale3))
68  * std::exp(-std::pow((loc-xpt)/(sqrt2_*scale_),2)) * (loc-xpt);
69  }
70  return val;
71  }
72 
73 public:
74  PointwiseCDFObjective(const std::vector<ROL::Ptr<Distribution<Real> > > &dist,
75  ROL::Ptr<BatchManager<Real> > &bman,
76  const Real scale = 1.e-2)
77  : Objective<Real>(), dist_(dist), bman_(bman), scale_(scale),
78  sqrt2_(std::sqrt(2.)), sqrtpi_(std::sqrt(ROL::ScalarTraits<Real>::pi())) {}
79 
80  Real value( const Vector<Real> &x, Real &tol ) {
81  const SROMVector<Real> &ex = dynamic_cast<const SROMVector<Real>&>(x);
82  const int dimension = ex.getDimension();
83  const int numSamples = ex.getNumSamples();
84  Real val = 0., diff = 0., xpt = 0., sum = 0.;
85  for (int d = 0; d < dimension; d++) {
86  for (int k = 0; k < numSamples; k++) {
87  xpt = (*ex.getPoint(k))[d];
88  diff = (valueCDF(d,xpt,ex)-dist_[d]->evaluateCDF(xpt));
89  val += std::pow(diff,2);
90  }
91  }
92  bman_->sumAll(&val,&sum,1);
93  return 0.5*sum;
94  }
95 
96  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
97  SROMVector<Real> &eg = dynamic_cast<SROMVector<Real>&>(g);
98  const SROMVector<Real> &ex = dynamic_cast<const SROMVector<Real>&>(x);
99  const int dimension = ex.getDimension();
100  const int numSamples = ex.getNumSamples();
101  std::vector<Real> gradx(numSamples,0.), gradp(numSamples,0.);
102  Real diff = 0., xpt = 0., val = 0., sum = 0.;
103  std::vector<Real> val_wt(numSamples,0.), tmp(dimension,0.);
104  std::vector<std::vector<Real> > val_pt(numSamples,tmp);
105  for (int d = 0; d < dimension; d++) {
106  for (int k = 0; k < numSamples; k++) {
107  xpt = (*ex.getPoint(k))[d];
108  val = gradientCDF(gradx,gradp,d,xpt,ex);
109  diff = (val-dist_[d]->evaluateCDF(xpt));
110  sum = 0.;
111  for (int j = 0; j < numSamples; j++) {
112  (val_pt[j])[d] += diff * gradx[j];
113  val_wt[j] += diff * gradp[j];
114  sum -= gradx[j];
115  }
116  (val_pt[k])[d] += diff * (sum - dist_[d]->evaluatePDF(xpt));
117  }
118  }
119  for (int k = 0; k < numSamples; k++) {
120  eg.setPoint(k,val_pt[k]);
121  eg.setWeight(k,val_wt[k]);
122  }
123  }
124 
125 // void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
126 // }
127 }; // class LinearCombinationObjective
128 
129 } // namespace ROL
130 
131 #endif
Provides the interface to evaluate objective functions.
Real valueCDF(const int dim, const Real loc, const SROMVector< Real > &x) const
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
PointwiseCDFObjective(const std::vector< ROL::Ptr< Distribution< Real > > > &dist, ROL::Ptr< BatchManager< Real > > &bman, const Real scale=1.e-2)
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Real hessVecCDF(std::vector< Real > &hvx, const int dim, const Real loc, const SROMVector< Real > &x, const SROMVector< Real > &v) const
Provides the std::vector implementation of the ROL::Vector interface.
ROL::Ptr< BatchManager< Real > > bman_
Real gradientCDF(std::vector< Real > &gradx, std::vector< Real > &gradp, const int dim, const Real loc, const SROMVector< Real > &x) const
constexpr auto dim
std::vector< ROL::Ptr< Distribution< Real > > > dist_