Q-learning: off-policy TD control
Learn optimal action values from any exploratory experience with the max-based TD update.
Animated explanation
The idea
Q-learning is the most famous reinforcement-learning algorithm, and its update is one line. After taking action in state and seeing reward and next state , move towards : the reward now, plus the value of acting optimally from the next state.
The max makes it off-policy: whatever the agent actually does next (it may explore randomly), the update always learns about the greedy policy. So the agent can explore freely, even learn from someone else's play or from old logs, and its still converges to the optimal . Act greedily on and you behave optimally, without ever having known the environment's rules.
Exploration schedules
Convergence requires visiting every state–action pair infinitely often. In practice: ε-greedy with ε decaying from 1 to 0.05, optimistic initial values (start Q high so untried actions look attractive), or softmax (Boltzmann) exploration with a temperature.
Maximisation bias
Taking a max over noisy estimates is biased upwards: some action looks better than it is just by chance. Double Q-learning keeps two tables, using one to choose the best next action and the other to evaluate it. The same trick (Double DQN) fixes overestimation in deep Q-networks.
From tables to networks
Tables need one entry per state–action pair: impossible for images or continuous states. Replacing the table with a function approximator, such as a neural network trained on the same TD target, gives DQN, the next module's starting point.
The algorithm, step by step
- 1Initialise Q(s, a) = 0 (terminal states stay 0).
- 2For each episode: s ← start. Repeat: choose a ε-greedily from Q(s, ·); take it, observe r, s′.
- 3Update (target r if s′ is terminal).
- 4s ← s′ until the episode ends. Decay ε over time.
The maths
Update
Double Q-learning target
Greedy policy
Strengths and weaknesses
Strengths
- Learns the optimal policy off-policy from any exploratory data
- Model-free and simple
- Proven convergence in the tabular case
Weaknesses
- Maximisation bias (fixed by Double Q-learning)
- Can behave riskily while exploring (it ignores its own exploration)
- Tabular form doesn't scale; function approximation can diverge
Reach for it when…
Check your understanding
Lambdian Lab
Q-learning on the slippery gridworld
Using the provided gridworld and env_step:
- q_learning(episodes, alpha, gamma, eps_start, eps_end, seed): ε decays linearly from eps_start to eps_end over the episodes; return Q as a dict state → array(4).
- greedy(Q): dict state → action for non-terminal states.
- The checks compare your greedy policy with the optimal one from value iteration.