Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Planning with models, prioritised sweeping and MCTS

Model-based planning uses a learned or given model of the environment to perform internal backups; prioritised sweeping focuses planning on states with large expected change, and Monte Carlo Tree Search (MCTS) uses simulated rollouts plus selection/exploration heuristics (UCT) to plan in large or tr

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Model-based planning is useful when an engineer can simulate outcomes faster than real-time or has a model: it reduces sample complexity by reusing simulated experience. Prioritised sweeping makes planning efficient by focusing updates where they matter most. Monte Carlo Tree Search (MCTS) is widely used in game-playing and planning for very large or combinatorial action spaces (e.g., Go, planning in robotics) where exhaustive dynamic programming is impossible.

Key ideas

  • Model-based RL vs model-free RL: model-free learns value or policy directly from real samples (e.g., Q-learning, SARSA). model-based learns/uses a model to perform planning as well as learning from real samples. Hybrid methods interleave acting, learning the model, and planning (Dyna-style).

  • Dyna architecture: Interleave three activities — (1) act in the real environment and collect (s,a,r,s'); (2) update the model using the new sample; (3) run planning steps that sample transitions from the model and perform value backups. Dyna emphasizes re-use of real experience to create many simulated updates.

  • Backup / Bellman operator: For a fixed policy π in a finite MDP with discount factor γ ∈ [0,1), the Bellman expectation operator Bπ maps a value function V to a new function: (Bπ V)(s) = E[R_{t+1} + γ V(S_{t+1}) | S_t = s, A_t ~ π]. For control, Bellman optimality operator B* gives: (B* V)(s) = max_a E[R_{t+1} + γ V(S_{t+1}) | S_t = s, A_t = a]. The operators are contraction mappings under max-norm if γ < 1, ensuring unique fixed points and convergence of iterative application (value iteration or policy evaluation).

    • Assumptions for contraction theorem: finite state space (or bounded V), γ ∈ [0,1), bounded immediate rewards.
  • Prioritised Sweeping: Idea: not all simulated backups are equally useful. Compute a priority for a (s,a) or s, e.g. using the magnitude of the Bellman error or expected change in value if we update now. Maintain a priority queue of states (or state-action pairs). Repeatedly pop the highest priority item, perform the backup, and then increase the priority of its predecessors (states that can transition into this state) because their values may change next. This focuses planning on parts of the state space where value estimates will change the most.

    • Key pieces: model must support finding predecessors (inverse model) or you can store seen predecessors while learning.
  • Monte Carlo Tree Search (MCTS): Used when action space or state space is very large and planning forms a search tree from the current state. The standard loop has four steps:

    1. Selection: start at root, recursively select child actions that balance exploitation and exploration, typically using UCT (Upper Confidence bound applied to Trees): choose child with highest UCT score.
    2. Expansion: when a leaf (nonterminal) node is reached that is not fully expanded, add one or more child nodes.
    3. Simulation (rollout): from the new node, simulate (using a default policy, possibly random) until terminal or maximum depth and compute return.
    4. Backpropagation: propagate the return up the selected path updating visit counts and value estimates.
    • UCT formula (used in Selection): choose action a at node s that maximises Q(s,a) + c * sqrt( ln N(s) / N(s,a) ), where Q is the mean return for that child, N(s) is visit count of parent, N(s,a) is visit count of the child, and c > 0 is an exploration constant. This is built from the UCB1 multi-armed bandit bound. UCB1 assumes bounded rewards; in practice returns are often normalised (e.g., to [0,1]) to satisfy this assumption.

    • MCTS variants differ by rollouts (random vs learned policy), leaf evaluation (value networks in AlphaGo), and how expansion/backprop are done.

  • Diagrams to draw for exams:

    1. Dyna loop: draw three boxes labelled "Act (real env)", "Update model", "Plan (simulate)" with arrows showing data flow and a memory of observed transitions.
    2. A small search tree for MCTS: root node with two child actions; annotate each child with visit counts N and mean value Q; show selection following UCT formula; label expansion and simulated rollout path and backprop up the tree.

Formulas

(Bπ V)(s) = Σ_{a} π(a|s) Σ_{s'} P(s'|s,a) [ R(s,a,s') + γ V(s') ] — V(s) has the same units as reward (return), π is a policy, P is transition probability, R is expected immediate reward (same units as reward), applies for policy evaluation in MDPs with reward and discount γ.

(B^* V)(s) = max_a Σ_{s'} P(s'|s,a) [ R(s,a,s') + γ V(s') ] — Bellman optimality backup; values have the same units as reward; applies for computing the optimal value function in an MDP.

UCT(s,a) = Q(s,a) + c * sqrt( ln N(s) / N(s,a) ) — Q(s,a) is the mean return observed for the child (same units as reward), N(s) parent visits (count), N(s,a) child visits (count), c is an exploration constant (dimensionless). Use when N(s,a) > 0; treat N(s,a)=0 as +∞ or force expansion first.

priority(s,a) = | r + γ max_{a'} Q(s',a') - Q(s,a) | — priority magnitude used in one variant of prioritised sweeping; same units as reward; here (s,a) leads to s' with observed reward r.

Q(s,a) ← Q(s,a) + α [ r + γ max_{a'} Q(s',a') - Q(s,a) ] — Q-learning style backup used in simulated updates during planning; α ∈ (0,1] is step-size (unitless). Applies when doing sample backups in planning.

V_{k+1} = B^* V_k (value iteration update) — iterating this converges to V* with the max-norm error contracted by at most a factor γ per iteration when γ < 1.

Worked examples

Example 1 — Small deterministic MDP: value iteration (exam-style) Problem: 3 states S = {s1 (start), s2, sg (goal, terminal)}. Actions A = {a}. Deterministic transitions:

  • From s1, a → s2 with reward 0.
  • From s2, a → sg with reward 10.
  • sg is terminal with V(sg) = 0. Let γ = 0.9. Compute V(s2) and V(s1) by value iteration (one-step derivation produces exact values here since deterministic chain): Formula: V(s) = max_a Σ_{s'} P(s'|s,a) [ R + γ V(s') ]. Step 1: V(sg) = 0 (terminal). Step 2: V(s2) = 10 + 0.9·V(sg) = 10 + 0.9·0 = 10. Step 3: V(s1) = 0 + 0.9·V(s2) = 0.9·10 = 9. Answer: V(s2) = 10 (unitless), V(s1) = 9 (unitless).

Example 2 — Prioritised sweeping numerical illustration (small) Problem: States {A,B}. Actions single deterministic a. Observed transitions and rewards: A --a--> B with r=0; B --a--> terminal with r=5. Initialize Q(A,a)=0, Q(B,a)=0. γ=0.9. We will compute priorities and perform a single prioritized backup pass. Step 1: For observed transition (A,a)→B with r=0, priority p_A = | r + γ max_a Q(B,a) - Q(A,a) | = |0 + 0.9·0 - 0| = 0. Step 2: For (B,a)→terminal with r=5, p_B = |5 + 0.9·0 - 0| = 5. Priority queue order: B first (p=5), then A (p=0). Perform update on B using Q-learning backup with α=1 (one-step simulated): Q(B,a) ← 5 + 0.9·0 = 5. (Exact because α=1) After updating B, recompute predecessor A's priority: p_A = |0 + 0.9·max_a Q(B,a) - Q(A,a)| = |0 + 0.9·5 - 0| = 4.5. Now A has p=4.5, so will be popped next. Update A: Q(A,a) ← 0 + 0.9·5 = 4.5. Final answers: Q(B,a)=5.0, Q(A,a)=4.5.

Example 3 — MCTS selection step with UCT (toy) Suppose a node s has two children a1 and a2 with statistics:

  • child a1: Q=3.0, N(s,a1)=10
  • child a2: Q=2.5, N(s,a2)=2 Parent visits N(s)=12. Choose c = 1.4. Compute UCT: UCT(a1) = 3.0 + 1.4 * sqrt( ln 12 / 10 ). ln 12 ≈ 2.4849. sqrt(2.4849/10)=sqrt(0.24849)=0.4985. So second term = 1.40.4985 ≈ 0.698. UCT(a1) ≈ 3.0 + 0.698 = 3.698. UCT(a2) = 2.5 + 1.4 * sqrt( ln 12 / 2 ). sqrt(2.4849/2)=sqrt(1.24245)=1.1156. Second term = 1.41.1156 ≈ 1.562. UCT(a2) ≈ 2.5 + 1.562 = 4.062. Decision: choose a2 because 4.062 > 3.698 (exploration favours less-visited a2). Final answer: pick a2.

Common mistakes

  • Using α incorrectly in planning: when using sample backups for planning, either use α < 1 for incremental updates or α = 1 for full backups; mixing conventions causes wrong results.
  • Ignoring γ < 1 requirement for contraction theorems; for γ = 1 need extra assumptions (e.g., episodic finite-horizon) for convergence.
  • In UCT, treating N(s,a)=0 without special handling (division by zero); common practical fix: treat unvisited children as highest priority (force expansion once).
  • Mis-specified priorities: using immediate reward magnitude rather than Bellman error as priority defeats the point because we need expected value change, not raw reward.
  • Ignoring model bias: planning extensively with an inaccurate learned model can degrade performance; always validate or regularise the model used for planning.

For your exam

Typical question types:

  • Definitions and short notes: define model, planning, Dyna, prioritised sweeping, MCTS, UCT.
  • Derivations: derive Bellman expectation and optimality equations; show contraction mapping proof sketch for Bellman operator (assumptions: γ<1, bounded rewards); derive priority expression from Bellman error.
  • Algorithm description and pseudocode: write Dyna loop or prioritised sweeping pseudocode and explain each step; draw the Dyna diagram.
  • Numericals: small MDP value iteration, single-step prioritized-sweeping updates, compute UCT selection values.
  • Long answers: compare model-based vs model-free, explain when you would use MCTS and its limitations.

What full-marks answers include:

  • Precise bold definitions of terms used.
  • Clear assumptions stated (finite states, discount factor, bounded rewards) when invoking theorems.
  • For derivations: step-by-step algebra, and statement of the contraction result with proof sketch: show sup-norm decreases by at most factor γ.
  • For algorithms: clear pseudocode or numbered steps and a correctness/complexity remark.
  • For diagrams: label states, transitions, counts and Q-values when needed.

Quick check

  1. What are the four steps of MCTS? 2) Give the UCT formula. 3) In prioritised sweeping, what do we push into the priority queue? Answers: Selection/Expansion/Simulation(rollout)/Backpropagation; UCT(s,a) = Q(s,a) + c * sqrt( ln N(s) / N(s,a) ); items (states or state-action pairs) with priority proportional to the magnitude of their Bellman error (expected value-change), e.g., |r + γ max_{a'} Q(s',a') - Q(s,a)|.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?