bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
rl_mixins.h
Go to the documentation of this file.
1#ifndef RL_MIXINS_H
2#define RL_MIXINS_H
7#include "bitrl/base/bitrl_config.h"
11
12#ifdef BITRL_DEBUG
13#include <cassert>
14#endif
15
16#include <map>
17#include <random>
18#include <tuple>
19#include <vector>
20
21namespace bitrl::rl
22{
23
24namespace
25{
26
27template <typename StateTp>
28DynVec<real_t> get_table_values_(const DynMat<real_t> &table, const StateTp &state)
29{
30 return table.row(state);
31}
32
33template <typename StateTp>
34const DynVec<real_t> &get_table_values_(const std::map<StateTp, DynVec<real_t>> &table,
35 const StateTp &state)
36{
37
38 auto itr = table.find(state);
39#ifdef CUBEAI_DEBUG
40 if (itr == table.end())
41 {
42 assert(false && "Invalid state given");
43 }
44#endif
45
46 return itr->second;
47}
48
49template <typename StateTp>
50DynVec<real_t> &get_table_values_(std::map<StateTp, DynVec<real_t>> &table, const StateTp &state)
51{
52
53 auto itr = table.find(state);
54#ifdef BITRL_DEBUG
55 if (itr == table.end())
56 {
57 assert(false && "Invalid state given");
58 }
59#endif
60
61 return itr->second;
62}
63
64} // namespace
65
73uint_t max_action(const DynMat<real_t> &qtable, uint_t state, uint_t n_actions);
74
79{
88
93 mutable std::mt19937 generator{};
94
98 void seed_generator() { generator.seed(seed); }
99
105 real_t decay_eps(uint_t episode_index);
106
110 template <typename VectorType> uint_t choose_action_index(const VectorType &values) const;
111};
112
113template <typename VectorType>
115{
116
117 // generate a number in [0, 1]
118 std::uniform_real_distribution<> real_dist_(0.0, 1.0);
119
120 if (real_dist_(generator) > this->with_decay_epsilon_option_mixin::eps)
121 {
122 // select greedy action with probability 1 - epsilon
123 return maths::arg_max(values);
124 }
125
126 std::uniform_int_distribution<uint_t> distrib_(
128 return distrib_(generator);
129}
130
135{
139
144
151 void initialize(state_type n_states, action_type n_actions, real_t init_value);
152};
153
154template <typename TableTp> struct with_double_q_table_mixin;
155
160{
165
170
175
182 void initialize(const std::vector<index_type> &indices, action_type n_actions,
183 real_t init_value);
184
189 void initialize(state_type n_states, action_type n_actions, real_t init_value)
190 {
191 q_table_1 = DynMat<value_type>::Constant(n_states, n_actions, init_value);
192 q_table_2 = DynMat<value_type>::Constant(n_states, n_actions, init_value);
193 }
194
198 template <int index> value_type get(const state_type &state, const action_type action) const;
199
200 template <int index>
201 void set(const state_type &state, const action_type action, const value_type value);
202};
203
204template <>
207 const action_type action) const
208{
209 return q_table_1(state, action);
210}
211
212template <>
215 const action_type action) const
216{
217 return q_table_2(state, action);
218}
219
220template <>
222 const action_type action,
223 const value_type value)
224{
225 q_table_1(state, action) = value;
226}
227
228template <>
230 const action_type action,
231 const value_type value)
232{
233 q_table_2(state, action) = value;
234}
235
236template <typename KeyTp> struct with_double_q_table_mixin<std::map<KeyTp, DynVec<real_t>>>
237{
238
239 typedef KeyTp index_type;
240 typedef KeyTp state_type;
243
247 std::map<KeyTp, DynVec<real_t>> q_table_1;
248
252 std::map<KeyTp, DynVec<real_t>> q_table_2;
253
260 void initialize(const std::vector<index_type> &indices, action_type n_actions,
261 real_t init_value);
262
266 template <int index> value_type get(const state_type &state, const action_type action) const;
267
271 template <int index>
272 void set(const state_type &state, const action_type action, const value_type value);
273};
274
275template <typename KeyTp>
277 const std::vector<index_type> &indices, action_type n_actions, real_t init_value)
278{
279
280 DynVec<real_t> init_vals(n_actions, init_value);
281
282 for (uint_t i = 0; i < indices.size(); ++i)
283 {
284
285 q_table_1[indices[i]] = init_vals;
286 q_table_2[indices[i]] = init_vals;
287 }
288}
289
290template <typename KeyTp>
291template <int index>
294 const action_type action) const
295{
296
297 static_assert(index == 1 || index == 2, "Invalid index for template parameter");
298 if (index == 1)
299 {
300 return get_table_values_(q_table_1, state)[action];
301 }
302
303 return get_table_values_(q_table_2, state)[action];
304}
305
306template <typename KeyTp>
307template <int index>
309 const action_type action,
310 const value_type value)
311{
312
313 static_assert(index == 1 || index == 2, "Invalid index for template parameter");
314
315 if (index == 1)
316 {
317 auto &vals1 = get_table_values_(q_table_1, state);
318 vals1[action] = value;
319 }
320
321 auto &vals2 = get_table_values_(q_table_2, state);
322 vals2[action] = value;
323}
324
326{
327
331 template <typename TableTp, typename StateTp>
332 static uint_t max_action(const TableTp &q1_table, const TableTp &q2_table, const StateTp &state,
333 uint_t n_actions);
334
338 template <typename TableTp, typename StateTp>
339 static uint_t max_action(const TableTp &q1_table, const StateTp &state, uint_t n_actions);
340};
341
342template <typename TableTp, typename StateTp>
344 const TableTp &q2_table,
345 const StateTp &state, uint_t /*n_actions*/)
346{
347
348 const DynVec<real_t> sum =
349 get_table_values_(q1_table, state) + get_table_values_(q2_table, state);
350 Eigen::Index idx = 0;
351 sum.maxCoeff(&idx);
352 return static_cast<uint_t>(idx);
353}
354
355template <typename TableTp, typename StateTp>
357 const StateTp &state, uint_t /*n_actions*/)
358{
359
360 const DynVec<real_t> vals = get_table_values_(q_table, state);
361 Eigen::Index idx = 0;
362 vals.maxCoeff(&idx);
363 return static_cast<uint_t>(idx);
364}
365
366} // namespace bitrl::rl
367
368#endif // RL_MIXINS_H
uint_t arg_max(const VectorType &vec)
Returns the index of the element that has the maximum value in the array. Implementation taken from h...
Definition vector_math.h:383
Definition dummy_agent.h:9
EpsilonDecayOptionType
Definition epsilon_decay_options.h:12
uint_t max_action(const DynMat< real_t > &qtable, uint_t state, uint_t n_actions)
max_action
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Eigen::MatrixX< T > DynMat
Dynamically sized matrix to use around the library.
Definition bitsim_types.h:51
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101
The with_decay_epsilon_option_mixin struct.
Definition rl_mixins.h:79
uint_t seed
Definition rl_mixins.h:86
real_t min_eps
Definition rl_mixins.h:82
EpsilonDecayOptionType decay_op
Definition rl_mixins.h:87
real_t epsilon_decay
Definition rl_mixins.h:84
uint_t n_actions
Definition rl_mixins.h:85
uint_t choose_action_index(const VectorType &values) const
Definition rl_mixins.h:114
std::mt19937 generator
The generator of the random numbers. seed_generator() seeds it with seed; a new generator in every ca...
Definition rl_mixins.h:93
void seed_generator()
seed_generator. Seeds the generator with seed
Definition rl_mixins.h:98
real_t max_eps
Definition rl_mixins.h:83
real_t eps
Definition rl_mixins.h:81
real_t eps_init
Definition rl_mixins.h:80
real_t decay_eps(uint_t episode_index)
decay_eps
static uint_t max_action(const TableTp &q1_table, const TableTp &q2_table, const StateTp &state, uint_t n_actions)
Returns the max action by averaging the state values from the two tables.
Definition rl_mixins.h:343
void set(const state_type &state, const action_type action, const value_type value)
DynMat< value_type > q_table_1
q_table_1
Definition rl_mixins.h:169
DynMat< value_type > q_table_2
q_table_2
Definition rl_mixins.h:174
value_type get(const state_type &state, const action_type action) const
void initialize(const std::vector< index_type > &indices, action_type n_actions, real_t init_value)
initialize
void initialize(state_type n_states, action_type n_actions, real_t init_value)
initialize. Sizes both tables to n_states x n_actions and sets every entry to init_value
Definition rl_mixins.h:189
void initialize(const std::vector< index_type > &indices, action_type n_actions, real_t init_value)
initialize
Definition rl_mixins.h:276
std::map< KeyTp, DynVec< real_t > > q_table_2
q_table_2
Definition rl_mixins.h:252
std::map< KeyTp, DynVec< real_t > > q_table_1
q_table_1
Definition rl_mixins.h:247
void set(const state_type &state, const action_type action, const value_type value)
Definition rl_mixins.h:308
value_type get(const state_type &state, const action_type action) const
Definition rl_mixins.h:154
The WithQTableMixin struct.
Definition rl_mixins.h:135
uint_t action_type
Definition rl_mixins.h:137
uint_t state_type
Definition rl_mixins.h:136
void initialize(state_type n_states, action_type n_actions, real_t init_value)
initialize
DynMat< value_type > q_table
q_table
Definition rl_mixins.h:143
real_t value_type
Definition rl_mixins.h:138