Author SHA1 Message Date
rektdieandclaude b7bfb0b916 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 <noreply@anthropic.com>
2026-11-16 21:36:02 +01:00
2 changed files with 33 additions and 29 deletions

No files matched your search

+27 -12
View File
@@ -486,8 +486,6 @@ void Searcher::verify_tree(const std::vector<u64>& 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 (!std::isfinite(e.q) || std::abs(e.q) > 1.0f) fail(idx, pos, "edge q out of range");
if (!e.child && e.q != 0.0f) fail(idx, pos, "edge q without a child");
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;
@@ -594,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;
@@ -622,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 ? cached_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;
@@ -640,7 +657,7 @@ void Searcher::recompute_q(Node& node) const {
for (u32 i = 0; i < node.num_edges; i++) {
const Edge& e = tree_.edge(node.first_edge + i);
const u32 w = e.visits - e.draws;
if (w) sum -= double(w) * double(e.q);
if (w) sum -= double(w) * double(tree_.node(e.child).q);
}
node.q = float(sum / double(node.visits));
}
@@ -673,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);
@@ -746,8 +763,6 @@ void Searcher::playout() {
Edge& e = tree_.edge(edge_path_[j]);
e.visits++;
if (leaf == PATH_DRAW && j + 1 == edge_path_.size()) e.draws++;
// The child is final for this playout (updated in the step before).
if (e.child) e.q = tree_.node(e.child).q;
butterfly_.update(root_pos_.stm ^ int(j & 1), Move::from_raw(e.move), bonus);
Node& node = tree_.node(path_[j]);
@@ -976,8 +991,8 @@ struct NodeSnapshot {
u32 base;
u8 state;
u16 check;
// (move, policy, visits, draws, child's key or 0, cached q) per edge
std::vector<std::tuple<u16, u16, u32, u32, u64, float>> edges;
// (move, policy, visits, draws, child's key or 0) per edge
std::vector<std::tuple<u16, u16, u32, u32, u64>> edges;
};
} // namespace
@@ -1036,7 +1051,7 @@ void Searcher::self_test() {
NodeSnapshot s{n.key, n.visits, n.q, n.u, n.base, n.state, n.check, {}};
for (u32 i = 0; i < n.num_edges; i++) {
const Edge& e = tree_.edge(n.first_edge + i);
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q);
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0);
if (e.child && !seen[e.child]) {
seen[e.child] = 1;
stack.push_back(e.child);
@@ -1081,7 +1096,7 @@ void Searcher::self_test() {
if (n.num_edges && idx != tree_.root()) kept_min = std::min(kept_min, n.visits);
for (u32 i = 0; i < n.num_edges; i++) {
const Edge& e = tree_.edge(n.first_edge + i);
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q) !=
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0) !=
s.edges[i])
self_test_failed("edge changed");
if (e.child && !seen[e.child]) {
+6 -17
View File
@@ -41,17 +41,12 @@ struct Edge {
u32 child = 0; // node index, 0 = not linked yet
u32 visits = 0; // N(n,a): how often search chose this move here
u32 draws = 0; // D(n,a): of those, playouts that ended in a draw of the path after this move
// The child's Q as of the last playout through this edge (plan variant 5b).
// Selection and the parent's recompute read it instead of the child, so
// they only touch the parent's contiguous edges. It goes stale when the
// child is updated through another parent, until a playout passes here.
float q = 0.0f;
u16 move = 0;
u16 policy = 0; // prior scaled to 0..65535
float prior() const { return float(policy) * (1.0f / 65535.0f); }
};
static_assert(sizeof(Edge) == 20);
static_assert(sizeof(Edge) == 16);
// A position, created (and evaluated) when search first reaches it.
struct Node {
@@ -76,9 +71,8 @@ u16 position_check(const Position& pos);
// check, about 80 bits.
class Tree {
public:
// Edges per node the arenas are sized for: with 20-byte edges the edge
// arena gets 160 of every 192 bytes, the node arena 32 (plus the table).
// Measured in step 1 of the MCGS plan
// Edges per node the arenas are sized for: the edge arena gets 128 of
// every 160 bytes, the node arena 32. Measured in step 1 of the MCGS plan
// (docs/mcgs-log.md, edges per visited node): 13 on average in 5 s
// searches without collections, 19-21 in middlegames, 3.6-5.5 in simple
// endgames, and 3-9 right after collections (pruned nodes keep their node
@@ -337,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;
@@ -372,12 +367,6 @@ private:
}
// The same, 0 when the edge is unvisited.
float edge_q(const Edge& e) const { return e.visits ? visited_edge_q(e) : 0.0f; }
// The same from the Q the edge last saw (selection and recompute).
static float cached_edge_q(const Edge& e) {
if (!e.draws) return e.q;
const u32 w = e.visits - e.draws;
return w ? float(w) / float(e.visits) * e.q : 0.0f;
}
void print_info() const;
std::vector<Move> principal_variation() const;
void draw_dashboard(bool done);