Monte-Carlo graph search (MCGS steps 2, 3 and 3b) #23

Merged
claude merged 19 commits from mcgs-step3-reroot into main 2026-11-16 23:18:22 +01:00
2 changed files with 46 additions and 22 deletions
Showing only changes of commit d38f0b133a - Show all commits

No files matched your search

+39 -17
View File
@@ -106,8 +106,13 @@ u32 Tree::alloc_edges(u32 count) {
} }
size_t Tree::reroot(u32 new_root) { size_t Tree::reroot(u32 new_root) {
if (new_root == root_) return nodes_used_ - 1; // Only the root moves. Packing what stays reachable would cost O(graph) on
return compact(new_root, 0); // every move, because in a reversible endgame almost the whole graph stays
// reachable from the new root. Nodes that are no longer reachable keep
// their memory and their table entries (search may link to them again)
// until the next collection, which keeps only what the root reaches.
root_ = new_root;
return nodes_used_ - 1;
} }
size_t Tree::collect_garbage(double keep_fraction) { size_t Tree::collect_garbage(double keep_fraction) {
@@ -406,9 +411,12 @@ size_t Searcher::reuse_tree(const Position& root) {
// history `history` (ending with the root's key). Each node is checked once, // history `history` (ending with the root's key). Each node is checked once,
// with the position of the first path that reaches it (positions do not // with the position of the first path that reaches it (positions do not
// depend on the path, apart from the clocks): // depend on the path, apart from the clocks):
// - every node is reachable, and the arenas hold nothing else; // - right after a collection every node is reachable, and the arenas hold
// nothing else; at other times a reroot may have left nodes the root no
// longer reaches (they wait for the next collection);
// - a node's key and check match its position, and the table finds it there; // - a node's key and check match its position, and the table finds it there;
// the table has one entry per node and nothing else; // the table has one entry per node in the arena, reachable or not, and
// nothing else;
// - its state is what the position itself decides: checkmate, stalemate or // - its state is what the position itself decides: checkmate, stalemate or
// insufficient material (not at the root, nor at an earlier root, i.e. a // insufficient material (not at the root, nor at an earlier root, i.e. a
// position of the game history). Repetitions and the 50-move rule never // position of the game history). Repetitions and the 50-move rule never
@@ -418,7 +426,7 @@ size_t Searcher::reuse_tree(const Position& root) {
// a visit that was not a draw; // a visit that was not a draw;
// - N = base + the edges' visits; a node without edges has q = u; // - N = base + the edges' visits; a node without edges has q = u;
// - Q and u are finite and in [-1, 1]. // - Q and u are finite and in [-1, 1].
void Searcher::verify_tree(const std::vector<u64>& history) const { void Searcher::verify_tree(const std::vector<u64>& history, bool collected) const {
const auto fail = [&](u32 idx, const Position& pos, const std::string& what) { const auto fail = [&](u32 idx, const Position& pos, const std::string& what) {
const Node& n = tree_.node(idx); const Node& n = tree_.node(idx);
std::cerr << "GRAPH CORRUPT at node " << idx << " (" << pos.fen() << "): " << what << " visits=" << n.visits std::cerr << "GRAPH CORRUPT at node " << idx << " (" << pos.fen() << "): " << what << " visits=" << n.visits
@@ -493,10 +501,11 @@ void Searcher::verify_tree(const std::vector<u64>& history) const {
fail(idx, pos, "visits != base + edge visits " + std::to_string(edge_visits)); fail(idx, pos, "visits != base + edge visits " + std::to_string(edge_visits));
} }
if (nodes != tree_.nodes_used() - 1 || edges != tree_.edges_used() - 1) const u64 all_nodes = tree_.nodes_used() - 1, all_edges = tree_.edges_used() - 1;
if (collected ? nodes != all_nodes || edges != all_edges : nodes > all_nodes || edges > all_edges)
fail(root, root_pos_, fail(root, root_pos_,
"unreachable nodes or edges: reached " + std::to_string(nodes) + " nodes, " + std::to_string(edges) + "unreachable nodes or edges: reached " + std::to_string(nodes) + " nodes, " + std::to_string(edges) +
" edges"); " edges of " + std::to_string(all_nodes) + " and " + std::to_string(all_edges));
// The table: one entry per node, each under its node's key. // The table: one entry per node, each under its node's key.
size_t entries = 0; size_t entries = 0;
@@ -507,12 +516,13 @@ void Searcher::verify_tree(const std::vector<u64>& history) const {
if (n >= tree_.nodes_used() || tree_.table_key_hi(s) != u32(tree_.node(n).key >> 32)) if (n >= tree_.nodes_used() || tree_.table_key_hi(s) != u32(tree_.node(n).key >> 32))
fail(root, root_pos_, "table slot " + std::to_string(s) + " points to a wrong node"); fail(root, root_pos_, "table slot " + std::to_string(s) + " points to a wrong node");
} }
if (entries != nodes || tree_.table_entries() != nodes) if (entries != all_nodes || tree_.table_entries() != all_nodes)
fail(root, root_pos_, fail(root, root_pos_,
"table has " + std::to_string(entries) + " entries for " + std::to_string(nodes) + " nodes"); "table has " + std::to_string(entries) + " entries for " + std::to_string(all_nodes) + " nodes");
uci_io::send("info string tree verified: " + std::to_string(nodes) + " nodes, " + std::to_string(edges) + uci_io::send("info string tree verified: " + std::to_string(nodes) + " nodes, " + std::to_string(edges) +
" edges, " + std::to_string(entries) + " table entries"); " edges reachable, " + std::to_string(all_nodes) + " nodes in the arena, " +
std::to_string(entries) + " table entries");
} }
#endif #endif
@@ -910,7 +920,8 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
gc_pending_ = false; gc_pending_ = false;
gc_runs_++; gc_runs_++;
#ifdef RIGBY_VERIFY_TREE #ifdef RIGBY_VERIFY_TREE
verify_tree(std::vector<u64>(history_.begin(), history_.begin() + std::ptrdiff_t(root_history_size_))); verify_tree(std::vector<u64>(history_.begin(), history_.begin() + std::ptrdiff_t(root_history_size_)),
true);
#endif #endif
} }
if (print && (iterations_ & info_mask) == 0) { if (print && (iterations_ & info_mask) == 0) {
@@ -1104,7 +1115,7 @@ void Searcher::self_test() {
const size_t count = compare(before, true, pruned_max, kept_min); const size_t count = compare(before, true, pruned_max, kept_min);
if (count != tree_.nodes_used() - 1) self_test_failed("node count differs after collection"); if (count != tree_.nodes_used() - 1) self_test_failed("node count differs after collection");
if (pruned_max >= kept_min) self_test_failed("pruned a node with more visits than one kept"); if (pruned_max >= kept_min) self_test_failed("pruned a node with more visits than one kept");
verify_tree({root.key}); verify_tree({root.key}, true);
uci_io::send("info string verify (b) gc " + std::string(fen) + ": " + std::to_string(before.size()) + uci_io::send("info string verify (b) gc " + std::string(fen) + ": " + std::to_string(before.size()) +
" -> " + std::to_string(count) + " nodes ok"); " -> " + std::to_string(count) + " nodes ok");
uci_io::send("info string verify (d) table after gc: " + std::to_string(tree_.table_entries()) + uci_io::send("info string verify (d) table after gc: " + std::to_string(tree_.table_entries()) +
@@ -1127,16 +1138,27 @@ void Searcher::self_test() {
} }
if (idx == tree_.root() || tree_.node(idx).state != EXPANDED) self_test_failed("no node to reroot to"); if (idx == tree_.root() || tree_.node(idx).state != EXPANDED) self_test_failed("no node to reroot to");
const auto before = snapshot(idx); const auto before = snapshot(idx);
const size_t nodes_before = tree_.nodes_used(), edges_before = tree_.edges_used();
tree_.reroot(idx); tree_.reroot(idx);
// A reroot only moves the root: everything stays where it was.
u32 pruned_max, kept_min; u32 pruned_max, kept_min;
const size_t count = compare(before, false, pruned_max, kept_min); const size_t count = compare(before, false, pruned_max, kept_min);
if (count != before.size() || count != tree_.nodes_used() - 1) self_test_failed("reroot lost nodes"); if (count != before.size() || tree_.nodes_used() != nodes_before || tree_.edges_used() != edges_before)
self_test_failed("reroot changed the graph");
root_pos_ = pos; root_pos_ = pos;
verify_tree(history); verify_tree(history);
uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(count) + // The next collection (with room for everything) keeps exactly what the
" nodes ok"); // new root reaches, unchanged.
uci_io::send("info string verify (d) table after reroot: " + std::to_string(tree_.table_entries()) + tree_.collect_garbage(tree_.node_capacity(), tree_.edge_capacity());
" entries for " + std::to_string(count) + " nodes ok"); 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");
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 " +
std::to_string(kept) + " ok");
uci_io::send("info string verify (d) table after reroot and collection: " +
std::to_string(tree_.table_entries()) + " entries for " + std::to_string(kept) + " nodes ok");
} }
// (c) Draws of the path never leak into nodes: search positions where // (c) Draws of the path never leak into nodes: search positions where
+7 -5
View File
@@ -94,9 +94,9 @@ public:
u32 find(u64 key, u16 check) const; u32 find(u64 key, u16 check) const;
// Enters a node, which must not be in the table yet, by its key. // Enters a node, which must not be in the table yet, by its key.
void insert(u32 idx); void insert(u32 idx);
// Makes `new_root` the root and packs what is reachable from it to the // Makes `new_root` the root. Nothing is moved or dropped: what the new
// front of the arenas; everything else is dropped. Returns the number of // root no longer reaches goes at the next collection. Returns the number
// nodes kept. // of nodes in the graph.
size_t reroot(u32 new_root); size_t reroot(u32 new_root);
// Frees memory when an arena is full: the least visited parts of the graph // Frees memory when an arena is full: the least visited parts of the graph
// are cut back to leaves until at most `keep_fraction` of each arena is in // are cut back to leaves until at most `keep_fraction` of each arena is in
@@ -337,7 +337,8 @@ private:
void recompute_q(Node& node) const; void recompute_q(Node& node) const;
bool is_repetition(const Position& pos) const; bool is_repetition(const Position& pos) const;
// Finds the node for `root` in the previous graph and makes it the root. // Finds the node for `root` in the previous graph and makes it the root.
// Returns the number of nodes kept, 0 when nothing could be reused. // Returns the number of nodes in the graph (some may no longer be
// reachable), 0 when nothing could be reused.
size_t reuse_tree(const Position& root); size_t reuse_tree(const Position& root);
// True when the root node's edges are exactly the legal moves of `root`. // True when the root node's edges are exactly the legal moves of `root`.
bool root_edges_valid(const Position& root) const; bool root_edges_valid(const Position& root) const;
@@ -345,7 +346,8 @@ private:
void clear_tree(const Position& root); void clear_tree(const Position& root);
#ifdef RIGBY_VERIFY_TREE #ifdef RIGBY_VERIFY_TREE
// Checks the graph from root_pos_; `history` ends with the root's key. // Checks the graph from root_pos_; `history` ends with the root's key.
void verify_tree(const std::vector<u64>& history) const; // `collected`: right after a collection, when every node must be reachable.
void verify_tree(const std::vector<u64>& history, bool collected = false) const;
#endif #endif
void compute_time_limit(const SearchLimits& limits); void compute_time_limit(const SearchLimits& limits);