1 Commits
Author SHA1 Message Date
rektdieandclaude bf62fe185f 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 <noreply@anthropic.com>
2026-11-15 16:01:27 +01:00
2 changed files with 18 additions and 8 deletions

No files matched your search

+14 -6
View File
@@ -137,11 +137,12 @@ i64 Searcher::elapsed_ms() const {
void Searcher::compute_time_limit(const SearchLimits& limits) { void Searcher::compute_time_limit(const SearchLimits& limits) {
time_limit_ms_ = 0; time_limit_ms_ = 0;
hard_limit_ms_ = 0;
clock_search_ = false; clock_search_ = false;
if (limits.infinite) return; if (limits.infinite) return;
if (limits.movetime) { if (limits.movetime) {
time_limit_ms_ = std::max<i64>(1, limits.movetime - params.move_overhead); time_limit_ms_ = hard_limit_ms_ = std::max<i64>(1, limits.movetime - params.move_overhead);
return; 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 moves_left = limits.movestogo ? std::min(limits.movestogo, 40) : 25;
const i64 target = remaining / moves_left + limits.inc[us] * 3 / 4; const i64 target = remaining / moves_left + limits.inc[us] * 3 / 4;
time_limit_ms_ = std::clamp<i64>(target, 1, remaining / 2 + 1); time_limit_ms_ = std::clamp<i64>(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; 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 // 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 // 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 // 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; if (leader != best_child(Tree::root())) return false;
const double remaining = 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); 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. // The clock is cheap but not free; look at it every 64 playouts.
if (time_limit_ms_ && (iterations_ & 63) == 0) { if (time_limit_ms_ && (iterations_ & 63) == 0) {
const i64 ms = elapsed_ms(); const i64 ms = elapsed_ms();
if (ms >= time_limit_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. // Under a game clock the time saved on a settled move goes to later
if (clock_search_ && best_move_settled(ms)) return true; // 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; return false;
} }
+4 -2
View File
@@ -151,7 +151,9 @@ private:
std::vector<u64> prev_history_; std::vector<u64> prev_history_;
Clock::time_point start_time_; 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. // The limit comes from the game clock (not movetime), so stopping early saves time.
bool clock_search_ = false; bool clock_search_ = false;
std::vector<u32> root_start_visits_; std::vector<u32> root_start_visits_;
@@ -187,7 +189,7 @@ private:
void compute_time_limit(const SearchLimits& limits); void compute_time_limit(const SearchLimits& limits);
i64 elapsed_ms() const; i64 elapsed_ms() const;
bool should_stop(const SearchLimits& limits) 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; u32 best_child(u32 idx) const;
void print_info() const; void print_info() const;