44 #ifndef ROL_MONTECARLOGENERATOR_HPP 45 #define ROL_MONTECARLOGENERATOR_HPP 60 std::vector<std::vector<Real> >
data_;
68 const std::vector<Teuchos::RCP<ROL::Distribution<Real> > >
dist_;
70 Real
ierf(Real input)
const {
71 std::vector<Real> coeff;
73 Real tmp = c * (std::sqrt(Teuchos::ScalarTraits<Real>::pi())/2.0 * input);
77 while (std::abs(tmp) > 1.e-4*std::abs(val)) {
79 for (
unsigned i = 0; i < coeff.size(); i++ ) {
80 c += coeff[i]*coeff[coeff.size()-1-i]/((i+1)*(2*i+1));
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);
91 return static_cast<Real
>(rand())/static_cast<Real>(RAND_MAX);
94 std::vector<std::vector<Real> >
sample(
int nSamp,
bool store =
true) {
96 const Real zero(0), one(1), two(2), tol(0.1);
98 const int dim = (!useDist_ ? data_.size() : dist_.size());
99 std::vector<Real> pts(nSamp*dim, zero);
102 for (
int i = 0; i < nSamp; ++i) {
104 for (
int j = 0; j < dim; ++j) {
106 pts[j + i*dim] = std::sqrt(two*(data_[j])[1])*
ierf(two*
random()-one) + (data_[j])[0];
109 pts[j + i*dim] = ((data_[j])[1]-(data_[j])[0])*
random()+(data_[j])[0];
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());
126 int frac = nSamp / nProc;
127 int rem = nSamp % nProc;
128 int N = frac + ((rank < rem) ? 1 : 0);
130 for (
int i = 0; i < rank; ++i) {
131 offset += frac + ((i < rem) ? 1 : 0);
133 std::vector<std::vector<Real> > mypts;
134 std::vector<Real> pt(dim);
135 for (
int i = 0; i < N; ++i) {
137 for (
int j = 0; j < dim; ++j) {
138 pt[j] = pts[j + I*dim];
143 std::vector<Real> mywts(N, one/static_cast<Real>(nSamp));
158 const bool use_SA =
false,
159 const bool adaptive =
false,
160 const int numNewSamps = 0)
166 numNewSamps_(numNewSamps),
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!");
180 std::vector<std::vector<Real> > &bounds,
182 const bool use_SA =
false,
183 const bool adaptive =
false,
184 const int numNewSamps = 0)
190 numNewSamps_(numNewSamps),
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();
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]);
209 data_.push_back(bounds[j]);
215 const std::vector<Real> &mean,
216 const std::vector<Real> &std,
218 const bool use_SA =
false,
219 const bool adaptive =
false,
220 const int numNewSamps = 0 )
226 numNewSamps_(numNewSamps),
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();
237 std::vector<Real> tmp(2,0.0);
238 for (
unsigned j = 0; j < dim; j++ ) {
241 data_.push_back(tmp);
258 if ( adaptive_ && !use_SA_ ) {
262 sum_val_ += vals[cnt];
263 sum_val2_ += vals[cnt]*vals[cnt];
266 Real mymean = sum_val_ /
nSamp_;
270 Real myvar = (sum_val2_ - mean*mean)/(nSamp_-1.0);
275 return std::sqrt(var/(nSamp_))*1.e-8;
284 if ( adaptive_ && !use_SA_ ) {
289 ng = (vals[cnt])->norm();
294 Real mymean = sum_ng_ /
nSamp_;
298 Real myvar = (sum_ng2_ - mean*mean)/(nSamp_-1.0);
303 return std::sqrt(var/(nSamp_))*1.e-4;
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++ ) {
319 std::vector<std::vector<Real> > pts_new =
sample(numNewSamps_,
false);
320 pts.insert(pts.end(),pts_new.begin(),pts_new.end());
322 std::vector<Real> wts(pts.size(),1.0/((Real)nSamp_));
virtual std::vector< Real > getMyPoint(const int i) const
virtual void update(const Vector< Real > &x)
Real ierf(Real input) const
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.
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_
int numBatches(void) const
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_