OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
SampleGaussianSequence.h
Go to the documentation of this file.
1 //
2 // Class SampleGaussianSequence
3 // This class provides a sequence of sampling points that have a Gaussian distribution
4 // with
5 // mean = 0.5 * (upper + lower)
6 // sigma = (upper - lower) / 10
7 // This can be achieved if the integral of the Gaussian between the sampling
8 // points are all equal. The sampling points are therefore computed using
9 // the inverse error function at equally distributed arguments between
10 // -1 and 1.
11 //
12 // Copyright (c) 2018, Christof Metzger-Kraus, Open Sourcerer
13 // All rights reserved
14 //
15 // This file is part of OPAL.
16 //
17 // OPAL is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
24 //
25 #ifndef OPAL_SAMPLE_GAUSSIAN_SEQUENCE_H
26 #define OPAL_SAMPLE_GAUSSIAN_SEQUENCE_H
27 
28 #include "Sample/SamplingMethod.h"
29 #include "Utilities/Util.h"
30 
31 #ifdef WITH_UNIT_TESTS
32 #include <gtest/gtest_prod.h>
33 #endif
34 
36 {
37 
38 public:
39 
40  SampleGaussianSequence(double lower, double upper, size_t modulo, int nSample)
41  : numSamples_m(nSample)
42  , volumeLowerDimensions_m(modulo)
43  {
44  double mean = 0.5 * (lower + upper);
45  double sigma = (upper - lower) / 10; // +- 5 sigma
46  double factor = sigma / sqrt(2);
47  double dx = 2.0 / nSample;
48  for (long i = 0; i < nSample; ++ i) {
49  double x = -1.0 + (i + 0.5) * dx;
50  double y = Util::erfinv(x);
51  sampleChain_m.push_back(mean + factor * y);
52  }
53  }
54 
55  void create(boost::shared_ptr<SampleIndividual>& ind, size_t i) {
56  ind->genes[i] = getNext(ind->id);
57  }
58 
59  double getNext(unsigned int id) {
60  int bin = int(id / volumeLowerDimensions_m) % numSamples_m;
61 
62  double sample = sampleChain_m[bin];
63  return sample;
64  }
65 
66 private:
67 #ifdef WITH_UNIT_TESTS
68  FRIEND_TEST(GaussianSampleTest, ChainTest);
69 #endif
70  std::vector<double> sampleChain_m;
71  unsigned int numSamples_m; // size of this "dimension"
72  size_t volumeLowerDimensions_m; // the "volume" of the sampling space of the lower "dimensions"
73 };
74 
75 #endif
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
double erfinv(double x)
Definition: Util.cpp:42
std::vector< double > sampleChain_m
SampleGaussianSequence(double lower, double upper, size_t modulo, int nSample)
void create(boost::shared_ptr< SampleIndividual > &ind, size_t i)
double getNext(unsigned int id)