Unit 9 of 13 · about 4 hours · implement

Q-learning and \(\epsilon\)-greedy exploration schedules

Q-learning learns optimal values from exploratory data. The max target separates what is learned from how data is collected, and schedules decide whether every pair gets visited.

You'll be able to

  • Implement Q-learning with -greedy behavior and match fixed updates exactly.
  • Derive -greedy action probabilities and design a decay schedule that keeps visiting all pairs.
  • Explain why Q-learning converges toward optimal values while its behavior stays exploratory.

Covers

Q-learning controlEpsilon-greedy behavior and exploration schedules
1

Read

In this order, only the parts listed

0 of 3 done

  1. Q-learning and -greedy behaviorReinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7

    Chapter 6, Sections 6.4 and 6.5, rule (6.8) with the Q-learning box (printed pp. 129 to 132)

    Focus on the max target and -greedy behavior; leave Expected Sarsa to the previous unit.

  2. Q-learning convergence and coverageStanford CS234 Winter 2026 - Policy Evaluation and Q-Learning

    Lecture 4, -greedy policies and their improvement (slides 19 to 21), GLIE (slides 29 to 30), Q-learning with -greedy exploration (slides 35 to 36) and Convergence Properties of Q-Learning with the Robbins-Monro step-size conditions (slide 37)

    Use the assumption statements; the function-approximation half of the lecture belongs to the next section.

  3. -greedy as a seed-sensitive choiceHands-on Modern RL - Dynamic Programming, Monte Carlo and TD

    Section 2.1 on exploration and exploitation with the bandit comparison (page read 2026-10-14)

    Read the method ranking as seed-dependent and compare across seeds; one run proves nothing.

2

Focus on

  • Q-learning's target assumes the greedy action next, whatever was. Convergence needs all pairs visited repeatedly plus standard step-size conditions.
  • -greedy picks the greedy action with probability and each other action with . With and two actions that is and .
  • Decay toward zero for asymptotic optimality, but keep it high enough early that coverage precedes exploitation. The cliff unit shows what happens otherwise.
Common mistakes · 3
  • Decaying to zero before values settle. Behavior turns greedy around bad values and coverage ends early.
  • Copying action indices across libraries. Deep-ML 518 orders the four moves up, down, left, right while Gymnasium orders them up, right, down, left. Index by name, not by number.
  • Reading online return as the learned policy's value. Under -greedy behavior, online returns include exploration steps; evaluate the greedy policy separately.
3

Try it yourself

Closed-book, before checking anything

From a blank file, process a single-action chain ending at a terminal state with rewards 1.0, 2.0 and 3.0 using alpha=0.5 and gamma=0.9 from zero values to get Q('A', 'go') = 0.5000, Q('B', 'go') = 1.0000 and Q('C', 'go') = 1.5000. State why the max changes nothing on this chain.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.With and four actions, what is the greedy-action probability?Answer

    . Each other action gets .

  2. 2.Why can Q-learning learn from random behavior?Answer

    Its target bootstraps the max, so updates point at the optimal values whenever all pairs keep being visited, regardless of which policy visits them.

  3. 3.Greedy actions tie. How should ties break?Answer

    Deterministically and documented, for example lowest index. Deep-ML relies on np.argmax returning index 0.

  4. 4.Online average improves while the greedy policy is unchanged. What happened?Answer

    Exploration artifacts or luck. Report the greedy policy's return on separate evaluation seeds.

5

Practice

  • Deep-ML 133: Q-Learning

    Implement q_learning on the given P and R matrices; the public tests call np.random.seed(42) and compare small learned Q-tables

    The tests pin stochastic outcomes, so match the update and the -greedy sampling order exactly, then re-run with two more seeds of your own.

You're done when

Deep-ML 133 passes, your chain values match by hand, and you can write -greedy probabilities for any without notes.

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.

  • 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/chapterNN folders do not follow the site chapter numbers.

Also in this section

  • 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, step returns terminated and truncated, and vector environments autoreset on the next step by default. Test observation, reward, terminated, truncated and info fields at the boundary.

  • 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 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.

  • 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.