ROL
ROL_MonteCarloGenerator.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_MONTECARLOGENERATOR_HPP
45 #define ROL_MONTECARLOGENERATOR_HPP
46 
47 #include "ROL_SampleGenerator.hpp"
48 #include "ROL_Distribution.hpp"
49 
50 namespace ROL {
51 
52 template<class Real>
53 class MonteCarloGenerator : public SampleGenerator<Real> {
54 private:
55  int nSamp_;
57  bool use_SA_;
58  bool adaptive_;
60  std::vector<std::vector<Real> > data_;
61 
62  Real sum_val_;
63  Real sum_val2_;
64  Real sum_ng_;
65  Real sum_ng2_;
66 
67  bool useDist_;
68  std::vector<Teuchos::RCP<ROL::Distribution<Real> > > dist_;
69 
70  Real ierf(Real input) {
71  std::vector<Real> coeff;
72  Real c = 1.0;
73  Real tmp = c * (std::sqrt(M_PI)/2.0 * input);
74  Real val = tmp;
75  coeff.push_back(c);
76  int cnt = 1;
77  while (std::abs(tmp) > 1.e-4*std::abs(val)) {
78  c = 0.0;
79  for ( unsigned i = 0; i < coeff.size(); i++ ) {
80  c += coeff[i]*coeff[coeff.size()-1-i]/((i+1)*(2*i+1));
81  }
82  tmp = c/(2.0*(Real)cnt+1.0) * std::pow(std::sqrt(M_PI)/2.0 * input,2.0*(Real)cnt+1.0);
83  val += tmp;
84  coeff.push_back(c);
85  cnt++;
86  }
87  return val;
88  }
89 
90  void sample(void) {
91  // Get process rank and number of processes
92  int rank = SampleGenerator<Real>::batchID();
94  // Separate samples across processes
95  int frac = nSamp_ / nProc;
96  int rem = nSamp_ % nProc;
97  unsigned N = (unsigned)frac;
98  unsigned sumN = N*(unsigned)rank;
99  for (int i = 0; i < rank; i++) {
100  if ( i < rem ) {
101  sumN++;
102  }
103  }
104  if ( rank < rem ) {
105  N++;
106  }
107  // Generate samples
108  std::vector<std::vector<Real> > pts;
109  std::vector<Real> p;
110  //srand((rank+1)*(rank+1)*time(NULL));
111  for ( unsigned i = 0; i < N; i++ ) {
112  srand(123456*(sumN + i + 1));
113  if ( !useDist_ ) {
114  p.resize(data_.size(),0.0);
115  for ( unsigned j = 0; j < data_.size(); j++ ) {
116  if ( use_normal_ ) {
117  p[j] = std::sqrt(2.0*(data_[j])[1])*ierf(2.0*((Real)rand())/((Real)RAND_MAX)-1.0) +
118  (data_[j])[0];
119  }
120  else {
121  p[j] = ((data_[j])[1]-(data_[j])[0])*((Real)rand())/((Real)RAND_MAX)+(data_[j])[0];
122  }
123  }
124  }
125  else {
126  p.resize(dist_.size(),0.0);
127  for ( unsigned j = 0; j < dist_.size(); j++ ) {
128  p[j] = (dist_[j])->invertCDF((Real)rand()/(Real)RAND_MAX);
129  while (std::abs(p[j]) > 0.1*ROL::ROL_OVERFLOW) {
130  p[j] = (dist_[j])->invertCDF((Real)rand()/(Real)RAND_MAX);
131  }
132  }
133  }
134  pts.push_back(p);
135  }
136  std::vector<Real> wts(N,1.0/((Real)nSamp_));
139  }
140 
141  std::vector<std::vector<Real> > sample(int nSamp, bool store = true) {
142  // Get process rank and number of processes
143  int rank = SampleGenerator<Real>::batchID();
144  int nProc = SampleGenerator<Real>::numBatches();
145  // Separate samples across processes
146  int frac = nSamp / nProc;
147  int rem = nSamp % nProc;
148  unsigned N = (unsigned)frac;
149  unsigned sumN = N*(unsigned)rank;
150  for (int i = 0; i < rank; i++) {
151  if ( i < rem ) {
152  sumN++;
153  }
154  }
155  if ( rank < rem ) {
156  N++;
157  }
158  // Generate samples
159  std::vector<std::vector<Real> > pts;
160  std::vector<Real> p;
161  //srand((rank+1)*(rank+1)*time(NULL));
162  for ( unsigned i = 0; i < N; i++ ) {
163  srand(123456*(sumN + i + 1));
164  if ( !useDist_ ) {
165  p.resize(data_.size(),0.0);
166  for ( unsigned j = 0; j < data_.size(); j++ ) {
167  if ( use_normal_ ) {
168  p[j] = std::sqrt(2.0*(data_[j])[1])*ierf(2.0*((Real)rand())/((Real)RAND_MAX)-1.0) +
169  (data_[j])[0];
170  }
171  else {
172  p[j] = ((data_[j])[1]-(data_[j])[0])*((Real)rand())/((Real)RAND_MAX)+(data_[j])[0];
173  }
174  }
175  }
176  else {
177  p.resize(dist_.size(),0.0);
178  for ( unsigned j = 0; j < dist_.size(); j++ ) {
179  p[j] = (dist_[j])->invertCDF((Real)rand()/(Real)RAND_MAX);
180  while (std::abs(p[j]) > 0.1*ROL::ROL_OVERFLOW) {
181  p[j] = (dist_[j])->invertCDF((Real)rand()/(Real)RAND_MAX);
182  }
183  }
184  }
185  pts.push_back(p);
186  }
187  if ( store ) {
188  std::vector<Real> wts(N,1.0/((Real)nSamp));
191  }
192  return pts;
193  }
194 
195 public:
196  MonteCarloGenerator( int nSamp, std::vector<Teuchos::RCP<Distribution<Real> > > &dist,
197  Teuchos::RCP<BatchManager<Real> > &bman,
198  bool use_SA = false, bool adaptive = false, int numNewSamps = 0 )
199  : SampleGenerator<Real>(bman), nSamp_(nSamp), use_normal_(false), use_SA_(use_SA), adaptive_(adaptive),
200  numNewSamps_(numNewSamps), sum_val_(0.0), sum_val2_(0.0), sum_ng_(0.0), sum_ng2_(0.0),
201  useDist_(true), dist_(dist) {
202  sample();
203  }
204 
205  MonteCarloGenerator( int nSamp, std::vector<std::vector<Real> > &bounds,
206  Teuchos::RCP<BatchManager<Real> > &bman,
207  bool use_SA = false, bool adaptive = false, int numNewSamps = 0 )
208  : SampleGenerator<Real>(bman), nSamp_(nSamp), use_normal_(false), use_SA_(use_SA), adaptive_(adaptive),
209  numNewSamps_(numNewSamps), sum_val_(0.0), sum_val2_(0.0), sum_ng_(0.0), sum_ng2_(0.0),
210  useDist_(false) {
211  unsigned dim = bounds.size();
212  data_.clear();
213  Real tmp = 0.0;
214  for ( unsigned j = 0; j < dim; j++ ) {
215  if ( (bounds[j])[0] > (bounds[j])[1] ) {
216  tmp = (bounds[j])[0];
217  (bounds[j])[0] = (bounds[j])[1];
218  (bounds[j])[1] = tmp;
219  data_.push_back(bounds[j]);
220  }
221  data_.push_back(bounds[j]);
222  }
223  sample();
224  }
225 
226  MonteCarloGenerator( int nSamp, std::vector<Real> mean, std::vector<Real> std,
227  Teuchos::RCP<BatchManager<Real> > &bman,
228  bool use_SA = false, bool adaptive = false, int numNewSamps = 0 )
229  : SampleGenerator<Real>(bman), nSamp_(nSamp), use_normal_(true), use_SA_(use_SA), adaptive_(adaptive),
230  numNewSamps_(numNewSamps), sum_val_(0.0), sum_val2_(0.0), sum_ng_(0.0), sum_ng2_(0.0),
231  useDist_(false) {
232  unsigned dim = mean.size();
233  data_.clear();
234  std::vector<Real> tmp(2,0.0);
235  for ( unsigned j = 0; j < dim; j++ ) {
236  tmp[0] = mean[j];
237  tmp[1] = std[j];
238  data_.push_back(tmp);
239  }
240  sample();
241  }
242 
243  void update( const Vector<Real> &x ) {
245  sum_val_ = 0.0;
246  sum_val2_ = 0.0;
247  sum_ng_ = 0.0;
248  sum_ng_ = 0.0;
249  if ( use_SA_ ) {
250  sample();
251  }
252  }
253 
254  Real computeError( std::vector<Real> &vals ) {
255  if ( adaptive_ && !use_SA_ ) {
256  // Compute unbiased sample variance
257  int cnt = 0;
258  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
259  sum_val_ += vals[cnt];
260  sum_val2_ += vals[cnt]*vals[cnt];
261  cnt++;
262  }
263  Real mymean = sum_val_ / nSamp_;
264  Real mean = 0.0;
265  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
266 
267  Real myvar = (sum_val2_ - mean*mean)/(nSamp_-1.0);
268  Real var = 0.0;
269  SampleGenerator<Real>::sumAll(&myvar,&var,1);
270  // Return Monte Carlo error
271  vals.clear();
272  return std::sqrt(var/(nSamp_))*1.e-8;
273  }
274  else {
275  vals.clear();
276  return 0.0;
277  }
278  }
279 
280  Real computeError( std::vector<Teuchos::RCP<Vector<Real> > > &vals, const Vector<Real> &x ) {
281  if ( adaptive_ && !use_SA_ ) {
282  // Compute unbiased sample variance
283  int cnt = 0;
284  Real ng = 0.0;
285  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
286  ng = (vals[cnt])->norm();
287  sum_ng_ += ng;
288  sum_ng2_ += ng*ng;
289  cnt++;
290  }
291  Real mymean = sum_ng_ / nSamp_;
292  Real mean = 0.0;
293  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
294 
295  Real myvar = (sum_ng2_ - mean*mean)/(nSamp_-1.0);
296  Real var = 0.0;
297  SampleGenerator<Real>::sumAll(&myvar,&var,1);
298  // Return Monte Carlo error
299  vals.clear();
300  return std::sqrt(var/(nSamp_))*1.e-4;
301  }
302  else {
303  vals.clear();
304  return 0.0;
305  }
306  }
307 
308  void refine(void) {
309  if ( adaptive_ && !use_SA_ ) {
310  std::vector<std::vector<Real> > pts;
311  std::vector<Real> pt(data_.size(),0.0);
312  for ( int i = 0; i < SampleGenerator<Real>::numMySamples(); i++ ) {
314  pts.push_back(pt);
315  }
316  std::vector<std::vector<Real> > pts_new = sample(numNewSamps_,false);
317  pts.insert(pts.end(),pts_new.begin(),pts_new.end());
318  nSamp_ += numNewSamps_;
319  std::vector<Real> wts(pts.size(),1.0/((Real)nSamp_));
323  }
324  }
325 
326 };
327 
328 }
329 
330 #endif
virtual void update(const Vector< Real > &x)
virtual std::vector< Real > getMyPoint(const int i) const
Real computeError(std::vector< Teuchos::RCP< Vector< Real > > > &vals, const Vector< Real > &x)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void sumAll(Real *input, Real *output, int dim) const
Real computeError(std::vector< Real > &vals)
std::vector< std::vector< Real > > sample(int nSamp, bool store=true)
virtual void refine(void)
MonteCarloGenerator(int nSamp, std::vector< std::vector< Real > > &bounds, Teuchos::RCP< BatchManager< Real > > &bman, bool use_SA=false, bool adaptive=false, int numNewSamps=0)
std::vector< Teuchos::RCP< ROL::Distribution< Real > > > dist_
MonteCarloGenerator(int nSamp, std::vector< Teuchos::RCP< Distribution< Real > > > &dist, Teuchos::RCP< BatchManager< Real > > &bman, bool use_SA=false, bool adaptive=false, int numNewSamps=0)
MonteCarloGenerator(int nSamp, std::vector< Real > mean, std::vector< Real > std, Teuchos::RCP< BatchManager< Real > > &bman, bool use_SA=false, bool adaptive=false, int numNewSamps=0)
void setPoints(std::vector< std::vector< Real > > &p)
static const double ROL_OVERFLOW
Platform-dependent maximum double.
Definition: ROL_Types.hpp:126
void update(const Vector< Real > &x)
void setWeights(std::vector< Real > &w)
std::vector< std::vector< Real > > data_