]> git.sesse.net Git - stockfish/blob - src/timeman.cpp
Do more LMR for captures
[stockfish] / src / timeman.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 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 <algorithm>
20 #include <cfloat>
21 #include <cmath>
22
23 #include "search.h"
24 #include "timeman.h"
25 #include "uci.h"
26
27 TimeManagement Time; // Our global time management object
28
29
30 /// TimeManagement::init() is called at the beginning of the search and calculates
31 /// the bounds of time allowed for the current game ply. We currently support:
32 //      1) x basetime (+ z increment)
33 //      2) x moves in y seconds (+ z increment)
34
35 void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
36
37   TimePoint moveOverhead    = TimePoint(Options["Move Overhead"]);
38   TimePoint slowMover       = TimePoint(Options["Slow Mover"]);
39   TimePoint npmsec          = TimePoint(Options["nodestime"]);
40
41   // optScale is a percentage of available time to use for the current move.
42   // maxScale is a multiplier applied to optimumTime.
43   double optScale, maxScale;
44
45   // If we have to play in 'nodes as time' mode, then convert from time
46   // to nodes, and use resulting values in time management formulas.
47   // WARNING: to avoid time losses, the given npmsec (nodes per millisecond)
48   // must be much lower than the real engine speed.
49   if (npmsec)
50   {
51       if (!availableNodes) // Only once at game start
52           availableNodes = npmsec * limits.time[us]; // Time is in msec
53
54       // Convert from milliseconds to nodes
55       limits.time[us] = TimePoint(availableNodes);
56       limits.inc[us] *= npmsec;
57       limits.npmsec = npmsec;
58   }
59
60   startTime = limits.startTime;
61
62   // Maximum move horizon of 50 moves
63   int mtg = limits.movestogo ? std::min(limits.movestogo, 50) : 50;
64
65   // Make sure timeLeft is > 0 since we may use it as a divisor
66   TimePoint timeLeft =  std::max(TimePoint(1),
67       limits.time[us] + limits.inc[us] * (mtg - 1) - moveOverhead * (2 + mtg));
68
69   // A user may scale time usage by setting UCI option "Slow Mover"
70   // Default is 100 and changing this value will probably lose elo.
71   timeLeft = slowMover * timeLeft / 100;
72
73   // x basetime (+ z increment)
74   // If there is a healthy increment, timeLeft can exceed actual available
75   // game time for the current move, so also cap to 20% of available game time.
76   if (limits.movestogo == 0)
77   {
78       optScale = std::min(0.0084 + std::pow(ply + 3.0, 0.5) * 0.0042,
79                            0.2 * limits.time[us] / double(timeLeft));
80       maxScale = std::min(7.0, 4.0 + ply / 12.0);
81   }
82
83   // x moves in y seconds (+ z increment)
84   else
85   {
86       optScale = std::min((0.8 + ply / 128.0) / mtg,
87                             0.8 * limits.time[us] / double(timeLeft));
88       maxScale = std::min(6.3, 1.5 + 0.11 * mtg);
89   }
90
91   // Never use more than 80% of the available time for this move
92   optimumTime = TimePoint(optScale * timeLeft);
93   maximumTime = TimePoint(std::min(0.8 * limits.time[us] - moveOverhead, maxScale * optimumTime));
94
95   if (Options["Ponder"])
96       optimumTime += optimumTime / 4;
97 }