Smart pruning stops clock searches whose best move is settled #14

Merged
claude merged 1 commits from smart-pruning into main 2026-11-15 15:57:38 +01:00
2 changed files with 50 additions and 1 deletions

No files matched your search

+43 -1
View File
@@ -137,6 +137,7 @@ i64 Searcher::elapsed_ms() const {
void Searcher::compute_time_limit(const SearchLimits& limits) {
time_limit_ms_ = 0;
clock_search_ = false;
if (limits.infinite) return;
if (limits.movetime) {
@@ -151,6 +152,34 @@ void Searcher::compute_time_limit(const SearchLimits& limits) {
const i64 moves_left = limits.movestogo ? std::min(limits.movestogo, 40) : 25;
const i64 target = remaining / moves_left + limits.inc[us] * 3 / 4;
time_limit_ms_ = std::clamp<i64>(target, 1, remaining / 2 + 1);
clock_search_ = true;
}
bool Searcher::best_move_settled(i64 ms) const {
// Smart pruning (Lc0): stop when no other root move can catch up with the
// leading one in the playouts the remaining time still allows, at the
// speed so far, scaled down by SMART_PRUNING_FACTOR. Only the visits of
// this search count: a reused tree often brings the best move so many
// visits that nothing could overtake it, which would stop every search
// at once. The move leading this search must also be the one played.
if (ms <= 0 || iterations_ < 256) return false;
const Node& root = tree_[Tree::root()];
if (root_start_visits_.size() != root.num_children) return false;
u32 first = 0, second = 0, leader = 0;
for (u32 i = 0; i < root.num_children; i++) {
const u32 v = tree_[root.first_child + i].visits - root_start_visits_[i];
if (v > first) {
second = first;
first = v;
leader = root.first_child + i;
} else if (v > second) {
second = v;
}
}
if (leader != best_child(Tree::root())) return false;
const double remaining =
double(iterations_) / double(ms) * double(time_limit_ms_ - ms) / SMART_PRUNING_FACTOR;
return double(second) + remaining < double(first);
}
bool Searcher::should_stop(const SearchLimits& limits) const {
@@ -163,7 +192,12 @@ bool Searcher::should_stop(const SearchLimits& limits) const {
if (iterations_ >= (u64(1000) << std::min(2 * limits.depth, 14))) return true;
}
// The clock is cheap but not free; look at it every 64 playouts.
if (time_limit_ms_ && (iterations_ & 63) == 0 && elapsed_ms() >= time_limit_ms_) return true;
if (time_limit_ms_ && (iterations_ & 63) == 0) {
const i64 ms = elapsed_ms();
if (ms >= time_limit_ms_) return true;
// Under a game clock the time saved on a settled move goes to later moves.
if (clock_search_ && best_move_settled(ms)) return true;
}
return false;
}
@@ -532,6 +566,14 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
expand(Tree::root(), root);
}
// Root move visits brought along by tree reuse, for smart pruning. The
// root's children keep their order through collections.
root_start_visits_.clear();
{
const Node& r = tree_[Tree::root()];
for (u32 i = 0; i < r.num_children; i++) root_start_visits_.push_back(tree_[r.first_child + i].visits);
}
// UCI reports once a second; the dashboard for a human is redrawn in place
// ten times a second.
const i64 info_interval = pretty ? 100 : 1000;
+7
View File
@@ -152,6 +152,12 @@ private:
Clock::time_point start_time_;
i64 time_limit_ms_ = 0;
// The limit comes from the game clock (not movetime), so stopping early saves time.
bool clock_search_ = false;
std::vector<u32> root_start_visits_;
// Lc0's default: the remaining playouts are divided by this before asking
// whether another root move could still overtake the best one.
static constexpr double SMART_PRUNING_FACTOR = 1.33;
u64 iterations_ = 0;
u64 depth_sum_ = 0;
int seldepth_ = 0;
@@ -181,6 +187,7 @@ private:
void compute_time_limit(const SearchLimits& limits);
i64 elapsed_ms() const;
bool should_stop(const SearchLimits& limits) const;
bool best_move_settled(i64 ms) const;
u32 best_child(u32 idx) const;
void print_info() const;