Simple Algorithm
Reinforcement Learning · Fixed Point · 28 min

Planning in Huge MDPs: Sparse Sampling

Near-optimal planning in time independent of the number of states.

Value iteration touches every state in every sweep, so its cost grows with the size of the state space: hopeless for a robot in a continuous world or a game with more positions than atoms. Kearns, Mansour and Ng showed that choosing near-optimal actions needs no dependence on the number of states at all, if you can query a simulator. The price moves into the horizon, exponentially, and a matching lower bound shows that this price cannot be avoided without further structure. This lesson proves both results, then follows the idea to rollouts, tree search and the sample complexity of planning from a generative model.

What you’ll learn

  • Define planning with a generative model, and implement the sparse sampling algorithm.
  • State Kearns, Mansour and Ng's theorem with its constants, and sketch why a sampled tree of width CC and depth HH gives ε\varepsilon-optimal actions.
  • Prove the matching lower bound: a planner that only samples can need a number of queries exponential in the effective horizon.
  • Compare sparse sampling with rollouts, Monte Carlo tree search, and model-based planning from samples, whose sample complexity is Θ~(SA/((1−γ)3ε2))\tilde\Theta\big(SA/((1-\gamma)^3\varepsilon^2)\big).

Planning from a simulator

A generative model is a simulator you can reset: given any state ss and action aa, it returns the reward and a next state s′∼P(⋅∣s,a)s'\sim P(\cdot\mid s,a). It is weaker than a full model, since you never see the probabilities, and stronger than experience, since you may query any state as often as you like. Game engines and physics simulators are generative models. A policy for a huge state space cannot even be written down, so Kearns, Mansour and Ng (2002) took the online view: a planner is handed the current state, computes, and returns one action, and its cost is counted per decision.

python
def estimate_q(sim, s, depth, width, gamma, actions):
    """Sparse-sampling estimates of Q_depth(s, a) for every action.
    sim(s, a) returns (reward, next_state), next_state ~ P(. | s, a)."""
    if depth == 0:
        return {a: 0.0 for a in actions}
    q = {}
    for a in actions:
        total = 0.0
        for _ in range(width):              # C sampled successors per action
            r, s2 = sim(s, a)
            v2 = max(estimate_q(sim, s2, depth - 1, width, gamma, actions).values())
            total += r + gamma * v2
        q[a] = total / width
    return q

def sparse_sampling_action(sim, s, depth, width, gamma, actions):
    q = estimate_q(sim, s, depth, width, gamma, actions)
    return max(q, key=q.get)

Each call at depth hh makes ACAC simulator calls and ACAC recursive calls, so a decision costs O((AC)H)\order{(AC)^H} calls. The recursion is the backward induction of Value Iteration, with every expectation replaced by an average of CC samples.

Think first What does sparse sampling become in a deterministic environment, and how large is its tree?

All CC samples of a pair return the same successor, so one is enough: the tree is exhaustive depth-HH lookahead with AHA^H leaves, classical search. More generally, merging repeated states at the same depth helps whenever few states are reachable: with only N0N_0 states reachable from the root, the merged tree has O(N0H)\order{N_0H} nodes (Kearns, Mansour & Ng, 2002).

The guarantee

