Foundations to Frontier
Tests

Exploration vs exploitation

An agent must exploit what it knows to collect reward and explore to find out whether something better exists. In bandits one decision is enough; in sequential problems a good discovery may need a long, deliberate series of exploratory actions. This page compares undirected exploration (ε-greedy), a per-state bonus (UCB) and optimistic initial values on chains of growing length, where a small reward near the start hides a large one at the end.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Trying new things on purpose

You know a café that is fine. There might be a better one further down the road, but you would have to walk past several streets to find out. If you only wander randomly now and then, you will almost never get that far. If instead you assume that unexplored streets are great until proven otherwise, you will walk there on purpose, check, and then know. Reinforcement learning agents face the same choice between exploiting what they know and exploring.

Reinforcement learning lab
A chain of 10 states: moving right from the start 9 times in a row pays +1; moving left at the start pays 0.001. Curves: cumulative successes over 1,000 episodes of 20 steps, for ε-greedy (ε = 0.1, solid), local UCB and optimistic initial values (dashed). First success: ε-greedy never, local UCB 29, optimistic 6. Optimism spreads through the values to states not yet visited, so it explores deeply; ε-greedy settles for the lure; a bonus in each state separately slows down quickly as the chain grows.
Why engineers care

Recommendation systems, clinical trials and game-playing agents all balance exploration against exploitation.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Exploiting means…
  2. 2. Random wandering rarely finds rewards that are…
  3. 3. Assuming the unknown is good makes an agent…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Exploration–exploitation dilemma: acting on current estimates maximises immediate expected reward; trying other actions improves the estimates and may find better rewards.
  • Undirected (dithering) exploration: ε-greedy and softmax add random actions, with no idea of which actions are uncertain.
  • Upper confidence bounds (UCB): pick argmax Q(s, a) + c√(ln n(s)/n(s, a)), favouring rarely tried actions.
  • Optimism in the face of uncertainty: initialise values to the largest possible return, or add bonuses, so unknown actions look good until tried.
  • Deep exploration: in sequential problems, reaching a reward may need many coordinated exploratory steps. Optimism carried by the value function (bootstrapped from the next states) plans those steps; independent random choices do not.
  • Bandits (topic B5-2) are the one-state special case, where ε-greedy, UCB and Thompson sampling are compared directly.
📝 Quick check (Level 1)
0/3 answered
  1. 1. ε-greedy exploration is…
  2. 2. A UCB bonus is larger for actions that…
  3. 3. Deep exploration means…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Exploring a chain

Exercise 1.

  • A chain of N states, starting in state 0. Moving right N − 1 times in a row reaches the end and pays +1; moving left in state 0 pays 0.001, a lure.
  • Q-learning with α = 0.5, γ = 0.99 and episodes of N + 10 steps.
  • ε-greedy (ε = 0.1, values start at 0) against greedy action selection with optimistic initial values (all values start at 1), for N = 10.

Practice.

  • Start simple: ε-greedy with a decaying ε is fine when rewards are dense and easy to stumble on.
  • Use optimism or bonuses when rewards are sparse: initialise values high, or add count-based (or, with function approximation, novelty-based) bonuses.
  • Make the optimism propagate: bonuses must enter the value targets, not only the action choice in the current state, to drive deep exploration.
  • Scale matters: an optimistic value must exceed every value achievable by the lure, or the agent stays optimistic about the lure itself.
  • Decay exploration once the environment is understood, or the agent keeps paying for random actions.
  • Tools: Gymnasium environments; exploration wrappers in Stable-Baselines3; intrinsic-motivation methods (topic L5-7) for large state spaces.
def agent(method, N, episodes, rnd, gamma=0.99, alpha=0.5, eps=0.1):
    Q = [[1.0 if method == "optimism" else 0.0] * 2 for _ in range(N)]
    for e in range(episodes):
        s = 0
        for _ in range(N + 10):
            q = Q[s]
            if method == "eps" and rnd.random() < eps: a = rnd.randrange(2)
            else: a = rnd.randrange(2) if q[0] == q[1] else int(q[1] > q[0])
            s2, r, done = chain_step(s, a, N)
            Q[s][a] += alpha * (r + (0.0 if done else gamma * max(Q[s2])) - Q[s][a])
            s = s2
            if done: break
🐍 Exercise 1: ε-greedy against optimism
Implement the chain and Q-learning with ε-greedy or optimistic exploration, and compare how soon each finds the reward at the end.
📝 Quick check (Level 2)
0/3 answered
  1. 1. For sparse rewards, prefer…
  2. 2. Deep exploration needs the optimism to…
  3. 3. An optimistic value must exceed…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why dithering fails. With random actions, one attempt reaches the end of an N-state chain only if it moves right N − 1 times in a row, with probability 2^(−(N−1)). Once ε-greedy has learned the lure, the greedy action is left, so a success needs N − 1 exploratory right moves in a row, with probability (ε/2)^(N−1). The expected waiting time is exponential in N.

