NOMAD Source  Version 4.0.0 Beta
LHS.cpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------------*/
2 /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct Search - */
3 /* */
4 /* NOMAD - Version 4.0.0 has been created by */
5 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
6 /* Christophe Tribes - Polytechnique Montreal */
7 /* */
8 /* The copyright of NOMAD - version 4.0.0 is owned by */
9 /* Charles Audet - Polytechnique Montreal */
10 /* Sebastien Le Digabel - Polytechnique Montreal */
11 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
12 /* Christophe Tribes - Polytechnique Montreal */
13 /* */
14 /* NOMAD v4 has been funded by Rio Tinto, Hydro-Québec, NSERC (Natural */
15 /* Sciences and Engineering Research Council of Canada), InnovÉÉ (Innovation */
16 /* en Énergie Électrique) and IVADO (The Institute for Data Valorization) */
17 /* */
18 /* NOMAD v3 was created and developed by Charles Audet, Sebastien Le Digabel, */
19 /* Christophe Tribes and Viviane Rochon Montplaisir and was funded by AFOSR */
20 /* and Exxon Mobil. */
21 /* */
22 /* NOMAD v1 and v2 were created and developed by Mark Abramson, Charles Audet, */
23 /* Gilles Couture, and John E. Dennis Jr., and were funded by AFOSR and */
24 /* Exxon Mobil. */
25 /* */
26 /* Contact information: */
27 /* Polytechnique Montreal - GERAD */
28 /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */
29 /* e-mail: nomad@gerad.ca */
30 /* */
31 /* This program is free software: you can redistribute it and/or modify it */
32 /* under the terms of the GNU Lesser General Public License as published by */
33 /* the Free Software Foundation, either version 3 of the License, or (at your */
34 /* option) any later version. */
35 /* */
36 /* This program is distributed in the hope that it will be useful, but WITHOUT */
37 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
38 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */
39 /* for more details. */
40 /* */
41 /* You should have received a copy of the GNU Lesser General Public License */
42 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
43 /* */
44 /* You can find information on the NOMAD software at www.gerad.ca/nomad */
45 /*---------------------------------------------------------------------------------*/
46 
47 #include "../Math/LHS.hpp"
48 #include "../Math/RNG.hpp"
49 #include "../Math/RandomPickup.hpp"
50 #include "../Util/Exception.hpp"
51 
52 #include <algorithm> // for shuffle
53 
54 
55 // Constructor
56 NOMAD::LHS::LHS(size_t n,
57  size_t p,
58  NOMAD::ArrayOfDouble lowerBound,
59  NOMAD::ArrayOfDouble upperBound)
60 : _n(n),
61  _p(p),
62  _lowerBound(lowerBound),
63  _upperBound(upperBound)
64 {
65  if (!_lowerBound.isComplete())
66  {
67  std::string s = "LHS Lower bound needs to be completely defined. Values given: ";
68  s += lowerBound.display();
69  throw NOMAD::Exception(__FILE__, __LINE__, s);
70  }
71  if (!_upperBound.isComplete())
72  {
73  std::string s = "LHS Upper bound needs to be completely defined. Values given: ";
74  s += upperBound.display();
75  throw NOMAD::Exception(__FILE__, __LINE__, s);
76  }
77 }
78 
79 
80 // Do the sample
81 // Audet & Hare Algorithm 3.9 Latin Hypercube Sampling
82 std::vector<NOMAD::Point> NOMAD::LHS::Sample() const
83 {
84  std::vector<NOMAD::Point> samplepoints;
85 
86  // 0 - Initialization
87  // Let Pi be a n x p matrix in which each of its n rows
88  // is a random permutation of the vector (1, 2, .., p).
89  //
90  std::vector<std::vector<size_t>> Pi;
91  for (size_t i = 0; i < _n; i++)
92  {
93  std::vector<size_t> v = Permutation(_p);
94  Pi.push_back(v);
95  }
96 
97  // 1 - Sample construction
98  for (size_t j = 0; j < _p; j++)
99  {
100  Point point(_n);
101  for (size_t i = 0; i < _n; i++)
102  {
103  NOMAD::Double r_ij = RNG::rand(0,1);
104  NOMAD::Double l_i = _lowerBound[i];
105  NOMAD::Double Pi_ij( Pi[i][j] );
106  NOMAD::Double pdouble( _p );
107  NOMAD::Double u_i( _upperBound[i] );
108 
109  NOMAD::Double x_ij = l_i + (Pi_ij - r_ij) / pdouble * (u_i - l_i);
110  point[i] = x_ij;
111 
112  }
113  samplepoints.push_back(point);
114  }
115 
116  return samplepoints;
117 }
118 
119 
120 // Input: p
121 // Output: Random permutation of the vector (1, 2, .., p)
122 std::vector<size_t> NOMAD::LHS::Permutation(const size_t p)
123 {
124  NOMAD::RandomPickup rp(p);
125 
126  std::vector<size_t> v;
127  for (size_t j = 0; j < p ; j++)
128  {
129  v.push_back(rp.pickup()+1);
130  }
131 
132  return v;
133 }
Point
Class for the representation of a point.
Definition: Point.hpp:68
RNG::rand
static result_type rand()
Get a random integer.