diff --git a/src/mcts.cpp b/src/mcts.cpp index ffdd7db..2aed98c 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -1007,6 +1007,8 @@ void Searcher::self_test() { // (b) Garbage collection and rerooting keep everything they keep exactly. // A node is identified by its position's key, which is unique in the graph. + // (d) After each of them, verify_tree also checks the table against the + // surviving nodes. const auto snapshot = [&](u32 from) { std::map snap; std::vector seen(tree_.nodes_used(), 0); @@ -1105,6 +1107,8 @@ void Searcher::self_test() { verify_tree({root.key}); uci_io::send("info string verify (b) gc " + std::string(fen) + ": " + std::to_string(before.size()) + " -> " + std::to_string(count) + " nodes ok"); + uci_io::send("info string verify (d) table after gc: " + std::to_string(tree_.table_entries()) + + " entries for " + std::to_string(count) + " nodes ok"); } // Reroot two plies down the most visited line, as between two moves of a game. @@ -1131,7 +1135,74 @@ void Searcher::self_test() { verify_tree(history); uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(count) + " nodes ok"); + uci_io::send("info string verify (d) table after reroot: " + std::to_string(tree_.table_entries()) + + " entries for " + std::to_string(count) + " nodes ok"); } + + // (c) Draws of the path never leak into nodes: search positions where + // repetitions or the 50-move rule are forced or frequent. verify_tree (at + // the end of each search) recomputes every node's state from its position + // alone; here the draws must also show up on edges. + struct RepetitionCase { + const char* fen; + const char* moves; + bool fifty_move; // draws by the 50-move rule must appear + }; + static constexpr RepetitionCase REPETITION_CASES[] = { + {"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "g1f3 g8f6 f3g1 f6g8 g1f3 g8f6", false}, + {"6k1/6p1/8/6KQ/1r6/q2b4/8/8 w - - 0 32", "", false}, + {"8/8/8/4k3/8/8/2KR4/8 w - - 95 100", "", true}, + {"8/8/3k4/2p1p3/2P1P3/3K4/8/8 w - - 0 1", "", false}, + {"8/5pk1/6p1/7p/7P/6P1/5PK1/8 w - - 0 40", "", false}, + }; + for (const RepetitionCase& c : REPETITION_CASES) { + Position root; + if (!root.set_fen(c.fen)) self_test_failed(std::string("bad fen ") + c.fen); + std::vector history{root.key}; + std::istringstream moves(c.moves); + for (std::string uci; moves >> uci;) { + const Move m = root.parse_uci(uci); + if (!m) self_test_failed("bad move " + uci); + root.make_move(m); + history.push_back(root.key); + } + SearchLimits limits; + limits.nodes = 30000; + new_game(); + stop_flag = false; + search(root, history, limits, false); // ends with verify_tree(history) + + u64 draw_edges = 0, draw_visits = 0, fifty_edges = 0; + std::vector seen(tree_.nodes_used(), 0); + std::vector> stack{{tree_.root(), root}}; + seen[tree_.root()] = 1; + while (!stack.empty()) { + const auto [idx, pos] = stack.back(); + stack.pop_back(); + const Node& n = tree_.node(idx); + for (u32 i = 0; i < n.num_edges; i++) { + const Edge& e = tree_.edge(n.first_edge + i); + Position next = pos; + next.make_move(Move::from_raw(e.move)); + if (e.draws) { + draw_edges++; + draw_visits += e.draws; + // The clock of the first path that reached the node. + if (next.halfmove >= 100) fifty_edges++; + } + if (e.child && !seen[e.child]) { + seen[e.child] = 1; + stack.push_back({e.child, next}); + } + } + } + if (!draw_visits) self_test_failed(std::string("no draws of the path recorded on edges: ") + c.fen); + if (c.fifty_move && !fifty_edges) self_test_failed(std::string("no 50-move draws on edges: ") + c.fen); + uci_io::send("info string verify (c) " + std::string(c.fen) + (*c.moves ? " moves " : "") + c.moves + + ": " + std::to_string(draw_visits) + " draw visits on " + std::to_string(draw_edges) + + " edges (" + std::to_string(fifty_edges) + " at half-move 100+), node states ok"); + } + new_game(); uci_io::send("info string verify passed"); }