Expand nodes on their second visit #3

Merged
claude merged 1 commits from expand-second-visit-nn into main 2026-11-15 10:41:04 +01:00
2 changed files with 40 additions and 10 deletions

No files matched your search

+35 -9
View File
@@ -199,7 +199,10 @@ void Searcher::verify_tree() const {
next.make_move(m);
stack.push_back({node.first_child + c, next});
}
if (!ok || node.visits < child_visits + 1) {
// The root is expanded before its first visit, so it may have exactly
// as many visits as its children.
const u64 own_visit = item.idx == Tree::root() ? 0 : 1;
if (!ok || node.visits < child_visits + own_visit) {
std::cerr << "TREE CORRUPT at node " << item.idx << " (" << item.pos.fen() << ") ok=" << ok
<< " visits=" << node.visits << " child_visits=" << child_visits
<< " children=" << int(node.num_children) << " legal=" << legal.size()
@@ -211,7 +214,7 @@ void Searcher::verify_tree() const {
}
#endif
float Searcher::expand(u32 idx, const Position& pos, int depth) {
float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) {
Node& node = tree_[idx];
MoveList moves;
@@ -228,11 +231,20 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) {
return 0.0f;
}
return eval::value(pos);
}
bool Searcher::expand(u32 idx, const Position& pos) {
Node& node = tree_[idx];
MoveList moves;
generate_legal(pos, moves);
const u32 first = tree_.alloc(u32(moves.size()));
if (!first) {
// Out of memory: keep the node as a leaf and just evaluate it again.
// Out of memory: the node stays a leaf and is evaluated again.
tree_full_ = true;
return eval::value(pos);
return false;
}
float priors[256];
@@ -248,8 +260,7 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) {
node.first_child = first;
node.num_children = u8(moves.size());
node.state = EXPANDED;
return eval::value(pos);
return true;
}
u32 Searcher::select_child(u32 idx) const {
@@ -304,8 +315,16 @@ void Searcher::playout() {
break;
}
if (node.state == UNEXPANDED) {
value = expand(idx, pos, depth);
break;
// Most leaves are only ever visited once, so a first visit just
// evaluates; children (moves, priors, memory) come on the second.
if (node.visits == 0) {
value = evaluate_leaf(idx, pos, depth);
break;
}
if (!expand(idx, pos)) {
value = eval::value(pos);
break;
}
}
idx = select_child(idx);
@@ -409,8 +428,15 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
return result;
}
// The root needs its children before the first playout, so that even a
// single playout leaves a move to play.
if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) {
tree_.clear();
tree_full_ = false;
expand(Tree::root(), root);
}
i64 last_info = 0;
// Always run at least one playout so the root gets expanded.
do {
playout();
if (print && (iterations_ & 1023) == 0) {
+5 -1
View File
@@ -117,7 +117,11 @@ private:
bool tree_full_ = false;
void playout();
float expand(u32 idx, const Position& pos, int depth);
// First visit of a node: settles terminal states, otherwise evaluates.
float evaluate_leaf(u32 idx, const Position& pos, int depth);
// Second visit: creates the children with their priors. False when the
// arena is full (a collection is then scheduled).
bool expand(u32 idx, const Position& pos);
u32 select_child(u32 idx) const;
bool is_repetition(const Position& pos) const;
// Finds the node for `root` in the previous tree and makes it the root.