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;