]> git.sesse.net Git - stockfish/blob - src/timeman.cpp
Remove slowMover
[stockfish] / src / timeman.cpp
1 /*
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-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
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.
11
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.
16
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/>.
19 */
20
21 #include <algorithm>
22 #include <cfloat>
23 #include <cmath>
24
25 #include "search.h"
26 #include "timeman.h"
27 #include "uci.h"
28
29 TimeManagement Time; // Our global time management object
30
31 namespace {
32
33   enum TimeType { OptimumTime, MaxTime };
34
35   const int MoveHorizon   = 50;   // Plan time management at most this many moves ahead
36   const double MaxRatio   = 7.09;  // When in trouble, we can step over reserved time with this ratio
37   const double StealRatio = 0.35; // However we must not steal time from remaining moves over this ratio
38
39
40   // move_importance() is an exponential function based on naive observation
41   // that a game is closer to be decided after each half-move. This function
42   // should be decreasing and with "nice" convexity properties.
43
44   double move_importance(int ply) {
45
46     const double PlyScale = 109.3265;
47     const double PlyGrowth = 4.0;
48
49     return exp(-pow(ply / PlyScale, PlyGrowth)) + DBL_MIN; // Ensure non-zero
50   }
51
52   template<TimeType T>
53   int remaining(int myTime, int movesToGo, int ply)
54   {
55     const double TMaxRatio   = (T == OptimumTime ? 1 : MaxRatio);
56     const double TStealRatio = (T == OptimumTime ? 0 : StealRatio);
57
58     double moveImportance = move_importance(ply);
59     double otherMovesImportance = 0;
60
61     for (int i = 1; i < movesToGo; ++i)
62         otherMovesImportance += move_importance(ply + 2 * i);
63
64     double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance);
65     double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance);
66
67     return int(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast
68   }
69
70 } // namespace
71
72
73 /// init() is called at the beginning of the search and calculates the allowed
74 /// thinking time out of the time control and current game ply. We support four
75 /// different kinds of time controls, passed in 'limits':
76 ///
77 ///  inc == 0 && movestogo == 0 means: x basetime  [sudden death!]
78 ///  inc == 0 && movestogo != 0 means: x moves in y minutes
79 ///  inc >  0 && movestogo == 0 means: x basetime + z increment
80 ///  inc >  0 && movestogo != 0 means: x moves in y minutes + z increment
81
82 void TimeManagement::init(Search::LimitsType& limits, Color us, int ply)
83 {
84   int minThinkingTime = Options["Minimum Thinking Time"];
85   int moveOverhead    = Options["Move Overhead"];
86   int npmsec          = Options["nodestime"];
87
88   // If we have to play in 'nodes as time' mode, then convert from time
89   // to nodes, and use resulting values in time management formulas.
90   // WARNING: Given npms (nodes per millisecond) must be much lower then
91   // the real engine speed to avoid time losses.
92   if (npmsec)
93   {
94       if (!availableNodes) // Only once at game start
95           availableNodes = npmsec * limits.time[us]; // Time is in msec
96
97       // Convert from millisecs to nodes
98       limits.time[us] = (int)availableNodes;
99       limits.inc[us] *= npmsec;
100       limits.npmsec = npmsec;
101   }
102
103   startTime = limits.startTime;
104   optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime);
105
106   const int MaxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon;
107
108   // We calculate optimum time usage for different hypothetical "moves to go"-values
109   // and choose the minimum of calculated search time values. Usually the greatest
110   // hypMTG gives the minimum values.
111   for (int hypMTG = 1; hypMTG <= MaxMTG; ++hypMTG)
112   {
113       // Calculate thinking time for hypothetical "moves to go"-value
114       int hypMyTime =  limits.time[us]
115                      + limits.inc[us] * (hypMTG - 1)
116                      - moveOverhead * (2 + std::min(hypMTG, 40));
117
118       hypMyTime = std::max(hypMyTime, 0);
119
120       int t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, ply);
121       int t2 = minThinkingTime + remaining<MaxTime    >(hypMyTime, hypMTG, ply);
122
123       optimumTime = std::min(t1, optimumTime);
124       maximumTime = std::min(t2, maximumTime);
125   }
126
127   if (Options["Ponder"])
128       optimumTime += optimumTime / 4;
129 }