Unit 10 of 13 · about 4 hours · measure
SARSA versus Q-learning on risky paths, with multiple seeds and independent evaluation
This is the on-policy versus off-policy split made visible. Identical exploration produces different policies because each update prices risk differently.
You'll be able to
- Explain why Sarsa learns the safe path and Q-learning learns the cliff edge under shared -greedy behavior.
- Run a multi-seed cliff comparison with independent evaluation streams and report spread, not one run.
- Attribute the performance gap to update-distribution effects, not to tuning.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Cliff walking: on-policy versus off-policy under riskReinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7
Chapter 6, Example 6.6 with its episode-reward graph (printed pp. 131 to 133)
Read the online-performance explanation closely: Q-learning learns optimal values but falls in more often during training.
- CliffWalking environment contractGymnasium Core API
Environment description: start state, goal state, cliff penalty with return to start, and the four-action movement set (Gymnasium 1.4.0; page read 2026-10-14)
Use the installed release behavior you check by running it; the page text is the contract, not the dynamics proof.
- Matched budgets for the comparisonReinforcement Learning Control Laboratory - Tabular Control
Measurement requirements: matched interaction budgets with per-seed raw curves
Focus on
- Sarsa prices the exploration it actually does: its updates include the -greedy falls, so the safe path wins. Q-learning prices the max, so the cliff edge wins on values while behavior still falls.
- Online episode return mixes learning and exploration. Compare greedy-policy evaluation streams on held-out seeds, averaged over many runs as in Figure 6.3.
- Use the same , the same and the same seeds for both algorithms. Otherwise the gap may come from tuning, not from the update.
Common mistakes · 3
- Judging by learned values alone. The Q-learning values sit closer to optimal while its online return is worse; report both.
- Running one seed each. Cliff falls are rare events; single runs move the averages by ten points or more. Keep every seed.
- Correcting the Gymnasium page number for Example 6.6. Its page 132 matches the second-edition printed page; cite the example number anyway, since printings and PDF page counts differ.
Try it yourself
Closed-book, before checking anything
(3, 0), goal (3, 11), cliff cells (3, 1) to (3, 10), reward -1 per step and -100 on cliff entry with return to start. Verify single transitions: stepping into the cliff returns the start with -100, and reaching the goal ends the episode.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Both algorithms use identical -greedy behavior. Why do their paths differ?Answer
Their targets differ. Sarsa backs up the exploratory action including falls; Q-learning backs up the max, which ignores the fall risk during learning.
2.The Q-learning sum of rewards per episode is worse although its values are more optimal. Explain.Answer
Online returns count exploration falls. The values describe the greedy policy; the behavior policy keeps stepping off the edge.
3.What happens as decays to zero for both?Answer
Both converge toward the optimal policy. The book states this at the end of Example 6.6.
4.A comparison tunes separately per algorithm and declares a winner. What is wrong?Answer
The tuning budget differs, so the gap may come from exploration, not the update. Match budgets and report seeds.
Practice
- Deep-ML 518: Cliff Walking Sarsa versus Q-Learning
Implement
cliff_walking_comparison; the public tests call it withnum_episodes=500,alpha=0.5,gamma=1.0,epsilon=0.1,seed=42and expect(-37.1, -59.7), then withnum_episodes=200andseed=0and expect(-53.7, -63.1)Each algorithm uses its own
RandomStateat the given seed, episodes cap at 1000 steps, and ties usenp.argmax. Treat the pinned averages as regression values for your implementation, not as targets: re-run with more seeds and report the spread.
You're done when
Your grid passes the single-transition checks, your multi-seed comparison shows the safer Sarsa path against the optimal Q-learning values, and you can explain the gap without appealing to tuning.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 4 more in the section
- Reinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7Richard Sutton and Andrew Barto · primary
The authors post the full second edition free at incompleteideas.net; cite the printed page numbers. Study Sections 2.4 and 2.5 (incremental and constant step-size estimates), Chapter 5 without the starred sections (Monte Carlo prediction and control, ordinary and weighted importance sampling), Chapter 6 Sections 6.1 to 6.7 (TD(0), Sarsa, Q-learning, Expected Sarsa, maximization bias) and Chapter 7 Sections 7.1 to 7.3 (n-step methods). Reproduce Example 5.4 (off-policy blackjack), Example 6.2 (random walk) and Example 6.6 (cliff walking), and implement each update from fixed hand-calculated trajectories before training an agent.
- Gymnasium Core APIFarama Foundation · reference
Use the reset, step, seeding and wrapper documentation as the behavior authority for the installed environment version:
reset(seed=...)seeds once,stepreturnsterminatedandtruncated, and vector environments autoreset on the next step by default. Test observation, reward, terminated, truncated and info fields at the boundary. - Reinforcement Learning Control Laboratory - Tabular ControlRoadmap-Final · required
Complete the MC, SARSA and Q-learning requirements with exact trajectory updates, stochastic and exploration-sensitive environments, visitation logs, matched interaction budgets and seeded alignment and bootstrap defects.
Also in this section
- Stanford CS234 Winter 2026 - Policy Evaluation and Q-LearningStanford University · required
Study Lecture 3 (tabular policy evaluation) and Lecture 4 (Q-learning) slides and the relevant independent Assignment 1 and 2 written questions. Use the course treatment to state model-access, convergence, coverage and function-approximation assumptions for each update.
- Hands-on Modern RL - Dynamic Programming, Monte Carlo and TDWalking Labs · support
Use the DP, MC and TD page and the bandit script as a code companion. Rebuild the algorithms in the shared track schema, and keep the bandit comparison as a seed-sensitivity case: which method wins changes with the seed. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; its
code/chapterNNfolders do not follow the site chapter numbers. - Deep-ML - Tabular Prediction and Control ProblemsDeep-ML · support
Use named problems as closed-book checks after your own implementation passes: 272 (first-visit Monte Carlo), 257 (TD error), 273 (n-step TD), 175 (SARSA), 476 (Expected SARSA), 133 (Q-learning) and 518 (cliff walking, SARSA versus Q-learning). Read each problem's tests before trusting its statement.
- Reinforcement Learning: An Introduction (2nd ed.) - Starred Sections 5.8 and 5.9 and Chapter 10 Sections 10.3 to 10.5Richard Sutton and Andrew Barto · support
Study starred Section 5.9 on per-decision importance sampling only for the per-decision estimator, and Chapter 10 Sections 10.3 to 10.5 on average reward and differential values only for the continuing-task unit. Cite printed pages; the authors post the full text free at incompleteideas.net.