]> git.sesse.net Git - stockfish/blob - src/timeman.cpp
Cleanup includes
[stockfish] / src / timeman.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
4
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.
9
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.
14
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/>.
17 */
18
19 #include "timeman.h"
20
21 #include <algorithm>
22 #include <cmath>
23
24 #include "search.h"
25 #include "uci.h"
26
27 namespace Stockfish {
28
29 TimeManagement Time; // Our global time management object
30
31
32 /// TimeManagement::init() is 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
37 void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
38
39   // if we have no time, no need to initialize TM, except for the start time,
40   // which is used by movetime.
41   startTime = limits.startTime;
42   if (limits.time[us] == 0)
43       return;
44
45   TimePoint moveOverhead    = TimePoint(Options["Move Overhead"]);
46   TimePoint slowMover       = TimePoint(Options["Slow Mover"]);
47   TimePoint npmsec          = TimePoint(Options["nodestime"]);
48
49   // optScale is a percentage of available time to use for the current move.
50   // maxScale is a multiplier applied to optimumTime.
51   double optScale, maxScale;
52
53   // If we have to play in 'nodes as time' mode, then convert from time
54   // to nodes, and use resulting values in time management formulas.
55   // WARNING: to avoid time losses, the given npmsec (nodes per millisecond)
56   // must be much lower than the real engine speed.
57   if (npmsec)
58   {
59       if (!availableNodes) // Only once at game start
60           availableNodes = npmsec * limits.time[us]; // Time is in msec
61
62       // Convert from milliseconds to nodes
63       limits.time[us] = TimePoint(availableNodes);
64       limits.inc[us] *= npmsec;
65       limits.npmsec = npmsec;
66   }
67
68   // Maximum move horizon of 50 moves
69   int mtg = limits.movestogo ? std::min(limits.movestogo, 50) : 50;
70
71   // Make sure timeLeft is > 0 since we may use it as a divisor
72   TimePoint timeLeft =  std::max(TimePoint(1),
73       limits.time[us] + limits.inc[us] * (mtg - 1) - moveOverhead * (2 + mtg));
74
75   // Use extra time with larger increments
76   double optExtra = std::clamp(1.0 + 12.0 * limits.inc[us] / limits.time[us], 1.0, 1.12);
77
78   // A user may scale time usage by setting UCI option "Slow Mover"
79   // Default is 100 and changing this value will probably lose elo.
80   timeLeft = slowMover * timeLeft / 100;
81
82   // x basetime (+ z increment)
83   // If there is a healthy increment, timeLeft can exceed actual available
84   // game time for the current move, so also cap to 20% of available game time.
85   if (limits.movestogo == 0)
86   {
87       optScale = std::min(0.0120 + std::pow(ply + 3.0, 0.45) * 0.0039,
88                            0.2 * limits.time[us] / double(timeLeft))
89                  * optExtra;
90       maxScale = std::min(7.0, 4.0 + ply / 12.0);
91   }
92
93   // x moves in y seconds (+ z increment)
94   else
95   {
96       optScale = std::min((0.88 + ply / 116.4) / mtg,
97                             0.88 * limits.time[us] / double(timeLeft));
98       maxScale = std::min(6.3, 1.5 + 0.11 * mtg);
99   }
100
101   // Never use more than 80% of the available time for this move
102   optimumTime = TimePoint(optScale * timeLeft);
103   maximumTime = TimePoint(std::min(0.8 * limits.time[us] - moveOverhead, maxScale * optimumTime)) - 10;
104
105   if (Options["Ponder"])
106       optimumTime += optimumTime / 4;
107 }
108
109 } // namespace Stockfish