Q-Learning
Learn the value of each action from raw experience alone, then act greedily on those values with no model of the world.
Why Does This Exist?
Real tasks hide their dynamics. You get no transition table and no reward formula , only the outcomes of actions you try. Dynamic programming needs the model, so it cannot run here. Q-learning (Watkins 1989, convergence by Watkins and Dayan 1992) learns the optimal action values from experience alone and never asks how the world works.
The trick is bootstrapping off your own estimates plus off policy updating. The agent may wander with epsilon-greedy exploration while its update already aims at the greedy future, so behavior and learning stay decoupled. That split is what lets one table improve toward optimal play while the agent still explores. For the Markov setup underneath, start with MDP foundations. For why the wander matters, read exploration versus exploitation. For pixels and nets instead of tables, go to deep Q-networks.
Think of It Like This
Ranking chess moves by played games, not by tree search
You never memorize the full game tree. You play thousands of blitz games, and after each move you nudge your feeling for that position toward what actually followed plus what your current judgment says the rest was worth. Moves that kept leading to wins climb; moves that kept fizzling sink. Eventually you just play the top ranked move per position.
That running rank is the Q-table, the nudge is the TD error, and playing a random novelty every so often while still ranking everything by best play is the off policy part.
Where it stops: chess has an opponent and a terminal win signal, while Q-learning also handles stochastic rewards, discounts, and continuing tasks with no final whistle. The update by experienced outcome carries over.
How It Actually Works
estimates expected discounted return from taking action in state then acting optimally after. Here is the immediate reward, in is the discount that shrinks future value, and is the step size. The optimal table satisfies . Policy falls out for free: at , take .
The update
After one transition , with state, taken action, reward, next state:
The bracket is the TD error: target minus current . Positive means too low, so raise it; negative means too high, so lower it. The makes it off policy because the target assumes the greedy next action even when exploration picked something else. Tabular Q-learning converges to with probability when every pair is visited infinitely often and steps satisfy with finite; in practice use small constant like and anneal exploration toward .
Worked example
State holds for . The agent takes (current best is also here, but the rule does not require it), earns , lands in holding . Best next value is .
With and :
The diagram draws this exact step: on the left, on the right with marked MAX, target below, arrow back raising from toward . Repeat across visits and the table climbs to the fixed point where each entry equals its own target.
Code
The snippet recomputes the worked target and update with the standard library only.
q_current = 3.5reward = 1.0gamma = 0.9next_best = 4.0alpha = 0.5
target = reward + gamma * next_bestprint(round(target, 4)) # -> 4.6
error = target - q_currentprint(round(error, 4)) # -> 1.1
q_new = q_current + alpha * errorprint(round(q_new, 4)) # -> 4.05The prints match the hand derived target , error , and updated value .
Watch Out For
Max of noisy values runs hot
The target takes over noisy estimates, and maxima of noise sit above the true maximum. That upward bias compounds into overoptimism and unstable jumps, the classic maximization bias.
Split selection from evaluation the Double Q-learning way: pick with one estimator, score it with another. If values climb early then crash across seeds, suspect the max before touching rewards.
Greedy too early starves the table
Convergence needs every pair visited plenty. Annealing exploration fast strands whole regions at zero visits while the policy already exploits, so the table looks decided but is just sheltered.
Track visit counts per pair and slow the anneal when large areas sit near zero. If late performance swings with the seed, coverage ran out before learning did.
The Quick Version
- Q-learning is model free and off policy: experience alone teaches optimal action values.
- Update is , where the bracket is the TD error.
- The assumes greedy next play even during random exploration, decoupling behavior from learning.
- With , , best next , target is and moves to at .
- Tables converge with infinite visits plus decaying steps; pixels need function approximation instead.