Simple Algorithm
· 8 min read

Exploration vs Exploitation: What a Row of Slot Machines Teaches About Learning

Stick with what has worked, or try something that might be better? Every learning system faces that choice, and the multi-armed bandit is the smallest problem that contains it. Three classic strategies show how to stop guessing.

You have a favorite restaurant. Tonight you could go back, and you would have a good meal. Or you could try the new place around the corner, which might be better and will probably be worse. Go back every time and you will never find out. Try somewhere new every time and you will eat a lot of forgettable dinners.

That is the explore–exploit dilemma. Exploiting means choosing the best option you know. Exploring means choosing something else in order to learn about it. You can’t do both with the same dinner, and the tension turns up wherever a decision is repeated under uncertainty: which ad to show, which treatment to give, which move to try. In reinforcement learning it is not a side issue. It is the first thing that makes the problem hard.

The multi-armed bandit

To study the dilemma on its own, strip away everything else. You face a row of slot machines. Each pays out at random, with its own average that you don’t know. You have a fixed number of pulls, say a thousand, and you want to win as much as you can.

This is the multi-armed bandit problem, named after an old nickname for the slot machine, the one-armed bandit. Each machine is an “arm”. There is no board, no opponent and no long-term consequence: pulling an arm tells you something about that arm and changes nothing else. Only the dilemma is left. Every pull spent learning about a doubtful machine is a pull not spent on the best one you have found.

Success is measured by regret: the gap between what you won and what you would have won by pulling the best arm every time. You can’t get regret down to zero, because you have to try the arms to learn which is best. The real question is how fast it grows. If your regret keeps growing in proportion to the number of pulls, you are losing the same amount per pull forever, which means you never really learned. A good strategy’s regret grows more and more slowly, because its mistakes become rarer.

Why the obvious strategy fails

The obvious plan is the greedy one: try each machine once, then always play the one with the best average so far.

Here is how it goes wrong. Take two machines. Machine A pays one coin 60% of the time and machine B only 40% of the time; otherwise they pay nothing. You try each once. With probability 0.4 × 0.4 = 0.16, A happens to pay nothing and B happens to pay. Greedy now believes A is worth 0 and B is worth 1, so it plays B. B’s average can never fall back to zero, because it already has one win, and A’s average never changes, because A is never played again. So at least one time in six, greedy locks onto the worse machine and stays there for good.

The flaw is not bad luck. It is that greedy treats an estimate based on a single pull as if it were a fact. A sensible strategy has to keep track of how sure it is, not just of what it currently believes.

Three strategies that work

Epsilon-greedy: explore at random, now and then

The smallest fix is to be greedy most of the time. With a small probability ε\varepsilon, say 10%, pick a machine completely at random. Otherwise play the current favorite. Every machine keeps getting sampled, so no early accident is permanent.

It is one line of code and it works surprisingly well, which is why it is still the default in a great deal of reinforcement learning code. But it explores blindly. It is as likely to re-test a machine it has tried five hundred times and knows to be bad as one it has barely touched. And it never stops: with ε\varepsilon fixed at 10%, a tenth of your pulls are random forever, so regret keeps growing in proportion to time. Shrinking ε\varepsilon as you go helps, at the price of having to choose a schedule.

UCB: give every option the benefit of the doubt

A smarter idea is to explore where you are uncertain. For each machine, compute not just its average payout but an optimistic estimate: the highest value its true average could plausibly have, given how few times you have tried it. Then play the machine whose optimistic estimate is highest.

This is the upper confidence bound rule, UCB for short, and the principle behind it has a name worth remembering: optimism in the face of uncertainty. A machine can earn a pull in two ways. Either its average is high, or it has been tried so rarely that it might still turn out to be great. Each pull shrinks the uncertainty, so a mediocre machine stops looking promising after enough tries and the rule moves on by itself. In the most common version, UCB1, which assumes rewards between 0 and 1, the bonus added to an arm’s average is

2ln⁡tn,\sqrt{\frac{2 \ln t}{n}},

where tt is the total number of pulls so far and nn is the number of times this arm has been pulled. The bonus shrinks as nn grows and creeps up slowly with tt, so no arm is ever abandoned completely. Our lesson on upper confidence bounds shows where the formula comes from.

The reward is a guarantee. UCB1’s regret grows only logarithmically with the number of pulls: ten times as many pulls adds a fixed amount of regret, not ten times as much. And a classic 1985 result by Tze Leung Lai and Herbert Robbins shows that no strategy that is reliable across all problems can beat logarithmic growth in the long run.

Thompson sampling: act on a plausible guess

