|
bitsim
Model, simulate, estimate and control robots in C++
|
Iterative policy evaluation on FrozenLake computed the value function of a policy on FrozenLake with iterative policy evaluation, which needs the transition probabilities of the environment. Monte Carlo methods need no model. They estimate the value of a state from the returns that follow it in episodes the agent plays. This example estimates the value function of a random policy on the 4x4 FrozenLake from Gymnasium with first visit Monte Carlo.
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. It is made with its default options, so the lake is slippery.
In every training episode the solver plays an episode, a trajectory of at most max_steps = 200 steps, and walks through it. The first time it meets a state \(s\) in the trajectory, it computes the return \(G\) from that step to the end of the trajectory and moves the value of the state towards it,
\[V(s) \leftarrow V(s) + \alpha \left[ G - V(s) \right] . \]
Later visits to the same state in the same trajectory are skipped; that is what makes it first visit. Every visit Monte Carlo would update the value at each of them.
FirstVisitMCSolver takes three types from the example:
The example runs 10000 training episodes itself, as RLSerialAgentTrainer would, so that it can write the value function every 200 episodes to sweeps.csv.
At the end the example writes the value of each state to first_visit_mc_frozen_lake.csv in the directory it runs from. It estimates the same quantity as Iterative policy evaluation on FrozenLake, the value function of the uniform random policy, so the two can be compared. With a constant \(\alpha = 0.5\) the estimates would follow the latest returns and stay noisy; with the decaying rate they average more of them and settle.
|
|---|
| The value estimates every 200 episodes, with the greedy actions of the estimates. |
After 10000 episodes the start has an estimate of 0.017 and the cell left of the goal 0.49, against the true values of 0.014 and 0.44 that Iterative policy evaluation on FrozenLake computes: estimates from sampled returns, close but not exact. The GIF is made with