Unit 3 of 11 · about 4 hours · explain, implement
Trajectories, episodes, horizons, discounting, termination and truncation
Every later target multiplies by . One wrong flag turns a time limit into a terminal value and poisons all values.
You'll be able to
- Explain episodic, continuing, finite-horizon and infinite-horizon tasks and where and belong.
- Distinguish termination with no bootstrap from truncation with bootstrap and implement the correct target.
- Run a Gymnasium time-limit check that proves which flag bootstraps on the installed release.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Returns, episodes and unified notationReinforcement Learning: An Introduction (2nd ed.) - Chapters 3 and 4
Chapter 3, Sections 3.3 Returns and Episodes and 3.4 Unified Notation for Episodic and Continuing Tasks, returns (3.7) to (3.11) with Exercises 3.8 and 3.9, printed pp. 54 to 57. The authors post the full text free at incompleteideas.net.
- Handling Time Limits: termination versus truncationGymnasium - Time Limits and Episode Semantics
Termination, Truncation, Importance in learning code and Solution; the correct target uses (page read 2026-10-14)
- Vector autoreset modesGymnasium - Time Limits and Episode Semantics
Next-Step Mode as default with
make_vec, Same-Step Mode withfinal_obsand Disabled Mode (page read 2026-10-14). The page does not state the autoreset step's reward; check it on your Gymnasium version as in Reproduce. - Assignment 1 horizon questionsStanford CS234 Winter 2026 - Foundations and Planning
Section 1, Effect of Effective Horizon, parts (a) to (d), pp. 1 to 2; inventory MDP with horizon versus discount
Answer on paper. Do not start the RiverSwim code here.
Focus on
- Episodic tasks end in terminal states. Continuing tasks have no terminal. Finite-horizon tasks stop after steps even without a terminal.
- Termination means after. Truncation means keep . Code must use , not .
- In Gymnasium 1.4.0 a single
Envneeds manualresetafter done.SyncVectorEnvwith defaultNEXT_STEPreturns the true final first, then a fake autoreset step with reward 0 and both flags false. Never store that step as a transition.
Common mistakes · 3
- Using
done = terminated or truncatedto cut the bootstrap. Truncation must still bootstrap. - Storing the
NEXT_STEPautoreset transition. Its observation is the reset state and its reward is 0. It is not a transition. - Adding remaining time to the observation for external truncation. The tutorial reserves that fix for finite-horizon inherent limits.
Try it yourself
Closed-book, before checking anything
gymnasium.make("CartPole-v1", max_episode_steps=5) and env.reset(seed=0), show step 5 ends with terminated=False and truncated=True. With a 2-env SyncVectorEnv show step 5 has reward 1 with truncation true, then step 6 has reward 0 with both flags false.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.With and rewards 1, 2, 6, 3, 2 over 5 steps, what are to ?Answer
, , , , , . Work backward from .
2.With and reward 2 then infinite 7s, what are and ?Answer
from . from .
3.What is the correct one-step value target at termination versus truncation?Answer
At termination use . At truncation use . In code use
vf_target = rew + gamma * (1 - terminated) * vf_next_state.4.What does the vector step after done return under the default mode?Answer
Reward 0 with both flags false and the reset observation. Skip it in buffers and losses.
Practice
- Stanford CS234 Assignment 1, Section 1: horizon versus discount
Section 1, parts (a) to (d), pp. 1 to 2; inventory starting at state 3 with horizon and discount
Answer on paper. Then confirm the flag behavior with the CartPole checks in Reproduce. The section mini-project covers seeded terminal defects.
You're done when
Your single-env and vector checks print the flags above, and you can state the bootstrap rule without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 4 more in the section
- Stanford CS234 Winter 2026 - Foundations and PlanningStanford University · primary
Study Lecture 1 (
lecture1pre.pdfandlecture1post.pdf) and Lecture 2 on tabular MDP planning (lecture2pre.pdfandlecture2post.pdfunder https://web.stanford.edu/class/cs234/slides/). Work Assignment 1 independently: the written MDP, horizon and Bellman-operator questions and thevi_and_pi.pyplanning code on RiverSwim (NumPy only). Do not copy or publish course solutions. - Reinforcement Learning: An Introduction (2nd ed.) - Chapters 3 and 4Richard Sutton and Andrew Barto · required
The authors post the full second edition free at incompleteideas.net; cite the printed page numbers. Study Chapter 3 (Sections 3.1 to 3.7, finite MDPs, returns, Bellman equations and optimality) and Chapter 4 (Sections 4.1 to 4.7, policy evaluation, improvement, policy and value iteration, asynchronous DP and GPI). Reproduce Example 4.1 and Figure 4.1 exactly, then Example 4.3 (the gambler), and write exact tests for stochastic transitions, terminals, ties and stopping error.
- Gymnasium - Time Limits and Episode SemanticsFarama Foundation · required
Read the termination, truncation and "importance in learning code" sections, then the vector autoreset note at https://farama.org/Vector-Autoreset-Mode (next-step autoreset is the default in Gymnasium 1.x). Build a wrapper test that distinguishes termination from truncation and proves which cases bootstrap before using any third-party environment.
Also in this section
- Hands-on Modern RL - MDPs, Values and Bellman EquationsWalking Labs · support
Use the value and Bellman page and the value experiment page (
code/chapter03_mdp/bellman_equation_verify.pyandgridworld_q_learning.pyrun on CPU in seconds) as a code companion. Check every equation against Sutton and Barto before adopting it. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; itscode/chapterNNfolders do not follow the site chapter numbers. - Deep-ML - Planning ProblemsDeep-ML · support
Use named problems as closed-book checks after your own implementation passes: 157 (Bellman equation for value iteration), 164 (the gambler's problem by value iteration) and 467 (policy iteration). Read each problem's tests before trusting its statement.
- Reinforcement Learning Control Laboratory - Planning CoreRoadmap-Final · required
Complete Milestone A planning requirements: an explicit finite MDP, exact policy evaluation, value iteration, policy iteration, simulator agreement and seeded reward, transition, terminal and tie defects.
- Reinforcement Learning: An Introduction (2nd ed.) - Chapter 2 banditsRichard Sutton and Andrew Barto · reference
Read Section 2.9, Associative Search (Contextual Bandits), printed p. 41, with Exercise 2.10 as the context versus no-context check. Use only this section from Chapter 2.