QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
qbpp_misc.hpp
Go to the documentation of this file.
1 
10 #ifndef QBPP_MISC_HPP
11 #define QBPP_MISC_HPP
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <iostream>
16 #include <numeric>
17 #include <random>
18 #include <vector>
19 
20 #include "qbpp.hpp"
21 
22 namespace qbpp {
23 
24 namespace misc {
25 
27  std::mt19937_64 mt;
28 
29  RandomGenerator() : mt(std::random_device{}()) {}
30 
31  RandomGenerator(const RandomGenerator &) = delete;
32 
34 
36  static RandomGenerator instance;
37  return instance;
38  }
39 
40  public:
41  static void set_seed(uint32_t seed = 1) { get_instance().mt.seed(seed); }
42 
43  static void rd_seed() { get_instance().mt.seed(std::random_device{}()); }
44 
45  static uint64_t gen() { return static_cast<uint64_t>(get_instance().mt()); }
46 
47  template <typename T>
48  static typename std::enable_if<std::is_integral<T>::value, T>::type gen(T n) {
49  std::uniform_int_distribution<qbpp::vindex_t> dist(0, n - 1);
50  return dist(get_instance().mt);
51  }
52 
53  static qbpp::cpp_int gen(const qbpp::cpp_int &n) {
54  qbpp::cpp_int val;
55  do {
56  val = (val << 64) + gen();
57  } while (val < (n << 32));
58  return val % n;
59  }
60 
61  static double gen_double() {
62  std::uniform_real_distribution<double> dist(0.0, 1.0);
63  return dist(get_instance().mt);
64  }
65 
66  static std::mt19937_64 &get_mt() { return get_instance().mt; }
67 };
68 
69 
71  const uint32_t size_;
72 
73  std::vector<uint32_t> perm;
74 
75  uint32_t index = 0;
76 
77  public:
78  explicit RandomPermutation(uint32_t size) : size_(size), perm(size) {
79  std::iota(perm.begin(), perm.end(), 0);
80  }
81 
82  uint32_t get() {
83  if (index == 0) {
84  std::shuffle(perm.begin(), perm.end(), RandomGenerator::get_mt());
85  }
86  uint32_t current = index;
87  index = (index + 1) % size_;
88  return perm[current];
89  }
90 };
91 
92 struct PcloseDeleter {
93  void operator()(FILE *file) const {
94  if (file) pclose(file);
95  }
96 };
97 
98 }
99 }
100 
101 #endif
RandomGenerator & operator=(const RandomGenerator &)=delete
static std::mt19937_64 & get_mt()
Definition: qbpp_misc.hpp:66
static void set_seed(uint32_t seed=1)
Definition: qbpp_misc.hpp:41
RandomGenerator(const RandomGenerator &)=delete
static RandomGenerator & get_instance()
Definition: qbpp_misc.hpp:35
static double gen_double()
Definition: qbpp_misc.hpp:61
static std::enable_if< std::is_integral< T >::value, T >::type gen(T n)
Definition: qbpp_misc.hpp:48
static uint64_t gen()
Definition: qbpp_misc.hpp:45
static qbpp::cpp_int gen(const qbpp::cpp_int &n)
Definition: qbpp_misc.hpp:53
std::vector< uint32_t > perm
Definition: qbpp_misc.hpp:73
RandomPermutation(uint32_t size)
Definition: qbpp_misc.hpp:78
Generates a QUBO Expression for the Graph Coloring Problem using QUBO++ library.
Definition: qbpp.hpp:53
boost::multiprecision::cpp_int cpp_int
Definition: qbpp.hpp:112
QUBO++, a C++ library for generating expressions for binary and spin variables.
void operator()(FILE *file) const
Definition: qbpp_misc.hpp:93