{"id":228,"date":"2026-11-13T12:35:14","date_gmt":"2026-11-13T04:35:14","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-monte-carlo-tree-search-balances-a-game-tree\/"},"modified":"2026-11-13T21:53:14","modified_gmt":"2026-11-13T13:53:14","slug":"how-monte-carlo-tree-search-balances-a-game-tree","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-monte-carlo-tree-search-balances-a-game-tree\/","title":{"rendered":"How Monte Carlo tree search balances a game tree"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Monte Carlo tree search<\/strong> grows a game tree by repeating selection, expansion, rollout, and backup. Selection walks from the root by a bandit rule, usually <strong>UCT<\/strong>: choose the child with the best win rate plus <strong>c \u00b7 sqrt(ln N \/ n)<\/strong>, where N is the parent&#8217;s visit count and n is the child&#8217;s. A leaf is expanded, a random or heuristic rollout finishes the game, and the result is added to every node on the path. The next move is the most visited root child. The search is an anytime approximation, not an exact minimax.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Minimax needs a reliable evaluation at the leaves. In Go-like games the horizon evaluation is weak and the tree is enormous. A random playout from a position is a noisy estimate of its value. UCT spends more playouts on children that look good and still visits children that look bad, so a lucky early win does not freeze the search. The logarithmic bonus goes to infinity as a child is ignored, which is what forces another visit.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The constant c trades exploitation against exploration. The theoretical UCT value is often sqrt(2) for rewards in [0, 1]. Practical programs tune c. The rule is not a proof that the chosen move is optimal after a finite budget.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">With infinite samples and a suitable exploration constant, UCT converges to the minimax value in a finite tree of bounded rewards. A finite budget returns the empirical best. Time is proportional to the number of iterations times the cost of a rollout. Extra memory is the tree that was actually expanded, which is a small fraction of the legal tree. The rollout policy affects the quality of the estimates long before the asymptotic regime.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The most visited child, not the child with the highest raw win rate, is the usual move to play. A rarely visited child can have a perfect win rate from one lucky rollout. The visit count is the confidence.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Start with the root. Each iteration begins there.<\/li>\n<li>While the current node has been fully expanded and has children, descend to the child that maximizes win rate plus c \u00b7 sqrt(ln N \/ n).<\/li>\n<li>If the node is not terminal and has an unexpanded legal move, add one child.<\/li>\n<li>Roll out from that child to a terminal position with a fast policy. Score the result for the player who just moved at each ancestor, or use a fixed score for the root player and negate as needed.<\/li>\n<li>Add the score to every node on the path and increment its visit count. After the budget, play the root child with the largest visit count.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Asymptotic convergence is not a finite-budget guarantee. Alpha-beta with a good evaluation can beat a weak rollout in a tactical game. MCTS is strongest when rollouts or a learned prior correlate with the truth. The exploration term must grow, however slowly, or a child can be starved forever.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the most winning child is played after one visit<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Choose the highest win rate regardless of visits<\/td>\n<td>One lucky rollout gives a win rate of 1. The visit count is what makes that rate meaningful. Play the most visited child.<\/td>\n<\/tr>\n<tr>\n<td>Set c = 0<\/td>\n<td>Selection becomes greedy on the empirical mean. An early loss can hide a good move for the rest of the budget.<\/td>\n<\/tr>\n<tr>\n<td>Treat a finite search as exact minimax<\/td>\n<td>The backed-up values are sample averages. They match minimax only in the limit.<\/td>\n<\/tr>\n<tr>\n<td>Skip the rollout and back up a random number<\/td>\n<td>The estimate then has nothing to do with the game. The rollout, or a learned value head in a later variant, is the signal.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Exact backup of a small tree is minimax, pruned by alpha-beta. MCTS is the sampled tree with a bandit selection rule and rollouts.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment selects by win rate plus an exploration term, expands one move, and rolls out at random. The position has 4 stones, and a move takes 1 or 2; taking the last stone wins. The child with the most visits is the move that takes <code>1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import math, random\nrng = random.Random(0)\n\nclass Node:\n    def __init__(self, stones, parent=None, move=None):\n        self.stones = stones\n        self.parent = parent\n        self.move = move\n        self.children = []\n        self.untried = [1, 2] if stones &gt;= 2 else ([1] if stones == 1 else [])\n        self.visits = 0\n        self.wins = 0\n\ndef rollout(stones):\n    winner_is_to_move = True\n    while stones:\n        stones -= 1 if stones == 1 else rng.choice((1, 2))\n        winner_is_to_move = not winner_is_to_move\n    return not winner_is_to_move\n\nroot = Node(4)\nfor _ in range(300):\n    node = root\n    while not node.untried and node.children:\n        node = max(node.children, key=lambda c: c.wins \/ c.visits + 1.4 * math.sqrt(math.log(node.visits) \/ c.visits))\n    if node.untried:\n        take = node.untried.pop()\n        child = Node(node.stones - take, node, take)\n        node.children.append(child)\n        node = child\n    mover_wins = True if node.stones == 0 else (not rollout(node.stones))\n    while node is not None:\n        node.visits += 1\n        if mover_wins:\n            node.wins += 1\n        mover_wins = not mover_wins\n        node = node.parent\nchoice = max(root.children, key=lambda c: c.visits)\nassert choice.move == 1\nprint(choice.move)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>AlphaGo<\/strong> and later board-game engines select moves with Monte Carlo tree search: many random playouts, biased toward moves that have won before. Classical chess engines use alpha-beta instead. The tree is grown where the playouts are promising, not uniformly.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop MCTS when the game is small enough for alpha-beta with a real evaluation to finish. Keep it when the branching factor defeats minimax and a rollout carries information, and play the most visited move.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Monte Carlo tree search returns the UCT selection rule and the reason the played move is the most visited child. <strong>Monte Carlo tree search grows a game tree by UCT selection, rollout, and backup, and after a finite budget it plays the most visited move rather than a proven minimax move.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Monte Carlo tree search grows a game tree by repeating selection, expansion, rollout, and backup. Selection walks from the root by a bandit rule, usually UCT: choose the child with the best win rate plus c \u00b7 sqrt(ln N \/ n), where N is the parent&#8217;s visit&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,39],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-games"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":627,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions\/627"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}