Why optimism works. With values initialised to an upper bound on the return, an untried action looks at least as good as anything tried. Q-learning bootstraps: Q(s, right) is pulled toward γ max Q(s + 1, ·), which stays optimistic until s + 1 has been explored. The optimism therefore propagates back through the chain and the greedy policy walks deliberately to the unexplored states. Each failure lowers the values along the path, and the frontier advances by about one state per episode.

Regret. Algorithms based on optimism, such as UCRL2 (Jaksch, Ortner and Auer 2010), have regret growing like √T in a finite MDP with T steps, against the linear regret of a fixed ε-greedy policy. A UCB rule used only in the action choice, without propagating bonuses into the values, explores each state locally, and its cost grows quickly with depth.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Random actions reach the end of an N-state chain with probability…
  2. 2. Optimism propagates through the chain because Q-learning…
  3. 3. Optimism-based algorithms such as UCRL2 have regret growing like…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How does the time to discover a distant reward grow with the chain length for ε-greedy, a per-state UCB rule and optimistic initial values?

History.

  • J. C. Gittins (1979) solved the discounted bandit with index policies; T. L. Lai and H. Robbins (1985) proved the logarithmic regret lower bound.
  • M. Kearns and S. Singh (2002) gave the first polynomial-time exploration algorithm for MDPs (E³); R. Brafman and M. Tennenholtz (2002) proposed R-max.
  • A. Strehl and M. Littman (2008) analysed count-based exploration bonuses (MBIE-EB); T. Jaksch, R. Ortner and P. Auer (2010) proved regret bounds for UCRL2.
  • I. Osband, B. Van Roy and others (2016–2019) studied deep exploration with randomised value functions.

Exercise 2.

  • Chains of N = 5, 10, 15, 20, 30 and 40 states; 5 runs of 1,000 episodes each.
  • ε-greedy (ε = 0.1, values start at 0), local UCB (c = 1, in the action choice only) and optimistic initial values (1).
  • How many runs ever reached the goal, the median episode of the first success, and the mean number of successes in the last 100 episodes.

Seed 0:

N ε-greedy: runs finding goal ε-greedy: first success local UCB: first success local UCB: last-100 successes optimistic: first success optimistic: last-100 successes
5 4/5 2 2 100 1 100
10 4/5 178 29 100 3 100
15 0/5 never 123 100 8 100
20 0/5 never 308 32 10 100
30 0/5 never never 0 27 100
40 0/5 never never 0 44 100
  • ε-greedy failed beyond 10 states. Even at N = 10 it found the goal late (median episode 178) and then abandoned it: 0.2 successes in the last 100 episodes, because it had settled on the lure.
  • Optimism found the goal in about N episodes at every length: median first success 10 at N = 20 and 44 at N = 40, and it succeeded in every one of the last 100 episodes.
  • Local UCB sat in between: it reached N = 10, 15 and 20 (first success 29, 123 and 308) but not 30 or 40 within 1,000 episodes. Its bonus encourages trying each action in each state, but does not pull the agent toward distant unexplored states.
  • Seeds 1 and 2: ε-greedy found the goal at N = 5 in 3 and 5 of 5 runs, at N = 10 in 1 and 0; optimism’s median first success was 10 and 12 at N = 20, and 45 and 43 at N = 40. Local UCB is deterministic here and gave the same numbers.
🐍 Exercise 2 (research): how does the time to discover a distant reward grow with the chain length for ε-greedy, a per-state UCB rule and optimistic initial values?
Measure, for chains of growing length, how soon ε-greedy, local UCB and optimistic Q-learning first reach the distant reward and whether they keep exploiting it.

Exploration in practice

  • Large state spaces: counts are replaced by pseudo-counts from density models, prediction errors (curiosity, random network distillation) or ensembles (bootstrapped DQN); see topic L5-7.
  • Posterior sampling (Thompson sampling) for RL samples a whole MDP or value function and acts greedily on it for an episode, which also produces deep exploration.
  • Safety: exploration in physical systems needs limits; see topic L5-6 on safe RL.
  • Hard-exploration benchmarks such as Montezuma’s Revenge drove much of the research on intrinsic motivation.

References

  1. M. Kearns and S. Singh (2002). “Near-optimal reinforcement learning in polynomial time.” Machine Learning.
  2. R. I. Brafman and M. Tennenholtz (2002). “R-max — a general polynomial time algorithm for near-optimal reinforcement learning.” JMLR.
  3. A. L. Strehl and M. L. Littman (2008). “An analysis of model-based interval estimation for Markov decision processes.” Journal of Computer and System Sciences.
  4. T. Jaksch, R. Ortner and P. Auer (2010). “Near-optimal regret bounds for reinforcement learning.” JMLR.
  5. I. Osband, C. Blundell, A. Pritzel and B. Van Roy (2016). “Deep exploration via bootstrapped DQN.” NeurIPS.