2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5 Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 TimeManagement Time; // Our global time management object
31 /// init() is called at the beginning of the search and calculates the bounds
32 /// of time allowed for the current game ply. We currently support:
33 // 1) x basetime (+z increment)
34 // 2) x moves in y seconds (+z increment)
36 void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
38 TimePoint moveOverhead = TimePoint(Options["Move Overhead"]);
39 TimePoint slowMover = TimePoint(Options["Slow Mover"]);
40 TimePoint npmsec = TimePoint(Options["nodestime"]);
42 // opt_scale is a percentage of available time to use for the current move.
43 // max_scale is a multiplier applied to optimumTime.
44 double opt_scale, max_scale;
46 // If we have to play in 'nodes as time' mode, then convert from time
47 // to nodes, and use resulting values in time management formulas.
48 // WARNING: to avoid time losses, the given npmsec (nodes per millisecond)
49 // must be much lower than the real engine speed.
52 if (!availableNodes) // Only once at game start
53 availableNodes = npmsec * limits.time[us]; // Time is in msec
55 // Convert from milliseconds to nodes
56 limits.time[us] = TimePoint(availableNodes);
57 limits.inc[us] *= npmsec;
58 limits.npmsec = npmsec;
61 startTime = limits.startTime;
63 //Maximum move horizon of 50 moves
64 int mtg = limits.movestogo ? std::min(limits.movestogo, 50) : 50;
66 // Make sure timeLeft is > 0 since we may use it as a divisor
67 TimePoint timeLeft = std::max(TimePoint(1),
68 limits.time[us] + limits.inc[us] * (mtg - 1) - moveOverhead * (2 + mtg));
70 // A user may scale time usage by setting UCI option "Slow Mover"
71 // Default is 100 and changing this value will probably lose elo.
72 timeLeft = slowMover * timeLeft / 100;
74 // x basetime (+ z increment)
75 // If there is a healthy increment, timeLeft can exceed actual available
76 // game time for the current move, so also cap to 20% of available game time.
77 if (limits.movestogo == 0)
79 opt_scale = std::min(0.008 + std::pow(ply + 3.0, 0.5) / 250.0,
80 0.2 * limits.time[us] / double(timeLeft));
81 max_scale = std::min(7.0, 4.0 + ply / 12.0);
84 // x moves in y seconds (+ z increment)
87 opt_scale = std::min((0.8 + ply / 128.0) / mtg,
88 0.8 * limits.time[us] / double(timeLeft));
89 max_scale = std::min(6.3, 1.5 + 0.11 * mtg);
92 // Never use more than 80% of the available time for this move
93 optimumTime = TimePoint(opt_scale * timeLeft);
94 maximumTime = TimePoint(std::min(0.8 * limits.time[us] - moveOverhead, max_scale * optimumTime));
96 if (Options["Ponder"])
97 optimumTime += optimumTime / 4;