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) {
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(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  sample();
215  }
216 
217  MonteCarloGenerator(const int nSamp,
218  std::vector<std::vector<Real> > &bounds,
219  const Teuchos::RCP<BatchManager<Real> > &bman,
220  const bool use_SA = false,
221  const bool adaptive = false,
222  const int numNewSamps = 0)
223  : SampleGenerator<Real>(bman),
224  nSamp_(nSamp),
225  use_normal_(false),
226  use_SA_(use_SA),
227  adaptive_(adaptive),
228  numNewSamps_(numNewSamps),
229  sum_val_(0.0),
230  sum_val2_(0.0),
231  sum_ng_(0.0),
232  sum_ng2_(0.0),
233  useDist_(false) {
234  unsigned dim = bounds.size();
235  data_.clear();
236  Real tmp = 0.0;
237  for ( unsigned j = 0; j < dim; j++ ) {
238  if ( (bounds[j])[0] > (bounds[j])[1] ) {
239  tmp = (bounds[j])[0];
240  (bounds[j])[0] = (bounds[j])[1];
241  (bounds[j])[1] = tmp;
242  data_.push_back(bounds[j]);
243  }
244  data_.push_back(bounds[j]);
245  }
246  sample();
247  }
248 
249  MonteCarloGenerator(const int nSamp,
250  const std::vector<Real> &mean,
251  const std::vector<Real> &std,
252  const Teuchos::RCP<BatchManager<Real> > &bman,
253  const bool use_SA = false,
254  const bool adaptive = false,
255  const int numNewSamps = 0 )
256  : SampleGenerator<Real>(bman),
257  nSamp_(nSamp),
258  use_normal_(true),
259  use_SA_(use_SA),
260  adaptive_(adaptive),
261  numNewSamps_(numNewSamps),
262  sum_val_(0.0),
263  sum_val2_(0.0),
264  sum_ng_(0.0),
265  sum_ng2_(0.0),
266  useDist_(false) {
267  unsigned dim = mean.size();
268  data_.clear();
269  std::vector<Real> tmp(2,0.0);
270  for ( unsigned j = 0; j < dim; j++ ) {
271  tmp[0] = mean[j];
272  tmp[1] = std[j];
273  data_.push_back(tmp);
274  }
275  sample();
276  }
277 
278  void update( const Vector<Real> &x ) {
280  sum_val_ = 0.0;
281  sum_val2_ = 0.0;
282  sum_ng_ = 0.0;
283  sum_ng_ = 0.0;
284  if ( use_SA_ ) {
285  sample();
286  }
287  }
288 
289  Real computeError( std::vector<Real> &vals ) {
290  if ( adaptive_ && !use_SA_ ) {
291  // Compute unbiased sample variance
292  int cnt = 0;
293  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
294  sum_val_ += vals[cnt];
295  sum_val2_ += vals[cnt]*vals[cnt];
296  cnt++;
297  }
298  Real mymean = sum_val_ / nSamp_;
299  Real mean = 0.0;
300  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
301 
302  Real myvar = (sum_val2_ - mean*mean)/(nSamp_-1.0);
303  Real var = 0.0;
304  SampleGenerator<Real>::sumAll(&myvar,&var,1);
305  // Return Monte Carlo error
306  vals.clear();
307  return std::sqrt(var/(nSamp_))*1.e-8;
308  }
309  else {
310  vals.clear();
311  return 0.0;
312  }
313  }
314 
315  Real computeError( std::vector<Teuchos::RCP<Vector<Real> > > &vals, const Vector<Real> &x ) {
316  if ( adaptive_ && !use_SA_ ) {
317  // Compute unbiased sample variance
318  int cnt = 0;
319  Real ng = 0.0;
320  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
321  ng = (vals[cnt])->norm();
322  sum_ng_ += ng;
323  sum_ng2_ += ng*ng;
324  cnt++;
325  }
326  Real mymean = sum_ng_ / nSamp_;
327  Real mean = 0.0;
328  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
329 
330  Real myvar = (sum_ng2_ - mean*mean)/(nSamp_-1.0);
331  Real var = 0.0;
332  SampleGenerator<Real>::sumAll(&myvar,&var,1);
333  // Return Monte Carlo error
334  vals.clear();
335  return std::sqrt(var/(nSamp_))*1.e-4;
336  }
337  else {
338  vals.clear();
339  return 0.0;
340  }
341  }
342 
343  void refine(void) {
344  if ( adaptive_ && !use_SA_ ) {
345  std::vector<std::vector<Real> > pts;
346  std::vector<Real> pt(data_.size(),0.0);
347  for ( int i = 0; i < SampleGenerator<Real>::numMySamples(); i++ ) {
349  pts.push_back(pt);
350  }
351  std::vector<std::vector<Real> > pts_new = sample(numNewSamps_,false);
352  pts.insert(pts.end(),pts_new.begin(),pts_new.end());
353  nSamp_ += numNewSamps_;
354  std::vector<Real> wts(pts.size(),1.0/((Real)nSamp_));
358  }
359  }
360 
361 };
362 
363 }
364 
365 #endif
Real ierf(Real input) 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)
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)
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)
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_