Q-Learning
Q-learning (Watkins, 1989) is the foundational off-policy TD algorithm. It learns directly without ever needing to know the transition dynamics.
The Update Rule
Section titled “The Update Rule”After observing a transition :
Three knobs:
| Symbol | Role | Typical |
|---|---|---|
| Learning rate — how much to move toward the target | ||
| Discount factor — how far ahead we plan | – | |
| Exploration rate for -greedy action selection | , often annealed |
The in the target makes this off-policy: regardless of what action the agent actually takes next, the update assumes it will subsequently act greedily. This is what lets us improve even when we sometimes act randomly for exploration.
Pseudocode (Tabular)
Section titled “Pseudocode (Tabular)”Initialize Q(s, a) arbitrarily (e.g., zeros), Q(terminal, ·) = 0for each episode: s ← initial state while s is not terminal: a ← ε-greedy action from Q(s, ·) Take action a, observe r and s' Q(s, a) ← Q(s, a) + α [r + γ · max_{a'} Q(s', a') − Q(s, a)] s ← s'Convergence guarantee. With a tabular representation, every visited infinitely often, and a step-size schedule satisfying , , converges to with probability 1. The guarantee evaporates once function approximation (neural nets) enters the picture — see DQN.
Worked Example: 1D Gridworld
Section titled “Worked Example: 1D Gridworld”Consider a 5-cell corridor. The agent starts at cell S, and the goal G is at the right end. A trap T sits in the middle.
| S | | T | | G | 0 1 2 3 4- Actions:
L(left),R(right). Hitting a wall keeps you in place. - Rewards: step = , trap = (terminal), goal = (terminal).
- Settings: , , deterministic transitions.
The optimal trajectory avoids the trap by going R, R, R, R is impossible (we’d hit the trap at cell 2). With only L/R available, every path from cell 0 to cell 4 passes through cell 2 — so the trap is unavoidable in this layout. Let us instead make the trap a penalty cell the agent merely passes through with reward (not terminal). The optimal return from start is:
Hand-Computing One Update
Section titled “Hand-Computing One Update”Suppose the agent is in cell 3 and currently has , . It takes action R, lands in cell 4 (goal), gets , and the episode terminates. The TD target is:
(The bootstrap term is zero because cell 4 is terminal.) The update:
On the next episode, when the agent reaches cell 3 and acts greedily, now beats — the value has propagated one step backward from the goal.
Value Propagation
Section titled “Value Propagation”After many episodes, the Q-values converge so that the value of acting optimally from cell matches the discounted optimal return. For the corridor (with trap as penalty, not terminal):
| Cell | Greedy action | ||
|---|---|---|---|
| 0 | (bumps wall, then same path) | R | |
| 1 | R | ||
| 2 | R | ||
| 3 | R | ||
| 4 | terminal | terminal | — |
The values “flow” from the goal backward through the chain — exactly what the Bellman recursion does, but learned from samples.
Interactive: Train Q-Learning on a Gridworld
Section titled “Interactive: Train Q-Learning on a Gridworld”Step or auto-run a tabular Q-learning agent on a 4×4 grid with a goal (+10), a trap (−10), and a wall. The colored heatmap shows ; the arrows show the current greedy policy; every step’s TD update is displayed on the right.
Exploration: Why -Greedy?
Section titled “Exploration: Why ϵ\epsilonϵ-Greedy?”If the agent always picked , it would commit to whatever looked best initially and never discover that a different action might be better. The simplest fix is -greedy:
Typical schedule: start (pure exploration), anneal toward as stabilizes.
SARSA, in Contrast
Section titled “SARSA, in Contrast”SARSA’s update uses the action actually taken next, not the argmax:
In the gridworld example, if SARSA’s -greedy occasionally moves left near a cliff, SARSA learns about those random left moves and becomes cautious — it learns the value of an exploratory policy. Q-learning ignores them in its target and learns the value of the greedy policy. The classic “cliff walking” experiment in Sutton & Barto shows SARSA picking a safer (longer) path and Q-learning hugging the cliff.
Limitations of Tabular Q-Learning
Section titled “Limitations of Tabular Q-Learning”Q-learning as described stores one number per state–action pair. This is fine for a 5-cell corridor, but:
- State explosion — a Go board has more states than atoms in the observable universe.
- Continuous states — you cannot index a table by a real number.
- No generalization — visiting state teaches you nothing about a similar state .
The fix is to replace the table with a function approximator — typically a neural network — and call it DQN.
References
Section titled “References”- Watkins. Learning from Delayed Rewards. PhD thesis, Cambridge, 1989 — where Q-learning was introduced.
- Watkins & Dayan. Q-learning. Machine Learning 8, 1992 — convergence with probability 1 under the step-size conditions quoted above. Springer