From bf62fe185fcbfca5cf70a338e7e89018c32fc6af Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 16:01:27 +0100 Subject: [PATCH] feat: hard time limit for moves still open at the soft limit With smart pruning most searches stop early and the clock piles up: after move 40 of 8+0.08 self-play about 5 s of the 8 s were left. A diagnostic of 202 searches showed 190 settled early; the 12 that reached the time limit were the open ones (the best move had a median 50% of the root visits, and was the best by Q in only 3 of 12). So reaching the soft limit (the old allocation) is itself the sign of an open move. Under a game clock such a search now runs on, with smart pruning measured against a hard limit of min(3 x soft, remaining / 3), until it settles or reaches that limit. movetime, nodes and infinite are unchanged (their hard limit is the soft one). Self-play diagnostic at 8+0.08 (3 games, 80 plies), main -> this: average time per move 152-156 -> 162-189 ms, longest move about 350 -> 860-1040 ms, clock left after move 40 4.8-5.4 -> 2.4-5.0 s. go movetime 500 still stops at 480 ms; a 300 ms clock uses 6 ms. The bench is unchanged: it searches by playout count. Bench: 554375 Co-Authored-By: Claude Opus 5.5 --- src/mcts.cpp | 20 ++++++++++++++------ src/mcts.h | 6 ++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/mcts.cpp b/src/mcts.cpp index f8c7f25..0530707 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -137,11 +137,12 @@ i64 Searcher::elapsed_ms() const { void Searcher::compute_time_limit(const SearchLimits& limits) { time_limit_ms_ = 0; + hard_limit_ms_ = 0; clock_search_ = false; if (limits.infinite) return; if (limits.movetime) { - time_limit_ms_ = std::max(1, limits.movetime - params.move_overhead); + time_limit_ms_ = hard_limit_ms_ = std::max(1, limits.movetime - params.move_overhead); return; } @@ -152,10 +153,12 @@ 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(target, 1, remaining / 2 + 1); + // A move that is still open at the soft limit may run on up to this. + hard_limit_ms_ = std::max(time_limit_ms_, std::min(time_limit_ms_ * HARD_LIMIT_FACTOR, remaining / 3)); clock_search_ = true; } -bool Searcher::best_move_settled(i64 ms) const { +bool Searcher::best_move_settled(i64 ms, i64 limit_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 @@ -178,7 +181,7 @@ bool Searcher::best_move_settled(i64 ms) const { } if (leader != best_child(Tree::root())) return false; const double remaining = - double(iterations_) / double(ms) * double(time_limit_ms_ - ms) / SMART_PRUNING_FACTOR; + double(iterations_) / double(ms) * double(limit_ms - ms) / SMART_PRUNING_FACTOR; return double(second) + remaining < double(first); } @@ -194,9 +197,14 @@ bool Searcher::should_stop(const SearchLimits& limits) const { // The clock is cheap but not free; look at it every 64 playouts. 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; + if (ms >= hard_limit_ms_) return true; + // Under a game clock the time saved on a settled move goes to later + // moves. A move that is not settled by the soft limit (time_limit_ms_) + // is an open one: it searches on until it settles against the hard + // limit or reaches it. Other limits stop at the soft limit, which is + // also their hard one. + const bool extended = ms >= time_limit_ms_; + if (clock_search_ && best_move_settled(ms, extended ? hard_limit_ms_ : time_limit_ms_)) return true; } return false; } diff --git a/src/mcts.h b/src/mcts.h index 8b8a7ea..c766761 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -151,7 +151,9 @@ private: std::vector prev_history_; Clock::time_point start_time_; - i64 time_limit_ms_ = 0; + i64 time_limit_ms_ = 0; // soft limit + i64 hard_limit_ms_ = 0; + static constexpr i64 HARD_LIMIT_FACTOR = 3; // The limit comes from the game clock (not movetime), so stopping early saves time. bool clock_search_ = false; std::vector root_start_visits_; @@ -187,7 +189,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; + bool best_move_settled(i64 ms, i64 limit_ms) const; u32 best_child(u32 idx) const; void print_info() const;