Learning/Reinforcement Learning

Lesson 5.53,402 words

Reinforcement Learning

Reinforcement learning is an MDP with the model unknown: the agent knows neither how its actions move the world nor which states are rewarded, and must recover good behaviour from experienced transitions and rewards alone. This first part builds the classical tabular theory — passive learning (fix a policy, learn its value, by direct estimation, adaptive dynamic programming, and temporal differences) and active learning (choose actions, trade exploration against exploitation, and learn control with Q-learning and SARSA).

╌╌╌╌

The decision-theory lesson handed the agent a finished Markov decision process: it knew the transition model , it knew the reward , and its only job was to compute an optimal policy by value or policy iteration. That is planning. Reinforcement learning removes the one thing planning takes for granted. The agent still lives in an MDP — states, actions, transitions, rewards, a discount, an optimal policy — but it is told none of the dynamics and none of the rewards in advance. It learns to act well from the only thing it actually receives: a stream of states, the actions it took, and the scalar rewards that followed.1

Consider a game whose rules nobody explained. The agent plays a hundred moves and learns only at the end that it lost. No teacher labelled a single move good or bad; the only signal was a delayed verdict. Learning to play well anyway — assigning credit backward through a long chain of actions to the ones that actually mattered — is the reinforcement learning problem.1 The reward is treated as part of the percept, and the agent is hardwired only to recognise which part of its input is the reward. Everything else it must infer.

Three agent designs recur, distinguished by what they choose to learn:

DesignWhat it learnsUses it how
Utility-based (model-based)a model , , and utilities one-step look-ahead through to pick actions
Q-learning (model-free)an action-utility function picks , no model needed
Reflexa policy mapping states to actions directlyexecutes

The utility-based agent can only choose actions if it also has a model, because knowing that a state is worth tells you nothing about which action reaches it. A Q-learning agent sidesteps this: scores the action itself, so the agent compares its options without ever predicting their outcomes — but for the same reason it cannot look ahead. Which representation is best is one of the oldest open questions in AI, and we return to it. The chapter splits along a second axis: passive learning, where the policy is fixed and the task is to evaluate it, and active learning, where the agent must also decide what to do.

Passive reinforcement learning

Start with the easier half. In passive learning the agent's policy is fixed — in state it always executes — and the only goal is to learn how good that policy is, its utility function . This is the policy evaluation step of policy iteration, except the agent cannot compute it in closed form: it does not know or .2 The utility is the expected discounted return obtained by following from :

where is the (random) state reached at time . The agent runs a set of trials: from a start state it acts under until it hits a terminal state, observing the state and reward at each step, then repeats. A trial in the standard grid world might read

each state subscripted with the reward received there. Three methods turn these trials into estimates of .

Direct utility estimation

The simplest idea reduces RL to something already understood. The utility of a state is the expected total reward from that state onward — the reward-to-go — and each trial supplies a sample of that quantity for every state it passes through. Average the samples per state, keep a running mean, and in the limit of infinitely many trials the average converges to the true .3

This is nothing but supervised learning: the input is a state, the output is the observed reward-to-go, and any regression method fits it. But direct estimation throws away the single most useful fact about an MDP — that the utilities of neighbouring states are not independent. They obey the Bellman equation for the fixed policy:

By ignoring the link between a state and its successor, direct estimation searches a hypothesis space far larger than it needs — one full of utility functions that violate Bellman's constraint — so it converges very slowly. If a trial reaches a brand-new state whose successor is already known to be excellent, direct estimation learns nothing about the new state until the trial ends; the Bellman equation would have told it immediately.

Adaptive dynamic programming

The fix is to put the Bellman constraint back. An adaptive dynamic programming (ADP) agent learns the model from experience and then solves the resulting MDP. Because the environment is fully observable, learning the model is itself a supervised problem: each observed transition is an example, and the agent estimates by the frequency with which follows in . Plug those estimates and the observed rewards into the fixed-policy Bellman equations and solve — they are linear, so no maximisation is needed — to get the utilities.4

The ADP loop: observed transitions feed a maximum-likelihood model and reward table ; solving the fixed-policy Bellman equations on that model yields the utilities .

ADP makes optimal use of the constraints between states, so it learns as fast as the model itself improves; it is the yardstick against which other passive methods are measured. Its weakness is scale. Solving the Bellman equations means working with a value per state, which is hopeless for large spaces — backgammon would need on the order of equations in unknowns. Two escapes exist: modified policy iteration re-uses the previous utilities as a warm start after each small model change so a few sweeps suffice, and prioritized sweeping updates only the states whose successors just changed a lot. But the exact method does not scale, which motivates temporal-difference learning.

Temporal-difference learning

