Skip to content
AI360XpertBeta

Watkins's Q(lambda) Algorithm

Watkins's Q lambda lets one reward teach a whole trail of past moves at once with eligibility traces. The moment the agent tries something exploratory, the trail wipes clean so detours never rewrite the optimal plan.

Greedy steps decay traces by 0.72 while teaching past states, then one exploratory action zeroes every trace and quarantines its reward
Greedy steps decay traces by 0.72 while teaching past states, then one exploratory action zeroes every trace and quarantines its reward

Why Does This Exist?

One-step Q-learning moves reward backward one state per visit. A goal a hundred steps away needs a hundred episodes before the start state hears about it. On-policy methods fixed this with eligibility traces: each visit leaves a fading memory, so one error updates the whole recent trail at once.

Traces clash with Q-learning's off-policy nature. Q-learning learns the greedy policy while behaving exploratorily, and rewards collected after a random detour describe the detour, not the optimal plan. Crediting the trail with them corrupts every value upstream.

Watkins's answer, from his 1989 thesis, is a cutoff rule: traces decay by γλ\gamma \lambda while actions stay greedy and reset to zero the instant an exploratory action is taken. Fast credit along the optimal path, quarantine everywhere else.

This page covers the five-step update and the cutoff, with a hand-computed three-step trail. The on-policy origin is SARSA lambda, the gentler modern fix is the tree backup algorithm, and the one-step base is Q-learning.

The one idea you need first

Q-learning's error is δt=Rt+1+γmax⁡aQ(St+1,a)−Q(St,At)\delta_t = R_{t+1} + \gamma \max_a Q(S_{t+1}, a) - Q(S_t, A_t): reward plus the best next value minus the current estimate. Traces decide how far back that error travels. The max is what makes the method off-policy, and the cutoff is what keeps the max honest.

Think of It Like This

Grading a relay team, ignoring the lap someone jogged

A coach grades relay legs by the team's finishing time, sharing credit down the whole order of runners. That works while everyone sprints. But on one lap a runner decides to jog sideways to test new shoes. The slow finish says nothing about the sprinters before him, so the coach draws a line: legs before the jog keep the credit they already banked, and the jogging lap is graded alone.

Watkins's cutoff is that line. Greedy legs share each error through decaying traces; the exploratory leg starts a fresh page. Earlier legs are protected from blame for a detour they did not choose.

Where the analogy stops: the coach's line is judgment, while the cutoff is arithmetic. A non-greedy action provably breaks the multi-step return's claim to estimate optimal values, so the zeroing is a correctness rule, not mercy.

How It Actually Works

Each step computes the Q-learning error δt\delta_t with the max over next actions, bumps the current pair's trace by one, updates every pair by α δt zt(s,a)\alpha \, \delta_t \, z_t(s, a), then carries traces forward. While the next action is greedy they shrink by γλ\gamma \lambda; the first exploratory action zeroes all of them:

zt+1(s,a)=γλ zt(s,a)greedy next action,zt+1(s,a)=0otherwisez_{t+1}(s, a) = \gamma \lambda \, z_t(s, a) \quad \text{greedy next action}, \qquad z_{t+1}(s, a) = 0 \quad \text{otherwise}

Here α\alpha is the step size, γ\gamma discounts the future, and λ\lambda in [0,1][0, 1] sets how hungrily credit reaches back. The zeroing is mandatory because an nn-step return estimates optimal values only when every intermediate action was optimal; one detour voids the warranty for the whole trail behind it.

Worked example

States S0→S1→S2→S_0 \to S_1 \to S_2 \to terminal, γ=0.9\gamma = 0.9, λ=0.8\lambda = 0.8 so traces decay by 0.720.72, α=0.2\alpha = 0.2. Values: Q(S0)=4.0Q(S_0) = 4.0, Q(S1)=5.0Q(S_1) = 5.0, Q(S2,explore)=1.0Q(S_2, \text{explore}) = 1.0, all traces zero.

