AutoPas  3.0.0
Loading...
Searching...
No Matches
Random.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <algorithm>
11#include <iterator>
12#include <random>
13#include <set>
14
16
17namespace autopas {
18
22class Random : public std::mt19937 {
23 public:
28 explicit Random(unsigned long seed = std::random_device()()) : std::mt19937(seed) {}
29
34 Random(const Random &other) = delete;
35
41 Random &operator=(const Random &other) = delete;
42
52 template <class Iter>
53 std::vector<typename std::iterator_traits<Iter>::value_type> uniformSample(Iter poolBegin, Iter poolEnd, size_t n) {
54 if (poolBegin == poolEnd) {
55 autopas::utils::ExceptionHandler::exception("Random.uniformSample: Cannot sample from empty set.");
56 }
57
58 std::vector<typename std::iterator_traits<Iter>::value_type> result;
59 result.reserve(n);
60
61 // copy the whole set until result is full
62 while (result.size() < n) {
63 result.insert(std::end(result), poolBegin, poolEnd);
64 }
65
66 // if too many elements added
67 if (result.size() > n) {
68 // randomize the last copy of the set
69 size_t extra = result.size() - n;
70 std::shuffle(std::end(result) - extra, std::end(result), *this);
71
72 // truncate the rest
73 result.resize(n);
74 }
75
76 // randomize the sample
77 std::shuffle(std::begin(result), std::end(result), *this);
78
79 return result;
80 }
81
90 std::vector<size_t> uniformSample(size_t min, size_t max, size_t n) {
91 std::set<size_t> allAllowed;
92 for (size_t i = min; i <= max; ++i) {
93 allAllowed.insert(i);
94 }
95 return uniformSample(allAllowed.begin(), allAllowed.end(), n);
96 }
97
106 template <class Container>
107 auto pickRandom(const Container &pool) {
108 std::uniform_int_distribution<size_t> distr(0ul, pool.size() - 1ul);
109 size_t pos = distr(*this);
110
111 auto it = std::begin(pool);
112 std::advance(it, pos);
113
114 return *it;
115 }
116
124 template <class T>
125 std::set<T> randomSubset(std::set<T> pool, size_t n) {
126 size_t size = std::min(n, pool.size());
127
128 // create randomly shuffled vector of points to all elements in the pool
129 std::vector<const T *> pointerVec;
130 pointerVec.reserve(pool.size());
131 for (const T &element : pool) {
132 pointerVec.push_back(&element);
133 }
134 std::shuffle(pointerVec.begin(), pointerVec.end(), *this);
135
136 // return first elements of the shuffled vector
137 std::set<T> result;
138 for (size_t i = 0; i < size; ++i) {
139 result.insert(*pointerVec[i]);
140 }
141
142 return result;
143 }
144};
145
146} // namespace autopas
Class for random algorithms.
Definition: Random.h:22
Random(const Random &other)=delete
Class should not be copied constructed.
Random(unsigned long seed=std::random_device()())
Constructor.
Definition: Random.h:28
std::set< T > randomSubset(std::set< T > pool, size_t n)
Pick up to n random elements from the set.
Definition: Random.h:125
Random & operator=(const Random &other)=delete
Class should not be copied assigned.
auto pickRandom(const Container &pool)
Get a uniformly randomly selected object from the given container.
Definition: Random.h:107
std::vector< size_t > uniformSample(size_t min, size_t max, size_t n)
Sample n points from the set {min;min+1;...;max}.
Definition: Random.h:90
std::vector< typename std::iterator_traits< Iter >::value_type > uniformSample(Iter poolBegin, Iter poolEnd, size_t n)
Sample n points from the pool.
Definition: Random.h:53
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32