ADP solves the whole MDP after every observation. Temporal-difference (TD) learning does the opposite: it adjusts one state's utility, a little, toward the value implied by the single transition just observed — and it never builds a model at all.5 Suppose the agent is in with current estimate , acts, and lands in . If the estimates were exactly consistent they would satisfy the local relation . They usually don't, and the gap between the two sides is the temporal-difference error. TD nudges to shrink it:

Here is a learning rate. The bracketed quantity is the difference between the estimate the agent held and the (bootstrapped) estimate the observed successor now suggests — hence temporal difference. The update looks only at the successor that actually happened, not at all possible successors weighted by their probabilities as the true Bellman equation would. That is why it needs no model: the environment supplies the connection between neighbouring states in the form of observed transitions, and TD simply reads it off.

The TD update. The observed successor s-prime yields a target ; the agent moves its estimate a fraction of the way toward that target, closing the temporal-difference error .

TD and ADP are two views of the same goal — make each state's estimate agree with its successors — differing only in bookkeeping. ADP adjusts a state to agree with all its successors weighted by probability and does as many updates as it takes to restore consistency; TD makes one adjustment per observed transition, toward the one successor it saw. The difference vanishes in the average, because over many transitions the observed successors appear in proportion to their true probabilities. TD is noisier and slower per unit of data, but it costs almost nothing per step and needs no model at all. If is decayed as a state is visited more often, converges to the correct value.5

