|
bitsim
Model, simulate, estimate and control robots in C++
|
Policy iteration on FrozenLake solved FrozenLake with policy iteration. This example solves it with value iteration,
and then plays the policy it finds. The environment is the 4x4 FrozenLake from Gymnasium, made with is_slippery set to false, so every action moves the agent where it was meant to. It 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.
Policy iteration alternates between evaluating a policy and improving it. Value iteration folds the two into one update. It sweeps over the states and replaces the value of each with the value of its best action,
\[V(s) \leftarrow \max_{\alpha} \sum_{s', r} p(s', r | s, \alpha) \left[ r + \gamma V(s') \right], \]
until a sweep changes no value by more than a tolerance. The policy is then read off the value function: in each state, take the action with the largest expected return. Like policy iteration, it needs the transition probabilities and rewards of the environment, which FrozenLake provides.
ValueIteration uses \(\gamma = 0.99\) and a tolerance of \(10^{-8}\). It updates the values in place, so a state later in a sweep already sees the new values of the states before it. 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, and stops as soon as a sweep changes no value by more than the tolerance, after 7 sweeps.
In the directory it runs from, the example writes
| File | What |
|---|---|
| value_iteration_frozen_lake_v1.csv | the value of each state |
| reward_per_itr.csv | for each sweep, the largest change of a value in it. ValueIteration reports it as the reward of the episode, so that the trainer keeps it |
| policy.csv | the greedy policy, the action to take in each state |
plot.py draws the value function on the grid of the lake and the largest change per sweep. play.py plays the policy in Gymnasium and draws each step. Both read the files from the directory they run in, or from the path given as their argument:
play.py needs the gymnasium Python package with its toy text environments.
The agent starts in the top left corner, the goal is in the bottom right, and the four cyan squares with a value of 0 are the holes. The goal itself also has a value of 0: the episode ends there, so no rewards come after it. Reaching the goal is the only reward, 1, so the value of a state is \(\gamma^{n-1}\), where \(n\) is the number of steps from it to the goal. The start is 6 steps away and has a value of \(0.99^5 \approx 0.951\).
|
|---|
| Figure 1: The state value function. |
|
|---|
| The value function sweep by sweep, with the greedy actions of the values. |
On the lake that does not slip, each sweep carries the value of the goal one more step back, so 6 sweeps reach the start and the 7th changes nothing. The GIF is made with
The policy reaches the goal in those 6 steps, the shortest path around the holes.
|
|---|
| Figure 2: Playing FrozenLake, step 1. |
|
|---|
| Figure 3: Step 2. |
|
|---|
| Figure 4: Step 3. |
|
|---|
| Figure 5: Step 4. |
|
|---|
| Figure 6: Step 5. |
|
|---|
| Figure 7: Step 6, the goal. |