Step 0, greedy: reward 2.02.0, next best 5.05.0. Error 2.0+0.9×5.0−4.0=2.50002.0 + 0.9 \times 5.0 - 4.0 = 2.5000. Trace for S0S_0 goes 0+1=1.00 + 1 = 1.0, so Q(S0)=4.0+0.2×2.5×1.0=4.5000Q(S_0) = 4.0 + 0.2 \times 2.5 \times 1.0 = 4.5000. Next action is greedy, trace decays to 0.72000.7200.

Step 1, greedy: reward 1.01.0, next best 6.06.0. Error 1.0+0.9×6.0−5.0=1.40001.0 + 0.9 \times 6.0 - 5.0 = 1.4000. Both traces active: Q(S1)=5.0+0.2×1.4=5.2800Q(S_1) = 5.0 + 0.2 \times 1.4 = 5.2800, and Q(S0)=4.5+0.2×1.4×0.72=4.7016Q(S_0) = 4.5 + 0.2 \times 1.4 \times 0.72 = 4.7016. Then the next action is exploratory, so every trace resets to 0.00.0.

Step 2, exploratory: reward 3.03.0 at the terminal. Error 3.0−1.0=2.00003.0 - 1.0 = 2.0000, and Q(S2,explore)=1.0+0.2×2.0=1.4000Q(S_2, \text{explore}) = 1.0 + 0.2 \times 2.0 = 1.4000. The older pairs sit at 4.70164.7016 and 5.28005.2800, untouched: the detour's reward never reaches them.

Code

The three steps in plain Python, with type hints, matching the hand computation:

Q: dict[str, float] = {"S0": 4.0, "S1": 5.0, "S2e": 1.0}z: dict[str, float] = {"S0": 0.0, "S1": 0.0, "S2e": 0.0}alpha, gamma, lam = 0.2, 0.9, 0.8
def step(key: str, reward: float, next_best: float, greedy_next: bool) -> float:    delta = reward + gamma * next_best - Q[key]    z[key] += 1.0    for k in Q:        Q[k] += alpha * delta * z[k]    for k in z:        z[k] = z[k] * gamma * lam if greedy_next else 0.0    return delta
print(f"d0 {step('S0', 2.0, 5.0, True):.4f}")  # -> d0 2.5000print(f"Q0 {Q['S0']:.4f} trace {z['S0']:.4f}")  # -> Q0 4.5000 trace 0.7200print(f"d1 {step('S1', 1.0, 6.0, False):.4f}")  # -> d1 1.4000print(f"Q0 {Q['S0']:.4f} Q1 {Q['S1']:.4f}")  # -> Q0 4.7016 Q1 5.2800print(f"d2 {step('S2e', 3.0, 0.0, True):.4f}")  # -> d2 2.0000print(f"Q0 {Q['S0']:.4f} Q2e {Q['S2e']:.4f}")  # -> Q0 4.7016 Q2e 1.4000

Watch Out For

Keeping traces alive across exploratory actions

Porting SARSA lambda code to Q-learning and leaving plain decay z←γλzz \leftarrow \gamma \lambda z is the classic bug: no greedy check, no reset. Earlier optimal decisions then absorb credit and blame from random detours, and values converge to a corrupted hybrid instead of the optimum. The symptom is control that plateaus below tabular Q-learning on the same task despite the fancier machinery.

Fix it with an explicit branch: compare the taken next action against the greedy set, and on mismatch zero every trace before the next step.

Exploring so much that traces never survive

The cutoff has a price: with heavy exploration, traces die every few steps and the method collapses to one-step Q-learning, losing the speed it was built for. The symptom is trace lifetimes averaging two or three steps in the logs while the algorithm was sold as multi-step.

Fix it by decaying exploration as values settle, or switch to tree backup, which scales traces by target probabilities instead of killing them outright.

The Quick Version

  • Watkins's Q lambda adds eligibility traces to off-policy Q-learning for multi-step credit in a single error.
  • Greedy next actions decay traces by γλ\gamma \lambda; the first exploratory action zeroes all of them.
  • In the worked trail, errors 2.5 and 1.4 lift Q(S0)Q(S_0) to 4.7016 and Q(S1)Q(S_1) to 5.2800, then the cutoff quarantines reward 3.0.
  • Zeroing is a correctness rule: post-detour rewards cannot evaluate the optimal plan.
  • Check the greedy branch first when values corrupt, and watch exploration rates that silently reduce the method to one step.