DerivationProof sketch
  1. γHVmax⁡≤λ\gamma^HV_{\max}\le\lambda

    Truncation. Rewards beyond depth HH are worth at most γHVmax⁡≤λ\gamma^HV_{\max}\le\lambda, so depth-HH values are within λ\lambda of infinite-horizon ones.

  2. Pr⁡(∣1C∑i=1CXi−EX∣>λ)≤2exp⁡(−λ2C2Vmax⁡2)\Pr\Big(\Big|\frac1C\sum_{i=1}^CX_i-\E X\Big|>\lambda\Big)\le2\exp\Big(-\frac{\lambda^2C}{2V_{\max}^2}\Big)

    One level of sampling. At a node, Q^h(s,a)\hat Q_h(s,a) averages CC independent terms r+γV^h−1(si′)r+\gamma\hat V_{h-1}(s'_i), each in [−Vmax⁡,Vmax⁡][-V_{\max},V_{\max}]. By Hoeffding's inequality the average is within λ\lambda of its expectation except with probability at most 2e−λ2C/(2Vmax⁡2)2e^{-\lambda^2C/(2V_{\max}^2)}. The first term of CC pays for a union bound over all the nodes of the tree, the second makes the overall failure probability small.

  3. αH≤λ(1+γ+⋯+γH−1)≤λ1−γ\alpha_H\le\lambda\big(1+\gamma+\dots+\gamma^{H-1}\big)\le\frac{\lambda}{1-\gamma}

    Errors compound geometrically. If every average is within λ\lambda, the error at depth hh obeys αh≤λ+γαh−1\alpha_h\le\lambda+\gamma\alpha_{h-1}, so the root's estimates are within λ/(1−γ)\lambda/(1-\gamma) of the depth-HH values, and with truncation within η=2λ/(1−γ)\eta=2\lambda/(1-\gamma) of Q⋆Q^\star:

  4. 2η1−γ=4λ(1−γ)2=ε\frac{2\eta}{1-\gamma}=\frac{4\lambda}{(1-\gamma)^2}=\varepsilon

    From actions to a policy. An action chosen from estimates within η\eta of Q⋆Q^\star loses at most 2η2\eta in that state, and a policy whose every action loses at most 2η2\eta is within 2η/(1−γ)2\eta/(1-\gamma) of optimal, by the performance difference lemma. With λ=ε(1−γ)2/4\lambda=\varepsilon(1-\gamma)^2/4 the main term is exactly ε\varepsilon; the paper's accounting with these constants covers the rare failures as well.

Settingλ\lambdaDepth HHWidth CCTree size (AC)H(AC)^H
γ=0.5\gamma=0.5, Rmax⁡=1R_{\max}=1, A=2A=2, ε=0.1\varepsilon=0.10.006259about 2.7×1072.7\times10^7about 107010^{70}
γ=0.9\gamma=0.9, Rmax⁡=1R_{\max}=1, A=2A=2, ε=1\varepsilon=10.002579about 5.5×10105.5\times10^{10}about 1087210^{872}
The theorem's constants, computed. The point is the shape of the bound, not these numbers: nothing depends on the number of states, and the depth grows with the effective horizon 1/(1−γ)1/(1-\gamma). Practical versions use far smaller trees: the paper notes that sampling Ci=γ2iCC_i=\gamma^{2i}C successors at depth ii costs only about the square root of the running time, and heuristic leaf values allow shallow trees.
Check your understanding

Sparse sampling's cost per decision does not depend on the number of states. What does it depend on exponentially?

  1. The number of actions AA alone
  2. The number of states reachable in one step
  3. The depth HH, which grows like 11−γlog⁡1ε(1−γ)\frac{1}{1-\gamma}\log\frac{1}{\varepsilon(1-\gamma)}
  4. The number of decisions the agent will make
Show the answer

C. The depth HH, which grows like 11−γlog⁡1ε(1−γ)\frac{1}{1-\gamma}\log\frac{1}{\varepsilon(1-\gamma)}

The cost is (AC)H(AC)^H: polynomial in AA and CC for each level, but raised to the power HH, which grows with the effective horizon. Each decision is planned afresh, so the number of decisions only multiplies the total.

The horizon is the price: a lower bound

DerivationProof: a needle in a binary tree
  1. Build a complete binary tree of depth HH. Its nodes are the states, the two actions move deterministically to the left or right child, and the leaves lead to an absorbing state. Choose one leaf vv uniformly at random and let it pay Rmax⁡R_{\max}; every other reward is 0.

  2. From the root, the only reward is reached by walking to vv, worth about γHRmax⁡=εRmax⁡\gamma^HR_{\max}=\varepsilon R_{\max}; a planner that has not found vv earns nothing from the root. With the depth tuned slightly, ε\varepsilon-optimality at the root forces the planner to locate vv.

  3. 2H=2log⁡(1/ε)/log⁡(1/γ)=(1ε)log⁡2/log⁡(1/γ)2^H=2^{\log(1/\varepsilon)/\log(1/\gamma)}=\Big(\frac{1}{\varepsilon}\Big)^{\log2/\log(1/\gamma)}

    The simulator reveals a leaf's reward only when that leaf is queried, and every leaf is equally likely to be vv. Any search strategy needs Ω(2H)\Omega(2^H) queries on average:

For γ=0.9\gamma=0.9 and ε=0.01\varepsilon=0.01 the tree has depth about 44 and 244≈1.8×10132^{44}\approx1.8\times10^{13} leaves. The two theorems bracket the problem: for a fixed γ\gamma the lower bound grows polynomially in 1/ε1/\varepsilon, the upper bound like (1/ε)O(log⁡(1/ε))(1/\varepsilon)^{\order{\log(1/\varepsilon)}}, and Kearns, Mansour and Ng left the gap open. Without further assumptions, exponential dependence on the horizon is the price of independence from the number of states.

Structure is what practical planners exploit. Most problems are not needles in binary trees: a few lines of play matter, many actions are clearly bad after a few samples, and a heuristic can value the leaves. The planners of the next step put their samples where those properties pay.

Adaptive lookahead: rollouts and tree search

Sparse sampling spends its samples uniformly on every branch. Two ideas spend them better.

Rollouts. Fix a cheap base policy πb\pi_b. At the current state, estimate each action's value Qπb(s,a)Q^{\pi_b}(s,a) by simulating aa and then following πb\pi_b, averaged over several rollouts, and pick the best. With exact estimates this is one step of policy improvement, so the rollout policy is at least as good as πb\pi_b in every state (Policy Iteration), and its cost per decision grows linearly with the horizon instead of exponentially. Tesauro and Galperin (1996) used it to strengthen backgammon programs, and Bertsekas, Tsitsiklis and Wu (1997) developed it for combinatorial optimization. The limit is built in: one improvement step over πb\pi_b, no more.

Adaptive tree search. Chang, Fu, Hu and Marcus (2005) chose actions at each node of a sampled tree with a bandit rule, and Kocsis and Szepesvári (2006) turned this into UCT: grow the tree one simulation at a time, choose actions inside it by an upper confidence bound, and estimate new leaves by rollouts (Monte Carlo tree search). Samples flow to the branches that look best, so far smaller trees find good actions when few lines matter. UCT is consistent, its probability of choosing a suboptimal root action tending to zero as simulations grow, but on adversarially built trees it can need far more simulations than uniform sampling (MCTS in Depth: UCT, PUCT & What Search Optimizes).

PlannerCost per decisionGuarantee
Value iteration on the full modelO(S2A)\order{S^2A} per sweep, about 11−γlog⁡1ε(1−γ)\frac{1}{1-\gamma}\log\frac{1}{\varepsilon(1-\gamma)} sweeps, once for all statesε\varepsilon-optimal everywhere; needs the probabilities
Sparse sampling(AC)H(AC)^H simulator calls, independent of SSε\varepsilon-optimal everywhere, in the worst case
Rollouts of a base policy πb\pi_bA×(rollouts)×(horizon)A\times(\text{rollouts})\times(\text{horizon}) callsAt least as good as πb\pi_b, up to sampling error
UCT and Monte Carlo tree searchAnytime: as many simulations as time allowsConsistent as simulations grow; no useful worst-case rate
Online planners trade generality of the guarantee for cost. The ones used in practice, from backgammon rollouts to AlphaZero's search, are adaptive and rely on learned or heuristic values at the frontier.

How many samples does planning need?

If the state space is finite, a generative model also supports the opposite strategy: estimate the whole model, then plan. Draw NN next states from every pair (s,a)(s,a), form the empirical model P^\hat P, and solve it by value or policy iteration. This certainty-equivalent planner uses SANSAN samples, and the question is how large NN must be for its greedy policy to be ε\varepsilon-optimal in the true MDP, with rewards in [0,1][0,1].

Predict before you look

An MDP has 100 states and 4 actions, with γ=0.9\gamma=0.9 and target ε=0.1\varepsilon=0.1. Ignoring logarithmic factors, SA(1−γ)3ε2\frac{SA}{(1-\gamma)^3\varepsilon^2} samples is 10 to what power?

Show the answer

7.6

4000.001×0.01=4×107≈107.6\frac{400}{0.001\times0.01}=4\times10^7\approx10^{7.6}, that is 10510^5 samples from every state–action pair. Halving ε\varepsilon multiplies it by 4; moving to γ=0.99\gamma=0.99 multiplies it by 1,000.

The two routes are complementary. Sparse sampling never looks at most states and pays exponentially in the horizon; the plug-in planner pays polynomially in the horizon but needs samples from every state–action pair. A crude Hoeffding analysis of the plug-in planner gives (1−γ)−4(1-\gamma)^{-4}; the sharp (1−γ)−3(1-\gamma)^{-3} comes from Bernstein's inequality and the law of total variance, which shows that the variances of the returns accumulated along a trajectory are smaller than the worst case suggests (Azar et al., 2013). Between the two routes lie problems with structure, such as few reachable states, features in which the values are linear, or good heuristics at the leaves, and that is where much of the current theory lives.

Key takeaways

  • With a generative model, sparse sampling picks ε\varepsilon-optimal actions at a per-decision cost of (AC)H(AC)^H simulator calls, independent of the number of states, with HH and CC set by ε\varepsilon, γ\gamma and Rmax⁡R_{\max} (Kearns, Mansour & Ng, 2002).
  • The exponential dependence on the horizon is unavoidable in general: a reward hidden in one of 2H2^H leaves, H=log⁡γεH=\log_\gamma\varepsilon, forces Ω(2H)\Omega(2^H) queries.
  • Rollouts give one step of policy improvement at linear cost, and UCT-style tree search spends samples adaptively; both are consistent but trade worst-case guarantees for practical speed.
  • For finite MDPs, planning in an empirical model needs Θ~(SA/((1−γ)3ε2))\tilde\Theta\big(SA/((1-\gamma)^3\varepsilon^2)\big) samples, and the plug-in approach attains it (Azar et al.; Agarwal et al.; Li et al.).

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What can a planner with a generative model do that an agent learning from its own experience cannot?

  1. Read the transition probabilities P(s′∣s,a)P(s'\mid s,a) directly
  2. Sample a transition from any state and action it chooses, as often as it likes
  3. Evaluate every state in one call
  4. Avoid the exploration problem without any samples at all
Show the answer

B. Sample a transition from any state and action it chooses, as often as it likes

A generative model is a resettable simulator: it returns samples, not probabilities, for any queried pair. An agent acting in the world sees only the states its own trajectory reaches, which is why exploration is hard there and easy here.

Question 2

Why must any ε\varepsilon-optimal planner with only a generative model make exponentially many queries in the worst case?

  1. Because the number of states is exponential
  2. Because Hoeffding's inequality needs exponentially many samples
  3. Because the reward can hide in one of 2H2^H leaves of a deterministic tree, with H=log⁡γεH=\log_\gamma\varepsilon, and only querying that leaf reveals it
  4. Because the optimal policy is stochastic
Show the answer

C. Because the reward can hide in one of 2H2^H leaves of a deterministic tree, with H=log⁡γεH=\log_\gamma\varepsilon, and only querying that leaf reveals it

The construction is a needle in a haystack: with all transitions deterministic, sampling noise plays no role; the planner simply has to find one leaf among 2H2^H, and the depth HH that still matters at accuracy ε\varepsilon is log⁡(1/ε)/log⁡(1/γ)\log(1/\varepsilon)/\log(1/\gamma).

Question 3

A rollout planner estimates Qπb(s,a)Q^{\pi_b}(s,a) by simulation and acts greedily. What does it guarantee, with exact estimates?

  1. An optimal policy
  2. A policy at least as good as πb\pi_b in every state: one step of policy improvement
  3. A policy within 2ε/(1−γ)2\varepsilon/(1-\gamma) of optimal for any base policy
  4. Nothing, because the rollouts use a suboptimal policy
Show the answer

B. A policy at least as good as πb\pi_b in every state: one step of policy improvement

Acting greedily on QπbQ^{\pi_b} is policy improvement, so the policy improvement theorem applies. Repeating the step would be policy iteration, which rollouts do not do; that is why rollouts pay off most with a good base policy.

Question 4

Planning in an empirical model built from NN samples per state–action pair is minimax optimal. How does its total sample need, O~(SA/((1−γ)3ε2))\tilde{\mathcal O}\big(SA/((1-\gamma)^3\varepsilon^2)\big), compare with sparse sampling's cost?

  1. It is worse in every respect
  2. It is identical up to constants
  3. It does not depend on the number of states
  4. It grows only polynomially with the horizon, but linearly with SASA, while sparse sampling is independent of SS and exponential in the horizon
Show the answer

D. It grows only polynomially with the horizon, but linearly with SASA, while sparse sampling is independent of SS and exponential in the horizon

The two strategies sit at opposite corners: sample every pair and pay (1−γ)−3(1-\gamma)^{-3}, or sample only around the current state and pay exponentially in the effective horizon. Which is cheaper depends on the size of the state space and on γ\gamma.

Further reading

  • M. Kearns, Y. Mansour & A. Y. Ng, "A sparse sampling algorithm for near-optimal planning in large Markov decision processes," Machine Learning 49, 193–208 (2002).
  • L. Kocsis & C. Szepesvári, "Bandit based Monte-Carlo planning," Proceedings of ECML (2006).
  • M. G. Azar, R. Munos & H. J. Kappen, "Minimax PAC bounds on the sample complexity of reinforcement learning with a generative model," Machine Learning 91(3), 325–349 (2013).
  • A. Agarwal, S. Kakade & L. F. Yang, "Model-based reinforcement learning with a generative model is minimax optimal," Proceedings of COLT (2020).
  • G. Li, Y. Wei, Y. Chi, Y. Gu & Y. Chen, "Breaking the sample size barrier in model-based reinforcement learning with a generative model," Advances in Neural Information Processing Systems 33 (2020).