Q-Learning and SARSA¶
TL;DR
With no model of the world, an agent can still learn to act well. It learns action values \(Q(s,a)\) from its own experience, one step at a time, and acts by picking the action with the biggest \(Q\). SARSA updates \(Q\) towards the value of the action it will actually take next (on-policy). Q-learning updates towards the value of the best next action, whatever it actually does (off-policy). On the cliff world from lesson 1 this small difference matters: Q-learning learns the short route along the cliff edge but keeps falling off while it explores; SARSA learns a safer detour and collects more reward while learning.
Level: beginner–intermediate · Prerequisites: What is RL? and Monte Carlo & TD learning (the TD update)
Why should I care?¶
Until now our agents have either been told their policy (lesson 1) or evaluated a fixed one. This lesson is the payoff: an agent that starts knowing nothing about the cliff world, not where the goal is and not where the cliff is, and finds a good route by itself, only by trying.
- Q-learning (Watkins, 1989) is one of the most famous algorithms in RL. Put a neural network in place of the table and you get Deep Q-Networks, the method that learned to play Atari games from pixels (next lesson).
- The difference between on-policy and off-policy learning that you meet here comes back everywhere: PPO (used to fine-tune chatbots) is on-policy, DQN and SAC are off-policy. Knowing which one you have tells you whether you can reuse old data, and how your agent behaves while it learns.
Step 1 — From V to Q: without a model, learn action values¶
The previous lesson learned the value \(V(s)\) of each state from experience with TD learning. That tells you how good it is to be somewhere. But to act, you need to compare actions. With \(V\) alone, choosing an action means looking one step ahead:
That needs \(p(s' \mid s, a)\): a model of where each action leads. A model-free agent doesn't have one. The fix is to learn the action value \(Q(s,a)\) directly, "how good is it to take action \(a\) in state \(s\), and carry on from there". Then acting needs no model at all:
The price is four numbers per cell instead of one. The board below shows the optimal action values \(Q^*(s,a)\) of the cliff world, as four triangles per cell.
No discount in this lesson
We use \(\gamma = 1\) here, as Sutton & Barto do for their cliff-walking example. Every episode ends at the goal or the cliff, so the sums stay finite, and \(Q\) is then simply the expected total reward from that point. That makes the numbers easy to read: the 7-step route along the edge is worth \(6 \times (-1) + 10 = +4\).
What to notice
- At the start cell, \(Q^*(s, \uparrow) = 4\) (the edge route) and \(Q^*(s, \rightarrow) = -20\) (straight into the cliff). The best action is just the biggest number.
- Just above the cliff at row 2, col 1, the four values are \(\uparrow 4\), \(\rightarrow 6\), \(\downarrow -20\), \(\leftarrow 4\). The greedy policy hugs the edge, because with a perfect, non-exploring agent the edge is safe.
- \(V^*(s)\) is just the largest of the four: \(V^*(s) = \max_a Q^*(s,a)\).
- Raise the slip chance: the values near the cliff drop, and from slip 0.1 on the arrows lead away from the edge (the optimal route becomes the 11-step detour). \(Q^*\) already includes the risks of the environment. Remember this; it will matter in Step 5.
Step 2 — SARSA: learn from the move you actually make next¶
The TD update from the previous lesson nudged \(V(s)\) towards a one-step target, \(r + \gamma V(s')\). Do exactly the same with \(Q\). After taking action \(a\) in state \(s\), getting reward \(r\) and landing in \(s'\), the agent picks its next action \(a'\) with its usual ε-greedy policy, and uses \(Q(s', a')\) as its guess of the future:
The bracket is the TD error \(\delta_t\); \(\alpha\) is the step size. Then the agent really does take \(a'\), and the loop repeats. The update uses five things, \((s, a, r, s', a')\), which is where the name comes from: State, Action, Reward, State, Action.
What to notice
- SARSA is the TD update from the previous lesson, with \(Q(s,a)\) in place of \(V(s)\). Nothing else is new.
- The target uses \(a'\), the action the agent really takes next, including random exploratory moves. So SARSA learns the value of the policy it is actually following, exploration and all. That is what on-policy means.
- At a terminal state (goal or cliff) there is no future, so the target is just \(r\).
Step 3 — Q-learning: learn about the best move, whatever you do next¶
Q-learning changes one thing. Instead of the action the agent will take next, it uses the best action in \(s'\):
The agent still behaves ε-greedily, so it keeps exploring. But its target always assumes "from the next state on, I will play greedily". So Q-learning learns \(Q^*\), the values of the greedy policy, even though it never follows that policy exactly. Learning about one policy (greedy) while following another (ε-greedy) is called off-policy learning.
A worked example shows the difference. Suppose the agent stands just above the cliff (row 2, col 1), steps right, gets \(r = -1\) and lands on (row 2, col 2). Say its \(Q\) already equals the \(Q^*\) values from Step 1, where \(Q^*(s', \cdot)\) is \(\uparrow 6\), \(\rightarrow 7\), \(\downarrow -20\), \(\leftarrow 5\). With \(\gamma = 1\) and \(\varepsilon = 0.3\):
| Method | Next action used in the target | Target |
|---|---|---|
| Q-learning | the best one, \(\rightarrow\) (whatever the agent does next) | \(-1 + 7 = 6\) |
| SARSA, if \(a' = \rightarrow\) (greedy) | \(\rightarrow\) | \(-1 + 7 = 6\) |
| SARSA, if \(a' = \downarrow\) (random move into the cliff) | \(\downarrow\) | \(-1 - 20 = -21\) |
| SARSA, averaged over ε-greedy \(a'\) | \(3.75\) |
Q-learning's target ignores the agent's own clumsiness. SARSA's target includes it: on average, standing next to the cliff is worth less to an agent that sometimes steps off at random. Over many updates, SARSA's values for the edge cells end up lower, and that is what pushes it away from the edge.
What to notice
- The two updates differ in one term: \(Q(s', a')\) versus \(\max_{a'} Q(s', a')\).
- With \(\varepsilon = 0\) they are identical, because then \(a'\) is the greedy action.
- Neither needs a model: both learn from single transitions \((s, a, r, s')\) as they happen.
Step 4 — Watch them learn the cliff¶
Here is lesson 1's cliff world again, but now nobody tells the agent anything. All \(Q\) values start at 0. Press Train and watch an agent learn by trial and error. Then switch the algorithm and compare.
What to notice
- The first episodes are chaos. The agent wanders, bumps into walls and falls off the cliff. Each fall turns the triangle pointing at the cliff red, so the cliff is learned one fall at a time.
- Q-learning settles on the edge route. With the defaults (α = 0.2, ε = 0.3), give it a few hundred episodes: the green dashed line runs along row 2, right next to the cliff. That is the optimal route (7 steps, +4).
- SARSA settles on a detour. Reset with SARSA: its dashed line usually goes up to the top row, the long way round (11 steps, 0). Its values next to the cliff stay redder, because they include its own random steps into the cliff.
- Training versus testing. Press Run greedy policy (ε = 0) after training both. Q-learning's route now scores +4, SARSA's only 0. But while training, the readout's "fell off the cliff" count is much higher for Q-learning. Which one is "better" depends on whether you care about the reward collected while learning.
- Try slip = 0.2. Now even a greedy agent can slide off the edge, so Q-learning's \(Q^*\) also learns to keep away. Q-learning accounts for the environment's randomness, just not for its own exploration.
Step 5 — Who collects more reward while learning?¶
One run of one agent is noisy. To compare fairly, the widget below trains 200 SARSA agents and 200 Q-learning agents (each with its own random seed) for 500 episodes and plots the average total reward per episode as they learn. This is the classic cliff-walking experiment from Sutton & Barto's textbook, on our smaller map.
What to notice
- With \(\varepsilon = 0.3\), SARSA's curve ends above Q-learning's. In our Python check (200 runs, α = 0.2, ε = 0.3, γ = 1, 500 episodes) the average reward per episode over the last 100 episodes was −6.80 for SARSA and −8.63 for Q-learning. In those episodes 39% of Q-learning's episodes ended in the cliff, against 14% for SARSA (SARSA still falls sometimes: the start cell itself is next to the cliff). Your widget numbers will differ slightly, since it runs its own seeds.
- Yet Q-learning learned the better policy. All 200 Q-learning agents ended with the 7-step edge route (+4 when run greedily). 196 of the 200 SARSA agents ended with the 11-step detour (0 when run greedily).
- Now press ε = 0.1. On this small map the result flips: Q-learning −0.05, SARSA −2.35. Our cliff is only 4 cells long, so with 10% random moves Q-learning falls in only about 1 episode in 8 (12.5%), and the short edge route pays even while exploring. Working out the exact average for a fixed route (a policy evaluation, as in the dynamic programming lesson) gives −0.12 for the ε-greedy edge route and −1.97 for the detour at ε = 0.1, but −8.76 and −7.46 at ε = 0.3. The two cross near ε = 0.2. Sutton & Barto's original map has a 10-cell cliff, which is why their famous figure shows SARSA winning already at ε = 0.1.
- SARSA at ε = 0.1 is also a lesson in noise: the edge is the better ε-greedy route there, but one unlucky fall makes a big update with α = 0.2, and 112 of the 200 SARSA agents still ended on the detour.
Step 6 — On-policy versus off-policy¶
Every RL agent has two roles for a policy:
- The behaviour policy is the one that actually picks the actions and generates the experience (here: ε-greedy).
- The target policy is the one whose values the agent is learning (the one the TD target "assumes" for the future).
On-policy methods like SARSA use the same policy for both. Off-policy methods like Q-learning learn about one policy (greedy) from data generated by another (ε-greedy).
Why would you want off-policy learning? Because the data can come from anywhere: an exploring agent, an old version of the agent, a human demonstrator. That is what lets DQN reuse old experience from a replay buffer (next lesson). On-policy methods have to learn from data that their current policy generated.
Two loose ends:
- Expected SARSA replaces the sampled \(Q(s', a')\) with its average over the policy: \(r + \gamma \sum_{a'} \pi(a' \mid s')\, Q(s', a')\). It learns the same thing as SARSA but with less noise per update, since it doesn't depend on which \(a'\) happened to be drawn. (That is the 3.75 in the table of Step 3.) In our check, with ε = 0.3, α = 0.1 and 3000 episodes, all 20 Expected SARSA runs ended on the same 11-step detour as SARSA. With a greedy \(\pi\) in the sum, Expected SARSA is Q-learning.
- If ε shrinks to zero, they meet. SARSA's caution only exists because of exploration. Let ε decay over time and SARSA's target becomes Q-learning's. In our check (ε = 0.3/(1 + episode/20), α = 0.2, 2000 episodes), all 100 Q-learning runs and 86 of 100 SARSA runs ended with the edge route. (The rest of SARSA's runs had stopped exploring before they had re-checked the edge: decaying ε too fast has its own cost.)
What to notice
- "On-policy" and "off-policy" are about which policy the target describes, not about how the agent behaves. Both of our agents behave identically.
- Neither is simply better. Off-policy finds the optimal greedy policy and can reuse any data. On-policy optimises the reward you actually get while acting the way you act, which matters when mistakes during learning are costly (a real robot near a real cliff).
Putting it all together¶
Both algorithms in plain Python, on lesson 1's cliff world. The only difference between them is one line, the target:
import random
from collections import Counter
MAP = ["......",
"..#...",
"......",
"SCCCCG"]
MOVES = [(-1, 0), (0, 1), (1, 0), (0, -1)] # up, right, down, left
class CliffWorld:
"""Lesson 1's grid world: −1 per step, +10 at the goal, −20 for the cliff."""
def __init__(self, slip=0.0, seed=0):
self.slip, self.rng = slip, random.Random(seed)
def reset(self):
self.pos = (3, 0)
return self.pos
def step(self, a):
if self.rng.random() < self.slip: # slide sideways
a = (a + self.rng.choice([1, 3])) % 4
r, c = self.pos[0] + MOVES[a][0], self.pos[1] + MOVES[a][1]
if 0 <= r < 4 and 0 <= c < 6 and MAP[r][c] != "#":
self.pos = (r, c)
tile = MAP[self.pos[0]][self.pos[1]]
if tile == "G":
return self.pos, +10, True
if tile == "C":
return self.pos, -20, True
return self.pos, -1, False
def epsilon_greedy(Q, s, eps, rng):
if rng.random() < eps:
return rng.randrange(4) # explore
best = max(Q[s])
return rng.choice([a for a in range(4) if Q[s][a] == best]) # exploit (break ties randomly)
def train(algo, episodes=500, alpha=0.2, eps=0.3, gamma=1.0, seed=0):
rng = random.Random(seed)
env = CliffWorld(seed=seed)
Q = {(r, c): [0.0] * 4 for r in range(4) for c in range(6)}
rewards = [] # total reward of every training episode
for _ in range(episodes):
s = env.reset()
a = epsilon_greedy(Q, s, eps, rng)
total, done = 0, False
while not done:
s2, r, done = env.step(a)
total += r
a2 = epsilon_greedy(Q, s2, eps, rng) # the action we will really take next
if done:
target = r # no future after the goal or the cliff
elif algo == "sarsa":
target = r + gamma * Q[s2][a2] # on-policy: value of the action we WILL take
else:
target = r + gamma * max(Q[s2]) # off-policy: value of the BEST action
Q[s][a] += alpha * (target - Q[s][a]) # TD update
s, a = s2, a2
rewards.append(total)
return Q, rewards
def greedy_route(Q):
"""Follow the learned greedy policy (ε = 0) from the start, without slipping."""
env, s, done, total, path = CliffWorld(), None, False, 0, []
s = env.reset()
while not done and len(path) < 50:
s, r, done = env.step(Q[s].index(max(Q[s])))
total += r
path.append(s)
return f"{len(path)} steps, return {total:+d}"
for eps in [0.3, 0.1]:
print(f"ε = {eps}")
for algo in ["sarsa", "q-learning"]:
last100, routes = [], Counter()
for seed in range(200):
Q, rewards = train(algo, eps=eps, seed=seed)
last100.append(sum(rewards[-100:]) / 100)
routes[greedy_route(Q)] += 1
print(f" {algo:10s} reward per episode (last 100) = {sum(last100) / 200:6.2f}"
f" greedy route: {routes.most_common(1)[0][0]} in {routes.most_common(1)[0][1]}/200 runs")
ε = 0.3
sarsa reward per episode (last 100) = -6.80 greedy route: 11 steps, return +0 in 196/200 runs
q-learning reward per episode (last 100) = -8.63 greedy route: 7 steps, return +4 in 200/200 runs
ε = 0.1
sarsa reward per episode (last 100) = -2.35 greedy route: 11 steps, return +0 in 112/200 runs
q-learning reward per episode (last 100) = -0.05 greedy route: 7 steps, return +4 in 200/200 runs
(It takes about 20 seconds in plain Python.) The table Q has \(4 \times 6 \times 4 = 96\) entries. That is fine here, but a game screen has far too many states for a table. Replacing the table with a neural network that outputs \(Q(s, \cdot)\) is the idea behind Deep Q-Networks, the next lesson.
Common misconceptions¶
\"Q-learning is better than SARSA because it finds the optimal policy.\"
Q-learning finds the optimal greedy policy, the best one if you stop exploring. If the agent keeps exploring (as many real systems do while they learn), SARSA can collect more reward, as the ε = 0.3 curves show. "Better" depends on whether you score the learned policy or the learning itself.
\"SARSA avoids the cliff because it knows the cliff is dangerous.\"
Both algorithms learn that the cliff is bad: both have \(Q(s, \downarrow) \approx -20\) next to it. SARSA avoids the edge because its values include its own future random moves. Set ε to 0 and that reason disappears.
\"Off-policy means the agent doesn't need to explore.\"
Q-learning still needs exploration to see the states and actions it learns about. Off-policy only means the target assumes a different policy (greedy) from the one that behaves (ε-greedy). No exploration, no data about the alternatives.
\"SARSA learns the optimal policy, just more slowly.\"
With a fixed ε, SARSA learns the best ε-greedy policy, which is not the optimal policy when exploration is risky. It only reaches the optimal policy if ε decays towards zero over time.
Check yourself¶
1. Why do model-free agents learn Q(s, a) instead of V(s)?
To act with \(V\) you have to look one step ahead: "which action leads to the best next state?" That needs a model \(p(s' \mid s, a)\). With \(Q\) you just pick \(\arg\max_a Q(s,a)\), no model required.
2. Write down the SARSA and Q-learning updates. Which single term differs?
SARSA: \(Q(s,a) \leftarrow Q(s,a) + \alpha[r + \gamma Q(s',a') - Q(s,a)]\), where \(a'\) is the next action the agent really takes. Q-learning: the same, with \(\max_{a'} Q(s',a')\) in place of \(Q(s',a')\).
3. An agent is next to the cliff. Its next action a′ happens to be a random step into the cliff, with Q(s′, a′) = −20, while max Q(s′, ·) = 7. With r = −1 and γ = 1, what are the SARSA and Q-learning targets?
SARSA: \(-1 + (-20) = -21\). Q-learning: \(-1 + 7 = 6\). SARSA "feels" its own random mistake; Q-learning assumes it will act greedily next.
4. In the learning-curves widget at ε = 0.3, Q-learning earns less reward per episode than SARSA, yet its greedy route scores +4 against SARSA's 0. How can both be true?
The curves measure reward while exploring (ε = 0.3). Q-learning's route runs along the cliff edge, where random moves often fall off (39% of its late training episodes ended in the cliff in our Python check). Its greedy route is optimal once exploration stops. SARSA's detour is worse without exploration, but safer with it.
5. Is Q-learning on-policy or off-policy? What practical advantage does that give?
Off-policy: it learns the values of the greedy policy while following an ε-greedy one. Because the target doesn't depend on how the data was generated, it can learn from old experience, other agents or demonstrations. DQN uses this to learn from a replay buffer of past transitions.
Further reading¶
- Richard Sutton & Andrew Barto, Reinforcement Learning: An Introduction (2nd ed.), Sections 6.4–6.6: SARSA, Q-learning, Expected SARSA, and Example 6.6 Cliff Walking, the experiment reproduced here.
- Christopher Watkins & Peter Dayan, Q-learning, Machine Learning 8, 1992. The convergence proof for Q-learning.
- Gavin Rummery & Mahesan Niranjan, On-line Q-learning using connectionist systems, Cambridge tech. report, 1994. The paper that introduced SARSA (under another name).
- Harm van Seijen et al., A theoretical and empirical analysis of Expected Sarsa, IEEE ADPRL 2009.
- David Silver, UCL Course on RL, Lecture 5: Model-Free Control.
Next lessons: Deep Q-Networks