skip to content

In a self-play board-game engine, how do learned policy and value networks change Monte Carlo tree search compared with plain UCT and random playouts?

level: seniorimportance: nice to knowfreq 22%

answer

  1. a prior inside selection
  2. evaluate the leaf, skip the playout
  3. visit counts become training targets
  4. search improves the policy
  5. noise at the root

basics

~20 s

The policy network supplies move priors that steer selection toward promising moves, and the value network scores new leaves instead of random playouts; self-play then trains the policy toward search visit counts and the value toward game results.

solid answer

~50 s

Plain MCTS selects with UCT, treating every move as equally promising at first, and estimates leaves with random playouts. Network guidance changes both. A **policy network** gives a prior `P(s, a)` used in a PUCT rule, `Q + c * P * sqrt(N_parent) / (1 + N_child)`, so visits concentrate on moves it rates highly, effectively narrowing the branching factor. A **value network** scores each new leaf directly, and that value is backed up with the sign flipped each ply. In self-play, the root's **visit-count distribution** becomes the policy target and the final result the value target; because search after many simulations beats the raw prior, training on it improves the network, which improves the next search. Dirichlet noise at the root and early-move sampling keep it exploring. Each simulation costs a network evaluation, so batching matters.

code

pseudocode · 15 lines
pseudocode
function selectChild(node, c):
    // child.valueSum is stored from the view of the player to move at node
    sqrtParent = sqrt(max(1, node.visitCount))   // max(1, .) lets priors order the very first pick
    best = none
    bestScore = -infinity
    for child in node.children:
        if child.visitCount > 0:
            q = child.valueSum / child.visitCount
        else:
            q = 0                                    // first-play value; engines differ here
        u = c * child.prior * sqrtParent / (1 + child.visitCount)
        if q + u > bestScore:
            bestScore = q + u
            best = child
    return best

go deeper

for a junior

Recall the two networks: a policy giving move probabilities and a value scoring positions, and that they replace uniform selection and random playouts.

for a middle

Explain the PUCT term, how the value backs up with a sign flip, and which target each head learns from in self-play.

for a senior

Show why search acts as policy improvement, what root noise and early sampling do, and how batching and virtual loss keep inference affordable.

for a principal

Weigh network-guided search against an authored evaluation for a shipping game: hardware budget, retraining after rule changes and difficulty levels.

## Plain MCTS in one paragraph Monte Carlo tree search builds a search tree from the current position by repeating four steps: select a path with a rule such as UCT, expand a new node, estimate it, and back the result up. In plain MCTS the estimate is a **random playout**: play random moves to the end of the game and score the result. The step-by-step mechanics belong with adversarial search; this question is about what learned networks change. Two weaknesses matter. UCT treats every legal move as equally promising until it has visited it, so a large branching factor spreads visits thin. And random playouts are noisy, biased estimates in games where the outcome depends on precise play. ## What the policy network changes A **policy network** takes a position and outputs a probability `P(s, a)` for each legal move, roughly how likely a strong player is to choose it. Network-guided search uses that probability as a **prior** inside selection. A widely used rule, often called **PUCT**, picks the child that maximises: `Q(s, a) + c * P(s, a) * sqrt(N(s)) / (1 + N(s, a))` - `Q(s, a)` is the mean value of the child so far, from the point of view of the player to move at `s`. - `N(s)` is the parent's visit count and `N(s, a)` the child's. - `c` sets how strongly the prior drives exploration. The exploration term is large for moves the network likes that have few visits, and it shrinks as a child is visited. Moves the network rates near zero are rarely tried, which effectively prunes the branching factor without a hand-written rule. ## What the value network changes A **value network** takes a position and outputs an estimate `v` of the expected result for the player to move, for example in `[-1, 1]`. When the search reaches a new leaf, it evaluates the leaf with the network instead of playing randomly to the end, and backs `v` up the path, flipping the sign each ply in a two-player game with alternating turns. One network evaluation typically costs far more than one random playout but tells far more, so the search reaches good decisions with far fewer simulations. In many designs both heads share one network body and are evaluated together, once per expanded leaf. ## The self-play training loop 1. **Play games against itself.** At each move, run network-guided MCTS from the current position and play a move. 2. **Record targets.** Store the position, the root's **visit-count distribution** `π` (how often search visited each move) and, when the game ends, its result `z`. 3. **Train.** Fit the policy head toward `π` with a cross-entropy loss and the value head toward `z` with a squared error. 4. **Repeat** with the new network. The loop works because search acts as a **policy-improvement operator**: the visit distribution after many simulations is stronger than the raw prior it started from, so training the prior toward it raises the network, which raises the next search. Two details keep it exploring. Noise drawn from a Dirichlet distribution is mixed into the root's priors during self-play, so moves the network underrates still get visits. And early moves are sampled in proportion to visit counts instead of always taking the most visited. Both are switched off for evaluation games. ## Plain versus network-guided | | Plain MCTS with UCT | Network-guided MCTS | |---|---|---| | Selection prior | none; all moves start equal | policy network probabilities | | Leaf estimate | random playout | value network | | Cost per simulation | low | high (a network evaluation) | | Simulations needed per move | many | far fewer | | Domain knowledge | hand-written playout policy, if any | learned from self-play | ## Engineering and failure modes - **Batch the inference.** Run several searches or threads at once and evaluate their leaves together. A **virtual loss** temporarily marks a path as worse while a thread is descending it, so parallel threads spread out instead of all taking the same path. - **Budget the hardware.** In a shipping game the network competes with the frame for processor time; a smaller network and fewer simulations trade strength for speed and give natural difficulty levels. - **Blind spots.** The value network can misjudge positions unlike anything self-play produced, and the search inherits the error. - **Rule changes.** Any change to the rules invalidates the training data, and the loop must run again.

  • Why train the policy toward visit counts rather than toward the single move that was played?
    The visit distribution carries more information: it says how strongly search preferred each move, including near-equal alternatives. It is also the improved policy that search produced from the prior, so learning it transfers search's gains into the network. A single played move, especially one sampled for exploration, is a much noisier target.
  • How does a network-guided engine offer weaker difficulty levels for players?
    Fewer simulations per move, an earlier and weaker network checkpoint, or sampling moves from the visit distribution at a higher temperature all lower strength smoothly. Each tends to play plausible moves rather than blunders, though its mistakes can still look unlike a human's.

  • The value network is used to pick which child to visit next.
  • Network-guided search still finishes every simulation with a random playout.
  • The policy network is trained toward the final game result.
  • Dirichlet noise is kept on in tournament play to stay unpredictable.
  • Network guidance makes each simulation cheaper than a random playout.