Skip to content

Multi-armed bandits

A one-armed bandit is a slot machine. A multi-armed bandit is a row of them, each paying out at its own unknown rate, and you only get so many pulls. Every pull spends one of two things: reward, or information about which machine to pull next. You cannot buy both with the same coin.

That trade is the whole subject of this chapter. It is reinforcement learning with state and delayed consequences deleted, leaving the one difficulty that neither supervised learning nor planning has to face — the learner has to act in order to find out what acting is worth.

Understand why exploration is necessary at all, and be able to state the exploration–exploitation trade-off precisely enough to measure it.

You face kk slot machines. Each machine aa pays out according to a fixed but unknown distribution with mean q∗(a)q_*(a):

q∗(a)=E[Rt∣At=a]q_*(a) = \mathbb{E}[R_t \mid A_t = a]

At each step you pick one machine and receive one reward. The distributions never change. You get 1000 pulls. Maximise the total.

That is the whole problem. There is no state — the situation after 500 pulls is identical to the situation at the start, apart from what you have learned. There is no delayed consequence — the reward arrives immediately and affects nothing else.

Removing state and delay leaves exactly one difficulty behind: you cannot learn about an action without giving up reward.

If you knew every q∗(a)q_*(a), the problem would be trivial — always pull the best machine. You do not, so you hold estimates Qt(a)Q_t(a). At any moment:

  • Exploiting means picking arg⁡max⁡aQt(a)\arg\max_a Q_t(a): the best return given what you currently believe.
  • Exploring means picking something else: worse in expectation right now, but it improves the estimate that all your future decisions depend on.

You cannot do both on the same pull. Every algorithm in this chapter is a different answer to when is a worse action worth taking?

Here is a complete agent for the problem, in eight lines. Everything else in the chapter is an explanation of one of them.

A simple bandit algorithm

Initialise, for a=1a = 1 to kk:

Q(a)←0\quad Q(a) \leftarrow 0

N(a)←0\quad N(a) \leftarrow 0

Loop forever:

A←{arg⁡max⁡aQ(a)with probability 1−ε(breaking ties randomly)a random actionwith probability ε\quad A \leftarrow \begin{cases} \arg\max_a Q(a) & \text{with probability } 1-\varepsilon \quad \text{(breaking ties randomly)} \\ \text{a random action} & \text{with probability } \varepsilon \end{cases}

R←bandit(A)\quad R \leftarrow \mathrm{bandit}(A)

N(A)←N(A)+1\quad N(A) \leftarrow N(A) + 1

Q(A)←Q(A)+1N(A)[R−Q(A)]\quad Q(A) \leftarrow Q(A) + \dfrac{1}{N(A)}\left[R - Q(A)\right]

Three pieces are doing the work, and each gets a page of its own:

  • Q(a)Q(a) and N(a)N(a) — the estimate of q∗(a)q_*(a) and the count it averages over. Why the running average is written incrementally rather than recomputed is action-value methods.
  • The choice of AA — the ε\varepsilon coin flip that decides exploit or explore on this step, and how much that choice costs, is epsilon-greedy.
  • bandit(A)\mathrm{bandit}(A) — the environment. It draws a reward from the unknown distribution behind arm AA and tells you nothing else. No transition, no next state; the loop returns to exactly where it started.

Two details in that box are easy to skim past and expensive to get wrong. Ties must be broken randomly — with every Q(a)Q(a) starting at 0, the first step is a kk-way tie, and an argmax that returns the lowest index turns the agent into one that only explores by accident. And the step size is 1/N(A)1/N(A), the count for the arm actually taken, not the global step number.

Total reward alone is a poor yardstick, because a lucky run on an easy set of arms beats a smart run on a hard one. Two better measures:

Percent optimal action — the share of pulls that hit the truly best arm. It goes to 100% for a method that eventually finds and keeps the best arm.

Regret — the reward you gave up by not always playing the best arm:

RegretT=∑t=1T(q∗(a∗)−q∗(At))\mathrm{Regret}_T = \sum_{t=1}^{T} \left( q_*(a^*) - q_*(A_t) \right)

Regret only ever increases. What matters is how fast. A method that keeps exploring at a fixed rate accrues regret linearly forever; a method that anneals its exploration can get sublinear regret.

The bandit problem is worth taking seriously rather than treating as a warm-up. Nearly every exploration idea in deep RL — optimistic initialisation, upper confidence bounds, decaying ε\varepsilon — is a bandit idea that got carried into a larger setting mostly unchanged.

  • Sutton, R. S. & Barto, A. G. Reinforcement Learning: An Introduction, 2nd ed., ch. 2.