Learn a model, then plan with it. Dyna-Q finds a shortcut, Monte Carlo tree search plays Connect Four against you, and self-play teaches noughts and crosses from nothing.
Learning from real experience is slow and often expensive. An agent with a model of the world — learned from what it has seen, or simply given as the rules of a game — can rehearse in its head instead: replay imagined experience, or search ahead through possible futures before it moves.
Press Play and watch the orange sparks: imagined steps that carry value back from the goal far faster than the agents walk. Then switch to the shortcut maze and see which agent notices when a new gap opens.
Reward 1 for reaching the green goal, 0 otherwise; γ = 0.95, ε = 0.1. α = 0.1, as in Sutton and Barto’s Example 8.1. Cells are coloured by the learned maxa Q(s, a); orange sparks mark cells updated by imagined transitions in the last moment; the thick line is the greedy path once it reaches the goal. Each agent takes one real step per tick, so the panels compare learning per unit of real experience, not per second of computing.
Richard Sutton’s Dyna architecture (1990) treats planning as learning from imagined experience. After each real step the agent does three things: it applies the ordinary Q-learning update, Q(s, a) ← Q(s, a) + α[r + γ maxₐ′ Q(s′, a′) − Q(s, a)]; it records the transition in a model, here a table that remembers that a in s led to s′ with reward r; and it then performs n further updates, each on a state–action pair picked at random from those it has tried, using the model’s answer in place of the world’s. Real and simulated transitions go through exactly the same update. With n = 0 it is plain Q-learning.
In the maze the only reward is at the goal. One-step Q-learning carries that news back one cell per visit, so it needs many episodes before the start knows which way to go. Once the goal has been reached even once, planning replays the final steps again and again, and value spreads back along the whole route within a single episode. Sutton and Barto’s Example 8.1 uses this 6 × 9 maze with γ = 0.95, α = 0.1 and ε = 0.1, and reports that n = 50 reaches near-optimal behaviour in about three episodes against about 25 for n = 0; the demo reproduces that (an empirical result for this maze, not a theorem). This is the model-based side of reinforcement learning: the agent spends computation to save experience.
The catch is that a model can be wrong. In the blocking maze (Example 8.2) the familiar route closes; the agent finds out the hard way, by walking into the new wall, and planning then spreads the bad news quickly. The shortcut maze (Example 8.3) is worse: a new, shorter route opens, but nothing ever tells the agent, and its model keeps insisting that the right end of the wall is solid. Plain Dyna-Q almost never finds the shortcut. Dyna-Q+ adds a bonus κ√τ to the imagined reward of any move last tried τ real steps ago (and lets planning try moves never taken, imagined as leaving the agent where it was with reward 0), so long-neglected moves start to look attractive and the agent goes back to check. A little reward is spent on curiosity before the change; much more is gained after it.
The demo’s changing worlds use α = 1, since the maze is deterministic, and n = 10 by default, so that both changes play out within the 3,000 and 6,000 steps shown; Sutton and Barto’s figures use their own settings, and κ here is simply a value that works. The panels step all agents in lockstep, so the comparison is per real step: Dyna’s advantage is in experience, paid for in computation.
Play Connect Four against MCTS and watch the bars and the tree as it thinks. You should be able to beat it at 100 simulations a move and struggle at 10,000; or set two searches against each other.
Each simulation walks down the tree choosing the move with the highest w/n + c√(ln N / n), adds one new node, plays random moves to the end of the game, and credits the result to every node it passed. In the tree, emerald means the move looks good for the player who made it (stripe colour), rose means bad; the white outline is the line the search expects. Larger c spreads visits more evenly; smaller c commits to early favourites. Simulations per second are measured while computing; “Watch it think” only spreads the same search over about a second and a half.
Monte Carlo tree search builds a game tree a little at a time, guided by random games. Each simulation has four phases. Selection: from the root, repeatedly step to the child with the highest score wᵢ/nᵢ + c √(ln N / nᵢ), where wᵢ/nᵢ is that move’s average result so far, nᵢ its visit count and N the parent’s. Expansion: on reaching a node with an untried move, add one child for it. Simulation: play uniformly random moves from there to the end of the game. Backpropagation: credit the result (win 1, draw ½, loss 0) to every node on the path, each from the point of view of the player who made that move. After the budget is spent the program plays the most visited move.
The selection rule is UCB1, the bandit algorithm from the first lesson of this module, applied at every node of the tree; Levente Kocsis and Csaba Szepesvári called this UCT (UCB applied to trees) in 2006 and showed that, given enough simulations, its estimates converge to the minimax values. The name Monte Carlo tree search was coined the same year by Rémi Coulom, whose Go program Crazy Stone used the idea. The demo uses pure random playouts and no knowledge of Connect Four beyond the rules, so its strength comes entirely from search.
Connect Four itself was solved in 1988, by James D. Allen and, independently, by Victor Allis: with perfect play the first player wins, and must start in the centre column. MCTS with a modest budget is nowhere near perfect, but it improves steadily with more simulations, as the match mode shows.
The same loop powers far stronger programs. DeepMind’s AlphaGo (Silver et al., 2016) guided MCTS with a policy network, which suggests promising moves, and a value network, which judges positions without playing them out, and beat Lee Sedol 4–1 in March 2016. AlphaGo Zero (2017) learned from self-play alone, with no human games; AlphaZero (2018) applied the same recipe to chess, shogi and Go; and MuZero (2020) learned its own model of the game instead of being given the rules.
Press Train and watch two agents learn noughts and crosses from nothing, one playing itself and one playing only a random opponent. Then play against either, or load an opening and read the values on the squares.
Click a square to play it. With “Agent replies” on, the agent answers your move; press “Agent moves” to let it play first. Here the agent’s favourite square (dashed) is one that perfect play also chooses.
Each agent keeps one number per position, its estimate of the final score (win 1, draw ½, loss 0) for the player who just moved there, and nudges it toward the estimate of its next position: V(a) ← V(a) + α[V(a′) − V(a)]. The step size α starts at 0.3 and shrinks to 0.015 over the first 100,000 games. Tests are 100 greedy games against each opponent (half as X, half as O) every 2,000 training games; the perfect player is an exact minimax search that picks at random among its best moves. Square colours: emerald = good for the player to move, rose = bad.
This is the example that opens Sutton and Barto’s book. The agent keeps a table with one number for each position that can arise — 5,478 reachable positions in all, counting the empty board — estimating the final score for the player who has just moved there. To move, it looks at the position each legal move would produce and picks the one with the highest value, except that with probability ε it moves at random. After a greedy move it nudges the value of its previous position toward the value of its new one: V(a) ← V(a) + α[V(a′) − V(a)], a TD(0) update. Winning positions are fixed at 1 and full boards at ½; a loss teaches the loser’s last position 0. The book’s example leaves the step size to be reduced over time; here it shrinks linearly from 0.3 to 0.015 over 100,000 games, without which the values keep jittering and the greedy policy can slip in and out of perfect play.
Noughts and crosses is a draw with perfect play (an exhaustive minimax search proves it, and the demo’s perfect player is exactly that search). An agent that never loses to it has learned perfect play, at least along every line the perfect player can choose. In our runs the self-play agent reaches that within the default 100,000 games for every seed we tried; the agent trained only against a random opponent usually does not. A random opponent rarely punishes a mistake, so the mistakes survive: what an agent learns depends on whom it plays. Self-play supplies an opponent that improves exactly as fast as the learner does.
The idea is old. Arthur Samuel’s checkers program (1959) improved by playing against itself. Donald Michie’s MENACE (1961) learned noughts and crosses with matchboxes, one per position, holding coloured beads for the possible moves: beads were added after wins and removed after losses. Gerald Tesauro’s TD-Gammon (1992–1995) learned backgammon by self-play with TD(λ) and a neural network, reaching near the level of the best human players. The AlphaZero recipe is self-play plus Monte Carlo tree search plus a network that learns to predict both good moves and the eventual winner — the three ideas of this page combined.
Spot a mistake or have an idea?