Chapter 2 · Sutton & Barto

Multi-armed bandits

The simplest setting where an agent must learn from rewards: one situation, k choices, and no instructions about which choice is best. It isolates the core tension of reinforcement learning, exploring to learn more versus exploiting what you already know, before states and delayed consequences enter the picture.

01The problem

Picture a row of k slot machines (“one-armed bandits”). On each time step t you pick one action At, pull that arm, and receive a reward Rt drawn from that arm's reward distribution. The distributions never change, but you don't know them. Your goal is to maximise the total reward over, say, 1000 pulls.

Each arm has a true action value, its expected reward:

True value of action aq*(a) = 𝔼[ Rt | At = a ]

If you knew q*, you would always pull the best arm. You don't, so you keep an estimate Qt(a) and choose based on it. Picking the arm with the highest estimate is exploiting; picking any other arm is exploring. Exploiting maximises the reward on this step; exploring may find a better arm and so earn more over many steps. You cannot do both on the same pull: that is the exploration–exploitation dilemma.

02Estimating action values

The natural estimate is the sample average: the mean of the rewards received so far from that arm. You don't need to store every reward. If Qn is the average of the first n−1 rewards from an arm and Rn is its n-th reward:

Incremental sample averageQn+1 = Qn + (1/n) [ Rn − Qn ]

This is the pattern behind every method in this series: new estimate ← old estimate + step size × (target − old estimate). Here the target is the reward just received. Later the target becomes a return (Monte Carlo) or a bootstrapped guess (TD).

When the arms' values drift over time, an average over all history reacts too slowly. A constant step size α ∈ (0, 1] weights recent rewards more:

Constant step size: an exponential recency-weighted averageQn+1 = Qn + α [ Rn − Qn ] = (1−α)nQ1 + Σi=1..n α(1−α)n−i Ri

03Four ways to choose an arm

ε-greedy

With probability 1−ε pick the arm with the highest Q (breaking ties at random); with probability ε pick an arm uniformly at random. Simple and effective, but it explores blindly: a clearly bad arm gets tried as often as a promising one.

ε-greedyAt = argmaxa Qt(a)  with probability 1−ε,   otherwise a random arm

Optimistic initial values

Start every Q at a value well above any plausible reward, such as +5, and act greedily. Every arm disappoints at first, so the agent tries them all before settling. This only drives exploration at the start, so it is a poor fit when values change over time.

Upper confidence bound (UCB)

Add a bonus that is large for arms tried rarely. The bonus shrinks as an arm's count Nt(a) grows and grows slowly with total time t, so neglected arms are eventually revisited. An arm with Nt(a) = 0 counts as maximising.

UCB action selectionAt = argmaxa [ Qt(a) + c √( ln t / Nt(a) ) ]

Gradient bandit

Learn a numerical preference Ht(a) per arm instead of a value, and choose with a softmax over preferences. After each reward, raise the preference of the chosen arm if the reward beat the baseline R̄t (the average of earlier rewards) and lower the others in proportion to their probability.

Softmax policy and preference updateπt(a) = eHt(a) / Σb eHt(b)
Ht+1(At) = Ht(At) + α (Rt − R̄t)(1 − πt(At)),   Ht+1(a) = Ht(a) − α (Rt − R̄t) πt(a)  for a ≠ At

This is stochastic gradient ascent on expected reward. The baseline doesn't change the expected update, but it cuts its variance a great deal.

04Pull the arms yourself

Below is a five-armed bandit. Each arm's true value q*(a) was drawn from a normal distribution with mean 0 and variance 1, and each pull returns q*(a) plus normal noise with variance 1. Choose a method, pull, and read exactly how the arm was chosen and how the estimate changed.

A five-armed bandit

Step 0 · average reward – · best arm pulled –
0.10
0.0

Estimates Q(a) and true values q*(a)

Average reward so far

Average rewardBest possible q*

The latest pull, number by number

What to try

05The 10-armed testbed

One bandit proves little. The standard comparison (Sutton & Barto, section 2.3) averages over many randomly generated 10-armed bandits. Below, each method plays 300 different bandits for 1000 steps, and the curves show the average reward at each step and the share of runs that picked the best arm. All methods face the same 300 bandits.

Average over 300 bandits

0.10
2.0
0.10

Average reward

% of runs choosing the best arm

What the testbed typically shows:

06From bandits to full reinforcement learning

A bandit has a single situation, and an action affects only its own immediate reward. Real problems add two things. First, states: the best action depends on the situation (a contextual bandit is this step alone). Second, delayed consequences: an action changes the next state and therefore every reward after it, so credit for a reward must be assigned to the actions that led there. Formalising both gives the Markov decision process, the subject of the next part.

What carries over: estimates updated by old + step size × (target − old), the choice between sample averages and a constant α, and exploration strategies like ε-greedy, which reappear in Monte Carlo control, Sarsa and Q-learning.