This example trains an agent on CliffWalking with double Q-learning. It has the settings of SARSA on CliffWorld, Q-learning on CliffWorld and Expected SARSA on CliffWorld, so the four can be compared. The environment is CliffWorld and runs on the bitrl-rest-api server, which the example talks to at http://0.0.0.0:8001/api, so start the server first.
Double Q-learning
Q-learning moves \(Q(s, \alpha)\) towards \(r + \gamma \max_{\alpha'} Q(s', \alpha')\). The values in the table are estimates with errors, and the largest of several estimates is more likely to be one that is too high than one that is too low, so Q-learning overestimates. Double Q-learning keeps two tables. Every step a coin picks one of them, say \(Q_1\), to update; the action that is best in \(Q_1\) at the next state is valued with the other table:
\[Q_1(s, \alpha) \leftarrow Q_1(s, \alpha) + \eta \left[ r + \gamma Q_2\left(s', \arg\max_{\alpha'} Q_1(s', \alpha')\right) - Q_1(s, \alpha) \right] .
\]
The errors of the two tables are independent, so the value of the chosen action is no longer biased upwards. The agent acts \(\epsilon\)-greedily on the sum of the two tables, with EpsilonDoubleQTableGreedyPolicy. After the step that ends an episode the target is the reward alone.
DoubleQLearning uses \(\gamma = 1\), \(\eta = 0.5\) and a constant \(\epsilon = 0.1\), and RLSerialAgentTrainer runs 2000 episodes of at most 100 steps, as for the other three.
What it writes
| File | What |
| double_q_learning_cliff_walking_v1.csv | both tables: a row per state and table, a column per action |
| reward_per_itr.csv | the reward of every training episode |
| policy.csv | the greedy policy of the sum of the two tables |
The learned policy
|
| The greedy policy double Q-learning learned, played in Gymnasium: return -17. |
Q-learning learns the shortest path, along the edge of the cliff, in 13 steps. Double Q-learning, with the same settings and the same number of episodes, kept two rows away from the cliff, as SARSA does, and takes 17 steps. A likely reason is that each of its tables is updated with only half of the steps, so after the same 2000 episodes the values of the cells along the edge, where the exploring agent falls off the cliff, have been pulled down by those falls more than they have been corrected by the greedy path along them; with more episodes it would be expected to move towards Q-learning's path.
|
| From the top: Q-learning (-13), double Q-learning (-17), expected SARSA (-15) and SARSA (-17). |
The driver code
#include <any>
#include <iostream>
#include <string>
#include <tuple>
#include <unordered_map>
{
const std::string
SERVER_URL =
"http://0.0.0.0:8001/api";
const std::string
TABLES_FILE =
"double_q_learning_cliff_walking_v1.csv";
typedef EpsilonDoubleQTableGreedyPolicy<DynMat<real_t>>
policy_type;
typedef DoubleQLearning<env_type, policy_type>
solver_type;
}
{
try
{
std::unordered_map<std::string, std::any> options;
std::unordered_map<std::string, std::any> reset_options;
env.make("v1", options, reset_options);
policy_type policy(0.1, env.n_actions(), EpsilonDecayOptionType::NONE, 0.01, 1.0, 0.2,
env.n_actions());
trainer.train(env);
rewards.open();
rewards.write_column_names({"epoch", "reward"});
for (auto val : trainer.episodes_total_rewards())
{
rewards.write_row(std::tuple<uint_t, real_t>{epoch++, val});
}
rewards.close();
greedy);
std::cout <<
"Saved the tables to " <<
TABLES_FILE <<
" and the greedy policy to "
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
The CliffWorld class.
Definition cliff_world_env.h:59
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:28
Definition rl_serial_agent_trainer.h:41
The class DoubleQLearning. Tabular double Q-learning, see van Hasselt, Double Q-learning,...
Definition double_q_learning.h:88
Definition epsilon_double_qtable_greedy_policy.h:21
class MaxTabularPolicy
Definition max_tabular_policy.h:33
void save(const std::string &filename) const
Save the state -> action map in a CSV file;.
The CSVWriter class. Handles writing into CSV file format.
Definition csv_file_writer.h:22
int main()
Definition discounted_returns.cpp:19
std::iterator_traits< IteratorType >::value_type sum(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:89
EpsilonDecayOptionType
Definition epsilon_decay_options.h:12
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
Definition double_q_learning.cpp:22
bitrl::envs::gymnasium::CliffWorld env_type
Definition double_q_learning.cpp:37
const std::string TABLES_FILE
Definition double_q_learning.cpp:33
DoubleQLearning< env_type, policy_type > solver_type
Definition double_q_learning.cpp:39
const std::string SERVER_URL
Definition double_q_learning.cpp:32
const std::string REWARDS_FILE
Definition double_q_learning.cpp:34
EpsilonDoubleQTableGreedyPolicy< DynMat< real_t > > policy_type
Definition double_q_learning.cpp:38
const std::string POLICY_FILE
Definition double_q_learning.cpp:35
The RLSerialTrainerConfig struct. Configuration struct for the serial RL agent trainer.
Definition rl_serial_agent_trainer.h:29
The configuration of DoubleQLearning.
Definition double_q_learning.h:32
real_t gamma
The discount factor.
Definition double_q_learning.h:47
uint_t max_num_iterations_per_episode
The most steps an episode can take.
Definition double_q_learning.h:57
std::string path
Where to save the two tables when training ends; nothing is saved if it is not set.
Definition double_q_learning.h:37
real_t eta
The learning rate.
Definition double_q_learning.h:52
Definition max_tabular_policy.h:131
void build_from_state_action_function(const DynMat< real_t > &q, MaxTabularPolicy &policy)