ROL
Loading...
Searching...
No Matches
ROL_UniformSimplexGeneratorDef.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef ROL_UNIFORMSIMPLEXGENERATORDEF_HPP
11#define ROL_UNIFORMSIMPLEXGENERATORDEF_HPP
12
13namespace ROL {
14
15template<typename Real>
16std::vector<std::vector<Real>> UniformSimplexGenerator<Real>::sample(int nSamp, bool store, bool refine) {
17 if (!refine) srand(MonteCarloGenerator<Real>::seed_);
18 const Real zero(0), one(1);
19 const int rank = SampleGenerator<Real>::batchID();
20 std::random_device rd;
21 std::mt19937 gen(rd());
22 std::uniform_real_distribution<Real> real_dist(zero, one);
23
24 std::vector<Real> pts(nSamp*dim_, zero), tmp(dim_+1, zero);
25 tmp[dim_] = one;
26 if (rank == 0) {
27 // Generate samples
28 for (int i = 0; i < nSamp; ++i) {
29 // Generate simplex sample
30 std::generate(tmp.begin()+1,tmp.end()-1,[&]() { return real_dist(gen); });
31 std::sort(tmp.begin(),tmp.end());
32 std::adjacent_difference(tmp.begin(),tmp.end(),tmp.begin());
33 for (int j = 0; j < dim_; ++j) pts[j + i*dim_] = tmp[j+1];
34 }
35 }
36 SampleGenerator<Real>::broadcast(&pts[0],nSamp*dim_,0);
37 // Separate samples across processes
39 int frac = nSamp / nProc;
40 int rem = nSamp % nProc;
41 int N = frac + ((rank < rem) ? 1 : 0);
42 int offset = 0;
43 for (int i = 0; i < rank; ++i) offset += frac + ((i < rem) ? 1 : 0);
44 std::vector<std::vector<Real>> mypts;
45 std::vector<Real> pt(dim_);
46 for (int i = 0; i < N; ++i) {
47 int I = offset+i;
48 for (int j = 0; j < dim_; ++j) pt[j] = pts[j + I*dim_];
49 mypts.push_back(pt);
50 }
51 if ( store ) {
52 std::vector<Real> mywts(N, one/static_cast<Real>(nSamp));
55 }
56 return mypts;
57}
58
59template<typename Real>
61 const Ptr<BatchManager<Real>> &bman,
62 bool use_SA,
63 bool adaptive,
64 int numNewSamps)
65 : MonteCarloGenerator<Real>(bman,use_SA,adaptive,numNewSamps),
66 dim_(dim) {
68 ROL_TEST_FOR_EXCEPTION( nSamp < nProc, std::invalid_argument,
69 ">>> ERROR (ROL::UniformSimplexGenerator): Total number of samples is less than the number of batches!");
71 sample(nSamp,true,false);
72}
73
74}
75#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void setPoints(std::vector< std::vector< Real > > &p)
void setWeights(std::vector< Real > &w)
void broadcast(Real *input, int cnt, int root) const
UniformSimplexGenerator(int nSamp, int dim, const Ptr< BatchManager< Real > > &bman, bool use_SA=false, bool adaptive=false, int numNewSamps=0)
std::vector< std::vector< Real > > sample(int nSamp, bool store=true, bool refine=false)
constexpr auto dim