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(Teuchos::ScalarTraits<Real>::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(Teuchos::ScalarTraits<Real>::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  Real random(void) const {
91  return static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
92  }
93 
94  std::vector<std::vector<Real> > sample(int nSamp, bool store = true) {
95  srand(123454321);
96  const Real zero(0), one(1), two(2), tol(0.1);
97  int rank = SampleGenerator<Real>::batchID();
98  const int dim = (!useDist_ ? data_.size() : dist_.size());
99  std::vector<Real> pts(nSamp*dim, zero);
100  if (rank == 0) {
101  // Generate samples
102  for (int i = 0; i < nSamp; ++i) {
103  if ( !useDist_ ) {
104  for (int j = 0; j < dim; ++j) {
105  if ( use_normal_ ) {
106  pts[j + i*dim] = std::sqrt(two*(data_[j])[1])*ierf(two*random()-one) + (data_[j])[0];
107  }
108  else {
109  pts[j + i*dim] = ((data_[j])[1]-(data_[j])[0])*random()+(data_[j])[0];
110  }
111  }
112  }
113  else {
114  for (int j = 0; j < dim; ++j) {
115  pts[j + i*dim] = (dist_[j])->invertCDF(random());
116  while (std::abs(pts[j + i*dim]) > tol*ROL::ROL_OVERFLOW<Real>()) {
117  pts[j + i*dim] = (dist_[j])->invertCDF(random());
118  }
119  }
120  }
121  }
122  }
123  SampleGenerator<Real>::broadcast(&pts[0],nSamp*dim,0);
124  // Separate samples across processes
125  int nProc = SampleGenerator<Real>::numBatches();
126  int frac = nSamp / nProc;
127  int rem = nSamp % nProc;
128  int N = frac + ((rank < rem) ? 1 : 0);
129  int offset = 0;
130  for (int i = 0; i < rank; ++i) {
131  offset += frac + ((i < rem) ? 1 : 0);
132  }
133  std::vector<std::vector<Real> > mypts;
134  std::vector<Real> pt(dim);
135  for (int i = 0; i < N; ++i) {
136  int I = offset+i;
137  for (int j = 0; j < dim; ++j) {
138  pt[j] = pts[j + I*dim];
139  }
140  mypts.push_back(pt);
141  }
142  if ( store ) {
143  std::vector<Real> mywts(N, one/static_cast<Real>(nSamp));
146  }
147  return mypts;
148  }
149 
150  void sample(void) {
151  sample(nSamp_,true);
152  }
153 
154 public:
155  MonteCarloGenerator(const int nSamp,
156  const std::vector<Teuchos::RCP<Distribution<Real> > > &dist,
157  const Teuchos::RCP<BatchManager<Real> > &bman,
158  const bool use_SA = false,
159  const bool adaptive = false,
160  const int numNewSamps = 0)
161  : SampleGenerator<Real>(bman),
162  nSamp_(nSamp),
163  use_normal_(false),
164  use_SA_(use_SA),
165  adaptive_(adaptive),
166  numNewSamps_(numNewSamps),
167  sum_val_(0.0),
168  sum_val2_(0.0),
169  sum_ng_(0.0),
170  sum_ng2_(0.0),
171  useDist_(true),
172  dist_(dist) {
173  int nProc = SampleGenerator<Real>::numBatches();
174  TEUCHOS_TEST_FOR_EXCEPTION( nSamp_ < nProc, std::invalid_argument,
175  ">>> ERROR (ROL::MonteCarloGenerator): Total number of samples is less than the number of batches!");
176  sample();
177  }
178 
179  MonteCarloGenerator(const int nSamp,
180  std::vector<std::vector<Real> > &bounds,
181  const Teuchos::RCP<BatchManager<Real> > &bman,
182  const bool use_SA = false,
183  const bool adaptive = false,
184  const int numNewSamps = 0)
185  : SampleGenerator<Real>(bman),
186  nSamp_(nSamp),
187  use_normal_(false),
188  use_SA_(use_SA),
189  adaptive_(adaptive),
190  numNewSamps_(numNewSamps),
191  sum_val_(0.0),
192  sum_val2_(0.0),
193  sum_ng_(0.0),
194  sum_ng2_(0.0),
195  useDist_(false) {
196  int nProc = SampleGenerator<Real>::numBatches();
197  TEUCHOS_TEST_FOR_EXCEPTION( nSamp_ < nProc, std::invalid_argument,
198  ">>> ERROR (ROL::MonteCarloGenerator): Total number of samples is less than the number of batches!");
199  unsigned dim = bounds.size();
200  data_.clear();
201  Real tmp = 0.0;
202  for ( unsigned j = 0; j < dim; j++ ) {
203  if ( (bounds[j])[0] > (bounds[j])[1] ) {
204  tmp = (bounds[j])[0];
205  (bounds[j])[0] = (bounds[j])[1];
206  (bounds[j])[1] = tmp;
207  data_.push_back(bounds[j]);
208  }
209  data_.push_back(bounds[j]);
210  }
211  sample();
212  }
213 
214  MonteCarloGenerator(const int nSamp,
215  const std::vector<Real> &mean,
216  const std::vector<Real> &std,
217  const Teuchos::RCP<BatchManager<Real> > &bman,
218  const bool use_SA = false,
219  const bool adaptive = false,
220  const int numNewSamps = 0 )
221  : SampleGenerator<Real>(bman),
222  nSamp_(nSamp),
223  use_normal_(true),
224  use_SA_(use_SA),
225  adaptive_(adaptive),
226  numNewSamps_(numNewSamps),
227  sum_val_(0.0),
228  sum_val2_(0.0),
229  sum_ng_(0.0),
230  sum_ng2_(0.0),
231  useDist_(false) {
232  int nProc = SampleGenerator<Real>::numBatches();
233  TEUCHOS_TEST_FOR_EXCEPTION( nSamp_ < nProc, std::invalid_argument,
234  ">>> ERROR (ROL::MonteCarloGenerator): Total number of samples is less than the number of batches!");
235  unsigned dim = mean.size();
236  data_.clear();
237  std::vector<Real> tmp(2,0.0);
238  for ( unsigned j = 0; j < dim; j++ ) {
239  tmp[0] = mean[j];
240  tmp[1] = std[j];
241  data_.push_back(tmp);
242  }
243  sample();
244  }
245 
246  void update( const Vector<Real> &x ) {
248  sum_val_ = 0.0;
249  sum_val2_ = 0.0;
250  sum_ng_ = 0.0;
251  sum_ng_ = 0.0;
252  if ( use_SA_ ) {
253  sample();
254  }
255  }
256 
257  Real computeError( std::vector<Real> &vals ) {
258  if ( adaptive_ && !use_SA_ ) {
259  // Compute unbiased sample variance
260  int cnt = 0;
261  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
262  sum_val_ += vals[cnt];
263  sum_val2_ += vals[cnt]*vals[cnt];
264  cnt++;
265  }
266  Real mymean = sum_val_ / nSamp_;
267  Real mean = 0.0;
268  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
269 
270  Real myvar = (sum_val2_ - mean*mean)/(nSamp_-1.0);
271  Real var = 0.0;
272  SampleGenerator<Real>::sumAll(&myvar,&var,1);
273  // Return Monte Carlo error
274  vals.clear();
275  return std::sqrt(var/(nSamp_))*1.e-8;
276  }
277  else {
278  vals.clear();
279  return 0.0;
280  }
281  }
282 
283  Real computeError( std::vector<Teuchos::RCP<Vector<Real> > > &vals, const Vector<Real> &x ) {
284  if ( adaptive_ && !use_SA_ ) {
285  // Compute unbiased sample variance
286  int cnt = 0;
287  Real ng = 0.0;
288  for ( int i = SampleGenerator<Real>::start(); i < SampleGenerator<Real>::numMySamples(); i++ ) {
289  ng = (vals[cnt])->norm();
290  sum_ng_ += ng;
291  sum_ng2_ += ng*ng;
292  cnt++;
293  }
294  Real mymean = sum_ng_ / nSamp_;
295  Real mean = 0.0;
296  SampleGenerator<Real>::sumAll(&mymean,&mean,1);
297 
298  Real myvar = (sum_ng2_ - mean*mean)/(nSamp_-1.0);
299  Real var = 0.0;
300  SampleGenerator<Real>::sumAll(&myvar,&var,1);
301  // Return Monte Carlo error
302  vals.clear();
303  return std::sqrt(var/(nSamp_))*1.e-4;
304  }
305  else {
306  vals.clear();
307  return 0.0;
308  }
309  }
310 
311  void refine(void) {
312  if ( adaptive_ && !use_SA_ ) {
313  std::vector<std::vector<Real> > pts;
314  std::vector<Real> pt(data_.size(),0.0);
315  for ( int i = 0; i < SampleGenerator<Real>::numMySamples(); i++ ) {
317  pts.push_back(pt);
318  }
319  std::vector<std::vector<Real> > pts_new = sample(numNewSamps_,false);
320  pts.insert(pts.end(),pts_new.begin(),pts_new.end());
321  nSamp_ += numNewSamps_;
322  std::vector<Real> wts(pts.size(),1.0/((Real)nSamp_));
326  }
327  }
328 
329 };
330 
331 }
332 
333 #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:76
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 broadcast(Real *input, int cnt, int root) const
void setWeights(std::vector< Real > &w)
std::vector< std::vector< Real > > data_