Skip to content

Q-Learning

Q-learning (Watkins, 1989) is the foundational off-policy TD algorithm. It learns QQ^* directly without ever needing to know the transition dynamics.

After observing a transition (s,a,r,s)(s, a, r, s'):

Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha \left[ r + \gamma \max_{a'} Q(s', a') - Q(s, a) \right]

Three knobs:

SymbolRoleTypical
α\alphaLearning rate — how much to move toward the target0.10.1
γ\gammaDiscount factor — how far ahead we plan0.90.90.990.99
ϵ\epsilonExploration rate for ϵ\epsilon-greedy action selection0.10.1, often annealed

The maxa\max_{a'} 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 QQ even when we sometimes act randomly for exploration.

Initialize Q(s, a) arbitrarily (e.g., zeros), Q(terminal, ·) = 0
for 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 (s,a)(s, a) visited infinitely often, and a step-size schedule satisfying αt=\sum \alpha_t = \infty, αt2<\sum \alpha_t^2 < \infty, QQ converges to QQ^* with probability 1. The guarantee evaporates once function approximation (neural nets) enters the picture — see DQN.

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 = 1-1, trap = 10-10 (terminal), goal = +10+10 (terminal).
  • Settings: γ=0.9\gamma = 0.9, α=0.5\alpha = 0.5, 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 10-10 (not terminal). The optimal return from start is:

G0=1+γ(1)+γ2(10)+γ3(1)+γ4(+10)=10.98.10.729+6.5614.17G_0 = -1 + \gamma(-1) + \gamma^2(-10) + \gamma^3(-1) + \gamma^4(+10) = -1 - 0.9 - 8.1 - 0.729 + 6.561 \approx -4.17

Suppose the agent is in cell 3 and currently has Q(3,R)=0Q(3, R) = 0, Q(4,)=0Q(4, \cdot) = 0. It takes action R, lands in cell 4 (goal), gets r=+10r = +10, and the episode terminates. The TD target is:

r+γmaxaQ(s,a)=10+0.90=10r + \gamma \max_{a'} Q(s', a') = 10 + 0.9 \cdot 0 = 10

(The bootstrap term is zero because cell 4 is terminal.) The update:

Q(3,R)0+0.5(100)=5Q(3, R) \leftarrow 0 + 0.5 \cdot (10 - 0) = 5

On the next episode, when the agent reaches cell 3 and acts greedily, Q(3,R)=5Q(3, R) = 5 now beats Q(3,L)=0Q(3, L) = 0 — the value has propagated one step backward from the goal.

After many episodes, the Q-values converge so that the value of acting optimally from cell ii matches the discounted optimal return. For the corridor (with trap as penalty, not terminal):

Cell iiQ(i,R)Q^*(i, R)Q(i,L)Q^*(i, L)Greedy action
04.17\approx -4.175.65\approx -5.65 (bumps wall, then same path)R
13.52\approx -3.525.07\approx -5.07R
22.80\approx -2.808.20\approx -8.20R
3+8.00\approx +8.003.49\approx -3.49R
4terminalterminal

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 maxaQ(s,a)\max_a Q(s,a); the arrows show the current greedy policy; every step’s TD update is displayed on the right.

If the agent always picked argmaxaQ(s,a)\arg\max_a Q(s, a), it would commit to whatever looked best initially and never discover that a different action might be better. The simplest fix is ϵ\epsilon-greedy:

a={uniform random actionwith probability ϵargmaxaQ(s,a)with probability 1ϵa = \begin{cases} \text{uniform random action} & \text{with probability } \epsilon \\ \arg\max_a Q(s, a) & \text{with probability } 1 - \epsilon \end{cases}

Typical schedule: start ϵ=1.0\epsilon = 1.0 (pure exploration), anneal toward 0.050.05 as QQ stabilizes.

SARSA’s update uses the action actually taken next, not the argmax:

Q(s,a)Q(s,a)+α[r+γQ(s,a)Q(s,a)],aπQ(s, a) \leftarrow Q(s, a) + \alpha \left[ r + \gamma\, Q(s', a') - Q(s, a) \right], \quad a' \sim \pi

In the gridworld example, if SARSA’s ϵ\epsilon-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.

Q-learning as described stores one number per state–action pair. This is fine for a 5-cell corridor, but:

  • State explosion — a 19×1919 \times 19 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 ss teaches you nothing about a similar state ss'.

The fix is to replace the table with a function approximator — typically a neural network — and call it DQN.

  • 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