1+ #include < algorithm>
12#include < bitbishop/interface/search_worker.hpp>
3+ #include < bitbishop/tools/time_guard.hpp>
4+ #include < limits>
5+
6+ namespace {
7+
8+ constexpr int DEFAULT_MOVES_TO_GO = 20 ;
9+ constexpr int MIN_THINK_TIME_MS = 1 ;
10+ constexpr int SAFETY_BUFFER_MS = 50 ;
11+ constexpr int MAX_DEPTH = 50 ;
12+
13+ [[nodiscard]] int estimate_clock_think_time_ms (int remaining_ms, int increment_ms) {
14+ remaining_ms = std::max (remaining_ms, 0 );
15+ increment_ms = std::max (increment_ms, 0 );
16+
17+ if (remaining_ms <= MIN_THINK_TIME_MS ) {
18+ return MIN_THINK_TIME_MS ;
19+ }
20+
21+ const int reserve_ms = std::min (remaining_ms - MIN_THINK_TIME_MS , std::max (SAFETY_BUFFER_MS , remaining_ms / 20 ));
22+ const int spendable_ms = std::max (MIN_THINK_TIME_MS , remaining_ms - reserve_ms);
23+ const int base_ms = spendable_ms / DEFAULT_MOVES_TO_GO ;
24+ const int increment_bonus_ms = (increment_ms * 3 ) / 4 ;
25+
26+ return std::clamp (base_ms + increment_bonus_ms, MIN_THINK_TIME_MS , spendable_ms);
27+ }
28+
29+ } // namespace
230
331Uci::SearchLimits Uci::SearchLimits::from_uci_cmd (const std::vector<std::string>& line) {
432 SearchLimits limits;
@@ -29,14 +57,31 @@ Uci::SearchLimits Uci::SearchLimits::from_uci_cmd(const std::vector<std::string>
2957 }
3058 }
3159
32- const bool has_time_control = limits.movetime || limits.wtime || limits.btime || limits.winc || limits.binc ;
33- if (!limits.depth && !has_time_control && !limits.infinite ) {
60+ if (!limits.depth && !limits.has_time_limit () && !limits.infinite ) {
3461 limits.infinite = true ;
3562 }
3663
3764 return limits;
3865}
3966
67+ bool Uci::SearchLimits::has_time_limit () const {
68+ return movetime.has_value () || wtime.has_value () || btime.has_value () || winc.has_value () || binc.has_value ();
69+ }
70+
71+ std::optional<int > Uci::SearchLimits::think_time_ms (Color side_to_move) const {
72+ if (movetime.has_value ()) {
73+ return std::max (*movetime, MIN_THINK_TIME_MS );
74+ }
75+
76+ const std::optional<int >& remaining_opt = (side_to_move == Color::WHITE ) ? wtime : btime;
77+ if (!remaining_opt.has_value ()) {
78+ return std::nullopt ;
79+ }
80+
81+ const std::optional<int >& increment_opt = (side_to_move == Color::WHITE ) ? winc : binc;
82+ return estimate_clock_think_time_ms (*remaining_opt, increment_opt.value_or (0 ));
83+ }
84+
4085Uci::SearchWorker::SearchWorker (Board board, SearchLimits limits)
4186 : board(board), position(Position(this ->board)), limits(limits) {}
4287
@@ -49,47 +94,52 @@ void Uci::SearchWorker::push_report(SearchReport report) {
4994
5095void Uci::SearchWorker::run () {
5196 using namespace Search ;
97+
98+ // scope_exit function is not yet shipped in all stdlibs (linux is fine, not windows nor macos - 2026-04-29)
99+ // const auto guard = std::experimental::scope_exit([this] { finished.store(true); });
100+ // Replacing it by a struct with custom destructor.
52101 struct FinishGuard {
53102 std::atomic<bool >& finished_ref;
54103 ~FinishGuard () { finished_ref.store (true ); }
55104 } guard{finished};
56105
57106 SearchStats stats{};
58- BestMove best;
59- BestMove last_best;
60-
61- if (limits.infinite ) {
62- for (int current_depth = 1 ; !stop_flag.load (); ++current_depth) {
63- best = negamax (position, current_depth, ALPHA_INIT , BETA_INIT , 0 , stats, &stop_flag);
64- if (!stop_flag.load ()) {
65- last_best = best;
66- push_report (SearchReport{
67- .kind = SearchReportKind::Iteration,
68- .best = last_best,
69- .depth = current_depth,
70- .stats = stats,
71- });
72- }
73- }
74- } else if (limits.depth ) {
75- best = negamax (position, *limits.depth , ALPHA_INIT , BETA_INIT , 0 , stats, &stop_flag);
107+ SearchReport current_best_report{.kind = SearchReportKind::Iteration};
108+
109+ const auto side = board.get_side_to_move ();
110+ const auto think_time = limits.infinite ? std::nullopt : limits.think_time_ms (side);
111+ std::optional<Tools::TimeGuard> timeguard;
112+ if (think_time) {
113+ timeguard.emplace (stop_flag, std::chrono::milliseconds (*think_time));
114+ }
115+
116+ auto perform_search_at_depth = [&](int depth) {
117+ auto result = negamax (position, depth, ALPHA_INIT , BETA_INIT , 0 , stats, &stop_flag);
118+
76119 if (!stop_flag.load ()) {
77- push_report (SearchReport{
78- .kind = SearchReportKind::Iteration,
79- .best = best,
80- .depth = *limits.depth ,
81- .stats = stats,
82- });
120+ current_best_report.best = result;
121+ current_best_report.depth = depth;
122+ current_best_report.stats = stats;
123+ push_report (current_best_report);
124+ return true ;
125+ }
126+ return false ;
127+ };
128+
129+ if (limits.depth && !limits.infinite && !think_time) {
130+ // Case: Fixed depth search (e.g., "go depth 10")
131+ perform_search_at_depth (*limits.depth );
132+ } else {
133+ // Case: Iterative deepening (Infinite or Time-limited)
134+ for (int depth = 1 ; depth <= MAX_DEPTH && !stop_flag.load (); ++depth) {
135+ if (!perform_search_at_depth (depth)) {
136+ break ;
137+ }
83138 }
84139 }
85140
86- const BestMove& final = (last_best.move ) ? last_best : best;
87- push_report (SearchReport{
88- .kind = SearchReportKind::Finish,
89- .best = final ,
90- .depth = limits.depth .value_or (0 ),
91- .stats = stats,
92- });
141+ current_best_report.kind = SearchReportKind::Finish;
142+ push_report (current_best_report);
93143}
94144
95145void Uci::SearchWorker::start () {
0 commit comments