discrete_distribution.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_
  15. #define ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_
  16. #include <cassert>
  17. #include <cstddef>
  18. #include <initializer_list>
  19. #include <istream>
  20. #include <limits>
  21. #include <ostream>
  22. #include <type_traits>
  23. #include <utility>
  24. #include <vector>
  25. #include "absl/base/config.h"
  26. #include "absl/random/bernoulli_distribution.h"
  27. #include "absl/random/internal/iostream_state_saver.h"
  28. #include "absl/random/uniform_int_distribution.h"
  29. namespace absl {
  30. ABSL_NAMESPACE_BEGIN
  31. // absl::discrete_distribution
  32. //
  33. // A discrete distribution produces random integers i, where 0 <= i < n
  34. // distributed according to the discrete probability function:
  35. //
  36. // P(i|p0,...,pn−1)=pi
  37. //
  38. // This class is an implementation of discrete_distribution (see
  39. // [rand.dist.samp.discrete]).
  40. //
  41. // The algorithm used is Walker's Aliasing algorithm, described in Knuth, Vol 2.
  42. // absl::discrete_distribution takes O(N) time to precompute the probabilities
  43. // (where N is the number of possible outcomes in the distribution) at
  44. // construction, and then takes O(1) time for each variate generation. Many
  45. // other implementations also take O(N) time to construct an ordered sequence of
  46. // partial sums, plus O(log N) time per variate to binary search.
  47. //
  48. template <typename IntType = int>
  49. class discrete_distribution {
  50. public:
  51. using result_type = IntType;
  52. class param_type {
  53. public:
  54. using distribution_type = discrete_distribution;
  55. param_type() { init(); }
  56. template <typename InputIterator>
  57. explicit param_type(InputIterator begin, InputIterator end)
  58. : p_(begin, end) {
  59. init();
  60. }
  61. explicit param_type(std::initializer_list<double> weights) : p_(weights) {
  62. init();
  63. }
  64. template <class UnaryOperation>
  65. explicit param_type(size_t nw, double xmin, double xmax,
  66. UnaryOperation fw) {
  67. if (nw > 0) {
  68. p_.reserve(nw);
  69. double delta = (xmax - xmin) / static_cast<double>(nw);
  70. assert(delta > 0);
  71. double t = delta * 0.5;
  72. for (size_t i = 0; i < nw; ++i) {
  73. p_.push_back(fw(xmin + i * delta + t));
  74. }
  75. }
  76. init();
  77. }
  78. const std::vector<double>& probabilities() const { return p_; }
  79. size_t n() const { return p_.size() - 1; }
  80. friend bool operator==(const param_type& a, const param_type& b) {
  81. return a.probabilities() == b.probabilities();
  82. }
  83. friend bool operator!=(const param_type& a, const param_type& b) {
  84. return !(a == b);
  85. }
  86. private:
  87. friend class discrete_distribution;
  88. void init();
  89. std::vector<double> p_; // normalized probabilities
  90. std::vector<std::pair<double, size_t>> q_; // (acceptance, alternate) pairs
  91. static_assert(std::is_integral<result_type>::value,
  92. "Class-template absl::discrete_distribution<> must be "
  93. "parameterized using an integral type.");
  94. };
  95. discrete_distribution() : param_() {}
  96. explicit discrete_distribution(const param_type& p) : param_(p) {}
  97. template <typename InputIterator>
  98. explicit discrete_distribution(InputIterator begin, InputIterator end)
  99. : param_(begin, end) {}
  100. explicit discrete_distribution(std::initializer_list<double> weights)
  101. : param_(weights) {}
  102. template <class UnaryOperation>
  103. explicit discrete_distribution(size_t nw, double xmin, double xmax,
  104. UnaryOperation fw)
  105. : param_(nw, xmin, xmax, std::move(fw)) {}
  106. void reset() {}
  107. // generating functions
  108. template <typename URBG>
  109. result_type operator()(URBG& g) { // NOLINT(runtime/references)
  110. return (*this)(g, param_);
  111. }
  112. template <typename URBG>
  113. result_type operator()(URBG& g, // NOLINT(runtime/references)
  114. const param_type& p);
  115. const param_type& param() const { return param_; }
  116. void param(const param_type& p) { param_ = p; }
  117. result_type(min)() const { return 0; }
  118. result_type(max)() const {
  119. return static_cast<result_type>(param_.n());
  120. } // inclusive
  121. // NOTE [rand.dist.sample.discrete] returns a std::vector<double> not a
  122. // const std::vector<double>&.
  123. const std::vector<double>& probabilities() const {
  124. return param_.probabilities();
  125. }
  126. friend bool operator==(const discrete_distribution& a,
  127. const discrete_distribution& b) {
  128. return a.param_ == b.param_;
  129. }
  130. friend bool operator!=(const discrete_distribution& a,
  131. const discrete_distribution& b) {
  132. return a.param_ != b.param_;
  133. }
  134. private:
  135. param_type param_;
  136. };
  137. // --------------------------------------------------------------------------
  138. // Implementation details only below
  139. // --------------------------------------------------------------------------
  140. namespace random_internal {
  141. // Using the vector `*probabilities`, whose values are the weights or
  142. // probabilities of an element being selected, constructs the proportional
  143. // probabilities used by the discrete distribution. `*probabilities` will be
  144. // scaled, if necessary, so that its entries sum to a value sufficiently close
  145. // to 1.0.
  146. std::vector<std::pair<double, size_t>> InitDiscreteDistribution(
  147. std::vector<double>* probabilities);
  148. } // namespace random_internal
  149. template <typename IntType>
  150. void discrete_distribution<IntType>::param_type::init() {
  151. if (p_.empty()) {
  152. p_.push_back(1.0);
  153. q_.emplace_back(1.0, 0);
  154. } else {
  155. assert(n() <= (std::numeric_limits<IntType>::max)());
  156. q_ = random_internal::InitDiscreteDistribution(&p_);
  157. }
  158. }
  159. template <typename IntType>
  160. template <typename URBG>
  161. typename discrete_distribution<IntType>::result_type
  162. discrete_distribution<IntType>::operator()(
  163. URBG& g, // NOLINT(runtime/references)
  164. const param_type& p) {
  165. const auto idx = absl::uniform_int_distribution<result_type>(0, p.n())(g);
  166. const auto& q = p.q_[idx];
  167. const bool selected = absl::bernoulli_distribution(q.first)(g);
  168. return selected ? idx : static_cast<result_type>(q.second);
  169. }
  170. template <typename CharT, typename Traits, typename IntType>
  171. std::basic_ostream<CharT, Traits>& operator<<(
  172. std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
  173. const discrete_distribution<IntType>& x) {
  174. auto saver = random_internal::make_ostream_state_saver(os);
  175. const auto& probabilities = x.param().probabilities();
  176. os << probabilities.size();
  177. os.precision(random_internal::stream_precision_helper<double>::kPrecision);
  178. for (const auto& p : probabilities) {
  179. os << os.fill() << p;
  180. }
  181. return os;
  182. }
  183. template <typename CharT, typename Traits, typename IntType>
  184. std::basic_istream<CharT, Traits>& operator>>(
  185. std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
  186. discrete_distribution<IntType>& x) { // NOLINT(runtime/references)
  187. using param_type = typename discrete_distribution<IntType>::param_type;
  188. auto saver = random_internal::make_istream_state_saver(is);
  189. size_t n;
  190. std::vector<double> p;
  191. is >> n;
  192. if (is.fail()) return is;
  193. if (n > 0) {
  194. p.reserve(n);
  195. for (IntType i = 0; i < n && !is.fail(); ++i) {
  196. auto tmp = random_internal::read_floating_point<double>(is);
  197. if (is.fail()) return is;
  198. p.push_back(tmp);
  199. }
  200. }
  201. x.param(param_type(p.begin(), p.end()));
  202. return is;
  203. }
  204. ABSL_NAMESPACE_END
  205. } // namespace absl
  206. #endif // ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_