Algorithm:Passive-TD\textsc{Passive-TD} — learn UπU^\pi for a fixed policy π\pi, model-free
  1. 1
    input: policy π\pi, step-size schedule α()\alpha(\cdot), discount γ\gamma
  2. 2
    U(s)0U(s) \gets 0 for all ss; visit counts N(s)0N(s) \gets 0
  3. 3
    for each episode do
  4. 4
    observe start state SS
  5. 5
    repeat
  6. 6
    take action π(S)\pi(S), observe reward RR and next state SS'
  7. 7
    N(S)N(S)+1N(S) \gets N(S) + 1
  8. 8
    U(S)U(S)+α(N(S))[R+γU(S)U(S)]U(S) \gets U(S) + \alpha(N(S))\,[R + \gamma\, U(S') - U(S)]
  9. 9
    SSS \gets S'
  10. 10
    until SS is terminal
  11. 11
    return UU

The three passive methods differ in how much of the MDP structure each exploits, and at what cost.

MethodUses Bellman link?Needs a model?Cost per stepData efficiency
Direct estimationnonotinypoor (slow)
ADPyes (fully)yes (learns it)solves the MDPbest
Temporal differenceyes (one successor)notinybetween the two

Active reinforcement learning

A passive agent is handed its policy. An active agent must decide what to do. It can no longer settle for under a fixed ; it must learn the utilities of the optimal policy, which obey the full Bellman equation with a maximisation over actions:

An active ADP agent can learn the complete model and solve these equations, then extract an action by one-step look-ahead. The subtle question is what to do with the policy it has learned. Just follow it?

Exploration versus exploitation

No. An agent that always executes the optimal action for its current learned model — a greedy agent — reliably fails. It finds one workable route to the reward, sticks to it, and never discovers a better one, because it never tries the actions that would reveal the better route. What the greedy agent overlooks is that actions do two things at once: they collect reward now, and they gather information that improves the model and so raises future reward. It must trade exploitation — maximising reward under current estimates — against exploration — trying unfamiliar actions to improve those estimates.6

The exploration-exploitation tradeoff. Pure exploitation gets stuck on the first workable route; pure exploration never cashes in what it learns; good agents explore early and exploit late.

Finding the optimal exploration policy is the province of bandit problems and is generally intractable. But a merely reasonable scheme is easy, and one property makes precise what reasonable must mean.

The scheme works but is glacially slow. A better idea is to make untried actions look attractive. Let be an optimistic utility estimate and the number of times action has been tried in , and replace the Bellman update with

The exploration function trades greed (preference for high value ) against curiosity (preference for rarely-tried actions, low ): it should increase in and decrease in . A simple choice returns an optimistic constant whenever and the plain value otherwise, which forces the agent to try each action at least times before trusting its estimate. Because (not the pessimistic ) appears on the right-hand side, the optimism propagates backward: states that merely lead toward unexplored regions become attractive too, so the agent is drawn toward the frontier rather than only to actions that are themselves novel. This converges to near-optimal behaviour far faster than — often after a handful of trials.

Two rules everyone actually uses

The exploration function is the principled route, but two simpler rules dominate practice, both borrowed straight from the bandit setting named above: strip the sequential MDP down to one state with several arms, and exploration is the whole problem. AIMA's scheme is the crude ancestor of the first; the second imports the upper-confidence-bound rule from the bandit literature.7 Let be the current estimate of action 's value and the number of times has been tried.

-greedy is blunt: when it explores, it picks any action with equal probability, wasting pulls on arms already known to be bad. UCB — upper confidence bound — spends its exploration where it can do the most good, on actions whose value is uncertain, not merely unfamiliar. It adds an optimism bonus that grows with how little an action has been tried and shrinks as evidence accumulates.

The bonus is not arbitrary. If the rewards for are bounded, a Hoeffding concentration bound says the true mean lies below with high probability, and that probability tightens as grows — so the bracket is a genuine high-confidence ceiling on each action's value, and taking the over ceilings enacts the optimism. UCB is the exploration rule inside the UCT tree-search that powers modern game-playing (Kocsis and Szepesvári, 2006). The three schemes differ in how they target exploration: -greedy explores blindly, the function explores until a visit count is met, and UCB explores in proportion to statistical uncertainty.

Three exploration rules on a five-arm bandit, ranked by how they spend exploration. epsilon-greedy pulls a uniform random arm on an epsilon fraction of steps (wasting some on known-bad arms); the R-plus/Ne function forces each arm to Ne pulls then trusts the value; UCB adds a bonus c sqrt(ln t / N(a)) that steers exploration toward high-uncertainty arms. Bar height is how often each arm is chosen; the true-best arm is marked.

Q-learning

The active ADP agent still learns a model. Q-learning does away with it. Instead of a utility per state it learns an action-utility function — the value of doing in — related to utilities by .8 At equilibrium the Q-values satisfy their own Bellman constraint,

but the temporal-difference version never touches . When action in leads to with reward , Q-learning applies

This is the passive TD update with two changes: it stores values indexed by action, and its target maxes over the successor's actions. That lets a state be evaluated purely from the Q-values of its neighbours, with no model of where actions lead.

The Q-learning backup. The agent takes a in s, lands in s-prime, and backs up the best next value into — a model-free update indexed by action.

A full active Q-learner combines this update with an exploration function (the same as the ADP agent, hence the need to keep visit counts), or more simply with the decaying -greedy rule defined above.

Algorithm:Q-Learning\textsc{Q-Learning} — model-free off-policy TD control, estimate QQQ \approx Q^\ast
  1. 1
    input: step-size schedule α()\alpha(\cdot), discount γ\gamma, exploration function ff
  2. 2
    Q(s,a)0Q(s,a) \gets 0 for all s,as, a; visit counts N(s,a)0N(s,a) \gets 0
  3. 3
    for each episode do
  4. 4
    observe start state SS
  5. 5
    repeat
  6. 6
    choose AA from SS using ff over Q(S,)Q(S, \cdot) and N(S,)N(S, \cdot)
  7. 7
    take action AA, observe reward RR and next state SS'
  8. 8
    N(S,A)N(S,A)+1N(S,A) \gets N(S,A) + 1
  9. 9
    Q(S,A)Q(S,A)+α(N(S,A))[R+γmaxaQ(S,a)Q(S,A)]Q(S,A) \gets Q(S,A) + \alpha(N(S,A))\,[R + \gamma \max_{a'} Q(S', a') - Q(S,A)]
  10. 10
    SSS \gets S'
  11. 11
    until SS is terminal
  12. 12
    return QQ

A worked Q-learning trace

A short numeric trace shows the update at work. Take a corridor of three states, , with terminal. From each non-terminal state the agent can go (toward ) or (back). The reward is everywhere except on entering , which pays . Use discount and learning rate , and start every Q-value at . The value of reaching the goal propagates backward, one transition per visit.

Step 1 — the agent is in , takes , lands in the terminal with reward . The successor is terminal, so . The update is

Only this one entry changes; every other Q-value is still . The goal reward has reached but no further.

Step 2 — a new episode: the agent is in , takes , lands in with reward . Now the successor is not terminal, and its best action value is from step 1. So

The bootstrap did the work: even though the agent received zero reward on this transition, it raised to , because was already known to be worth . This propagation is precisely what direct estimation could not do.

Step 3 — the agent visits again, takes into , reward . The same target as step 1, but is no longer :

Each visit moves halfway from its current value toward the target : , converging on the true value (reward , then a terminal state worth ). The corridor's optimal values are and ; the trace is crawling toward them, and it never once used a transition model.

A Q-learning trace on a three-state corridor (reward +10 on entering the terminal s3, discount 0.9, learning rate 0.5). Q(s2, right) climbs 0, 5.0, 7.5, 8.75 toward its true value 10 as the goal reward is bootstrapped backward; Q(s1, right) reaches 2.25 after one visit even with zero immediate reward.

SARSA

Q-learning has a close relative, SARSA (for State-Action-Reward-State-Action). Its update looks almost identical but backs up the value of the action actually taken next, , rather than the best available:

The rule fires on the full quintuple — hence the name. The difference from Q-learning is small but real. Q-learning backs up the best next Q-value regardless of what the agent will actually do, so it pays no attention to the exploration policy: it is off-policy, learning about the greedy policy while behaving by some other (perhaps random) rule. SARSA backs up the value of the action its own policy will really take, so it learns about the policy it is following: it is on-policy.8 For a purely greedy agent the two coincide. Under exploration they diverge — Q-learning is more flexible (it can learn good behaviour even under an adversarial exploration policy), while SARSA is more realistic when the policy is partly out of the agent's hands, as when other agents share control.

The entire difference is one symbol in the backup target — a versus a sample. The twin backup diagram makes it visible. Both updates take action in , land in , and back a value into ; they part only in which successor Q-value they use. Q-learning takes the maximum over 's actions, ignoring which one the agent will really pick; SARSA takes the single action its own policy actually samples next.

SARSA versus Q-learning backups, side by side. Both take a in s and land in s-prime. Q-learning (left) backs up the MAX over s-prime's action values, max Q(s-prime, a-prime), so it learns the greedy policy no matter how it behaves (off-policy). SARSA (right) backs up the value of the single action a-prime its own policy actually samples next (solid arrow; the unchosen action is dashed), so it learns the value of the policy it follows (on-policy). The one-symbol difference, max versus sample, is the whole on/off-policy distinction.
Q-learningSARSA
Target action (best) actually taken next
Policy learnedgreedy (off-policy)the behaviour policy (on-policy)
Under explorationlearns regardless of behaviourlearns value of what it really does
Model requirednonenone

Both Q-learning and SARSA find the optimal policy for the world, but more slowly than active ADP, because their purely local updates do not enforce consistency across states through a model. This reopens the model-based versus model-free question: as environments grow more complex, the knowledge a model carries tends to pay off, which is why the strongest game-playing systems have usually learned an evaluation function rather than raw Q-values.

Q-learning and SARSA close the tabular story, but they share the limitation that has run through this whole part: they store one number per state or per state-action pair. That is fine for a grid and hopeless for backgammon or chess. Lifting RL off the lookup table with function approximation, and learning a policy directly rather than reading it off a value, continue in Reinforcement Learning: Generalization and Policy Search.

Footnotes

  1. AIMA, §21.1 — Introduction: reinforcement learning as the task of using observed rewards to learn an optimal policy in an environment where the agent knows neither the transition model nor the reward function in advance; the reward is part of the percept, and games like chess deliver it only at the end. 2
  2. AIMA, §21.2 — Passive Reinforcement Learning: with a fixed policy , the agent learns (the policy-evaluation task) from trials in the environment, without knowing or .
  3. AIMA, §21.2.1 — Direct utility estimation: the utility of a state is the expected reward-to-go; averaging the sampled reward-to-go per state reduces RL to supervised learning but ignores the Bellman links between states, so it converges slowly.
  4. AIMA, §21.2.2 — Adaptive dynamic programming: learn the transition model by frequency counting, then solve the fixed-policy Bellman equations; modified policy iteration and prioritized sweeping keep it tractable, but the exact method does not scale to large state spaces.
  5. AIMA, §21.2.3 — Temporal-difference learning: the update adjusts a state toward its observed successor, needs no transition model, and converges when decays with visit count; TD is a model-free approximation to ADP. 2
  6. AIMA, §21.3.1 — Exploration: the greedy agent that acts optimally for its learned model rarely converges to the optimal policy; a GLIE scheme (greedy in the limit of infinite exploration) and an exploration function with an optimistic trade exploitation against exploration; the random-action rule is the simplest GLIE scheme, and the bandit sidebar (§21.3.1) frames exploration as an -armed bandit problem with the Gittins index as its exact-but-intractable solution.
  7. The two rules are standard in the bandit literature rather than derived in AIMA. -greedy with a decaying schedule appears throughout Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed., 2018), §2.2–2.3. UCB1 and its regret bound are from Auer, Cesa-Bianchi, and Fischer (2002), Finite-time analysis of the multiarmed bandit problem, Machine Learning 47, building on Lai and Robbins (1985). UCB is the selection rule inside the UCT tree search of Kocsis and Szepesvári (2006), Bandit based Monte-Carlo planning, ECML — the algorithm AIMA cites for game-tree search and the basis of AlphaGo's Monte-Carlo tree search.
  8. AIMA, §21.3.2 — Learning an action-utility function: Q-learning's model-free TD update and its on-policy relative SARSA, which backs up the action actually taken; Q-learning is off-policy, SARSA on-policy. 2

╌╌ END ╌╌