Q-Learning & Temporal-Difference Methods
Prerequisites
- MDPs — MDP tuple, Bellman equations, value functions
- Value Iteration — Bellman optimality operator
- Policy Iteration — policy evaluation and improvement
🎯 Learning Objectives
- Distinguish model-free from model-based RL
- Derive the TD(0) update rule and explain the TD error δ
- Implement SARSA (on-policy) and Q-Learning (off-policy) updates
- Explain ε-greedy exploration and its role in convergence
- Compare SARSA vs Q-Learning on the cliff walking problem
- State the convergence conditions for Q-learning
1. Model-Free Reinforcement Learning
📚 Model-Free RL
Model-free methods learn value functions (or policies) directly from experience — sequences of (state, action, reward, next-state) tuples — without requiring knowledge of the transition function P(s′|s,a) or the reward function R(s,a,s′).
Dynamic programming methods (value iteration, policy iteration) are model-based: they need P and R. In many real-world problems the environment dynamics are unknown, making model-free methods essential.
💡 Two Families of Model-Free Methods
- Monte Carlo (MC): Wait until the end of an episode, compute the actual return Gt, and use it to update value estimates. Requires complete episodes.
- Temporal-Difference (TD): Update value estimates after every step using a bootstrapped estimate. Does not require complete episodes.
| Property | Monte Carlo | Temporal-Difference | Dynamic Programming |
|---|---|---|---|
| Requires model? | No | No | Yes |
| Requires episodes? | Yes (complete) | No (online, step-by-step) | No |
| Bootstraps? | No (uses actual Gt) | Yes (uses estimated V or Q) | Yes |
| Bias | Unbiased | Biased (due to bootstrap) | N/A (exact model) |
| Variance | High | Lower | N/A |
2. Temporal-Difference Learning: TD(0)
📚 TD(0) Update Rule
After observing the transition (St, At, Rt+1, St+1), update the value estimate:
V(St) ← V(St) + α [Rt+1 + γ V(St+1) − V(St)]
where α ∈ (0, 1] is the learning rate (step size).
2.1 The TD Error (TD Target − Current Estimate)
📚 TD Error δt
The temporal-difference error measures the discrepancy between the current estimate and the bootstrapped target:
δt = Rt+1 + γ V(St+1) − V(St)
- TD target: Rt+1 + γ V(St+1) — a one-step look-ahead estimate of the return
- δt > 0: The outcome was better than expected → increase V(St)
- δt < 0: The outcome was worse than expected → decrease V(St)
- δt = 0: The estimate is consistent — no update needed
💡 Bootstrapping Intuition
TD(0) updates V(St) toward Rt+1 + γV(St+1) rather than waiting for the full return Gt. It “bootstraps” by using its own estimate V(St+1) as a stand-in for future rewards. This introduces bias but reduces variance compared to Monte Carlo.
📝 Example: Single TD(0) Update
Current estimate: V(A) = 5.0. Parameters: α = 0.1, γ = 0.9.
Transition: State A → action a → reward +2 → State B where V(B) = 8.0.
TD target = 2 + 0.9 × 8.0 = 9.2
δ = 9.2 − 5.0 = 4.2
V(A) ← 5.0 + 0.1 × 4.2 = 5.42
The value of A increased because the observed outcome (reward 2 plus being in a high-value state B) exceeded the current estimate.
3. Exploration: ε-Greedy Policy
📚 ε-Greedy Policy
The agent selects actions according to:
- With probability 1 − ε: choose the greedy action a* = argmaxa Q(s, a) (exploit)
- With probability ε: choose a random action uniformly from A (explore)
The probability of each action under ε-greedy:
π(a|s) = (1 − ε) + ε/|A| if a = a*; π(a|s) = ε/|A| otherwise
⚠ Why Explore?
A purely greedy policy may converge to a suboptimal solution because it never tries actions that initially appear worse but could lead to higher long-term rewards. The ε-greedy strategy ensures every action has non-zero probability, which is necessary for convergence guarantees.
3.1 Decaying ε
In practice, ε is often decayed over time: start with high exploration (ε ≈ 1.0) and gradually reduce (ε → 0) as the agent gains experience. Common schedules:
- Linear decay: εt = max(εmin, ε0 − t · Δ)
- Exponential decay: εt = ε0 · dt where d ∈ (0, 1)
4. SARSA: On-Policy TD Control
📚 SARSA Update
SARSA updates the action-value function Q after observing the quintuple (St, At, Rt+1, St+1, At+1) — hence the name S-A-R-S-A:
Q(St, At) ← Q(St, At) + α [Rt+1 + γ Q(St+1, At+1) − Q(St, At)]
The next action At+1 is the action actually taken by the agent’s policy (e.g., ε-greedy).
Algorithm: SARSA (On-Policy TD Control)
function SARSA(env, α, γ, ε, num_episodes):
Initialize Q(s, a) = 0 for all s ∈ S, a ∈ A
for episode = 1 to num_episodes:
S ← env.reset()
A ← ε-greedy(Q, S, ε)
while S is not terminal:
R, S' ← env.step(A)
A' ← ε-greedy(Q, S', ε)
// SARSA update
Q(S, A) ← Q(S, A) + α [R + γ Q(S', A') - Q(S, A)]
S ← S'
A ← A'
return Q
💡 On-Policy Property
SARSA is on-policy because the Q-value update uses the action At+1 that the current policy actually selects. This means SARSA evaluates and improves the same ε-greedy policy it is following. As a consequence, SARSA tends to learn a safer policy that accounts for its own exploratory behaviour.
5. Q-Learning: Off-Policy TD Control
📚 Q-Learning Update
Q-Learning updates Q using the maximum Q-value of the next state, regardless of which action the agent actually takes:
Q(St, At) ← Q(St, At) + α [Rt+1 + γ maxa Q(St+1, a) − Q(St, At)]
Algorithm: Q-Learning (Off-Policy TD Control)
function Q-LEARNING(env, α, γ, ε, num_episodes):
Initialize Q(s, a) = 0 for all s ∈ S, a ∈ A
for episode = 1 to num_episodes:
S ← env.reset()
while S is not terminal:
A ← ε-greedy(Q, S, ε)
R, S' ← env.step(A)
// Q-Learning update (note: max over actions, not the action taken)
Q(S, A) ← Q(S, A) + α [R + γ max_a Q(S', a) - Q(S, A)]
S ← S'
return Q
🔧 Q-Learning Convergence Theorem (Watkins & Dayan, 1992)
Q-Learning converges to the optimal action-value function Q* with probability 1 provided:
- All state-action pairs are visited infinitely often (sufficient exploration)
- The learning rate αt(s, a) satisfies the Robbins-Monro conditions:
∑t αt(s,a) = ∞ and ∑t αt(s,a)² < ∞
A constant α does not satisfy condition 2 but works well in non-stationary environments and in practice.
💡 Off-Policy Property
Q-Learning is off-policy because the update uses maxa Q(St+1, a) — the value of the best action in the next state — rather than the action the agent actually takes. This means Q-Learning directly approximates Q* regardless of the behaviour policy. The agent can follow an ε-greedy exploration policy while learning about the optimal greedy policy.
6. SARSA vs Q-Learning: Detailed Comparison
| Property | SARSA | Q-Learning |
|---|---|---|
| Type | On-policy | Off-policy |
| Update target | R + γ Q(S′, A′) where A′ ∼ π | R + γ maxa Q(S′, a) |
| Learns about | The policy being followed (ε-greedy) | The optimal policy (greedy) |
| Safety | Learns “safer” policies that account for exploration | Learns optimal policy assuming greedy execution |
| Convergence target | Qπ for ε-greedy π | Q* (optimal) |
| Sensitivity to ε | High — large ε changes the target policy | Low — always targets optimal Q |
| Exploration risk | Avoids risky exploratory states | May walk near dangerous states (but optimal under greedy execution) |
⚠ GATE Exam Tip: The Key Difference
The only difference between SARSA and Q-Learning is in the TD target:
- SARSA: R + γ Q(S′, A′) — A′ is the action actually chosen by the policy
- Q-Learning: R + γ maxa Q(S′, a) — max over all possible actions
If the policy is greedy (ε = 0), both updates are identical because A′ = argmaxa Q(S′, a).
7. The Learning Rate α
The learning rate α controls how much new information overrides old estimates:
- α = 0: The agent learns nothing (Q never changes).
- α = 1: The agent fully replaces old estimates with the latest sample — high variance, no averaging.
- Small α (e.g., 0.01–0.1): Slow but stable learning. Good for convergence.
- Large α (e.g., 0.5–0.9): Fast initial learning but noisy. Works in deterministic environments.
💡 Practical Guideline
In deterministic environments, α = 1 works well (each sample is exact). In stochastic environments, smaller α is needed to average out noise. A decaying schedule αt = 1/(1 + visit_count(s,a)) satisfies the Robbins-Monro conditions.
8. Cliff Walking: SARSA vs Q-Learning in Action
📝 The Cliff Walking Problem
A classic benchmark that highlights the on-policy vs off-policy distinction.
4×12 Grid World
............
............
............
S CCCCCCCCCC G
S = Start (bottom-left)
G = Goal (bottom-right)
C = Cliff cells (bottom row, between S and G)
Rewards:
- Step into cliff: −100 (agent returns to S)
- All other steps: −1
- Reaching G: 0 (terminal)
Actions: Up, Down, Left, Right (deterministic)
8.1 Q-Learning’s Behaviour (Off-Policy)
Q-Learning learns the optimal policy, which is to walk along the bottom row, right next to the cliff:
Optimal path (Q-Learning's greedy policy):
............
............
............
S→→→→→→→→→→→G
This is optimal under greedy execution. However, during training with ε-greedy exploration, the agent frequently falls off the cliff (random actions near the edge), accumulating large negative rewards per episode.
8.2 SARSA’s Behaviour (On-Policy)
SARSA accounts for the ε-greedy exploration in its value estimates. It learns to avoid the cliff edge, taking a safer but longer path along the top:
Safe path (SARSA's policy):
→→→→→→→→→→→↓
............↓
............↓
S CCCCCCCCCC G
8.3 Learning Curve Comparison
| Metric | SARSA | Q-Learning |
|---|---|---|
| Average reward during training | Higher (fewer cliff falls) | Lower (frequent cliff falls during exploration) |
| Optimal policy learned? | No (safe path, slightly suboptimal) | Yes (shortest path) |
| Performance if ε → 0 | Converges to optimal | Already optimal |
💡 The Takeaway
If the agent must perform well during training (e.g., a robot that can be damaged), SARSA is preferred because it accounts for its own exploratory mistakes. If only the final learned policy matters, Q-Learning directly targets optimality.
9. Worked Example: Q-Learning Updates
📝 Step-by-Step Q-Learning
States: {A, B, C (terminal)}. Actions: {left, right}. γ = 0.9, α = 0.5.
Initial Q-table (all zeros):
| State | Q(s, left) | Q(s, right) |
|---|---|---|
| A | 0 | 0 |
| B | 0 | 0 |
Episode 1, Step 1: S=A, A=right (random), R=0, S′=B
Q(A, right) ← 0 + 0.5 × [0 + 0.9 × max(Q(B,left), Q(B,right)) − 0]
= 0 + 0.5 × [0 + 0.9 × 0 − 0] = 0.0
Episode 1, Step 2: S=B, A=right (random), R=+10, S′=C (terminal)
Q(B, right) ← 0 + 0.5 × [10 + 0.9 × 0 − 0] = 0.5 × 10 = 5.0
Episode 2, Step 1: S=A, A=right (ε-greedy), R=0, S′=B
Q(A, right) ← 0 + 0.5 × [0 + 0.9 × max(0, 5.0) − 0] = 0.5 × 4.5 = 2.25
Episode 2, Step 2: S=B, A=right, R=+10, S′=C
Q(B, right) ← 5.0 + 0.5 × [10 + 0 − 5.0] = 5.0 + 2.5 = 7.5
After 2 episodes, the Q-table:
| State | Q(s, left) | Q(s, right) |
|---|---|---|
| A | 0 | 2.25 |
| B | 0 | 7.5 |
The greedy policy already prefers “right” in both states. With more episodes, Q(A, right) → 0.9 × 10 = 9.0 and Q(B, right) → 10.0 (the true Q* values).
10. Expected SARSA
📚 Expected SARSA Update
Instead of sampling the next action A′, take the expectation over all possible next actions under the policy:
Q(St, At) ← Q(St, At) + α [Rt+1 + γ ∑a π(a|St+1) Q(St+1, a) − Q(St, At)]
Expected SARSA has lower variance than SARSA (no sampling of A′) while remaining on-policy. If the target policy is greedy, Expected SARSA reduces to Q-Learning.
| Method | Target | Type |
|---|---|---|
| SARSA | R + γ Q(S′, A′) | On-policy, sampled |
| Expected SARSA | R + γ ∑a π(a|S′) Q(S′, a) | On-policy, expected |
| Q-Learning | R + γ maxa Q(S′, a) | Off-policy |
Summary
- Model-free methods learn from experience without knowing P or R
- TD(0) bootstraps: V(S) ← V(S) + α[R + γV(S′) − V(S)]
- SARSA (on-policy): updates Q(S,A) using the actual next action A′ — learns a safe, exploration-aware policy
- Q-Learning (off-policy): updates Q(S,A) using maxa Q(S′, a) — directly learns the optimal Q*
- ε-greedy balances exploration and exploitation; ε is typically decayed
- Q-Learning converges to Q* under Robbins-Monro conditions and infinite exploration
- In cliff walking: SARSA avoids the cliff (safe); Q-Learning walks the edge (optimal)
- Expected SARSA uses the expected next-action Q-value, reducing variance
References
- Sutton & Barto, Reinforcement Learning: An Introduction, Chapters 6.1–6.5
- Watkins & Dayan, “Q-Learning,” Machine Learning, 1992
- Russell & Norvig, AIMA, Chapter 22.3