bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
epsilon_greedy_policy.h
Go to the documentation of this file.
1#ifndef EPSILON_GREEDY_POLICY_H
2#define EPSILON_GREEDY_POLICY_H
3
4#include "bitrl/base/bitrl_config.h"
8
9#ifdef USE_PYTORCH
11#endif
12
13#include <cmath>
14#include <random>
15
16namespace bitrl
17{
18namespace rl
19{
20namespace policies
21{
22
33
38{
39 public:
44
45 constexpr static real_t MIN_EPS = 0.01;
46 constexpr static real_t MAX_EPS = 1.0;
47 constexpr static real_t EPSILON_DECAY_FACTOR = 0.01;
48
53
57 explicit EpsilonGreedyPolicy(real_t eps, uint_t seed);
58
63 explicit EpsilonGreedyPolicy(real_t eps, uint_t seed, EpsilonDecayOption decay_op,
64 real_t min_eps = MIN_EPS, real_t max_eps = MAX_EPS,
65 real_t epsilon_decay = EPSILON_DECAY_FACTOR);
66
70 template <typename MapType> output_type operator()(const MapType &q_map, uint_t state) const;
71
75 template <typename VecType> output_type operator()(const VecType &vec) const;
76
80 template <typename MatType> output_type get_action(const MatType &q_map, uint_t state_idx);
81
87 template <typename VecTp> output_type get_action(const VecTp &q_map);
88
89#ifdef USE_PYTORCH
94#endif
95
100 void on_episode(uint_t episode_idx) noexcept;
101
105 void reset() noexcept { eps_ = eps_init_; }
106
110 real_t eps_value() const noexcept { return eps_; }
111
117
121 EpsilonDecayOption decay_option() const noexcept { return decay_op_; }
122
123 private:
124 real_t eps_init_;
125 real_t eps_;
126 real_t min_eps_;
127 real_t max_eps_;
128 real_t epsilon_decay_;
129 EpsilonDecayOption decay_op_;
130
134 mutable std::mt19937 generator_;
135
136 // how to select the action
137 RandomTabularPolicy random_policy_;
138 MaxTabularPolicy max_policy_;
139};
140
142 EpsilonDecayOption decay_op, real_t min_eps,
143 real_t max_eps, real_t epsilon_decay)
144 : eps_init_(eps), eps_(eps), min_eps_(min_eps), max_eps_(max_eps),
145 epsilon_decay_(epsilon_decay), decay_op_(decay_op), generator_(seed), random_policy_(seed),
146 max_policy_()
147{
148}
149
151 : eps_init_(eps), eps_(eps), min_eps_(eps), max_eps_(eps), epsilon_decay_(eps),
152 decay_op_(EpsilonDecayOption::NONE), random_policy_(), max_policy_()
153{
154}
155
157 : EpsilonGreedyPolicy(eps, seed, EpsilonDecayOption::NONE, eps, eps, eps)
158{
159}
160
161template <typename VecType>
163{
164
165 // generate a number in [0, 1]
166 std::uniform_real_distribution<> real_dist_(0.0, 1.0);
167
168 if (real_dist_(generator_) > eps_)
169 {
170 // select greedy action with probability 1 - epsilon
171 return max_policy_.get_action(vec);
172 }
173
174 // else select a random action
175 return random_policy_(vec);
176}
177
178template <typename VecTp>
180{
181 // generate a number in [0, 1]
182 std::uniform_real_distribution<> real_dist_(0.0, 1.0);
183
184 if (real_dist_(generator_) > eps_)
185 {
186 // select greedy action with probability 1 - epsilon
187 return max_policy_.get_action(vec);
188 }
189
190 // else select a random action
191 return random_policy_(vec);
192}
193
194} // namespace policies
195} // namespace rl
196} // namespace bitrl
197
198#endif // EPSILON_GREEDY_POLICY_H
static constexpr real_t EPSILON_DECAY_FACTOR
Definition epsilon_greedy_policy.h:47
static constexpr real_t MAX_EPS
Definition epsilon_greedy_policy.h:46
static constexpr real_t MIN_EPS
Definition epsilon_greedy_policy.h:45
void set_eps_value(real_t eps)
Set the epsilon value.
output_type operator()(const torch_tensor_t &vec, torch_tensor_value_type< lint_t >) const
uint_t output_type
The type returned when calling this->operator().
Definition epsilon_greedy_policy.h:43
output_type operator()(const torch_tensor_t &vec, torch_tensor_value_type< int_t >) const
output_type operator()(const torch_tensor_t &vec, torch_tensor_value_type< float_t >) const
output_type get_action(const MatType &q_map, uint_t state_idx)
get_action. Given a
EpsilonDecayOption decay_option() const noexcept
Returns the decay option.
Definition epsilon_greedy_policy.h:121
void on_episode(uint_t episode_idx) noexcept
any actions the policy should perform on the given episode index
EpsilonGreedyPolicy(real_t eps)
Constructor. Creates an epsilon-greedy tabular policy.
Definition epsilon_greedy_policy.h:150
real_t eps_value() const noexcept
Returns the value of the epsilon.
Definition epsilon_greedy_policy.h:110
output_type operator()(const torch_tensor_t &vec, torch_tensor_value_type< real_t >) const
void reset() noexcept
Reset the policy.
Definition epsilon_greedy_policy.h:105
output_type operator()(const MapType &q_map, uint_t state) const
operator() Select action for the given state
class MaxTabularPolicy
Definition max_tabular_policy.h:33
class RandomTabularPolicy
Definition random_tabular_policy.h:25
Definition policy_stochastic_adaptor.h:21
EpsilonDecayOption
The EpsilonDecayOption enum. Enumerate various decaying options.
Definition epsilon_greedy_policy.h:27
@ CONSTANT_RATE
Definition epsilon_greedy_policy.h:31
@ NONE
Definition epsilon_greedy_policy.h:28
@ EXPONENTIAL
Definition epsilon_greedy_policy.h:29
@ INVERSE_STEP
Definition epsilon_greedy_policy.h:30
Definition dummy_agent.h:9
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
torch::Tensor torch_tensor_t
torch_tensor_t
Definition bitrl_types.h:95
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Definition bitrl_types.h:98