1#ifndef INDEPENDENT_Q_LEARNING_H
2#define INDEPENDENT_Q_LEARNING_H
62 template <
typename TimeStepType>
64 const TimeStepType &next);
88 void save(
const std::string &prefix)
const;
98 return learner.act_greedy(step);
105 std::vector<QLearningAgent> agents_;
112 if (
n_agents == 0 || n_states == 0 || n_actions == 0)
114 throw std::invalid_argument(
"IndependentQLearning: needs agents, states and actions");
119 agents_.emplace_back(a, n_states, n_actions, config);
123template <
typename TimeStepType>
129 actions[a] = agents_[a].act(step);
134template <
typename TimeStepType>
141 actions[a] = agents_[a].act_greedy(step);
146template <
typename TimeStepType>
148 const TimeStepType &next)
152 agents_[a].update(step, actions[a], next);
158 for (
auto &
agent : agents_)
160 agent.on_episode_end(episode);
170 const auto &q = agents_[a].q_table();
171 for (Eigen::Index s = 0; s < q.rows(); ++s)
173 std::vector<real_t> row(q.cols());
174 for (Eigen::Index c = 0; c < q.cols(); ++c)
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