Foundations to Frontier
Tests

SARSA and Q-learning

SARSA and Q-learning apply temporal-difference learning to action values, so an agent can learn to act without a model. SARSA learns the value of the policy it follows, including its exploration; Q-learning learns the optimal values whatever it does. This page implements both, and Expected SARSA, on cliff walking, and measures how exploration rate and step size change the paths they learn and the reward they collect while learning.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learning which move to make

Imagine walking along a cliff to reach a goal. The shortest route hugs the edge; a safer route keeps a few steps away. If you sometimes stumble in a random direction, the safe route is better, because a stumble at the edge is a fall. SARSA learns with its own stumbles in mind and picks the safe route. Q-learning learns the best route as if it never stumbled, which is the shortest one, but it falls more often while still learning.

Reinforcement learning lab
S
G
Cliff walking: −1 per step, −100 and back to S for stepping into the cliff (bottom row). Curves: reward per episode (20-episode moving average, 5 runs, clipped at −150) for SARSA (solid), Q-learning and Expected SARSA (dashed). Mean over episodes 401–500: SARSA -22.8, Q-learning -54.7, Expected SARSA -20.8. The grid shows the greedy path of Q-learning after the last run (13 steps): Q-learning learns the shortest path along the edge, but while exploring it falls more often; SARSA learns a safer path because its targets include its own random moves.
Why engineers care

Q-learning and its deep-network version, DQN, are behind agents that learned Atari games directly from the screen.

📝 Quick check (Level 0)
0/3 answered
  1. 1. SARSA takes into account…
  2. 2. Q-learning learns the route that is best…
  3. 3. Near a cliff, an agent that stumbles should prefer…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Action values: Q(s, a) is the expected return after taking a in s and then following the policy. Acting greedily on Q needs no model.
  • SARSA (on-policy): Q(s, a) ← Q(s, a) + α[r + γQ(s’, a’) − Q(s, a)], where a’ is the action actually taken next. The name lists the five quantities used: s, a, r, s’, a’.
  • Q-learning (off-policy): Q(s, a) ← Q(s, a) + α[r + γ max_a’ Q(s’, a’) − Q(s, a)]. It learns Q* while following any exploring policy.
  • Expected SARSA: the target averages over the next action, r + γ Σ_a’ π(a’|s’)Q(s’, a’). It removes the variance of sampling a’, and with a greedy target policy it is Q-learning.
  • ε-greedy: take a random action with probability ε, otherwise a greedy one.
  • Maximisation bias: the max over noisy estimates overestimates, which Double Q-learning (topic L3-2) corrects.
📝 Quick check (Level 1)
0/3 answered
  1. 1. SARSA's target uses…
  2. 2. Q-learning is off-policy because…
  3. 3. Expected SARSA with a greedy target policy equals…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Cliff walking

Exercise 1.

  • Cliff walking: a 4×12 grid, start bottom left, goal bottom right, the ten cells between them are a cliff. −1 per step; stepping into the cliff costs −100 and returns the agent to the start.
  • SARSA and Q-learning with α = 0.5, ε = 0.1, no discounting, 500 episodes.
  • The greedy path each one learns, and the reward each collects per episode while still exploring.

Practice.

  • Use SARSA or Expected SARSA when mistakes during learning are costly: they account for exploration.
  • Use Q-learning when you can learn offline or in simulation and want the optimal policy for later deployment without exploration.
  • Prefer Expected SARSA: lower variance, works with larger step sizes, and is never worse in these experiments.
  • Decay ε over time: SARSA then converges to the optimal policy too, and Q-learning stops paying for exploration.
  • Break ties randomly in the greedy choice, or the agent can get stuck repeating one action.
  • Tools: Gymnasium’s CliffWalking and FrozenLake environments; tabular code in NumPy; DQN for large state spaces.
def update(Q, s, a, r, s2, a2, done, method, alpha, eps, gamma=1.0):
    q2 = Q[s2]
    if done: target = r
    elif method == "sarsa": target = r + gamma * q2[a2]
    elif method == "q": target = r + gamma * max(q2)
    else: target = r + gamma * (eps / 4 * sum(q2) + (1 - eps) * max(q2))
    Q[s][a] += alpha * (target - Q[s][a])
🐍 Exercise 1: SARSA and Q-learning on the cliff
Implement cliff walking, SARSA and Q-learning with ε-greedy exploration, and compare the greedy paths and the reward collected while learning.
📝 Quick check (Level 2)
0/3 answered
  1. 1. When exploration mistakes are costly, prefer…
  2. 2. To make SARSA converge to the optimal policy…
  3. 3. Expected SARSA's advantage is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Convergence. Tabular Q-learning converges to Q* with probability 1 if every state–action pair is updated infinitely often and the step sizes satisfy Σα = ∞, Σα² < ∞ (Watkins and Dayan 1992). Its expected target is the Bellman optimality operator TQ, whose unique fixed point is Q. SARSA’s expected target is T^πQ for the current policy π; it converges to Q* if, in addition, the policy becomes greedy in the limit while still exploring infinitely often (GLIE; Singh et al. 2000).

