bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
space_type.h
Go to the documentation of this file.
1#ifndef SPACE_TYPE_H
2#define SPACE_TYPE_H
3
5#include <random>
6#include <vector>
7
8#include <type_traits>
9
10namespace bitrl
11{
12namespace envs
13{
14
21template <uint_t s, uint_t e> struct ScalarDiscreteSpace
22{
23
28
33
38 static constexpr uint_t size = e - s; // IntegralRange<s, e>::size;
39
44 static space_item_type sample(bool use_end);
45
52 static space_item_type sample(uint_t seed, bool use_end);
53
61 static std::vector<space_item_type> sample(uint_t seed, uint_t size, bool use_end);
62};
63
64template <uint_t s, uint_t e>
66{
67
69
70 if (!use_end)
71 {
72 E -= 1;
73 }
74
75 std::uniform_int_distribution<> dist(IntegralRange<s, e>::S, E);
76 std::random_device rd;
77 std::mt19937 gen(rd());
78 return dist(gen);
79}
80
81template <uint_t s, uint_t e>
83 bool use_end)
84{
85
87
88 if (!use_end)
89 {
90 E -= 1;
91 }
92
93 std::uniform_int_distribution<> dist(IntegralRange<s, e>::S, E);
94 std::mt19937 gen(seed);
95 return dist(gen);
96}
97
98template <uint_t s, uint_t e>
99std::vector<typename ScalarDiscreteSpace<s, e>::space_item_type>
101{
102
103 auto E = IntegralRange<s, e>::E;
104
105 if (!use_end)
106 {
107 E -= 1;
108 }
109
110 std::vector<typename ScalarDiscreteSpace<s, e>::space_item_type> vals_;
111 vals_.reserve(size);
112
113 std::uniform_int_distribution<> dist(IntegralRange<s, e>::S, E);
114 std::mt19937 gen(seed);
115 for (uint_t i = 0; i < size; ++i)
116 {
117 vals_.push_back(dist(gen));
118 }
119
120 return vals_;
121}
122
123template <uint_t Size> struct ContinuousScalareSpace
124{
129
134 static constexpr uint_t size = Size;
135};
136
137template <uint_t Size, typename T = real_t> struct ContinuousVectorSpace
138{
139 static_assert(std::is_floating_point_v<T> == true && "Floating point type is expected");
140
145 static constexpr uint_t size = Size;
146
150 typedef std::vector<T> space_item_type;
151};
152
153template <uint_t SpaceSize, typename T = uint_t> struct DiscreteVectorSpace
154{
155 static_assert(std::is_integral_v<T> == true && "Integral type is expected");
156
160 typedef std::vector<T> space_item_type;
161
165 static constexpr uint_t size = SpaceSize;
166};
167
168} // namespace envs
169} // namespace bitrl
170#endif
Definition cart_pole.h:18
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Definition space_type.h:124
static constexpr uint_t size
The overall size of the space meaning how many elements can potentially the space have.
Definition space_type.h:134
real_t space_item_type
item_t
Definition space_type.h:128
Definition space_type.h:138
std::vector< StateSpaceItemType > space_item_type
Definition space_type.h:150
static constexpr uint_t size
Definition space_type.h:145
Definition space_type.h:154
std::vector< StateSpaceItemType > space_item_type
Definition space_type.h:160
static constexpr uint_t size
Definition space_type.h:165
A scalar discrete space can be used to denote a space that only has a single value at each time....
Definition space_type.h:22
static constexpr IntegralRange< s, e > limits
Definition space_type.h:32
static std::vector< space_item_type > sample(uint_t seed, uint_t size, bool use_end)
sample
Definition space_type.h:100
static space_item_type sample(uint_t seed, bool use_end)
sample
Definition space_type.h:82
static space_item_type sample(bool use_end)
sample
Definition space_type.h:65
static constexpr uint_t size
Definition space_type.h:38
A range of integer values in [s, e].
Definition bitsim_types.h:155
static constexpr uint_t E
Definition bitsim_types.h:158
static constexpr uint_t S
Definition bitsim_types.h:157