Reinforcement Learning and Q-Learning
Learning to act well without a model of the world: temporal-difference updates, the Q-learning rule, exploration versus exploitation, and a run that recovers the planned optimum from experience alone.
Prerequisites: Markov Decision Processes
Markov Decision Processes solved the planning problem: given and , value iteration returns an optimal policy. But an agent dropped into an unfamiliar environment has neither. It does not know what its actions do, or where the rewards are. It has to find out by acting.
That is the reinforcement learning problem, and it is much closer to the situation any real agent is in.
A. Learning from the difference between successive estimates
Suppose the agent is following some policy in Russell & Norvig's world, where each step costs and rewards are not discounted (), and currently believes and . It then observes a transition from to . If that transition happened every time, the two utilities would have to satisfy the Bellman relation for , - and they do not quite. The estimate at looks a little low. (With the used later in this article the target would be , and the same estimate would look high.)
The fix is to nudge it toward consistency. On observing a transition :
where is a learning rate. This is the temporal-difference (TD) update, so called because it is driven by the difference between utility estimates at successive time steps.
The bracketed quantity is the TD error: what we just observed, , minus what we previously believed, . Zero error means our estimates are locally consistent and nothing changes. All TD methods work this way - adjusting estimates toward the equilibrium that holds when they are correct.
Why this is remarkable. The update mentions no probabilities at all. The agent never estimates . Yet because transitions are sampled from the true model, frequent transitions drive the update proportionally often, and the averaging happens implicitly. The model is never learned, only obeyed.
B. From utilities to action values
TD as written learns - how good a state is. That is not directly actionable: to choose, an agent must know how good each available action is, and converting into a choice requires knowing where actions lead, which is exactly the model we do not have.
So learn action values instead. Define as the expected utility of taking action in state . The two are related by
and an agent holding can act without any model at all: look up the row for the current state and take the largest entry.
C. The Q-learning update
This is applied whenever action is taken in and leads to . It is the TD idea with the state utility replaced by the best action value available at the next state.
The is the crucial detail. The agent backs up the value of the best action at , regardless of what it actually does next. That makes Q-learning off-policy: it learns about the optimal policy while behaving according to some other, more exploratory one.
Its close relative SARSA - for state, action, reward, state, action - instead uses the action actually taken:
That makes SARSA on-policy: it learns the value of the policy being followed, exploration and all. For a purely greedy agent the two coincide. When exploration is happening they differ, and the difference matters: Q-learning can learn good behaviour even while guided by a random or adversarial exploration policy, whereas SARSA is more realistic when the policy is partly out of the agent's control - for instance when other agents share the environment.
D. The mechanics, step by step
Take the world from the previous article: non-terminal states with
, terminals GOAL and PIT , and . All
values start at zero. Use .
Suppose the agent goes GOAL, twice.
Update 1, . Every is still :
Update 2, GOAL, whose value is :
Update 3, again - but now has value:
Update 4, GOAL: .
| Update | Pair | Before | After |
|---|---|---|---|
| 1 | 0.0000 | −0.0200 | |
| 2 | 0.0000 | 0.4300 | |
| 3 | −0.0200 | 0.1635 | |
| 4 | 0.4300 | 0.6450 |
Notice how value propagates backwards: nothing useful reaches until has learned something first.
This particular sequence converges to the wrong number, and that is the point. Both episodes above were hand-picked to reach
GOAL. Repeating only them drives toward - the value of a Right that always succeeds. But Right only reachesGOAL80% of the time; the other 20% it falls intoPIT. The correct value is . Q-learning gets there only because real experience samples both outcomes in the right proportion. A biased sample gives a biased answer.
E. Exploration versus exploitation
An agent that always takes its current best action may never discover a better one. An agent that always acts randomly learns about everything and exploits nothing. This is the exploration–exploitation tradeoff.
The simplest workable answer is -greedy: act greedily with probability , act randomly with probability . Provided decays over time, the agent explores enough early to find the good actions and exploits enough later to accumulate reward. More refined schemes use an exploration function that inflates the value of state–action pairs that have been tried rarely, which requires keeping visit counts.
The figure below runs the same update in a slightly different world, so its numbers are not the table's: two states A and B, a terminal worth reached by going right from B, and a Right that fails 20% of the time by slipping back to A rather than into a pit. It starts with five scripted trips that all succeed, in the order B, B, A, B, A. Step through them and watch the order rather than the numbers. A sits at exactly zero for the first two trips, because neither starts from A, and its first update then finds B already holding to learn from. Then sample the environment the agent really faces. A diet of nothing but successful trips converges on 0.8600 for B - the value of a world where going right always works. Only slipping teaches otherwise.
Interactive: value arrives one step backwards at a time
Five trips from the lesson, then the environment the agent really faces.
The Q table
- Q(A, right)
- 0.0000
- Q(B, right)
- 0.0000
- Trips taken
- 0
- Last TD error
- -
All Q values start at zero, including the terminal - the agent has not been there and does not know it pays. That is why the first trip from B moves the value down to -0.02 rather than up: all it has learned so far is that living costs 0.04. Take the trip and watch.
F. Does it actually recover the planned optimum?
The previous article computed the exact answer by planning with full knowledge of the model: and . A Q-learner never sees that model. Run it for 200,000 episodes with and a decaying learning rate:
Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.
It prints:
| Pair | Learned |
|---|---|
| 0.3847 | |
| 0.3082 | |
| 0.3253 | |
| 0.4926 |
Taking gives and , against the planned and - and the greedy policy read off these values is Right in both states, exactly the policy value iteration produced. The agent recovered the optimal behaviour without ever being told what its actions do.
The small residual gap is honest sampling error, not a bug: the estimates are averages over finitely many sampled transitions, and they tighten slowly as the learning rate decays. This is the standard tradeoff - Q-learning asks far less of you than value iteration, and pays for it in sample efficiency.
G. The limits
Everything here assumes a table with one entry per state–action pair. That is fine for four entries and hopeless for chess or for any environment with continuous state. Real problems need generalisation: approximating with a parameterised function rather than enumerating it, which is where reinforcement learning meets the rest of machine learning.
A second limitation is that Q-learning agents cannot look ahead. Because they do not know where actions lead, they cannot plan a sequence the way a model-based agent can - they can only compare the action values they have learned. Freedom from the model is bought at the cost of foresight.
Key takeaways
- Reinforcement learning drops the MDP assumption that the model is known; the agent learns from experience instead.
- The TD update nudges estimates toward local consistency, using no probabilities - sampling supplies the averaging implicitly.
- makes action selection model-free, with .
- The Q-learning rule backs up , making it off-policy; SARSA backs up the action actually taken and is on-policy.
- Value propagates backwards from rewards, one update per step.
- A biased sample of transitions yields a biased ; correctness relies on experiencing outcomes in their true proportions.
- Our learner reached and against the planned and , recovering the optimal policy with no model.
- Tabular does not scale; generalisation is the bridge to the rest of ML.
What's next
The exploration problem, the sampling error above, and the credit-assignment question all have an information-theoretic flavour. Entropy and Information makes the notion of "how much uncertainty is left" precise.
References & further reading
- Stuart Russell, Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (3rd edition), 2010· Kudos AI reference library
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.