From cdeb318016e17ee7e8a1f4a94a268c72012ede0c Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 03:29:07 +0100 Subject: [PATCH] feat: expand nodes on their second visit Most leaves are visited only once. Their first visit now only settles terminal states and evaluates the position; the move list, the priors and the child nodes are created when search comes back a second time. The root is expanded before the first playout, so a search always has a move. With the value network the tree is full on nearly every move at 8+0.08 with Hash=64 (hashfull 1000 in a 30-ply self-play diagnostic); with this change many moves stay at 320-890. The -DRIGBY_VERIFY_TREE build passed 1700 tree checks in UCI-driven self-play with 1 MB and 16 MB Hash, and a 1-playout search still returns a move. Rebased from the earlier expand-second-visit branch onto the network main, without the tree collection it was built on. Bench: 546018 Co-Authored-By: Claude Opus 5.5 --- src/mcts.cpp | 44 +++++++++++++++++++++++++++++++++++--------- src/mcts.h | 6 +++++- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/mcts.cpp b/src/mcts.cpp index 5fa3f50..bb3c089 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -199,7 +199,10 @@ void Searcher::verify_tree() const { next.make_move(m); stack.push_back({node.first_child + c, next}); } - if (!ok || node.visits < child_visits + 1) { + // The root is expanded before its first visit, so it may have exactly + // as many visits as its children. + const u64 own_visit = item.idx == Tree::root() ? 0 : 1; + if (!ok || node.visits < child_visits + own_visit) { std::cerr << "TREE CORRUPT at node " << item.idx << " (" << item.pos.fen() << ") ok=" << ok << " visits=" << node.visits << " child_visits=" << child_visits << " children=" << int(node.num_children) << " legal=" << legal.size() @@ -211,7 +214,7 @@ void Searcher::verify_tree() const { } #endif -float Searcher::expand(u32 idx, const Position& pos, int depth) { +float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) { Node& node = tree_[idx]; MoveList moves; @@ -228,11 +231,20 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) { return 0.0f; } + return eval::value(pos); +} + +bool Searcher::expand(u32 idx, const Position& pos) { + Node& node = tree_[idx]; + + MoveList moves; + generate_legal(pos, moves); + const u32 first = tree_.alloc(u32(moves.size())); if (!first) { - // Out of memory: keep the node as a leaf and just evaluate it again. + // Out of memory: the node stays a leaf and is evaluated again. tree_full_ = true; - return eval::value(pos); + return false; } float priors[256]; @@ -248,8 +260,7 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) { node.first_child = first; node.num_children = u8(moves.size()); node.state = EXPANDED; - - return eval::value(pos); + return true; } u32 Searcher::select_child(u32 idx) const { @@ -304,8 +315,16 @@ void Searcher::playout() { break; } if (node.state == UNEXPANDED) { - value = expand(idx, pos, depth); - break; + // Most leaves are only ever visited once, so a first visit just + // evaluates; children (moves, priors, memory) come on the second. + if (node.visits == 0) { + value = evaluate_leaf(idx, pos, depth); + break; + } + if (!expand(idx, pos)) { + value = eval::value(pos); + break; + } } idx = select_child(idx); @@ -409,8 +428,15 @@ SearchResult Searcher::search(const Position& root, const std::vector& hist return result; } + // The root needs its children before the first playout, so that even a + // single playout leaves a move to play. + if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) { + tree_.clear(); + tree_full_ = false; + expand(Tree::root(), root); + } + i64 last_info = 0; - // Always run at least one playout so the root gets expanded. do { playout(); if (print && (iterations_ & 1023) == 0) { diff --git a/src/mcts.h b/src/mcts.h index ed8c4fe..abe53a5 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -117,7 +117,11 @@ private: bool tree_full_ = false; void playout(); - float expand(u32 idx, const Position& pos, int depth); + // First visit of a node: settles terminal states, otherwise evaluates. + float evaluate_leaf(u32 idx, const Position& pos, int depth); + // Second visit: creates the children with their priors. False when the + // arena is full (a collection is then scheduled). + bool expand(u32 idx, const Position& pos); u32 select_child(u32 idx) const; bool is_repetition(const Position& pos) const; // Finds the node for `root` in the previous tree and makes it the root. -- 2.47.3