bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
DQN with experience replay on Gridworld

DQN on Gridworld trained a DQN on a static board, where every episode starts the same. Here every episode starts on a random board of Gridworld, with the player, the goal, the pit and the wall in four random cells, so the network has to learn to read the board rather than one path. The model is the one of Deep Reinforcement Learning in Action.

Experience replay

Updating the network with each step as it comes, as DQN on Gridworld does, trains it on steps that follow each other and are alike; with random boards that makes it chase the latest board. Here the steps go into a buffer of the latest 1000 and, once it holds 200, every step trains the network on a batch of 200 steps drawn from it at random. The batch mixes many boards, and every step is used in many updates. The network, the targets and \(\gamma = 0.9\) are those of DQN on Gridworld; \(\epsilon\) stays at 0.3, episodes stop after 50 steps, and the example trains for 5000 episodes, which take about a minute.

The learned policy

The greedy policy on six of the boards it was tested on: five it solves and the last one it does not.

The mean length of the training episodes, 1000 at a time, went 13.3, 8.0, 5.9, 5.5, 5.7. After training the example plays the greedy policy on 100 new random boards, at most 20 steps each, and writes every board to episode.csv. It reached the goal on 85 of them, in 2.9 steps on average, and never fell into the pit; on the other 15 it got stuck, as on the last board of the GIF, where it keeps walking into the edge of the grid. More training episodes, or an \(\epsilon\) that decays, would help. The GIF shows boards 1, 2, 3, 6, 7 and 0:

python examples/tools/grid_world_gif.py episode.csv --title DQN --episodes 1,2,3,6,7,0 --out dqn_experience_replay.gif

The driver code

