BayesOpt
lhs.hpp
Go to the documentation of this file.
1 
2 /*
3 -------------------------------------------------------------------------
4  This file is part of BayesOpt, an efficient C++ library for
5  Bayesian optimization.
6 
7  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
8 
9  BayesOpt is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Affero General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BayesOpt is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Affero General Public License for more details.
18 
19  You should have received a copy of the GNU Affero General Public License
20  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
21 ------------------------------------------------------------------------
22 */
23 
24 #ifndef _LHS_HPP_
25 #define _LHS_HPP_
26 
27 #include "randgen.hpp"
28 #include "log.hpp"
29 #include "indexvector.hpp"
30 #if defined (USE_SOBOL)
31 # include "sobol.hpp"
32 #endif
33 
34 namespace bayesopt
35 {
36  namespace utils
37  {
38 
39 
41  template<class M>
42  void samplePoints(M& xPoints, int method, randEngine& mtRandom);
43 
44 
48  template<class M>
49  void lhs(M& Result,randEngine& mtRandom);
50 
55 #if defined (USE_SOBOL)
56  template<class M>
57  void sobol(M& result, long long int seed);
58 #endif
59 
64  template<class M>
65  void uniformSampling(M& Result, randEngine& mtRandom);
66 
72  template<class D>
73  void randomPerms(D& arr, randEngine& mtRandom);
74 
76  // Implementations
78 
79  template<class D>
80  void randomPerms(D& arr, randEngine& mtRandom)
81  {
82  typedef typename D::iterator iter;
83 
84  randInt sample(mtRandom, intUniformDist(0,arr.size()-1));
85  for (iter it=arr.begin(); it!=arr.end(); ++it)
86  iter_swap(arr.begin()+sample(),it);
87  } // randomPerms
88 
89 
90  template<class M>
91  void lhs(M& Result, randEngine& mtRandom)
92  {
93  randFloat sample( mtRandom, realUniformDist(0,1) );
94  size_t nA = Result.size1();
95  size_t nB = Result.size2();
96  double ndA = static_cast<double>(nA);
97  // std::vector<int> perms(nA);
98 
99  for (size_t i = 0; i < nB; i++)
100  {
101  // TODO: perms starts at 1. Check this
102  std::vector<int> perms = return_index_vector(nA);
103  randomPerms(perms, mtRandom);
104 
105  for (size_t j = 0; j < nA; j++)
106  {
107  Result(j,i) = ( static_cast<double>(perms[j]) - sample() ) / ndA;
108  }
109  }
110  }
111 
112 #if defined (USE_SOBOL)
113  template<class M>
114  void sobol(M& result, long long int seed)
115  {
116  size_t nSamples = result.size1();
117  size_t nDims = result.size2();
118 
119  double *sobol_seq = i8_sobol_generate(nDims,nSamples,seed);
120 
121  std::copy(sobol_seq,sobol_seq+(nSamples*nDims),result.data().begin());
122  }
123 #endif
124 
125  template<class M>
126  void uniformSampling(M& Result,
127  randEngine& mtRandom)
128  {
129  randFloat sample( mtRandom, realUniformDist(0,1) );
130  size_t nA = Result.size1();
131  size_t nB = Result.size2();
132 
133  // TODO: Improve with iterators
134  // std::generate(Result.begin2(),Result.end2(),sample);
135  for (size_t i = 0; i < nA; i++)
136  for (size_t j = 0; j < nB; j++)
137  Result(i,j) = sample();
138  }
139 
140  template<class M>
141  void samplePoints(M& xPoints, int method,
142  randEngine& mtRandom)
143  {
144  if (method == 1)
145  {
146  FILE_LOG(logINFO) << "Latin hypercube sampling";
147  lhs(xPoints, mtRandom);
148  }
149  else if (method == 2)
150  {
151 #if defined (USE_SOBOL)
152  FILE_LOG(logINFO) << "Sobol sampling";
153  sobol(xPoints, 0);
154 #else
155  FILE_LOG(logINFO) << "Latin hypercube sampling";
156  lhs(xPoints, mtRandom);
157 #endif
158  }
159  else
160  {
161  FILE_LOG(logINFO) << "Uniform sampling";
162  uniformSampling(xPoints, mtRandom);
163  }
164  }
165 
166 
167  } //namespace utils
168 
169 } //namespace bayesopt
170 
171 #endif
Namespace of the library interface.
Definition: using.dox:1
Generators for index vectors.
void lhs(M &Result, randEngine &mtRandom)
Latin hypercube sampling It is used to generate a uniform Latin hypercube.
Definition: lhs.hpp:91
void randomPerms(D &arr, randEngine &mtRandom)
Modify an array using ramdom permutations.
Definition: lhs.hpp:80
void samplePoints(M &xPoints, int method, randEngine &mtRandom)
Selects the sampling method.
Definition: lhs.hpp:141
Boost types for random number generation.
void uniformSampling(M &Result, randEngine &mtRandom)
Hypercube sampling based on Sobol sequences It uses the external Sobol library.
Definition: lhs.hpp:126
std::vector< int > return_index_vector(size_t n)
Generates a vector of indexes (1..n)
Definition: indexvector.hpp:50
Modules and helper macros for logging.