bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
q_learning_agent.h
Go to the documentation of this file.
1#ifndef Q_LEARNING_AGENT_H
2#define Q_LEARNING_AGENT_H
3
7
8#include <stdexcept>
9
10namespace bitrl
11{
12namespace rl::multi_agent
13{
14
27
40{
41 public:
43
44 QLearningAgent(uint_t agent, uint_t n_states, uint_t n_actions,
45 const IndependentQLearningConfig &config = {});
46
50 uint_t agent() const noexcept { return agent_; }
51
55 template <typename TimeStepType> uint_t act(const TimeStepType &step);
56
60 template <typename TimeStepType> uint_t act_greedy(const TimeStepType &step) const;
61
65 template <typename TimeStepType>
66 void update(const TimeStepType &step, uint_t action, const TimeStepType &next);
67
68 void on_episode_begin(uint_t /*episode*/) {}
69
73 void on_episode_end(uint_t episode) { policy_.on_episode(episode + 1); }
74
75 real_t epsilon() const noexcept { return policy_.eps_value(); }
76
77 const DynMat<real_t> &q_table() const noexcept { return q_table_; }
78
82 struct Greedy
83 {
85 template <typename TimeStepType> uint_t act(const TimeStepType &step) const
86 {
87 return agent.act_greedy(step);
88 }
89 };
90
91 Greedy greedy() const { return Greedy{*this}; }
92
93 private:
94 uint_t agent_;
96 DynMat<real_t> q_table_;
98};
99
101 const IndependentQLearningConfig &config)
102 : agent_(agent), config_(config),
103 q_table_(DynMat<real_t>::Zero(static_cast<Eigen::Index>(n_states),
104 static_cast<Eigen::Index>(n_actions))),
105 // the agents must not explore in step, nor share a generator
106 policy_(config.eps_max, bitsim::utils::concurrency::task_seed(config.seed, agent),
107 policies::EpsilonDecayOption::EXPONENTIAL, config.eps_min, config.eps_max,
108 config.eps_decay)
109{
110 if (n_states == 0 || n_actions == 0)
111 {
112 throw std::invalid_argument("QLearningAgent: needs states and actions");
113 }
114}
115
116template <typename TimeStepType> uint_t QLearningAgent::act(const TimeStepType &step)
117{
118 return step.done(agent_) ? 0 : policy_(q_table_.row(step.observation(agent_)));
119}
120
121template <typename TimeStepType> uint_t QLearningAgent::act_greedy(const TimeStepType &step) const
122{
123 if (step.done(agent_))
124 {
125 return 0;
126 }
127 Eigen::Index best = 0;
128 q_table_.row(step.observation(agent_)).maxCoeff(&best);
129 return static_cast<uint_t>(best);
130}
131
132template <typename TimeStepType>
133void QLearningAgent::update(const TimeStepType &step, uint_t action, const TimeStepType &next)
134{
135 if (step.done(agent_))
136 {
137 return;
138 }
139 const uint_t o = step.observation(agent_);
140 const uint_t o_next = next.observation(agent_);
141 const real_t bootstrap =
142 next.done(agent_) ? 0.0 : config_.gamma * q_table_.row(o_next).maxCoeff();
143 const real_t target = next.reward(agent_) + bootstrap;
144 q_table_(o, action) += config_.eta * (target - q_table_(o, action));
145}
146
147} // namespace rl::multi_agent
148} // namespace bitrl
149
150#endif // Q_LEARNING_AGENT_H
const DynMat< real_t > & q_table() const noexcept
Definition q_learning_agent.h:77
uint_t act_greedy(const TimeStepType &step) const
The greedy action; 0 if the agent is done.
Definition q_learning_agent.h:121
uint_t agent() const noexcept
The index of the agent in the time steps.
Definition q_learning_agent.h:50
uint_t act(const TimeStepType &step)
An exploring action; 0 if the agent is done.
Definition q_learning_agent.h:116
uint_t action_type
Definition q_learning_agent.h:42
Greedy greedy() const
Definition q_learning_agent.h:91
real_t epsilon() const noexcept
Definition q_learning_agent.h:75
QLearningAgent(uint_t agent, uint_t n_states, uint_t n_actions, const IndependentQLearningConfig &config={})
Definition q_learning_agent.h:100
void update(const TimeStepType &step, uint_t action, const TimeStepType &next)
Learn from the transition step –action--> next; nothing once the agent is done.
Definition q_learning_agent.h:133
void on_episode_begin(uint_t)
Definition q_learning_agent.h:68
void on_episode_end(uint_t episode)
Decay the exploration.
Definition q_learning_agent.h:73
The EpsilonGreedyPolicy class.
Definition epsilon_greedy_policy.h:38
Definition agent_trainer.h:14
Definition policy_stochastic_adaptor.h:21
@ EXPONENTIAL
Definition epsilon_decay_options.h:15
Definition bitrl_concepts.h:22
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
Definition dc_motor_backend_base.h:9
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
The settings of a Q-learning agent among others, and of IndependentQLearning.
Definition q_learning_agent.h:19
real_t eta
the learning rate
Definition q_learning_agent.h:21
real_t eps_min
the exploration it decays to
Definition q_learning_agent.h:23
real_t eps_decay
eps = eps_min + (eps_max - eps_min) exp(-eps_decay * episode)
Definition q_learning_agent.h:24
uint_t seed
every agent explores with its own generator, seeded from this
Definition q_learning_agent.h:25
real_t eps_max
the exploration at the start
Definition q_learning_agent.h:22
real_t gamma
the discount factor
Definition q_learning_agent.h:20
An actor that acts greedily with this table.
Definition q_learning_agent.h:83
const QLearningAgent & agent
Definition q_learning_agent.h:84
uint_t act(const TimeStepType &step) const
Definition q_learning_agent.h:85