Why the paths differ. With fixed ε > 0, SARSA evaluates the ε-greedy policy, so cells next to the cliff look bad (a random ‘down’ costs −100) and it prefers the detour. Q-learning evaluates the greedy policy, for which the edge is safe, so it learns the 13-step path, but the ε-greedy behaviour it actually follows falls off the edge.

Maximisation bias. If the estimates Q̂(s’, a) are unbiased but noisy, E[max_a Q̂(s’, a)] ≥ max_a E[Q̂(s’, a)] by Jensen’s inequality, because max is convex. Q-learning therefore overestimates, the more so the more actions and the noisier the estimates.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Q-learning's expected target is…
  2. 2. SARSA converges to Q* if the policy is…
  3. 3. Maximisation bias follows from…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do the exploration rate and the step size change what SARSA, Q-learning and Expected SARSA learn on cliff walking, and how much reward each collects while learning?

History.

  • C. Watkins (1989) introduced Q-learning; Watkins and P. Dayan (1992) proved its convergence.
  • G. Rummery and M. Niranjan (1994) introduced SARSA, under the name modified connectionist Q-learning; the name SARSA is due to R. S. Sutton.
  • S. P. Singh, T. Jaakkola, M. Littman and C. Szepesvári (2000) proved SARSA’s convergence under GLIE policies.
  • H. van Seijen, H. van Hasselt, S. Whiteson and M. Wiering (2009) analysed Expected SARSA.

Exercise 2.

  • Cliff walking, no discounting, 10 runs of 500 episodes for each setting.
  • ε = 0.05, 0.1 and 0.2 with α = 0.5; and α = 1 with ε = 0.1.
  • Online reward: the mean reward per episode over episodes 401–500, while still exploring. Greedy path: its length when it reaches the goal, and in how many of the 10 runs it does.

Seed 0:

setting SARSA online SARSA path (runs reaching goal) Q-learning online Q-learning path Expected SARSA online Expected SARSA path
α = 0.5, ε = 0.05 −21.6 17.0 (8/10) −33.3 13 (10/10) −17.8 15.0 (10/10)
α = 0.5, ε = 0.1 −27.5 17.4 (9/10) −48.3 13 (10/10) −20.4 15.0 (10/10)
α = 0.5, ε = 0.2 −45.6 17.3 (6/10) −109.8 13 (10/10) −28.1 16.4 (10/10)
α = 1, ε = 0.1 −117.1 19.0 (3/10) −47.7 13 (10/10) −22.1 15.0 (10/10)
  • Q-learning always learned the optimal 13-step path, but collected the least reward while exploring at α = 0.5: −48.3 per episode at ε = 0.1, against −27.5 for SARSA, because exploratory steps along the edge fall off.
  • SARSA learned detours of about 17 steps, and in some runs its greedy policy did not reach the goal at all: it evaluates the ε-greedy policy, not the greedy one.
  • Expected SARSA collected the most reward in every setting and mostly learned 15-step paths, one row away from the edge.
  • Exploration is costly here: raising ε from 0.05 to 0.2 lowered Q-learning’s online reward from −33.3 to −109.8.
  • SARSA broke down at α = 1 (−117.1; only 3 runs found the goal), because the sampled next action makes its target very noisy. Expected SARSA was unaffected (−22.1).
  • Seeds 1 and 2, α = 0.5, ε = 0.1: SARSA −25.8 and −26.0, Q-learning −53.9 and −49.0, Expected SARSA −19.2 and −21.1. At α = 1, SARSA −119.6 and −140.0 against Expected SARSA −20.4 and −20.8.
🐍 Exercise 2 (research): how do the exploration rate and the step size change what SARSA, Q-learning and Expected SARSA learn on cliff walking, and how much reward each collects while learning?
Compare SARSA, Q-learning and Expected SARSA on cliff walking across exploration rates and step sizes, recording the online reward and the greedy path each learns.

SARSA and Q-learning in practice

  • Deep Q-networks (next section) replace the table by a neural network and add experience replay and target networks to keep Q-learning stable.
  • Overestimation: Double Q-learning keeps two estimates, choosing the action with one and evaluating it with the other.
  • Safe exploration: in robotics and other physical systems, on-policy methods, conservative exploration or learning in simulation first are used to avoid costly mistakes.
  • n-step and λ versions (n-step SARSA, Q(λ)) propagate rewards faster along long paths.

References

  1. C. J. C. H. Watkins and P. Dayan (1992). “Q-learning.” Machine Learning.
  2. G. A. Rummery and M. Niranjan (1994). “On-line Q-learning using connectionist systems.” Technical report, Cambridge University.
  3. S. P. Singh, T. Jaakkola, M. L. Littman and C. Szepesvári (2000). “Convergence results for single-step on-policy reinforcement-learning algorithms.” Machine Learning.
  4. H. van Seijen, H. van Hasselt, S. Whiteson and M. Wiering (2009). “A theoretical and empirical analysis of Expected Sarsa.” IEEE ADPRL.
  5. R. S. Sutton and A. G. Barto (2018). Reinforcement Learning: An Introduction, 2nd ed., chapter 6. MIT Press.