bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
independent_q_learning.h
Go to the documentation of this file.
1#ifndef INDEPENDENT_Q_LEARNING_H
2#define INDEPENDENT_Q_LEARNING_H
3
7
8#include <stdexcept>
9#include <string>
10#include <tuple>
11#include <vector>
12
13namespace bitrl
14{
15namespace rl::multi_agent
16{
17
40{
41 public:
42 typedef std::vector<uint_t> joint_action_type;
43
45 const IndependentQLearningConfig &config = {});
46
47 uint_t n_agents() const noexcept { return static_cast<uint_t>(agents_.size()); }
48
52 template <typename TimeStepType> joint_action_type act(const TimeStepType &step);
53
57 template <typename TimeStepType> joint_action_type act_greedy(const TimeStepType &step) const;
58
62 template <typename TimeStepType>
63 void update(const TimeStepType &step, const joint_action_type &actions,
64 const TimeStepType &next);
65
66 void on_episode_begin(uint_t /*episode*/) {}
67
71 void on_episode_end(uint_t episode);
72
76 real_t epsilon() const noexcept { return agents_.front().epsilon(); }
77
78 const DynMat<real_t> &q_table(uint_t agent) const { return agents_.at(agent).q_table(); }
79
83 const QLearningAgent &agent(uint_t agent) const { return agents_.at(agent); }
84
88 void save(const std::string &prefix) const;
89
93 struct Greedy
94 {
96 template <typename TimeStepType> joint_action_type act(const TimeStepType &step) const
97 {
98 return learner.act_greedy(step);
99 }
100 };
101
102 Greedy greedy() const { return Greedy{*this}; }
103
104 private:
105 std::vector<QLearningAgent> agents_;
106};
107
109 uint_t n_actions,
110 const IndependentQLearningConfig &config)
111{
112 if (n_agents == 0 || n_states == 0 || n_actions == 0)
113 {
114 throw std::invalid_argument("IndependentQLearning: needs agents, states and actions");
115 }
116 agents_.reserve(n_agents);
117 for (uint_t a = 0; a < n_agents; ++a)
118 {
119 agents_.emplace_back(a, n_states, n_actions, config);
120 }
121}
122
123template <typename TimeStepType>
125{
126 joint_action_type actions(n_agents(), 0);
127 for (uint_t a = 0; a < n_agents(); ++a)
128 {
129 actions[a] = agents_[a].act(step);
130 }
131 return actions;
132}
133
134template <typename TimeStepType>
136IndependentQLearning::act_greedy(const TimeStepType &step) const
137{
138 joint_action_type actions(n_agents(), 0);
139 for (uint_t a = 0; a < n_agents(); ++a)
140 {
141 actions[a] = agents_[a].act_greedy(step);
142 }
143 return actions;
144}
145
146template <typename TimeStepType>
147void IndependentQLearning::update(const TimeStepType &step, const joint_action_type &actions,
148 const TimeStepType &next)
149{
150 for (uint_t a = 0; a < n_agents(); ++a)
151 {
152 agents_[a].update(step, actions[a], next);
153 }
154}
155
157{
158 for (auto &agent : agents_)
159 {
160 agent.on_episode_end(episode);
161 }
162}
163
164inline void IndependentQLearning::save(const std::string &prefix) const
165{
166 for (uint_t a = 0; a < n_agents(); ++a)
167 {
168 bitsim::utils::io::CSVWriter writer(prefix + std::to_string(a) + ".csv", ',');
169 writer.open();
170 const auto &q = agents_[a].q_table();
171 for (Eigen::Index s = 0; s < q.rows(); ++s)
172 {
173 std::vector<real_t> row(q.cols());
174 for (Eigen::Index c = 0; c < q.cols(); ++c)
175 {
176 row[c] = q(s, c);
177 }
178 writer.write_row(row);
179 }
180 writer.close();
181 }
182}
183
184} // namespace rl::multi_agent
185} // namespace bitrl
186
187#endif // INDEPENDENT_Q_LEARNING_H
void on_episode_end(uint_t episode)
Decay the exploration of every agent.
Definition independent_q_learning.h:156
joint_action_type act_greedy(const TimeStepType &step) const
The greedy action of every agent, for evaluation.
Definition independent_q_learning.h:136
Greedy greedy() const
Definition independent_q_learning.h:102
const DynMat< real_t > & q_table(uint_t agent) const
Definition independent_q_learning.h:78
void on_episode_begin(uint_t)
Definition independent_q_learning.h:66
uint_t n_agents() const noexcept
Definition independent_q_learning.h:47
real_t epsilon() const noexcept
The current exploration, the same for all agents.
Definition independent_q_learning.h:76
void update(const TimeStepType &step, const joint_action_type &actions, const TimeStepType &next)
Learn from the transition step –actions--> next.
Definition independent_q_learning.h:147
std::vector< uint_t > joint_action_type
Definition independent_q_learning.h:42
joint_action_type act(const TimeStepType &step)
An exploring action for every agent; agents that are done get action 0.
Definition independent_q_learning.h:124
void save(const std::string &prefix) const
Write the Q-table of every agent to <prefix><agent>.csv, a row per state.
Definition independent_q_learning.h:164
const QLearningAgent & agent(uint_t agent) const
The learner of one agent.
Definition independent_q_learning.h:83
IndependentQLearning(uint_t n_agents, uint_t n_states, uint_t n_actions, const IndependentQLearningConfig &config={})
Definition independent_q_learning.h:108
One agent of a multi-agent environment that learns its own Q-table with Q-learning,...
Definition q_learning_agent.h:40
The CSVWriter class. Handles writing into CSV file format.
Definition csv_file_writer.h:22
void write_row(const std::vector< T > &vals)
Write a row of the file.
Definition csv_file_writer.h:89
virtual void close()
Closes the file if it is currently open.
Definition file_handler_base.h:188
virtual void open() override
Open the file for writing.
Definition file_writer_base.cpp:21
Definition agent_trainer.h:14
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
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
A team that acts greedily with these tables, for run_episode().
Definition independent_q_learning.h:94
const IndependentQLearning & learner
Definition independent_q_learning.h:95
joint_action_type act(const TimeStepType &step) const
Definition independent_q_learning.h:96