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_;
56  const bool use_normal_;
57  const bool use_SA_;
58  const bool adaptive_;
59  const int numNewSamps_;
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  const bool useDist_;
68  const std::vector<Teuchos::RCP<ROL::Distribution<Real> > > dist_;
69 
70  Real ierf(Real input) const {
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<Real>()) {
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<Real>()) {
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(const int nSamp,
197  const std::vector<Teuchos::RCP<Distribution<Real> > > &dist,
198  const Teuchos::RCP<BatchManager<Real> > &bman,
199  const bool use_SA = false,
200  const bool adaptive = false,
201  const int numNewSamps = 0)
202  : SampleGenerator<Real>(bman),
203  nSamp_(nSamp),
204  use_normal_(false),
205  use_SA_(use_SA),
206  adaptive_(adaptive),
207  numNewSamps_(numNewSamps),
208  sum_val_(0.0),
209  sum_val2_(0.0),
210  sum_ng_(0.0),
211  sum_ng2_(0.0),
212  useDist_(true),
213  dist_(dist) {
214  int nProc = SampleGenerator<Real>::numBatches();
215  TEUCHOS_TEST_FOR_EXCEPTION( nSamp_ < nProc, std::invalid_argument,
216  ">>> ERROR (ROL::MonteCarloGenerator): Total number of samples is less than the number of batches!");
217  sample();
218  }
219 
220  MonteCarloGenerator(const int nSamp,
221  std::vector<std::vector<Real> > &bounds,
222  const Teuchos::RCP<BatchManager<Real> > &bman,
223  const bool use_SA = false,
224  const bool adaptive = false,
225  const int numNewSamps = 0)
226  : SampleGenerator<Real>(bman),
227  nSamp_(nSamp),
228  use_normal_(false),
229  use_SA_(use_SA),
230  adaptive_(adaptive),
231  numNewSamps_(numNewSamps),
232  sum_val_(0.0),
233  sum_val2_(0.0),
234  sum_ng_(0.0),
235  sum_ng2_(0.0),
236  useDist_(false) {
237  int nProc = SampleGenerator<Real>::numBatches();
238  TEUCHOS_TEST_FOR_EXCEPTION( nSamp_ < nProc, std::invalid_argument,
239  ">>> ERROR (ROL::MonteCarloGenerator): Total number of samples is less than the number of batches!");
240  unsigned dim = bounds.size();
241  data_.clear();
242  Real tmp = 0.0;
243  for ( unsigned j = 0; j < dim; j++ ) {
244  if ( (bounds[j])[0] > (bounds[j])[1] ) {
245  tmp = (bounds[j])[0];
246  (bounds[j])[0] = (bounds[j])[1];
247  (bounds[j])[1] = tmp;
248  data_.push_back(bounds[j]);
249  }
250  data_.push_back(bounds[j]);
251  }
252  sample();
253  }
254 
255  MonteCarloGenerator(const int nSamp,
256  const std::vector<Real> &mean,
257  const std::vector<Real> &std,
258  const Teuchos::RCP<BatchManager<Real> > &bman,
259  const bool use_SA = false,
260  const bool adaptive = false,
261  const int numNewSamps = 0 )
262  : SampleGenerator<Real>(bman),
263  nSamp_(nSamp),
264  use_normal_(true),
265  use_SA_(use_SA),
266  adaptive_(adaptive),
267  numNewSamps_(numNewSamps),
268  sum_val_(0.0),
269  sum_val2_(0.0),
270  sum_ng_(0.0),
271  sum_ng2_(0.0),
272  useDist_(false) {
273  int nProc = SampleGenerator<Real>::numBatches();
274  TEUCHOS_TEST_FOR_EXCEPTION( nSamp_ < nProc, std::invalid_argument,
275  ">>> ERROR (ROL::MonteCarloGenerator): Total number of samples is less than the number of batches!");
276  unsigned dim = mean.size();
277  data_.clear();
278  std::vector<Real> tmp(2,0.0);
279  for ( unsigned j = 0; j < dim; j++ ) {
280  tmp[0] = mean[j];
281  tmp[1] = std[j];
282  data_.push_back(tmp);
283  }
284  sample();
285  }
286 
287  void update( const Vector<Real> &x ) {
289  sum_val_ = 0.0;
290  sum_val2_ = 0.0;
291  sum_ng_ = 0.0;
292  sum_ng_ = 0.0;
293  if ( use_SA_ ) {
294  sample();
295  }
296  }
297 
298  Real computeError( std::vector<Real> &vals ) {
299  if ( adaptive_ && !use_SA_ ) {
300  // Compute unbiased sample variance
301  int cnt = 0;
302  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
303  sum_val_ += vals[cnt];
304  sum_val2_ += vals[cnt]*vals[cnt];
305  cnt++;
306  }
307  Real mymean = sum_val_ / nSamp_;
308  Real mean = 0.0;
309  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
310 
311  Real myvar = (sum_val2_ - mean*mean)/(nSamp_-1.0);
312  Real var = 0.0;
313  SampleGenerator<Real>::sumAll(&myvar,&var,1);
314  // Return Monte Carlo error
315  vals.clear();
316  return std::sqrt(var/(nSamp_))*1.e-8;
317  }
318  else {
319  vals.clear();
320  return 0.0;
321  }
322  }
323 
324  Real computeError( std::vector<Teuchos::RCP<Vector<Real> > > &vals, const Vector<Real> &x ) {
325  if ( adaptive_ && !use_SA_ ) {
326  // Compute unbiased sample variance
327  int cnt = 0;
328  Real ng = 0.0;
329  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
330  ng = (vals[cnt])->norm();
331  sum_ng_ += ng;
332  sum_ng2_ += ng*ng;
333  cnt++;
334  }
335  Real mymean = sum_ng_ / nSamp_;
336  Real mean = 0.0;
337  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
338 
339  Real myvar = (sum_ng2_ - mean*mean)/(nSamp_-1.0);
340  Real var = 0.0;
341  SampleGenerator<Real>::sumAll(&myvar,&var,1);
342  // Return Monte Carlo error
343  vals.clear();
344  return std::sqrt(var/(nSamp_))*1.e-4;
345  }
346  else {
347  vals.clear();
348  return 0.0;
349  }
350  }
351 
352  void refine(void) {
353  if ( adaptive_ && !use_SA_ ) {
354  std::vector<std::vector<Real> > pts;
355  std::vector<Real> pt(data_.size(),0.0);
356  for ( int i = 0; i < SampleGenerator<Real>::numMySamples(); i++ ) {
358  pts.push_back(pt);
359  }
360  std::vector<std::vector<Real> > pts_new = sample(numNewSamps_,false);
361  pts.insert(pts.end(),pts_new.begin(),pts_new.end());
362  nSamp_ += numNewSamps_;
363  std::vector<Real> wts(pts.size(),1.0/((Real)nSamp_));
367  }
368  }
369 
370 };
371 
372 }
373 
374 #endif
virtual std::vector< Real > getMyPoint(const int i) const
virtual void update(const Vector< Real > &x)
MonteCarloGenerator(const int nSamp, const std::vector< Teuchos::RCP< Distribution< Real > > > &dist, const Teuchos::RCP< BatchManager< Real > > &bman, const bool use_SA=false, const bool adaptive=false, const int numNewSamps=0)
void sumAll(Real *input, Real *output, int dim) 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
Real computeError(std::vector< Real > &vals)
std::vector< std::vector< Real > > sample(int nSamp, bool store=true)
virtual void refine(void)
const std::vector< Teuchos::RCP< ROL::Distribution< Real > > > dist_
MonteCarloGenerator(const int nSamp, std::vector< std::vector< Real > > &bounds, const Teuchos::RCP< BatchManager< Real > > &bman, const bool use_SA=false, const bool adaptive=false, const int numNewSamps=0)
MonteCarloGenerator(const int nSamp, const std::vector< Real > &mean, const std::vector< Real > &std, const Teuchos::RCP< BatchManager< Real > > &bman, const bool use_SA=false, const bool adaptive=false, const int numNewSamps=0)
void setPoints(std::vector< std::vector< Real > > &p)
void update(const Vector< Real > &x)
void setWeights(std::vector< Real > &w)
std::vector< std::vector< Real > > data_