2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 TimeManagement Time; // Our global time management object
32 // Called at the beginning of the search and calculates
33 // the bounds of time allowed for the current game ply. We currently support:
34 // 1) x basetime (+ z increment)
35 // 2) x moves in y seconds (+ z increment)
36 void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
38 // If we have no time, no need to initialize TM, except for the start time,
39 // which is used by movetime.
40 startTime = limits.startTime;
41 if (limits.time[us] == 0)
44 TimePoint moveOverhead = TimePoint(Options["Move Overhead"]);
45 TimePoint npmsec = TimePoint(Options["nodestime"]);
47 // optScale is a percentage of available time to use for the current move.
48 // maxScale is a multiplier applied to optimumTime.
49 double optScale, maxScale;
51 // If we have to play in 'nodes as time' mode, then convert from time
52 // to nodes, and use resulting values in time management formulas.
53 // WARNING: to avoid time losses, the given npmsec (nodes per millisecond)
54 // must be much lower than the real engine speed.
57 if (!availableNodes) // Only once at game start
58 availableNodes = npmsec * limits.time[us]; // Time is in msec
60 // Convert from milliseconds to nodes
61 limits.time[us] = TimePoint(availableNodes);
62 limits.inc[us] *= npmsec;
63 limits.npmsec = npmsec;
66 // Maximum move horizon of 50 moves
67 int mtg = limits.movestogo ? std::min(limits.movestogo, 50) : 50;
69 // Make sure timeLeft is > 0 since we may use it as a divisor
70 TimePoint timeLeft = std::max(TimePoint(1), limits.time[us] + limits.inc[us] * (mtg - 1)
71 - moveOverhead * (2 + mtg));
73 // Use extra time with larger increments
74 double optExtra = std::clamp(1.0 + 12.5 * limits.inc[us] / limits.time[us], 1.0, 1.11);
76 // Calculate time constants based on current time left.
77 double optConstant = std::min(0.00334 + 0.0003 * std::log10(limits.time[us] / 1000.0), 0.0049);
78 double maxConstant = std::max(3.4 + 3.0 * std::log10(limits.time[us] / 1000.0), 2.76);
80 // x basetime (+ z increment)
81 // If there is a healthy increment, timeLeft can exceed actual available
82 // game time for the current move, so also cap to 20% of available game time.
83 if (limits.movestogo == 0)
85 optScale = std::min(0.0120 + std::pow(ply + 3.1, 0.44) * optConstant,
86 0.21 * limits.time[us] / double(timeLeft))
88 maxScale = std::min(6.9, maxConstant + ply / 12.2);
91 // x moves in y seconds (+ z increment)
94 optScale = std::min((0.88 + ply / 116.4) / mtg, 0.88 * limits.time[us] / double(timeLeft));
95 maxScale = std::min(6.3, 1.5 + 0.11 * mtg);
98 // Limit the maximum possible time for this move
99 optimumTime = TimePoint(optScale * timeLeft);
101 TimePoint(std::min(0.84 * limits.time[us] - moveOverhead, maxScale * optimumTime)) - 10;
103 if (Options["Ponder"])
104 optimumTime += optimumTime / 4;
107 } // namespace Stockfish