The third strategy is the oldest, and one of the best. Keep a belief about each machine: not a single number but a probability distribution over what its true payout rate might be, wide when you have little data and narrow when you have a lot. To choose, draw one random sample from each machine’s distribution and play the machine whose sample is highest. Then update that machine’s belief with what happened.

A machine you know to be good produces high samples reliably, so it gets played often. A machine you know little about produces samples all over the place, and every so often one of them comes out on top, which earns it a trial. As evidence builds, its distribution narrows and the samples settle down. Exploration switches itself off exactly as fast as the uncertainty disappears, with no schedule to tune.

William Thompson proposed this in 1933, in a paper about choosing between two medical treatments. It was largely overlooked for decades. Around 2011, large experiments showed it matching or beating the alternatives on advertising and news recommendation data, and proofs that it achieves logarithmic regret followed. For yes-or-no rewards the beliefs are Beta distributions, and the whole update is “add one to the count of successes or to the count of failures”. Our lesson on Thompson Sampling & Bayesian Bandits builds it up from there.

The strategies side by side

StrategyHow it exploresRegret in the long run
GreedyIt doesn’t. One unlucky pull can lock it onto a bad arm.Grows in proportion to time
Epsilon-greedyAt random, a fixed fraction of the time, even on arms it already knows are badGrows in proportion to time while ε stays fixed
UCBBy optimism: it favors the arms it is unsure about. No randomness involved.Logarithmic
Thompson samplingBy sampling from its beliefs, so it needs a probability model of the rewardsLogarithmic

You can run the last three against each other in the first lesson of our bandits topic, where a simulation plots their regret on the same set of machines. Switch the time axis to a log scale: logarithmic regret becomes a straight line, while epsilon-greedy’s curve keeps bending upward. Notice the start as well. Over the first couple of thousand pulls epsilon-greedy is slightly ahead of UCB, which explores generously at first. The order reverses as the pulls add up.

Where bandits are used

  • Website and app experiments. A classic A/B test explores first and exploits afterwards: split the traffic evenly for two weeks, then send everyone to the winner. A bandit blends the two, shifting traffic toward the better version while the evidence is still coming in, so fewer visitors see the worse one.
  • Recommendations. Choosing which article or product to show is a bandit problem with a twist: the best choice depends on who is looking. That version is called a contextual bandit, and it is one of the most widely deployed forms of reinforcement learning.
  • Clinical trials. The original motivation. Adaptive trial designs assign more patients to the treatment that is doing better as results come in.

From slot machines to full reinforcement learning

A bandit is reinforcement learning with a single situation. In the full problem, actions also change the situation you are in, and exploration gets harder: finding out whether a distant part of a maze is worth visiting takes a whole sequence of deliberate steps, not one curious pull. (If the vocabulary is new, Supervised vs Unsupervised vs Reinforcement Learning explains how this kind of learning differs from the rest.)

The bandit ideas carry over almost unchanged. The Deep Q-Network that learned to play Atari games from pixels explored with epsilon-greedy, starting fully random and settling at 10%. Monte Carlo tree search, the planning method behind the strongest Go programs, treats every position in its search tree as a small bandit problem and solves it with a UCB-style rule. Thompson sampling’s descendants explore by acting, for a while, on one sampled guess about how the whole environment works.

That is why our reinforcement learning course puts bandits second, straight after the basic vocabulary. Once the dilemma is clear with one state, you recognize it in every algorithm that comes later.

Common questions

What is the difference between exploration and exploitation?

Exploitation is choosing the option that looks best given what you know now. Exploration is choosing a different option in order to learn more about it. Exploitation earns reward today. Exploration improves the decisions you make tomorrow.

What is a good value for epsilon?

There is no universal answer, but 0.1 is a common starting point, and letting it shrink over time usually beats any fixed value. If you find yourself tuning it at length, that is a sign to try UCB or Thompson sampling, which adapt by themselves.

Is Thompson sampling better than UCB?

Often, in practice. Both have logarithmic regret in theory. Thompson sampling tends to do a little better in experiments and handles delayed or batched feedback gracefully, while UCB is deterministic, which makes it easier to analyze and to debug.

Why is it called a multi-armed bandit?

“One-armed bandit” is slang for a slot machine: it has one lever, and it takes your money. A gambler facing several machines with different unknown payouts is up against a bandit with many arms.

Is the explore–exploit trade-off only about machine learning?

No. Choosing a restaurant, hiring, deciding how long to keep looking for an apartment and funding research all have the same shape: a known good option against an uncertain one that might be better. The mathematics was worked out for slot machines, but the lesson is general. When you are uncertain and have many decisions still ahead of you, some exploration pays for itself.

Keep reading