Brush C++ API
A flexible interpretable machine learning framework
Toggle main menu visibility
Loading...
Searching...
No Matches
rnd.h
Go to the documentation of this file.
1
/* Brush
2
copyright 2020 William La Cava
3
license: GNU/GPL v3
4
*/
5
6
#ifndef RND_H
7
#define RND_H
8
//external includes
9
#include <random>
10
#include <limits>
11
#include <vector>
12
13
#include "
../init.h
"
14
15
// Defines a multi-core random number generator and its operators.
16
17
using namespace
std
;
18
using
std::swap;
19
20
namespace
Brush
{
namespace
Util
{
21
23
28
class
Rnd
29
{
30
public
:
31
32
static
Rnd
*
initRand
();
33
34
static
void
destroy
();
35
36
void
set_seed
(
unsigned
int
seed);
37
38
int
rnd_int
(
int
lowerLimit,
int
upperLimit );
39
40
float
rnd_flt
(
float
min=0.0,
float
max=1.0);
41
42
float
rnd_alpha_beta
(
float
alpha,
float
beta);
43
44
float
rnd_dbl
(
float
min=0.0,
float
max=1.0);
45
46
float
operator()
(
unsigned
i);
47
48
float
operator()
();
49
50
template
<
class
RandomAccessIterator>
51
void
shuffle
(RandomAccessIterator first, RandomAccessIterator last)
52
{
53
for
(
auto
i=(last-first)-1; i>0; --i)
54
{
55
std::uniform_int_distribution<
decltype
(i)> d(0,i);
56
swap (first[i], first[d(
rg
[
omp_get_thread_num
()])]);
57
}
58
}
59
60
vector<size_t>
shuffled_index
(
size_t
n);
61
62
template
<
typename
Iter>
63
Iter
select_randomly
(
Iter
start,
Iter
end)
64
{
65
std::uniform_int_distribution<> dis(0, distance(start, end) - 1);
66
advance(start, dis(
rg
[
omp_get_thread_num
()]));
67
return
start;
68
}
69
71
// The probability of picking the i-th element is w_i/S, with S
72
// being the sum of all weights. select_randomly works even if the
73
// weights does not sum up to 1
74
template
<
typename
Iter,
typename
Iter2>
75
Iter
select_randomly
(
Iter
start,
Iter
end, Iter2 wstart, Iter2 wend)
76
{
77
// discrete_distribution creates a Generator for a probability
78
// distribution function. `dis` generate integers from [0, s),
79
// where `s` is the number of probabilities in the iterator
80
// passed as argument. To generate a new value, the
81
// `operator()( Generator& g )` needs a uniform random bit
82
// generator object, which is stored in `rg`.
83
84
// std::uniform_int_distribution<> dis(0, distance(start, end) - 1);
85
std::discrete_distribution<size_t> dis(wstart, wend);
86
87
// `advance(it, n)` increments the iterator by n elements
88
advance(start, dis(
rg
[
omp_get_thread_num
()]));
89
90
// start was originally an iterator pointing to the beggining
91
// of the sequence we want to take a random value. It is incremented
92
// to point to a random element, with probabilities taken from
93
// the second iterator Iter2.
94
return
start;
95
}
96
97
template
<
typename
T>
98
T
random_choice
(
const
map<T, float>& m)
99
{
104
105
assert(m.size()>0
106
&&
" attemping to return random choice from an empty map"
);
107
108
vector<T> keys;
109
vector<float> w;
110
for
(
const
auto
& [k, v]: m)
111
{
112
keys.push_back(k);
113
w.push_back(v);
114
}
115
return
*
select_randomly
(keys.begin(),keys.end(),
116
w.begin(), w.end());
117
}
118
template
<
class
V,
class
T>
119
T
random_choice
(
const
V& v)
120
{
124
assert(v.size()>0
125
&&
" attemping to return random choice from empty vector"
);
126
return
*
select_randomly
(v.begin(),v.end());
127
}
128
129
template
<
template
<
class
,
class
>
class
C,
class
T>
130
T
random_choice
(
const
C<T, std::allocator<T>>& v,
const
vector<float>& w )
131
{
135
136
if
(w.size() == 0)
137
{
138
fmt::format(
"w size = {} and v size = {}, returning uniform random choice\n"
,
139
w.size(), v.size());
140
return
random_choice
(v);
141
}
142
if
(w.size() != v.size())
143
{
144
fmt::format(
"w ({}) != v size ({}), returning uniform random choice\n"
,
145
w.size(), v.size());
146
return
random_choice
(v);
147
}
148
else
149
{
150
assert(v.size() == w.size());
151
std::discrete_distribution<size_t> dis(w.begin(), w.end());
152
return
v.at(dis(
rg
[
omp_get_thread_num
()]));
153
}
154
}
155
156
float
gasdev
();
157
158
private
:
159
160
Rnd
();
161
162
~Rnd
();
163
164
// Vector of pseudo-random number generators, one for each thread
165
vector<std::mt19937>
rg
;
166
167
// private static attribute used by every instance of the class.
168
// All threads share common static members of the class
169
static
Rnd
*
instance
;
170
};
171
172
// `Brush.Util` static attribute holding an singleton instance of Rnd.
173
// the instance is created by calling `initRand`, which creates
174
// an instance of the private static attribute `instance`. `r` will contain
175
// one generator for each thread (since it called the constructor)
176
static
Rnd
&
r
= *
Rnd::initRand
();
177
}
// Util
178
}
// Brush
179
#endif
Brush::Util::Rnd
Defines a multi-core random number generator and its operators.
Definition
rnd.h:29
Brush::Util::Rnd::random_choice
T random_choice(const map< T, float > &m)
Definition
rnd.h:98
Brush::Util::Rnd::rnd_alpha_beta
float rnd_alpha_beta(float alpha, float beta)
Definition
rnd.cpp:80
Brush::Util::Rnd::random_choice
T random_choice(const V &v)
Definition
rnd.h:119
Brush::Util::Rnd::shuffle
void shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition
rnd.h:51
Brush::Util::Rnd::rnd_flt
float rnd_flt(float min=0.0, float max=1.0)
Definition
rnd.cpp:74
Brush::Util::Rnd::initRand
static Rnd * initRand()
Definition
rnd.cpp:26
Brush::Util::Rnd::shuffled_index
vector< size_t > shuffled_index(size_t n)
returns a shuffled index vector of length n
Definition
rnd.cpp:147
Brush::Util::Rnd::~Rnd
~Rnd()
Definition
rnd.cpp:154
Brush::Util::Rnd::select_randomly
Iter select_randomly(Iter start, Iter end, Iter2 wstart, Iter2 wend)
select randomly with weighted distribution.
Definition
rnd.h:75
Brush::Util::Rnd::set_seed
void set_seed(unsigned int seed)
Definition
rnd.cpp:46
Brush::Util::Rnd::destroy
static void destroy()
Definition
rnd.cpp:38
Brush::Util::Rnd::rg
vector< std::mt19937 > rg
Definition
rnd.h:165
Brush::Util::Rnd::Rnd
Rnd()
Definition
rnd.cpp:12
Brush::Util::Rnd::random_choice
T random_choice(const C< T, std::allocator< T > > &v, const vector< float > &w)
Definition
rnd.h:130
Brush::Util::Rnd::select_randomly
Iter select_randomly(Iter start, Iter end)
Definition
rnd.h:63
Brush::Util::Rnd::instance
static Rnd * instance
Definition
rnd.h:169
Brush::Util::Rnd::rnd_dbl
float rnd_dbl(float min=0.0, float max=1.0)
Definition
rnd.cpp:106
Brush::Util::Rnd::operator()
float operator()()
Definition
rnd.cpp:117
Brush::Util::Rnd::gasdev
float gasdev()
Definition
rnd.cpp:119
Brush::Util::Rnd::rnd_int
int rnd_int(int lowerLimit, int upperLimit)
Definition
rnd.cpp:68
init.h
omp_get_thread_num
#define omp_get_thread_num()
Definition
init.h:12
Brush::Util
namespace containing various utility functions
Definition
error.cpp:11
Brush::Util::r
static Rnd & r
Definition
rnd.h:176
Brush
< nsga2 selection operator for getting the front
Definition
bandit.cpp:3
Brush::Iter
tree< Node >::pre_order_iterator Iter
Definition
program.h:36
std
STL namespace.
src
util
rnd.h
Generated by
1.17.0