Pick the next move in MCTS with the PUCT rule

AlphaGo and AlphaZero search with Monte Carlo Tree Search. At each node they choose the child that balances its average value Q against an exploration bonus driven by the policy network's prior P:

score(a) = Q(a) + c * P(a) * sqrt(N_parent) / (1 + N(a))

where N(a) is the child's visit count, N_parent = sum of all children's visits, and Q(a) = W(a) / N(a) (0 if unvisited).

Write puct_select(W, N, P, c) for arrays of total value W, visit counts N and priors P, one entry per child. Return the index with the highest score (ties: lowest index).

Signaturedef puct_select(W, N, P, c)

Understand the idea

In Monte Carlo Tree Search each node must pick which child move to explore next. PUCT adds two parts: Q, the average value seen so far, and an exploration bonus that is large when the policy prior P likes the move and the move has few visits. As a child gets visited more, its bonus shrinks and measured value takes over. Like trying a highly recommended restaurant a few times before trusting your own experience. Worked example: W = [3, 0], N = [4, 0], P = [0.5, 0.5], c = 1. N_parent = 4, sqrt = 2. Child 0: Q = 3 / 4 = 0.75, bonus = 1 x 0.5 x 2 / (1 + 4) = 0.2, score 0.95. Child 1: Q = 0 (unvisited), bonus = 0.5 x 2 / 1 = 1.0, score 1.0. Pick child 1. Rule: score(a) = Q(a) + c P(a) sqrt(sum N) / (1 + N(a)), Q = W / N or 0, argmax with lowest index on ties. AlphaGo and AlphaZero select moves this way.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: exploit a strong movepuct_select([9.0, 1.0, 0.0], [10, 5, 0], [0.4, 0.4, 0.2], 1.0)
  • Sample 2: explore an unvisited high-prior movepuct_select([6.0, 0.0], [10, 0], [0.3, 0.7], 2.0)