1#ifndef Q_LEARNING_AGENT_H
2#define Q_LEARNING_AGENT_H
55 template <
typename TimeStepType>
uint_t act(
const TimeStepType &step);
60 template <
typename TimeStepType>
uint_t act_greedy(
const TimeStepType &step)
const;
65 template <
typename TimeStepType>
66 void update(
const TimeStepType &step,
uint_t action,
const TimeStepType &next);
85 template <
typename TimeStepType>
uint_t act(
const TimeStepType &step)
const
87 return agent.act_greedy(step);
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))),
106 policy_(config.eps_max,
bitsim::
utils::concurrency::task_seed(config.seed,
agent),
110 if (n_states == 0 || n_actions == 0)
112 throw std::invalid_argument(
"QLearningAgent: needs states and actions");
118 return step.done(agent_) ? 0 : policy_(q_table_.row(step.observation(agent_)));
123 if (step.done(agent_))
127 Eigen::Index best = 0;
128 q_table_.row(step.observation(agent_)).maxCoeff(&best);
129 return static_cast<uint_t>(best);
132template <
typename TimeStepType>
135 if (step.done(agent_))
139 const uint_t o = step.observation(agent_);
140 const uint_t o_next = next.observation(agent_);
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));
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