Skip to main content
Open In Colab This section trains SARSA, Expected SARSA and Q-learning on the cliff-walking gridworld of Sutton and Barto, Example 6.6, with the same step size and the same exploration, and compares the policies they learn. A second experiment, Sutton and Barto’s Example 6.7, shows the maximization bias of Q-learning and how Double Q-learning removes it (Sutton & Barto, 2018).

Cliff walking

The grid has 4 rows and 12 columns. The agent starts in the bottom-left cell S, and the episode ends in the bottom-right cell G. The cells between them on the bottom row are a cliff: stepping into one gives reward −100-100 and sends the agent back to S. Every other step gives reward −1-1, the moves are deterministic, and γ=1\gamma = 1. All three agents act with an ϵ\epsilon-greedy behavior policy, ϵ=0.1\epsilon = 0.1, and use the step size α=0.5\alpha = 0.5. They differ only in the value of the next state in the TD target:
  • SARSA uses Q(S′,A′)Q(S', A') for the next action A′A' that the ϵ\epsilon-greedy policy actually takes.
  • Expected SARSA uses ∑aπ(a∣S′) Q(S′,a)\sum_a \pi(a \mid S')\, Q(S', a), the expected value of the next action under the ϵ\epsilon-greedy policy.
  • Q-learning uses max⁡aQ(S′,a)\max_a Q(S', a), the value under the greedy policy.
Each method trains for 500 episodes, with 20 seeds.
Output from cell 4 Return of each training episode under the ϵ\epsilon-greedy behavior policy, averaged over 20 seeds and smoothed over 20 episodes. Over the last 50 episodes the mean return is −21.9-21.9 for Expected SARSA, −27.0-27.0 for SARSA and −47.9-47.9 for Q-learning. These returns are collected while the agents still explore, so they measure the ϵ\epsilon-greedy behavior, not the greedy policy. Q-learning learns the values of the greedy policy, whose route runs along the edge of the cliff, and its ϵ\epsilon-greedy behavior follows that route. A random step down from the edge falls off the cliff and costs −100-100, so Q-learning has the lowest training returns. SARSA and Expected SARSA learn the values of the ϵ\epsilon-greedy policy itself, including the risk of a random step, and keep away from the edge. Expected SARSA does better than SARSA with the same step size. Its target averages over the next action instead of sampling it, so the target no longer changes with every random next action. With a large step size such as α=0.5\alpha = 0.5, each update follows its target closely, and the lower variance of the target matters more.
Output from cell 6 Greedy paths read off the learned QQ, for the first seed of each method whose greedy path reaches G. Q-learning’s greedy path takes 13 steps along the edge of the cliff, the shortest route, in all 20 seeds. Expected SARSA’s takes 15 steps, one row above the edge, in all 20 seeds. SARSA’s takes 17 steps along the top row in 17 seeds and 19 steps in one. In the other two seeds SARSA’s greedy policy does not reach G. As on the windy gridworld, estimates that are only approximately learned can make the greedy action one that goes nowhere. A route further from the cliff gives a random step less chance to fall off, so the two on-policy methods trade path length for safety. Q-learning ignores that risk, because its target assumes greedy behavior from the next state on. If ϵ\epsilon decreased toward 0 during training, the risk would disappear, and SARSA and Expected SARSA would also converge to the path along the edge.

Maximization bias

Q-learning’s target takes the maximum of estimates. When the estimates are noisy, their maximum is biased upward, even if each estimate is unbiased. Sutton and Barto’s Example 6.7 makes this visible. Every episode starts in state A, which has two actions. Right ends the episode with reward 0. Left moves to state B with reward 0. State B has 10 actions, and each of them ends the episode with a reward drawn from a normal distribution with mean −0.1-0.1 and variance 1. Left is worse than right on average, so the best ϵ\epsilon-greedy policy with ϵ=0.1\epsilon = 0.1 moves left only when exploration picks it, 5% of the time. The code runs Q-learning and Double Q-learning with α=0.1\alpha = 0.1 and ϵ=0.1\epsilon = 0.1 for 300 episodes, in 2,000 independent runs, and records how often each one moves left from A.
Output from cell 8 Share of the 2,000 runs that move left from A in each episode. The dashed line is the 5% of the best ϵ\epsilon-greedy policy. In the first episode every estimate is 0, ties are broken at random, and about half of the runs move left. Q-learning then moves left more and more often, up to 95% of the runs around episode 20, and after 300 episodes it still moves left in 12% of them. With 10 noisy estimates in B, the largest of them is usually above −0.1-0.1, and often positive, after a few visits, so the target for the left action in A is too high. Double Q-learning keeps two independent estimates. One picks the best action in B and the other evaluates it, so the noise that makes an action look best does not also inflate its value. Its share of left moves falls from the first episodes on and is 6.5% over episodes 281 to 300, close to the 5% of the best ϵ\epsilon-greedy policy. Key references: (Sutton & Barto, 2018)

References

  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link

Found an error on this page? Report an issue.