NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LHS.cpp
Go to the documentation of this file.
1 
2 #include "../Math/LHS.hpp"
3 #include "../Math/RNG.hpp"
4 #include "../Util/Exception.hpp"
5 
6 #include <algorithm> // for random_shuffle
7 
8 // Constructor
9 NOMAD::LHS::LHS(size_t n,
10  size_t p,
11  NOMAD::ArrayOfDouble lowerBound,
12  NOMAD::ArrayOfDouble upperBound,
13  int seed)
14 : _n(n),
15  _p(p),
16  _lowerBound(lowerBound),
17  _upperBound(upperBound),
18  _seed(seed)
19 {
20  if (!_lowerBound.isComplete())
21  {
22  std::string s = "LHS Lower bound needs to be completely defined. Values given: ";
23  s += lowerBound.display();
24  throw NOMAD::Exception(__FILE__, __LINE__, s);
25  }
26  if (!_upperBound.isComplete())
27  {
28  std::string s = "LHS Upper bound needs to be completely defined. Values given: ";
29  s += upperBound.display();
30  throw NOMAD::Exception(__FILE__, __LINE__, s);
31  }
32 
33  std::srand(_seed);
34 }
35 
36 
37 // Do the sample
38 // Audet & Hare Algorithm 3.9 Latin Hypercube Sampling
39 std::vector<NOMAD::Point> NOMAD::LHS::Sample() const
40 {
41  std::vector<NOMAD::Point> samplepoints;
42 
43  // 0 - Initialization
44  // Let Pi be a n x p matrix in which each of its n rows
45  // is a random permutation of the vector (1, 2, .., p).
46  //
47  std::vector<std::vector<size_t>> Pi;
48  for (size_t i = 0; i < _n; i++)
49  {
50  std::vector<size_t> v = Permutation(_p);
51  Pi.push_back(v);
52  }
53 
54  // 1 - Sample construction
55  for (size_t j = 0; j < _p; j++)
56  {
57  Point point(_n);
58  for (size_t i = 0; i < _n; i++)
59  {
60  NOMAD::Double r_ij = RNG::rand(0,1);
61  NOMAD::Double l_i = _lowerBound[i];
62  NOMAD::Double Pi_ij( Pi[i][j] );
63  NOMAD::Double pdouble( _p );
64  NOMAD::Double u_i( _upperBound[i] );
65 
66  NOMAD::Double x_ij = l_i + (Pi_ij - r_ij) / pdouble * (u_i - l_i);
67  point[i] = x_ij;
68 
69  }
70  samplepoints.push_back(point);
71  }
72 
73  return samplepoints;
74 }
75 
76 // Input: p
77 // Output: Random permutation of the vector (1, 2, .., p)
78 std::vector<size_t> NOMAD::LHS::Permutation(const size_t p)
79 {
80  std::vector<size_t> v;
81  for (size_t j = 1; j <= p; j++)
82  {
83  v.push_back(j);
84  }
85 
86  std::random_shuffle(v.begin(), v.end());
87 
88  return v;
89 }
static uint32_t rand()
Get a random integer as uint32.
Class for the representation of a point.
Definition: Point.hpp:23