How does Monte Carlo tree search with UCT choose a move, and when would you prefer it to alpha-beta for a game AI?
answer
- four phases, repeated many times
- average reward plus an exploration bonus
- random games played to the end
- no evaluation function required
basics
~20 sMonte Carlo tree search repeats four steps: select a path with UCT, expand one new node, play a cheap game to the end, and back the result up. It suits games lacking a good evaluation or with huge branching.
solid answer
~50 sEach MCTS iteration **selects** a path from the root by picking, at every node, the child maximising UCT, `mean reward + c * sqrt(ln(parent visits) / child visits)`; **expands** one untried move; runs a cheap **playout**, often random moves, to the end of the game; and **backs up** the result, adding a visit and the reward to every node on the path. The first term exploits moves that have done well, the second explores moves tried rarely; `c = sqrt(2)` is the textbook value for rewards in `[0, 1]`, and it is tuned in practice. After the budget is spent, the AI usually plays the **most-visited** root child. Prefer MCTS when no reliable evaluation function exists, when branching is too high for alpha-beta, or when you want an anytime search that grows unevenly towards promising lines. Prefer alpha-beta when good evaluations exist and the game is sharply tactical, because random playouts can miss a narrow refutation.
code
pseudocode · 21 linesfunction mctsIteration(root, c):
node = root
// 1. selection: descend while every move here already has a child
while node.untriedMoves is empty and not node.isGameOver():
node = argmax over child in node.children of
child.totalReward / child.visits
+ c * sqrt(ln(node.visits) / child.visits)
// 2. expansion: add one child for an untried move
if not node.isGameOver():
move = node.untriedMoves.popRandom()
node = node.addChild(move)
// 3. simulation: cheap playout to the end of the game
result = playout(node.state)
// 4. backpropagation: credit each node for the player who moved into it
while node is not none:
node.visits = node.visits + 1
node.totalReward = node.totalReward + result.rewardFor(node.playerWhoMoved)
node = node.parent
function chooseMove(root):
return the child of root with the most visitsgo deeper
Recall the four phases, selection, expansion, playout and backup, and that UCT adds an exploration bonus to each move's average result.
Compute a UCT value by hand, explain what the constant c trades off, and get the two-player reward bookkeeping right in backup.
Choose between MCTS and alpha-beta for a game from its branching factor, evaluation quality and tactical sharpness, and explain how traps fool random playouts.
Judge the cost of a search-based opponent against authored behaviour for the game's design goals, such as readability, difficulty tuning and CPU budget per turn.
## The four phases **Monte Carlo tree search (MCTS)** builds a search tree gradually, guided by the results of simulated games rather than by an evaluation function. Each **iteration** has four phases: 1. **Selection.** Starting at the root, repeatedly move to a child chosen by a selection rule until reaching a node that still has untried moves or ends the game. 2. **Expansion.** Add one child for an untried move. 3. **Simulation (playout or rollout).** From the new node, play the game to the end with a cheap policy, often uniformly random legal moves. 4. **Backpropagation (backup).** Walk back to the root, adding one visit to each node on the path and adding the playout's reward, for example 1 for a win, 0.5 for a draw and 0 for a loss. Thousands or millions of iterations later, each child's **mean reward** estimates how good that move is, and the tree is deep where play was promising and shallow elsewhere. ## UCT: the selection rule **UCT** (Upper Confidence bounds applied to Trees) applies the UCB1 bandit formula at every node. For a child `j` of a node with `N` visits: ``` uct(j) = meanReward(j) + c * sqrt( ln(N) / visits(j) ) ``` - The first term is **exploitation**: prefer moves whose playouts have gone well. - The second is **exploration**: a bonus that shrinks as a child is visited more and grows slowly with the parent's visits, so no move is starved forever. - The constant `c` sets the balance. `sqrt(2)` (about 1.41) is the value from UCB1's analysis for rewards in `[0, 1]`; in practice it is tuned per game. A worked example with `c = 1.41`: a node with 100 visits has, among its children, A (60 visits, mean 0.6) and B (10 visits, mean 0.4). Since `ln(100)` is about 4.61, A scores `0.6 + 1.41 * sqrt(4.61 / 60)`, about 0.99, and B scores `0.4 + 1.41 * sqrt(4.61 / 10)`, about 1.36. UCT visits B next: its estimate is uncertain enough that it might still be the better move. ## Choosing the move to play When the time or iteration budget runs out, the usual choice is the **most-visited** root child (the "robust child"). Visits concentrate on moves that stayed good under repeated scrutiny, while a high mean on a few visits can be luck. Because the tree is usable after any number of iterations, MCTS is naturally **anytime**, and the subtree under the move actually played can be kept for the next turn. ## Two-player bookkeeping In a two-player game each node must store reward from the point of view of the **player who made the move into it**, because that is the player choosing among those children at the parent. A common bug is backing up rewards from one fixed side, which makes the opponent's nodes choose the AI's best moves instead of their own. ## When MCTS beats alpha-beta and when it does not | Factor | Alpha-beta | MCTS with UCT | |---|---|---| | Needs a hand-made evaluation | yes, its quality caps playing strength | no, playouts estimate value | | Very high branching factor | struggles: full width at each ply | copes: grows selectively | | Narrow tactical traps | finds them within its depth | can miss a single refutation that random playouts rarely play | | Interruptible search | yes, with iterative deepening | yes, every iteration | | Cost per sample | one evaluation per leaf | a whole simulated game | The classic MCTS weakness is a **trap**: a move that wins against almost every reply but loses to one specific reply. Random playouts seldom find that reply, so the move's mean stays high until the tree itself grows deep enough there. ## Common improvements - **Heavier playouts** that follow simple domain rules (take a winning capture, avoid an obvious blunder) give less noisy results at a higher cost per playout. - **Cut-off playouts**: stop a playout after some moves and score it with a rough evaluation. - **Tree reuse** across turns, and **RAVE**-style sharing of statistics between moves played at different points in the playouts. - Guiding selection and playouts with learned networks is a separate machine-learning subject; plain UCT needs none.
- What happens to MCTS play if the exploration constant c is set much too high or too low?Too high, and the bonus swamps the mean, so visits spread almost evenly and the tree stays shallow, close to sampling every move flatly. Too low, and the search locks onto whichever move looked good early and rarely revisits alternatives, so an unlucky first playout can bury the best move. The right value depends on the game and the reward scale, so it is tuned by test matches.
- Why is the most-visited root child usually played rather than the child with the best mean reward?A child with few visits can have a high mean by luck, and UCT would visit it more if it were really best. A heavily visited child has a well-sampled estimate that kept beating the alternatives. When the two choices disagree, some implementations extend the search until they agree rather than gamble on either.
Choosing restaurants in a new city: you return to places that were good, but a place you have tried only once still gets a chance, because one meal tells you little. The fewer visits a place has had, the bigger its benefit of the doubt.
- MCTS needs a good evaluation function at the leaves.
- UCT always picks the child with the highest average reward.
- The exploration bonus grows the more often a child is visited.
- MCTS never misses tactics, because it samples every line.
- Backed-up rewards can be stored from the AI's side at every node.