|
| 1 | +# ch8_td_lambda · Eligibility Traces and TD(λ) |
| 2 | + |
| 3 | +Reference implementations and experiments for **Chapter 8** of |
| 4 | +_Reinforcement Learning Fundamentals: From Theory to Practice_. |
| 5 | + |
| 6 | +This chapter covers the forward/backward views, TD(λ) prediction, SARSA(λ) control, and true-online TD(λ) with linear function approximation. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Folder layout |
| 11 | + |
| 12 | +``` |
| 13 | +ch8_td_lambda/ |
| 14 | +├─ gridworld_small.py # 4×4 tabular gridworld (start=(3,0), goal=(0,3)) |
| 15 | +├─ td_lambda.py # TD(λ) prediction (backward view; accumulating/replacing) |
| 16 | +├─ sarsa_lambda.py # SARSA(λ) control with ε-greedy |
| 17 | +├─ true_online_td_lambda.py # True Online TD(λ) for linear FA |
| 18 | +├─ plot_tdlambda_learning.py # Produces learning curves for λ ∈ {0, 0.5, 1} |
| 19 | +├─ tests/ |
| 20 | +│ └─ test_forward_backward_equiv.py # Forward ↔ backward numerical check |
| 21 | +``` |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Quick start |
| 26 | + |
| 27 | +> Assumes Python ≥ 3.9 and `matplotlib`, `numpy`, `pytest`. |
| 28 | +
|
| 29 | +### 1) Run the unit test (forward ↔ backward equivalence) |
| 30 | + |
| 31 | +```bash |
| 32 | +pytest ch8_td_lambda/tests -q |
| 33 | +``` |
| 34 | + |
| 35 | +Expected: |
| 36 | +``` |
| 37 | +. [100%] |
| 38 | +1 passed in ~0.02s |
| 39 | +``` |
| 40 | + |
| 41 | +### 2) Generate learning curves (SARSA(λ) on gridworld) |
| 42 | + |
| 43 | +```bash |
| 44 | +python ch8_td_lambda/plot_tdlambda_learning.py |
| 45 | +``` |
| 46 | + |
| 47 | +Artifacts written to the project (figure under `figs/`): |
| 48 | +- `ch8_tdlambda_learning.csv` |
| 49 | +- `figs/ch8_tdlambda_learning.png` |
| 50 | + |
| 51 | +The plot compares success rates for **λ ∈ {0.0, 0.5, 1.0}**. |
| 52 | +(Intermediate λ typically balances speed and stability in this task.) |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Minimal examples |
| 57 | + |
| 58 | +### TD(λ) prediction (tabular; backward view) |
| 59 | +```python |
| 60 | +import numpy as np |
| 61 | +from ch8_td_lambda.gridworld_small import GridworldSmall |
| 62 | +from ch8_td_lambda.td_lambda import td_lambda_prediction |
| 63 | + |
| 64 | +env = GridworldSmall(seed=0) |
| 65 | + |
| 66 | +def random_policy(s: int): |
| 67 | + return np.ones(env.n_actions) / env.n_actions # uniform |
| 68 | + |
| 69 | +V = td_lambda_prediction(env, random_policy, gamma=0.99, alpha=0.1, lam=0.9, episodes=200) |
| 70 | +print(V.reshape(env.n_rows, env.n_cols)) |
| 71 | +``` |
| 72 | + |
| 73 | +### SARSA(λ) control (ε-greedy) |
| 74 | +```python |
| 75 | +from ch8_td_lambda.gridworld_small import GridworldSmall |
| 76 | +from ch8_td_lambda.sarsa_lambda import sarsa_lambda_control |
| 77 | +import numpy as np |
| 78 | + |
| 79 | +env = GridworldSmall(seed=0) |
| 80 | +Q = sarsa_lambda_control(env, gamma=0.99, alpha=0.1, lam=0.8, epsilon=0.1, episodes=1000) |
| 81 | +print(Q.argmax(axis=1).reshape(env.n_rows, env.n_cols)) # greedy policy |
| 82 | +``` |
| 83 | + |
| 84 | +### True Online TD(λ) (linear FA; one-hot features) |
| 85 | +```python |
| 86 | +import numpy as np |
| 87 | +from ch8_td_lambda.gridworld_small import GridworldSmall |
| 88 | +from ch8_td_lambda.true_online_td_lambda import true_online_td_lambda_linear |
| 89 | + |
| 90 | +env = GridworldSmall(seed=0) |
| 91 | +def phi(s: int): |
| 92 | + x = np.zeros(env.n_states, dtype=float) |
| 93 | + x[s] = 1.0 |
| 94 | + return x |
| 95 | + |
| 96 | +w = true_online_td_lambda_linear(env, phi, gamma=0.99, alpha=0.15, lam=0.8, episodes=800, seed=0) |
| 97 | +print(w.reshape(env.n_rows, env.n_cols)) # value estimates |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Expected outputs |
| 103 | + |
| 104 | +- **Learning curves:** `ch8_tdlambda_learning.png` — success rate vs. episodes for λ=0, 0.5, 1.0. |
| 105 | +- **CSV:** `ch8_tdlambda_learning.csv` — columns: `episodes, lambda_0.0, lambda_0.5, lambda_1.0`. |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## LaTeX snippet (embed figure in the book) |
| 110 | + |
| 111 | +After generating the figure, move/commit it under `figs/` and include: |
| 112 | + |
| 113 | +```latex |
| 114 | +\begin{figure}[h!] |
| 115 | + \centering |
| 116 | + \includegraphics[width=0.75\linewidth]{figs/ch8_tdlambda_learning.png} |
| 117 | + \caption{Learning curves for TD($\lambda$) on a $4\times4$ gridworld under SARSA($\lambda$). Intermediate $\lambda$ values (e.g., $0.5$) often balance speed and stability.} |
| 118 | + \label{fig:tdlambda-learning} |
| 119 | +\end{figure} |
| 120 | +``` |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Notes |
| 125 | + |
| 126 | +- `sarsa_lambda_control` supports `trace_type="accumulating"` or `"replacing"` (default is replacing in the learning-curve script for stability when states repeat). |
| 127 | +- For reproducibility, seeds are set inside scripts; you can adjust α, ε, and λ from the script/CLI if desired. |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## References |
| 132 | + |
| 133 | +- Sutton, R. S. (1988). *Learning to Predict by the Methods of Temporal Differences*. |
| 134 | +- Sutton, R. S., & Barto, A. G. (2018). *Reinforcement Learning: An Introduction (2nd ed.)*. |
| 135 | +- van Seijen, H., & Sutton, R. S. (2014). *True Online TD(λ)*. |
| 136 | +- Tesauro, G. (1995). *TD-Gammon*. |
| 137 | +- Schulman, J. et al. (2016). *Generalized Advantage Estimation*. |
0 commit comments