OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
LatinHyperCube.h
Go to the documentation of this file.
1 #ifndef OPAL_LATIN_HYPERCUBE_H
2 #define OPAL_LATIN_HYPERCUBE_H
3 
5 #include "Sample/RNGStream.h"
6 
7 #include <algorithm>
8 #include <deque>
9 
11 {
12 
13 public:
14  typedef typename std::uniform_real_distribution<double> dist_t;
15 
16  LatinHyperCube(double lower, double upper)
17  : binsize_m(0.0)
18  , upper_m(upper)
19  , lower_m(lower)
20  , dist_m(0.0, 1.0)
21  , RNGInstance_m(RNGStream::getInstance())
22  , seed_m(RNGStream::getGlobalSeed())
23  {}
24 
25  LatinHyperCube(double lower, double upper, int seed)
26  : binsize_m(0.0)
27  , upper_m(upper)
28  , lower_m(lower)
29  , dist_m(0.0, 1.0)
30  , RNGInstance_m(nullptr)
31  , seed_m(seed)
32  {}
33 
35  if ( RNGInstance_m )
37  }
38 
39  void create(boost::shared_ptr<SampleIndividual>& ind, std::size_t i) {
40  /* values are created within [0, 1], thus, they need to be mapped
41  * the domain [lower, upper]
42  */
43  ind->genes[i] = map2domain_m(RNGInstance_m->getNext(dist_m));
44  }
45 
46  void allocate(const CmdArguments_t& args, const Comm::Bundle_t& comm) {
47  int id = comm.island_id;
48 
49  if ( !RNGInstance_m )
51 
52  int nSamples = args->getArg<int>("nsamples", true);
53  int nMasters = args->getArg<int>("num-masters", true);
54 
55  int nLocSamples = nSamples / nMasters;
56  int rest = nSamples - nMasters * nLocSamples;
57 
58  if ( id < rest )
59  nLocSamples++;
60 
61  int startBin = 0;
62 
63  if ( rest == 0 )
64  startBin = nLocSamples * id;
65  else {
66  if ( id < rest ) {
67  startBin = nLocSamples * id;
68  } else {
69  startBin = (nLocSamples + 1) * rest + (id - rest) * nLocSamples;
70  }
71  }
72 
73  binsize_m = ( upper_m - lower_m ) / double(nSamples);
74 
75  this->fillBins_m(nSamples, nLocSamples, startBin, seed_m);
76  }
77 
78 private:
79  double map2domain_m(double val) {
80  /* y = mx + q
81  *
82  * [0, 1] --> [a, b]
83  *
84  * y = (b - a) * x + a
85  *
86  * where a and b are the lower, respectively, upper
87  * bound of the current bin.
88  */
89 
90  std::size_t bin = bin_m.back();
91  bin_m.pop_back();
92 
93  return binsize_m * (val + bin) + lower_m;
94  }
95 
96  void fillBins_m(std::size_t nTotal, std::size_t nLocal, int startBin,
97  std::size_t seed)
98  {
99  std::deque<std::size_t> tmp;
100  tmp.resize(nTotal);
101  std::iota(tmp.begin(), tmp.end(), 0);
102 
103  // all masters need to shuffle the same way
104  std::mt19937_64 eng(seed);
105  std::shuffle(tmp.begin(), tmp.end(), eng);
106 
107  // each master takes its bins
108  std::copy(tmp.begin()+startBin,
109  tmp.begin()+startBin+nLocal,
110  std::back_inserter(bin_m));
111  }
112 
113 private:
114  std::deque<std::size_t> bin_m;
115  double binsize_m;
116 
117  double upper_m;
118  double lower_m;
119 
121 
123 
124  std::size_t seed_m;
125 };
126 
127 #endif
int seed
The current random seed.
Definition: Options.cpp:41
std::uniform_real_distribution< double > dist_t
void fillBins_m(std::size_t nTotal, std::size_t nLocal, int startBin, std::size_t seed)
void create(boost::shared_ptr< SampleIndividual > &ind, std::size_t i)
void allocate(const CmdArguments_t &args, const Comm::Bundle_t &comm)
int island_id
Definition: types.h:15
boost::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:169
LatinHyperCube(double lower, double upper)
LatinHyperCube(double lower, double upper, int seed)
static void deleteInstance(RNGStream *&generator)
Definition: RNGStream.cpp:21
std::size_t seed_m
DISTR::result_type getNext(DISTR &RNGDist)
Definition: RNGStream.h:20
double map2domain_m(double val)
static RNGStream * getInstance()
Definition: RNGStream.cpp:9
bundles all communicators for a specific role/pid
Definition: types.h:14
RNGStream * RNGInstance_m
std::deque< std::size_t > bin_m