bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
Iterative policy evaluation on FrozenLake

Before an agent can improve a policy, it has to know how good the policy is. This example computes the value function of a policy on the 4x4 FrozenLake from Gymnasium. It is the first half of policy iteration, which Policy iteration on FrozenLake completes, and value iteration in Value iteration on FrozenLake builds on the same update.

The environment runs on the bitrl-rest-api server, which the example talks to over HTTP at http://0.0.0.0:8001/api, so start the server first. The example makes the environment with its default options, so the lake is slippery: an action moves the agent in the intended direction only some of the time.

Iterative policy evaluation

The value \(V_{\pi}(s)\) of a state is the return the agent expects from it when it follows the policy \(\pi\). It satisfies the Bellman expectation equation

\[V_{\pi}(s) = \sum_{\alpha} \pi(\alpha | s) \sum_{s', r} p(s', r | s, \alpha) \left[ r + \gamma V_{\pi}(s') \right] . \]

Iterative policy evaluation starts from \(V = 0\) and sweeps over the states, replacing \(V(s)\) with the right hand side, until a sweep changes no value by more than a tolerance. It needs the transition probabilities and rewards \(p(s', r | s, \alpha)\), which FrozenLake provides.

The policy is a UniformDiscretePolicy, which picks each of the four actions with probability 1/4. IterativePolicyEvalutationSolver uses \(\gamma = 1\) and a tolerance of \(10^{-8}\). The example runs the sweeps itself, at most 10000 of them, rather than with RLSerialAgentTrainer, so that it can write the value function after every sweep to sweeps.csv. It stops once a sweep changes no value by more than the tolerance, after 57 sweeps.

The results

The example writes the value of each state to iterative_policy_evaluation_frozen_lake.csv in the directory it runs from. plot.py draws it on the grid of the lake:

python plot.py iterative_policy_evaluation_frozen_lake.csv

Figure 1: The value of each state under the uniform random policy.

The holes and the goal have a value of 0, because the episode ends there. The only reward is 1, for reaching the goal, so with \(\gamma = 1\) the value of a state is the probability that the random policy gets from it to the goal before it falls into a hole. From the start, in the top left corner, that is only about 1.4%. The state to the left of the goal has the largest value, 0.44.

Figure 2: The value function sweep by sweep. The arrows are the greedy actions of the values.

The values spread back from the goal one cell per sweep, and most of the 57 sweeps only refine them. examples/tools/value_gif.py draws sweeps.csv, with the arrows from FrozenLake's own transition model:

python examples/tools/value_gif.py sweeps.csv --slippery --out iterative_policy_evaluation.gif

The driver code

#include <fstream>
#include <vector>
#include <iostream>
namespace example_12
{
const std::string SERVER_URL = "http://0.0.0.0:8001/api";
typedef FrozenLake<4> env_type;
} // namespace example_12
int main()
{
using namespace example_12;
RESTRLEnvClient server(SERVER_URL, true);
// create the environment
FrozenLake<4> env(server);
std::cout << "Environment URL: " << env.get_url() << std::endl;
std::unordered_map<std::string, std::any> options;
std::unordered_map<std::string, std::any> reset_options;
std::cout << "Creating the environment..." << std::endl;
env.make("v1", options, reset_options);
env.reset();
std::cout << "Done..." << std::endl;
UniformDiscretePolicy policy(env.n_states(), env.n_actions());
config.tolerance = 1.0e-8;
// the sweeps, as RLSerialAgentTrainer would run them, keeping the value
// function after every one in sweeps.csv, which examples/tools/value_gif.py
// draws
std::ofstream sweeps("sweeps.csv");
std::vector<real_t> reward;
algorithm.actions_before_training_begins(env);
for (uint_t sweep = 0; sweep < 10000; ++sweep)
{
const auto sweep_info = algorithm.on_training_episode(env, sweep);
reward.push_back(sweep_info.episode_reward);
const auto values = algorithm.value_function();
sweeps << sweep;
for (Eigen::Index s = 0; s < values.size(); ++s)
{
sweeps << "," << values[s];
}
sweeps << "\n";
if (sweep_info.stop_training)
{
break;
}
}
algorithm.actions_after_training_ends(env);
BITRL_LOG_INFO << "Done after " << reward.size() << " sweeps";
// save the value function for plotting
algorithm.save("iterative_policy_evaluation_frozen_lake.csv");
return 0;
}
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 frozen_lake_env.h:104
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 IterativePolicyEval class.
Definition iterative_policy_evaluation.h:31
The UniformDiscretePolicy class.
Definition uniform_discrete_policy.h:20
int main()
Definition discounted_returns.cpp:19
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Definition iterative_policy_evaluation.cpp:15
FrozenLake< 4 > env_type
Definition iterative_policy_evaluation.cpp:28
const std::string SERVER_URL
Definition iterative_policy_evaluation.cpp:17
reward
Definition play.py:49
The RLSerialTrainerConfig struct. Configuration struct for the serial RL agent trainer.
Definition rl_serial_agent_trainer.h:29
Definition iterative_policy_evaluation.h:20
real_t tolerance
Definition iterative_policy_evaluation.h:22