From 79fd0173482e4008abb9360640dbc1b410fa706e Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 16 Nov 2026 21:28:14 +0100 Subject: [PATCH] mcts: reconcile the root's draw counts after reuse (MCGS plan 3.6.7) A reused root's edges carry draw counts collected under other game histories, while it was an inner node. Once it is the root, the game history is common to every path, so a root move either draws (repetition or 50-move rule, mate excepted) on every path or on none. After reuse each root edge becomes all draws or none: a drawn edge keeps its visits as draws, an edge that no longer draws loses its draw visits, and so does the root, whose Q is then recomputed. UCI prints "info string reconciled N root edges" when N > 0. In reuse sequences from KR v K and two pawn endgames, 1 to 8 root edges were reconciled on most moves. The bench is unchanged: it never reuses a graph. Checked: zero warnings; VERIFY build: `verify` (a)-(d), the VERIFY searches of plan section 6 with forced GC at Hash 1 and 2, the reuse sequence at Hash 64, Hash 1 and from the 960 start, and the go nodes 1/2, go depth 1 smoke positions. The VERIFY build also checks that every visited root edge is all draws exactly when its move draws, after every reuse, collection and search. Bench: 764208 Co-Authored-By: Claude Opus 5.5 --- src/mcts.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/mcts.h | 8 +++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/mcts.cpp b/src/mcts.cpp index d28c69d..4d4495a 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -259,6 +259,46 @@ size_t Tree::compact(u32 new_root, u32 min_visits) { Searcher::Searcher() { set_hash(64); } +bool Searcher::root_child_drawn(const Position& child, const std::vector& keys) { + const int n = int(keys.size()); + for (int back = 4; back <= child.halfmove && back < n; back += 2) + if (keys[n - 1 - back] == child.key) return true; + return child.halfmove >= 100 && !(child.in_check() && !has_legal_move(child)); +} + +int Searcher::reconcile_root_edges(const Position& root) { + // Draw counts on the root's edges were collected under other game + // histories (the root was an inner node then). Now the history is common + // to every path: a move into a repetition (or past the 50-move rule) + // draws on every path, any other move on none. A drawn edge keeps its + // visits, all of them draws; an edge that is no longer drawn loses the + // draw visits, and so does the root. + Node& r = tree_.node(tree_.root()); + if (r.state != EXPANDED) return 0; + int changed = 0; + std::vector keys = history_; + for (u32 i = 0; i < r.num_edges; i++) { + Edge& e = tree_.edge(r.first_edge + i); + if (!e.visits) continue; + Position child = root; + child.make_move(Move::from_raw(e.move)); + keys.push_back(child.key); + const bool drawn = root_child_drawn(child, keys); + keys.pop_back(); + if (drawn && e.draws != e.visits) { + e.draws = e.visits; + changed++; + } else if (!drawn && e.draws) { + r.visits -= e.draws; + e.visits -= e.draws; + e.draws = 0; + changed++; + } + } + if (changed) recompute_q(r); + return changed; +} + void Searcher::clear_tree(const Position& root) { // The root is created like any node and evaluated once; its own value // takes part in its Q with weight 1, as at every other node. @@ -486,6 +526,17 @@ void Searcher::verify_tree(const std::vector& history, bool collected) cons if (it == legal.end() || legal_seen[it - legal.begin()]) fail(idx, pos, "illegal or repeated edge move"); legal_seen[it - legal.begin()] = true; if (e.draws > e.visits) fail(idx, pos, "edge with more draws than visits"); + if (idx == root && e.visits) { + // The game history is common to every path from the root, so a + // root edge is all draws or none. + Position child = pos; + child.make_move(Move::from_raw(e.move)); + std::vector keys = history; + keys.push_back(child.key); + if (e.draws != (root_child_drawn(child, keys) ? e.visits : 0)) + fail(idx, pos, "root edge " + pos.uci(Move::from_raw(e.move)) + " has " + + std::to_string(e.draws) + " draws of " + std::to_string(e.visits)); + } if (e.child >= tree_.nodes_used()) fail(idx, pos, "edge child out of range"); if (e.visits > e.draws && !e.child) fail(idx, pos, "edge with non-draw visits and no child"); edge_visits += e.visits; @@ -864,10 +915,13 @@ SearchResult Searcher::search(const Position& root, const std::vector& hist // rests on the 80-bit key and check; the root is checked in full. if (reused && !root_edges_valid(root)) reused = 0; if (!reused) clear_tree(root); + const int reconciled = reused ? reconcile_root_edges(root) : 0; #ifdef RIGBY_VERIFY_TREE if (reused) verify_tree(history_); #endif if (print && !pretty && reused) uci_io::send("info string reused " + std::to_string(reused) + " nodes"); + if (print && !pretty && reconciled) + uci_io::send("info string reconciled " + std::to_string(reconciled) + " root edges"); iterations_ = 0; depth_sum_ = 0; seldepth_ = 0; @@ -1145,14 +1199,16 @@ void Searcher::self_test() { const size_t count = compare(before, false, pruned_max, kept_min); if (count != before.size() || tree_.nodes_used() != nodes_before || tree_.edges_used() != edges_before) self_test_failed("reroot changed the graph"); - root_pos_ = pos; - verify_tree(history); // The next collection (with room for everything) keeps exactly what the // new root reaches, unchanged. tree_.collect_garbage(tree_.node_capacity(), tree_.edge_capacity()); const size_t kept = compare(before, false, pruned_max, kept_min); if (kept != before.size() || kept != tree_.nodes_used() - 1) self_test_failed("collection after a reroot kept the wrong nodes"); + // As search does after reuse: the root's edges follow the game history. + root_pos_ = pos; + history_ = history; + reconcile_root_edges(pos); verify_tree(history, true); uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(nodes_before - 1) + " nodes, " + std::to_string(count) + " reachable from the new root, collected to " + diff --git a/src/mcts.h b/src/mcts.h index a619c20..18272ff 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -342,6 +342,14 @@ private: size_t reuse_tree(const Position& root); // True when the root node's edges are exactly the legal moves of `root`. bool root_edges_valid(const Position& root) const; + // After reuse the game history is common to every path, so whether a + // root move is a draw by repetition or the 50-move rule is exact. Makes + // each root edge all draws or no draws accordingly (plan 3.6.7). Returns + // the number of edges changed. + int reconcile_root_edges(const Position& root); + // Whether `child`, reached from the root, is a draw of the game history + // `keys` (which ends with the child's key). + static bool root_child_drawn(const Position& child, const std::vector& keys); // Empties the graph, leaving an evaluated, unexpanded root for `root`. void clear_tree(const Position& root); #ifdef RIGBY_VERIFY_TREE