09 Distributed Consensus & Task Allocation · #41 of 46

Exploration vs Exploitation

The slot-machine math that tells a swarm when to gamble

You walk into a casino with a row of slot machines. One of them pays out more than the others, but you don't know which, and every pull you spend checking is a pull you didn't spend winning.

That, exactly, is the hardest decision a robot ever makes: stick with what works, or risk a pull on the unknown.

Every learning agent (a foraging swarm, a recommendation engine, a rat in a maze) runs into the same wall. To know which option is best, you have to try them all. But trying the bad ones costs you. Knowledge and reward pull in opposite directions, and there is no way to buy both at once.

A row of slot machines glowing in a casino.
A wall of slot machines, each with its own hidden payout rate. Pick the best one and you have to lose money sampling the rest first. That is the explore-versus-exploit dilemma in brass and neon. · Joe Ross, CC BY-SA 2.0

The restaurant you keep going back to

Forget robots for a second. You've just moved to a new city.

The honest answer is neither, all the time. Early on, when you know almost nothing, explore aggressively. As evidence piles up, lean harder on your favourites, but never stop poking at the edges, because the city changes and so do you.

What it costs to be wrong: regret

To do better than vibes, we need a number to minimise. That number is regret: the difference between the reward you got and the reward you would have gotten if you'd known the best option from the start.

Picture two failing extremes:

The whole game is to make that regret line bend toward flat: explore enough to find the best arm quickly, then stop paying for arms you've already ruled out.

UCB1: optimism in the face of uncertainty

Before the elegant fix, here is the crude one almost everyone reaches for first. It is called epsilon-greedy: pick the best-looking arm almost always, but every so often (say one pull in ten) flip a coin and try something random instead. That random sliver is your exploration budget. It works, and it is two lines of code, but it is dumb in one specific way: it keeps wasting those random pulls on arms it has already proven are terrible, right alongside the ones it is genuinely unsure about. The smarter rules below spend their curiosity where it actually buys information.

The elegant fix is a rule called UCB1, and its philosophy is one of the best in all of decision-making: be optimistic about what you haven't tried yet.

For each arm, don't judge it by its average reward alone. Judge it by its average plus a bonus that grows the less you've sampled it. An arm you've barely touched gets a big optimistic boost: maybe it's secretly great. An arm you've pulled a thousand times gets almost no bonus, because you already know what it is. Then simply pick the arm with the highest total. Exploration falls out automatically: under-tried arms float to the top and get a fair shot, while proven winners win on merit.

UCBi=xˉi+clntniUCB_i = \bar{x}_i + c \sqrt{\frac{\ln t}{n_i}}

Here xˉi\bar{x}_i is the average reward seen from arm ii, nin_i is how many times you've pulled it, tt is the total number of pulls so far, and cc is an exploration constant (typically 2\sqrt{2}). The first term is exploitation; the second is exploration. Notice the nin_i in the denominator: pull an arm more and its bonus shrinks. Notice the lnt\ln t on top: as time passes, every arm slowly earns a little curiosity back, so the algorithm never goes permanently blind.

Same math, everywhere

The reason this matters far beyond casinos: the bandit is a universal shape. Any system that has to learn while it acts, paying real costs in real time, never getting a free practice round, is solving a bandit.

Different dressing, identical skeleton: try, measure, lean toward what works, never fully stop checking.

The math: regret, the logarithmic bound, and a one-line implementation

Defining regret. Let arm ii have true (unknown) mean reward μi\mu_i, and let μ=maxiμi\mu^* = \max_i \mu_i be the best arm's mean. If iti_t is the arm you actually pulled at step tt, your cumulative regret after TT pulls is

RT=t=1T(μμit)R_T = \sum_{t=1}^T (\mu^* - \mu_{i_t})

Every time you pull a sub-optimal arm you add its gap to the best arm. Pull only the best arm and the sum stays zero.

The bound. UCB1 guarantees

RT=O(lnT)R_T = O(\ln T)

which is asymptotically optimal: no strategy can do better by more than a constant factor. The intuition: a bad arm's optimistic bound can only stay above the best arm's true value for so long before its lnt/ni\sqrt{\ln t / n_i} bonus shrinks below the gap, and after that it's essentially never chosen again.

Implementation. Each arm just needs its running reward and pull count:

struct UCB1Arm {
    rewards: Vec<f32>,
    count: usize,
}

impl UCB1Arm {
    fn ucb(&self, total_count: usize, c: f32) -> f32 {
        if self.count == 0 { return f32::INFINITY; }

        let avg = self.rewards.iter().sum::<f32>() / self.count as f32;
        let exploration = c * (total_count as f32 / self.count as f32).ln().sqrt();
        avg + exploration
    }
}

The f32::INFINITY for an untried arm is the optimism made literal: anything you've never pulled is, for one turn, the most promising thing in the world.

A cousin worth knowing. Thompson sampling attacks the same problem probabilistically: keep a Beta distribution over each arm's reward, draw a sample from each, and pull whichever sample comes out highest. Uncertain arms have fat distributions and occasionally draw high, so they get explored; confident arms draw tightly around their known value. Often it matches or beats UCB1 in practice, and the regret bound is comparably logarithmic.

Key takeaways

There is something almost human about a robot facing a bandit. It would love to be certain, and it cannot be: the only way to know is to risk, and every risk has a price.

So it does the wise thing instead: it stays a little optimistic about the roads it hasn't taken, leans on what it has learned, and keeps one eye open for something better. That isn't indecision. That's how anything (a swarm, a scientist, a stranger in a new city) gets smarter without going broke.

full glossary →