1 /* Copyright 2017-2021 PaGMO development team
2
3 This file is part of the PaGMO library.
4
5 The PaGMO library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 or
13
14 * the GNU General Public License as published by the Free Software
15 Foundation; either version 3 of the License, or (at your option) any
16 later version.
17
18 or both in parallel, as here.
19
20 The PaGMO library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the PaGMO library. If not,
27 see https://www.gnu.org/licenses/. */
28
29 #include <algorithm>
30 #include <stdexcept>
31 #include <string>
32 #include <vector>
33
34 #include <pagmo/detail/custom_comparisons.hpp>
35 #include <pagmo/detail/prime_numbers.hpp>
36 #include <pagmo/exceptions.hpp>
37 #include <pagmo/utils/discrepancy.hpp>
38
39 namespace pagmo
40 {
41
42 /// Sample from a simplex
43 /**
44 * Samples a point on a \f$n\f$ dimensional simplex from a \f$n-1\f$ dimensional point
45 *
46 * In order to generate a uniform distribution on a simplex, that is to sample a \f$n\f$-dimensional
47 * point \f$\mathbf x\f$ such that \f$\sum_{i=1}^{n} x_i = 1\f$ one can follow the following approach:
48 * take \f$n-1\f$ random numbers from the interval (0,1)(0,1), then add a 0 and 1 to get a list of \f$n+1\f$ numbers.
49 * Sort the list and record the differences between two consecutive elements. This creates
50 * a list of \f$n\f$ number that, by construction, will sum up to 1. Moreover this sampling is uniform.
51 * As an example the following code would generate points distributed on a \f$n\f$ dimensional simplex:
52 *
53 * @code{.unparsed}
54 * std::vector<std::vector<double>> points_on_a_simplex;
55 * halton ld_rng(n-1);
56 * for (auto i = 0u; i < 100u; ++i) {
57 * points_on_a_simplex.push_back(project_to_simplex(ld_rng()));
58 * }
59 * @endcode
60 *
61 * @param in a <tt>std::vector</tt> containing a point in \f$n+1\f$ dimensions.
62 * @return a <tt>std::vector</tt> containing the projected point of \f$n\f$ dimensions.
63 *
64 * @throws std::invalid_argument if the input vector elements are not in [0,1]
65 * @throws std::invalid_argument if the input vector has size 0 or 1.
66 *
67 * See: Donald B. Rubin, The Bayesian bootstrap Ann. Statist. 9, 1981, 130-134.
68 */
sample_from_simplex(std::vector<double> in)69 std::vector<double> sample_from_simplex(std::vector<double> in)
70 {
71 if (std::any_of(in.begin(), in.end(), [](double item) { return (item < 0 || item > 1); })) {
72 pagmo_throw(std::invalid_argument, "Input vector must have all elements in [0,1]");
73 }
74 if (in.size() > 0u) {
75 std::sort(in.begin(), in.end(), detail::less_than_f<double>);
76 in.insert(in.begin(), 0.0);
77 in.push_back(1.0);
78 for (decltype(in.size()) i = 0u; i < in.size() - 1u; ++i) {
79 in[i] = in[i + 1u] - in[i];
80 }
81 in.pop_back();
82 return in;
83 } else {
84 pagmo_throw(std::invalid_argument, "Input vector must have at least dimension 1, a size of "
85 + std::to_string(in.size()) + " was detected instead.");
86 }
87 }
88
van_der_corput(unsigned b,unsigned n)89 van_der_corput::van_der_corput(unsigned b, unsigned n) : m_base(b), m_counter(n)
90 {
91 if (b < 2u) {
92 pagmo_throw(std::invalid_argument, "The base of the van der Corput sequence must be at least 2: "
93 + std::to_string(b) + " was detected");
94 }
95 }
96
97 /// Returns the next number in the sequence
98 /**
99 * @return the next number in the sequence
100 */
operator ()()101 double van_der_corput::operator()()
102 {
103 double retval = 0.;
104 double f = 1.0 / m_base;
105 unsigned i = m_counter;
106 while (i > 0u) {
107 retval += f * (i % m_base);
108 i = i / m_base;
109 f = f / m_base;
110 }
111 ++m_counter;
112 return retval;
113 }
114
halton(unsigned dim,unsigned n)115 halton::halton(unsigned dim, unsigned n) : m_dim(dim)
116 {
117 for (auto i = 0u; i < m_dim; ++i) {
118 m_vdc.push_back(van_der_corput(detail::prime(i + 1), n));
119 }
120 }
121
122 /// Returns the next number in the sequence
123 /**
124 * @return the next number in the sequence
125 */
operator ()()126 std::vector<double> halton::operator()()
127 {
128 std::vector<double> retval;
129 for (auto i = 0u; i < m_dim; ++i) {
130 retval.push_back(m_vdc[i]());
131 }
132 return retval;
133 }
134
135 } // namespace pagmo
136