Merge pull request 'Smart pruning stops clock searches whose best move is settled' (#14) from smart-pruning into main
SPRT https://bench.rektdie.com/test/5295/ Elo | 24.75 +- 13.56 (95%) SPRT | 8.0+0.08s Threads=1 Hash=64MB LLR | 3.24 (-2.94, 2.94) [0.00, 10.00] Games | N: 1364 W: 501 L: 404 D: 459 Penta | [39, 133, 273, 166, 71] Bench: 554375
This commit was merged in pull request #14.
This commit is contained in:
commit
ca921ce2e2
2 files changed
+50
-1
No files matched your search
+43
-1
@@ -137,6 +137,7 @@ 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;
|
||||||
|
clock_search_ = false;
|
||||||
if (limits.infinite) return;
|
if (limits.infinite) return;
|
||||||
|
|
||||||
if (limits.movetime) {
|
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 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);
|
||||||
|
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 {
|
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;
|
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.
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -532,6 +566,14 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
|||||||
expand(Tree::root(), root);
|
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
|
// UCI reports once a second; the dashboard for a human is redrawn in place
|
||||||
// ten times a second.
|
// ten times a second.
|
||||||
const i64 info_interval = pretty ? 100 : 1000;
|
const i64 info_interval = pretty ? 100 : 1000;
|
||||||
|
|||||||
@@ -152,6 +152,12 @@ private:
|
|||||||
|
|
||||||
Clock::time_point start_time_;
|
Clock::time_point start_time_;
|
||||||
i64 time_limit_ms_ = 0;
|
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 iterations_ = 0;
|
||||||
u64 depth_sum_ = 0;
|
u64 depth_sum_ = 0;
|
||||||
int seldepth_ = 0;
|
int seldepth_ = 0;
|
||||||
@@ -181,6 +187,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;
|
||||||
|
|
||||||
u32 best_child(u32 idx) const;
|
u32 best_child(u32 idx) const;
|
||||||
void print_info() const;
|
void print_info() const;
|
||||||
|
|||||||
Reference in new issue
Block a user