ROL
ROL_PointwiseCDFObjective.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_POINTWISECDFOBJECTIVE_H
45 #define ROL_POINTWISECDFOBJECTIVE_H
46 
47 #include "ROL_Objective.hpp"
48 #include "ROL_Vector.hpp"
49 #include "ROL_Distribution.hpp"
50 #include "Teuchos_RCP.hpp"
51 #include <math.h>
52 
53 namespace ROL {
54 
55 template <class Real>
56 class PointwiseCDFObjective : public Objective<Real> {
57 private:
58  std::vector<Teuchos::RCP<Distribution<Real> > > dist_;
59  const Real scale_;
60  const Real sqrt2_;
61  const Real sqrtpi_;
62 
63  Real valueCDF(const size_t dim, const Real loc, const SROMVector<Real> &x) const {
64  const size_t numSamples = x.getNumSamples();
65  Real val = 0., hs = 0., xpt = 0., xwt = 0.;
66  for (size_t k = 0; k < numSamples; k++) {
67  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
68  hs = 0.5 * (1. + erf((loc-xpt)/(sqrt2_*scale_)));
69  val += xwt * hs;
70  }
71  return val;
72  }
73 
74  Real gradientCDF(std::vector<Real> &gradx, std::vector<Real> &gradp,
75  const size_t dim, const Real loc, const SROMVector<Real> &x) const {
76  const size_t numSamples = x.getNumSamples();
77  gradx.resize(numSamples,0.); gradp.resize(numSamples,0.);
78  Real val = 0., hs = 0., xpt = 0., xwt = 0.;
79  for (size_t k = 0; k < numSamples; k++) {
80  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
81  hs = 0.5 * (1. + erf((loc-xpt)/(sqrt2_*scale_)));
82  val += xwt * hs;
83  gradx[k] = -(xwt/(sqrt2_*sqrtpi_*scale_))
84  * std::exp(-std::pow((loc-xpt)/(sqrt2_*scale_),2));
85  gradp[k] = hs;
86  }
87  return val;
88  }
89 
90  Real hessVecCDF(std::vector<Real> &hvx,
91  const size_t dim, const Real loc, const SROMVector<Real> &x, const SROMVector<Real> &v) const {
92  const size_t numSamples = x.getNumSamples();
93  hvx.resize(numSamples,0.);
94  Real val = 0., hs = 0., xpt = 0., xwt = 0., scale3 = std::pow(scale_,3);
95  for (size_t k = 0; k < numSamples; k++) {
96  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
97  hs = 0.5 * (1. + erf((loc-xpt)/(sqrt2_*scale_)));
98  val += xwt * hs;
99  hvx[k] = -(xwt/(sqrt2_*sqrtpi_*scale3))
100  * std::exp(-std::pow((loc-xpt)/(sqrt2_*scale_),2)) * (loc-xpt);
101  }
102  return val;
103  }
104 
105 public:
106  PointwiseCDFObjective(const std::vector<Teuchos::RCP<Distribution<Real> > > &dist,
107  const Real scale = 1.e-2)
108  : Objective<Real>(), dist_(dist), scale_(scale),
109  sqrt2_(std::sqrt(2.)), sqrtpi_(std::sqrt(M_PI)) {}
110 
111  Real value( const Vector<Real> &x, Real &tol ) {
112  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
113  const size_t dimension = ex.getDimension();
114  const size_t numSamples = ex.getNumSamples();
115  Real val = 0., diff = 0., xpt = 0.;
116  for (size_t d = 0; d < dimension; d++) {
117  for (size_t k = 0; k < numSamples; k++) {
118  xpt = (*ex.getPoint(k))[d];
119  diff = (valueCDF(d,xpt,ex)-dist_[d]->evaluateCDF(xpt));
120  val += std::pow(diff,2);
121  }
122  }
123  return 0.5*val;
124  }
125 
126  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
127  SROMVector<Real> &eg = Teuchos::dyn_cast<SROMVector<Real> >(g);
128  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
129  const size_t dimension = ex.getDimension();
130  const size_t numSamples = ex.getNumSamples();
131  std::vector<Real> gradx(numSamples,0.), gradp(numSamples,0.);
132  Real diff = 0., xpt = 0., val = 0., sum = 0.;
133  std::vector<Real> val_wt(numSamples,0.), tmp(dimension,0.);
134  std::vector<std::vector<Real> > val_pt(numSamples,tmp);
135  for (size_t d = 0; d < dimension; d++) {
136  for (size_t k = 0; k < numSamples; k++) {
137  xpt = (*ex.getPoint(k))[d];
138  val = gradientCDF(gradx,gradp,d,xpt,ex);
139  diff = (val-dist_[d]->evaluateCDF(xpt));
140  sum = 0.;
141  for (size_t j = 0; j < numSamples; j++) {
142  (val_pt[j])[d] += diff * gradx[j];
143  val_wt[j] += diff * gradp[j];
144  sum -= gradx[j];
145  }
146  (val_pt[k])[d] += diff * (sum - dist_[d]->evaluatePDF(xpt));
147  }
148  }
149  for (size_t k = 0; k < numSamples; k++) {
150  eg.setPoint(k,val_pt[k]);
151  eg.setWeight(k,val_wt[k]);
152  }
153  }
154 
155 // void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
156 // }
157 }; // class LinearCombinationObjective
158 
159 } // namespace ROL
160 
161 #endif
Provides the interface to evaluate objective functions.
Teuchos::RCP< const std::vector< Element > > getPoint(const size_t i) const
const size_t getDimension(void) const
const Element getWeight(const size_t i) const
void setWeight(const size_t i, const Element wt)
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:74
Real value(const Vector< Real > &x, Real &tol)
Compute value.
const size_t getNumSamples(void) const
std::vector< Teuchos::RCP< Distribution< Real > > > dist_
Provides the std::vector implementation of the ROL::Vector interface.
Real valueCDF(const size_t dim, const Real loc, const SROMVector< Real > &x) const
Real gradientCDF(std::vector< Real > &gradx, std::vector< Real > &gradp, const size_t dim, const Real loc, const SROMVector< Real > &x) const
PointwiseCDFObjective(const std::vector< Teuchos::RCP< Distribution< Real > > > &dist, const Real scale=1.e-2)
Real hessVecCDF(std::vector< Real > &hvx, const size_t dim, const Real loc, const SROMVector< Real > &x, const SROMVector< Real > &v) const
void setPoint(const size_t i, const std::vector< Element > &pt)