|
bitsim
Model, simulate, estimate and control robots in C++
|
This example trains an agent on CliffWalking from Gymnasium with expected SARSA. It has the same settings as SARSA on CliffWorld, which uses SARSA, so the two can be compared. The environment is CliffWorld, and 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.
The agent walks on a 4 x 12 grid from the bottom left corner to the bottom right one. The cells between them along the bottom are a cliff: stepping onto it costs 100 and sends the agent back to the start. Every other step costs 1.
SARSA moves the value of the action it took towards the reward plus the value of the action it takes next,
\[Q(s_t, \alpha_t) \leftarrow Q(s_t, \alpha_t) + \eta \left[ r_{t+1} + \gamma Q(s_{t+1}, \alpha_{t+1}) - Q(s_t, \alpha_t) \right] . \]
The next action is random some of the time, so the target is noisy. Expected SARSA replaces the value of that one action by the value the policy expects at the next state, every action weighed with the probability the policy takes it:
\[Q(s_t, \alpha_t) \leftarrow Q(s_t, \alpha_t) + \eta \left[ r_{t+1} + \gamma \sum_{\alpha} \pi(\alpha | s_{t+1}) Q(s_{t+1}, \alpha) - Q(s_t, \alpha_t) \right] . \]
For the \(\epsilon\)-greedy policy the agent follows, with \(n\) actions, \(\pi(\alpha | s)\) is \(\epsilon / n\) for every action, plus \(1 - \epsilon\) for the greedy one, so the expected value is \(\epsilon\) times the mean of the values at \(s_{t+1}\) plus \(1 - \epsilon\) times the largest. ExpectedSarsaSolver computes it with the same greedy action as the policy, the first of the largest values, and asks the policy for its \(\epsilon\) every time, so a decaying \(\epsilon\) works too. After the step that ends an episode the target is the reward alone.
The target no longer depends on which action happens to come next, so the updates vary less than SARSA's, and expected SARSA can learn with a larger learning rate. With \(\epsilon = 0\) the policy is greedy, the expected value is the largest value, and expected SARSA is Q-learning.
Like SARSA, expected SARSA learns the value of the policy it follows, which explores. A random step taken from next to the cliff falls off it, so both learn to keep away from the cliff, while Q-learning, which learns the value of the greedy policy, takes the shortest path along its edge. The unit test test_td_cliff shows this on a 3 x 5 cliff walk: for every one of five seeds SARSA and expected SARSA go by the top row, and Q-learning along the edge.
The policy is \(\epsilon\)-greedy with a constant \(\epsilon = 0.1\). The solver uses \(\gamma = 1\) and \(\eta = 0.5\), and RLSerialAgentTrainer runs 2000 episodes of at most 100 steps. In the directory it runs from, the example writes
| File | What |
|---|---|
| expected_sarsa_cliff_walking_v1.csv | the value of every state and action, a column per action |
| reward_per_itr.csv | the total reward of every training episode |
| policy.csv | the greedy policy, the action to take in each state |
|
|---|
| The greedy policy Expected SARSA learned, played in Gymnasium: return -15. |
The greedy policy of the table keeps one row away from the cliff and takes 15 steps, between SARSA and Q-learning. Like SARSA it values the policy it follows, but it averages over the actions that policy may take instead of following the one it took, which makes its estimates vary less. The example saves it to policy.csv; examples/tools/episode_gif.py plays it to make the GIF: