From b7bfb0b916dcc859574cc99c32c2bf3b0554f14d Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 16 Nov 2026 21:27:06 +0100 Subject: [PATCH] mcts: score an edge that repeats the current path as a draw in selection 26.8 % of playouts at 5 s (without the perpetual-check position) end in a draw of their path, 30 % in KR v K, where the root score falls to 739 cp against main's 1701: a shared node's Q says nothing about whether going there now repeats this path. Selection now gives Q = 0 to an edge whose child position already occurred on this playout's path or in the game, with the same side to move, within the reversible window. The window keys are collected once per selection (3, 5, ... plies back, up to the half-move clock) behind a 64-bit filter, so most children need no search. The exploration term is unchanged, and if the edge is still chosen the draw is recorded as before. The bench changes because selection avoids repetitions. 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. Bench: 755448 Co-Authored-By: Claude Opus 5.5 --- src/mcts.cpp | 25 ++++++++++++++++++++++--- src/mcts.h | 5 +++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/mcts.cpp b/src/mcts.cpp index d28c69d..a9c0227 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -592,9 +592,27 @@ bool Searcher::expand(u32 idx, const Position& pos) { return true; } -u32 Searcher::select_edge(u32 idx) const { +u32 Searcher::select_edge(u32 idx, const Position& pos) const { const Node& node = tree_.node(idx); + // Repetitions of this path: a child whose position already occurred on + // this playout's path or in the game, with the same side to move and + // within the reversible window, would be a draw if chosen now, whatever + // its Q says about other paths. Such an edge scores Q = 0. The window + // keys are those history_ holds 3, 5, ... plies before `pos` (4, 6, ... + // before the child), up to pos.halfmove. A 64-bit filter skips the exact + // search for most children. + const int n_hist = int(history_.size()); + const int max_back = std::min(pos.halfmove, n_hist - 1); + u64 window_filter = 0; + for (int back = 3; back <= max_back; back += 2) window_filter |= 1ULL << (history_[n_hist - 1 - back] & 63); + const auto repeats = [&](u64 key) { + if (!(window_filter & (1ULL << (key & 63)))) return false; + for (int back = 3; back <= max_back; back += 2) + if (history_[n_hist - 1 - back] == key) return true; + return false; + }; + // node.q is from the view of the side that moved into this node, so the side // to move here sees -q. Visits are N(n), the node's own evaluation included. const float parent_q = node.visits ? -node.q : 0.0f; @@ -620,7 +638,8 @@ u32 Searcher::select_edge(u32 idx) const { const u32 c = node.first_edge + i; const Edge& e = tree_.edge(c); // The child's Q is from the view of the side to move here. - const float q = e.visits ? visited_edge_q(e) : fpu; + float q = e.visits ? visited_edge_q(e) : fpu; + if (window_filter && e.child && repeats(tree_.node(e.child).key)) q = 0.0f; const float score = q + explore * e.prior() / float(1 + e.visits); if (score > best_score) { best_score = score; @@ -671,7 +690,7 @@ void Searcher::playout() { break; } - const u32 e = select_edge(idx); + const u32 e = select_edge(idx, pos); Edge& edge = tree_.edge(e); pos.make_move(Move::from_raw(edge.move)); history_.push_back(pos.key); diff --git a/src/mcts.h b/src/mcts.h index a619c20..a8f47e7 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -331,8 +331,9 @@ private: // Second visit: creates the edges with their priors. False when the // arena is full (a collection is then scheduled). bool expand(u32 idx, const Position& pos); - // The edge PUCT picks at node `idx`. - u32 select_edge(u32 idx) const; + // The edge PUCT picks at node `idx`, whose position is `pos` (the last + // entry of history_). + u32 select_edge(u32 idx, const Position& pos) const; // A node's Q from its own value and its children's (see Edge). void recompute_q(Node& node) const; bool is_repetition(const Position& pos) const;