#include "bitrl/base/bitrl_config.h"
#ifdef USE_PYTORCH
#include <torch/torch.h>
#include <any>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
namespace rl_example_12
{
namespace F = torch::nn::functional;
const std::string EXPERIMENT_ID = "1";
constexpr uint_t l1 = 64;
constexpr uint_t l2 = 150;
constexpr uint_t l3 = 100;
constexpr uint_t l4 = 4;
constexpr uint_t SEED = 42;
constexpr uint_t BATCH_SIZE = 200;
constexpr uint_t EXPERIENCE_BUFFER_SIZE = 1000;
constexpr uint_t TOTAL_EPOCHS = 5000;
constexpr real_t GAMMA = 0.9;
constexpr real_t EPSILON = 0.3;
constexpr real_t LEARNING_RATE = 1.0e-3;
class QNetImpl : public torch::nn::Module
{
public:
private:
torch::nn::Linear fc1_;
torch::nn::Linear fc2_;
torch::nn::Linear fc3_;
};
: fc1_(torch::nn::Linear(l1, l2)), fc2_(torch::nn::Linear(l2, l3)),
fc3_(torch::nn::Linear(l3, l4))
{
register_module("fc1", fc1_);
register_module("fc2", fc2_);
register_module("fc3", fc3_);
}
torch_tensor_t QNetImpl::forward(torch_tensor_t x)
{
// ReLU after the hidden layers only: the Q values are negative, every step
// pays -1, and a ReLU on the output could not give them
x = F::relu(fc1_->forward(x));
x = F::relu(fc2_->forward(x));
return fc3_->forward(x);
}
// create the model
// 4x4 grid
typedef Gridworld<4> env_type;
typedef env_type::time_step_type time_step_type;
typedef std::tuple<std::vector<float_t>, uint_t, float_t, std::vector<float_t>, uint_t>
typedef env_type::state_type state_type;
std::vector<float_t> flattened_observation(const state_type &state)
{
std::vector<float_t> data;
data.reserve(64);
for (uint_t i = 0; i < state.size(); ++i)
{
for (uint_t j = 0; j < state[i].size(); ++j)
{
for (uint_t k = 0; k < state[i][j].size(); ++k)
{
data.push_back(state[i][j][k]);
}
}
}
return data;
}
template <typename T, uint_t index>
std::vector<T> get(const std::vector<experience_tuple_type> &experience)
{
std::vector<T> result;
result.reserve(experience.size());
auto b = experience.begin();
auto e = experience.end();
for (; b != e; ++b)
{
auto item = *b;
result.push_back(std::get<index>(item));
}
return result;
}
} // namespace rl_example_12
int main()
{
using namespace rl_example_12;
using namespace bitrl;
try
{
BITRL_LOG_INFO << "Starting agent training...";
BITRL_LOG_INFO << "Number of episodes to train: " << TOTAL_EPOCHS;
// let's create a directory where we want to
// store all the results from running a simulation
std::filesystem::create_directories("experiments/" + EXPERIMENT_ID);
// set the seed for PyTorch
torch::manual_seed(SEED);
BITRL_LOG_INFO << "Creating the environment...";
// create a 4x4 grid
auto env = env_type();
// initialize the environment using random mode
std::unordered_map<std::string, std::any> options;
std::unordered_map<std::string, std::any> reset_options;
env.make("v0", options, reset_options);
BITRL_LOG_INFO << "Done...";
BITRL_LOG_INFO << "Environment name: " << env.name;
BITRL_LOG_INFO << "Number of actions available: " << env.n_actions();
BITRL_LOG_INFO << "Number of states available: " << env.n_states();
// the network to train for the q values
QNet qnet;
auto optimizer_ptr = std::make_unique<torch::optim::Adam>(
qnet->parameters(), torch::optim::AdamOptions(LEARNING_RATE));
// we will use an epsilon-greedy policy
// the loss function to use
auto loss_fn = torch::nn::MSELoss();
// track the average loss per epoch
std::vector<real_t> losses;
losses.reserve(TOTAL_EPOCHS);
// hold random values
std::vector<float_t> rand_vec(64, 0.0);
float_t a = 0.0;
float_t b = 1.0;
// loop over the epochs
for (uint_t epoch = 0; epoch < TOTAL_EPOCHS; ++epoch)
{
BITRL_LOG_INFO << "Starting epoch: " << epoch << std::endl;
// for every new epoch we reset the environment
auto time_step = env.reset();
uint_t step_counter = 0;
// the loss associated with the epoch
std::vector<real_t> epoch_loss;
epoch_loss.reserve(TOTAL_ITRS_PER_EPOCH);
auto done = false;
while (!done)
{
auto obs1 = flattened_observation(time_step.observation());
rand_vec = bitrl::maths::randomize(rand_vec, a, b, 64);
rand_vec = bitrl::maths::divide(rand_vec, 100.0);
// randomize the flattened observation by adding
// some noise
obs1 = maths::add(obs1, rand_vec);
auto torch_state_1 =
// get the qvals
auto qvals = qnet(torch_state_1);
auto action_idx = policy(qvals, bitrl::torch_tensor_value_type<float_t>());
BITRL_LOG_INFO << "Action selected: " << action_idx << std::endl;
// step in the environment
time_step = env.step(action_idx);
auto reward = time_step.reward();
auto step_finished =
time_step.done() ? static_cast<uint_t>(1) : static_cast<uint_t>(0);
auto obs2 = flattened_observation(time_step.observation());
rand_vec = bitrl::maths::randomize(rand_vec, a, b, 64);
rand_vec = bitrl::maths::divide(rand_vec, 100.0);
obs2 = maths::add(obs2, rand_vec);
auto torch_state_2 =
experience_tuple_type exp = {obs1, action_idx, reward, obs2, step_finished};
// put the observation into the buffer
experience_buffer.append(exp);
// if we reach the batch size we will do
// a backward propagation
if (experience_buffer.size() >= BATCH_SIZE)
{
std::vector<experience_tuple_type> batch_sample;
batch_sample.reserve(BATCH_SIZE);
// sample from the experience
experience_buffer.sample(BATCH_SIZE, batch_sample, SEED);
// stack the experiences
auto state_1_batch = get<std::vector<float_t>, 0>(batch_sample);
auto action_batch = get<uint_t, 1>(batch_sample);
auto state_2_batch = get<std::vector<float_t>, 3>(batch_sample);
auto reward_batch = get<float_t, 2>(batch_sample);
auto done_batch = get<uint_t, 4>(batch_sample);
state_1_batch, bitrl::DeviceType::CPU);
auto q1 = qnet(state_1_batch_t);
// tell the model that we don't use grad here
qnet->eval();
state_2_batch, bitrl::DeviceType::CPU);
auto q2 = qnet(state_2_batch_t);
// we are training again
qnet->train();
reward_batch, bitrl::DeviceType::CPU);
done_batch, bitrl::DeviceType::CPU);
action_batch, bitrl::DeviceType::CPU);
auto state_max = torch::max(q2, 1);
auto state_max_val = std::get<0>(state_max);
auto Y = reward_batch_t + GAMMA * ((1 - done_batch_t) * state_max_val);
auto X = q1.gather(1, action_batch_t.unsqueeze(1)).squeeze();
auto loss = loss_fn(X, Y.detach());
optimizer_ptr->zero_grad();
loss.backward();
optimizer_ptr->step();
BITRL_LOG_INFO << "Loss at epoch: " << loss.item<real_t>();
epoch_loss.push_back(loss.item<real_t>());
}
step_counter += 1;
// update done
done = time_step.done();
if (done || step_counter > TOTAL_ITRS_PER_EPOCH)
{ //#Q
// done = true;
BITRL_LOG_INFO << "Reward: " << reward;
BITRL_LOG_INFO << "Finishing epoch at step: " << step_counter;
step_counter = 0;
}
}
BITRL_LOG_INFO << "Epoch finished...";
// get the epsilon
/*auto eps = policy.eps_value();
if(eps > 0.1){
eps -= 1/static_cast<real_t>(TOTAL_EPOCHS);
policy.set_eps_value(eps);
}*/
losses.push_back(bitrl::maths::mean(epoch_loss.begin(), epoch_loss.end()));
}
// play the greedy policy, the action of the largest Q value, for 100 episodes on random
// boards,
// at most 20 steps each, and write every board to episode.csv for
// examples/tools/grid_world_gif.py: a row per step, the episode, the
// action that led to the board (-1 for the first), the reward and the 64
// values of the board
{
qnet->eval();
torch::NoGradGuard no_grad;
std::ofstream episode_file("episode.csv");
episode_file << "# episode,action,reward,board\n";
uint_t won = 0;
for (uint_t episode = 0; episode < 100; ++episode)
{
auto time_step = env.reset();
int action = -1;
for (uint_t step = 0; step <= 20; ++step)
{
const auto obs = flattened_observation(time_step.observation());
episode_file << episode << "," << action << "," << time_step.reward();
for (auto v : obs)
{
episode_file << "," << v;
}
episode_file << "\n";
if (time_step.done() || step == 20)
{
won += time_step.reward() > 0.0 ? 1 : 0;
break;
}
auto state =
bitrl::utils::pytorch::TorchAdaptor::to_torch(obs, bitrl::DeviceType::CPU);
action = static_cast<int>(torch::argmax(qnet(state)).item<int64_t>());
time_step = env.step(static_cast<uint_t>(action));
}
}
std::cout << "The greedy policy reached the goal in " << won << " of " << 100
<< " episodes" << std::endl;
}
// save the rewards per episode for visualization
// purposes
auto filename = std::string("experiments/") + EXPERIMENT_ID;
filename += "/dqn_grid_world_policy_loss.csv";
CSVWriter csv_writer(filename, CSVWriter::default_delimiter());
csv_writer.open();
csv_writer.write_column_vector(losses);
csv_writer.close();
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
catch (...)
{
std::cout << "Unknown exception occured" << std::endl;
}
return 0;
}
#else
#include <iostream>
int main()
{
std::cout << "This example requires PyTorch" << std::endl;
std::cout << "Reconfigure bitrl with USE_PYTORCH flag turned ON." << std::endl;
return 0;
}
#endif
bitrl's logging: the logger of bitsim, bitsim/utils/logger.h, with bitrl's messages going to the logg...
#define BITRL_LOG_INFO
Definition bitrl_log.h:36
Definition grid_world_env.h:253
Definition rl_serial_agent_trainer.h:41
The EpsilonGreedyPolicy class.
Definition epsilon_greedy_policy.h:38
The ExperienceBuffer class. A buffer based on boost::circular_buffer to accumulate items of type Expe...
Definition experience_buffer.h:28
The CSVWriter class. Handles writing into CSV file format.
Definition csv_file_writer.h:22
static char default_delimiter()
The default column delimiter.
Definition csv_file_writer.h:28
torch_tensor_t forward(torch_tensor_t)
Definition dqn.cpp:79
QNetImpl()
Definition dqn.cpp:70
int main()
Definition discounted_returns.cpp:19
@ RANDOM
Definition grid_world_env.h:41
Vec & add(Vec &v1, const Vec &v2)
Definition vector_math.h:248
std::vector< T > & randomize(std::vector< T > &vec, T a, T b, uint_t seed=42)
Fill in the given vector with random values.
Definition vector_math.h:230
real_t mean(IteratorType begin, IteratorType end, bool parallel=true)
mean Compute the mean value of the values in the provided iterator range
Definition vector_math.h:119
Vec & divide(Vec &v1, typename Vec::value_type val)
Definition vector_math.h:237
@ NONE
Definition epsilon_greedy_policy.h:28
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
torch::Tensor torch_tensor_t
torch_tensor_t
Definition bitrl_types.h:95
constexpr int X
Definition cart_pole_dynamics.h:34
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
float float_t
float
Definition bitsim_types.h:25
double real_t
real_t
Definition bitsim_types.h:20
int int_t
integer type
Definition bitsim_types.h:30
FrozenLake< 4 > env_type
Definition iterative_policy_evaluation.cpp:28
TORCH_MODULE(LogisticRegressionModel)
dict action
Definition play.py:46
reward
Definition play.py:49
state
Definition play.py:39
bool done
Definition play.py:40
env
Definition play.py:33
dict policy
Definition play.py:28
filename
Definition plot_losses.py:8
Vector step(const QuadrotorDynamicsConfig &config, const Vector &x, const Vector &u, real_t max_rotor_speed)
Definition quadrotor_lqr.cpp:114
Definition dqn.cpp:30
const std::string EXPERIMENT_ID
Definition dqn.cpp:32
constexpr uint_t TOTAL_EPOCHS
Definition dqn.cpp:51
constexpr uint_t EXPERIENCE_BUFFER_SIZE
Definition dqn_experience_replay.cpp:60
constexpr uint_t l1
Definition dqn.cpp:46
Gridworld< 4 > env_type
Definition dqn.cpp:93
constexpr real_t LEARNING_RATE
Definition dqn.cpp:55
constexpr real_t EPSILON
Definition dqn.cpp:54
constexpr uint_t TOTAL_ITRS_PER_EPOCH
Definition dqn.cpp:52
std::tuple< std::vector< float_t >, uint_t, float_t, std::vector< float_t >, uint_t > experience_tuple_type
Definition dqn_experience_replay.cpp:106
constexpr uint_t BATCH_SIZE
Definition dqn_experience_replay.cpp:59
constexpr uint_t l3
Definition dqn.cpp:48
constexpr real_t GAMMA
Definition dqn.cpp:53
constexpr uint_t SEED
Definition dqn.cpp:50
constexpr uint_t l2
Definition dqn.cpp:47
env_type::state_type state_type
Definition dqn.cpp:94
constexpr uint_t l4
Definition dqn.cpp:49
std::vector< T > get(const std::vector< experience_tuple_type > &experience)
Definition dqn_experience_replay.cpp:132
env_type::time_step_type time_step_type
Definition dqn_experience_replay.cpp:103
std::vector< float_t > flattened_observation(const state_type &state)
Definition dqn.cpp:96
bitrl::utils::ExperienceBuffer< experience_tuple_type > experience_buffer_type
Definition dqn_experience_replay.cpp:108
The RLSerialTrainerConfig struct. Configuration struct for the serial RL agent trainer.
Definition rl_serial_agent_trainer.h:29
Definition bitrl_types.h:98
static value_type to_torch(const std::vector< real_t > &data, DeviceType dtype=DeviceType::CPU, bool requires_grad=false)
static value_type stack(const std::vector< T > &values, DeviceType type=DeviceType::CPU, bool requires_grad=false)