bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
uniform_distribution.h
Go to the documentation of this file.
1#ifndef UNIFORM_DIST_H
2#define UNIFORM_DIST_H
3
5#include "bitrl/bitrl_types.h"
6
7#include <initializer_list>
8#include <random>
9#include <utility>
10#include <vector>
11
12namespace bitrl
13{
14namespace utils
15{
16namespace maths
17{
18namespace stats
19{
20
21namespace
22{
23template <typename t> struct uniform_distribution_selector;
24
25template <> struct uniform_distribution_selector<uint_t>
26{
27 typedef std::uniform_int_distribution<uint_t> distribution_type;
28};
29
30template <> struct uniform_distribution_selector<int_t>
31{
32 typedef std::uniform_int_distribution<int_t> distribution_type;
33};
34
35template <> struct uniform_distribution_selector<lint_t>
36{
37 typedef std::uniform_int_distribution<lint_t> distribution_type;
38};
39
40template <> struct uniform_distribution_selector<real_t>
41{
42 typedef std::uniform_real_distribution<real_t> distribution_type;
43};
44
45template <> struct uniform_distribution_selector<float_t>
46{
47 typedef std::uniform_real_distribution<float_t> distribution_type;
48};
49
50} // namespace
51
56template <typename OutType> class UniformDist
57{
58
59 typedef OutType out_type;
60 typedef std::pair<out_type, out_type> range_type;
61
65 UniformDist(out_type a, out_type b);
66
70 UniformDist(range_type range);
71
75 out_type sample();
76
80 out_type sample(uint_t seed);
81
85 range_type range() const { return range_; }
86
87 private:
88 range_type range_;
90};
91
92template <typename OutType>
93UniformDist<OutType>::UniformDist(typename UniformDist<OutType>::out_type a,
94 typename UniformDist<OutType>::out_type b)
95 : range_(a, b), distribution_(a, b)
96{
97}
98
99template <typename OutType>
100UniformDist<OutType>::UniformDist(typename UniformDist<OutType>::range_type range)
101 : UniformDist<OutType>(range.first, range.second)
102{
103}
104
105template <typename OutType> typename UniformDist<OutType>::out_type UniformDist<OutType>::sample()
106{
107
108 // a seed source for the random number engine
109 std::random_device rd;
110
111 // mersenne_twister_engine seeded with rd()
112 std::mt19937 gen(rd());
113
114 return distribution_(gen);
115}
116
117template <typename OutType>
118typename UniformDist<OutType>::out_type UniformDist<OutType>::sample(uint_t seed)
119{
120 // mersenne_twister_engine seeded with seed
121 std::mt19937 gen(seed);
122 return distribution_(gen);
123}
124
125template <typename OutType, typename WeightType = real_t> class UniformWeightedDist
126{
127 public:
129 typedef std::vector<WeightType> weights_type;
130
135 template <typename VectorType> UniformWeightedDist(const VectorType &weights);
136
141 UniformWeightedDist(std::initializer_list<WeightType> weights);
142
147
151 out_type sample(uint_t seed);
152
156 std::vector<real_t> probabilities() const { return distribution_.probabilities(); }
157
158 private:
159 weights_type weights_;
160 std::discrete_distribution<out_type> distribution_;
161};
162
163template <typename OutType, typename WeightType>
164template <typename VectorType>
166 : weights_(weights), distribution_(weights.begin(), weights.end())
167{
168}
169
170template <typename OutType, typename WeightType>
172 std::initializer_list<WeightType> weights)
173 : weights_(weights), distribution_(weights)
174{
175}
176
177template <typename OutType, typename WeightType>
180{
181
182 // a seed source for the random number engine
183 std::random_device rd;
184
185 // mersenne_twister_engine seeded with rd()
186 std::mt19937 gen(rd());
187 return distribution_(gen);
188}
189
190template <typename OutType, typename WeightType>
193{
194 // mersenne_twister_engine seeded with seed
195 std::mt19937 gen(seed);
196 return distribution_(gen);
197}
198
199} // namespace stats
200} // namespace maths
201} // namespace utils
202} // namespace rlenvscpp
203
204#endif
Definition uniform_distribution.h:57
Definition uniform_distribution.h:126
UniformWeightedDist(const VectorType &weights)
Initialize with a list of weights see also: https://www.cplusplus.com/reference/random/discrete_distr...
Definition uniform_distribution.h:165
out_type sample()
Get a sample.
Definition uniform_distribution.h:179
std::vector< real_t > probabilities() const
Definition uniform_distribution.h:156
std::vector< WeightType > weights_type
Definition uniform_distribution.h:129
OutType out_type
Definition uniform_distribution.h:128
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
int int_t
integer type
Definition bitrl_types.h:33
long int lint_t
long int type
Definition bitrl_types.h:38
double real_t
real_t
Definition bitrl_types.h:23
std::size_t uint_t
uint_t
Definition bitrl_types.h:43
float float_t
float
Definition bitrl_types.h:28