]> git.sesse.net Git - stockfish/blob - src/timeman.cpp
Little timeman.cpp massage
[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-2010 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #include <cmath>
26
27 #include "misc.h"
28 #include "ucioption.h"
29
30 ////
31 //// Local definitions
32 ////
33
34 namespace {
35
36   /// Constants
37
38   const int MaxMoveHorizon  = 50;   // Plan time management at most this many moves ahead
39   const float MaxRatio      = 3.0;  // When in trouble, we can step over reserved time with this ratio
40   const float MaxStealRatio = 0.33; // However we must not steal time from remaining moves over this ratio
41
42
43   // MoveImportance[] is based on naive statistical analysis of "how many games are still undecided
44   // after n half-moves". Game is considered "undecided" as long as neither side has >275cp advantage.
45   // Data was extracted from CCRL game database with some simple filtering criteria.
46   const int MoveImportance[512] = {
47     7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780,
48     7780, 7780, 7780, 7780, 7778, 7778, 7776, 7776, 7776, 7773, 7770, 7768, 7766, 7763, 7757, 7751,
49     7743, 7735, 7724, 7713, 7696, 7689, 7670, 7656, 7627, 7605, 7571, 7549, 7522, 7493, 7462, 7425,
50     7385, 7350, 7308, 7272, 7230, 7180, 7139, 7094, 7055, 7010, 6959, 6902, 6841, 6778, 6705, 6651,
51     6569, 6508, 6435, 6378, 6323, 6253, 6152, 6085, 5995, 5931, 5859, 5794, 5717, 5646, 5544, 5462,
52     5364, 5282, 5172, 5078, 4988, 4901, 4831, 4764, 4688, 4609, 4536, 4443, 4365, 4293, 4225, 4155,
53     4085, 4005, 3927, 3844, 3765, 3693, 3634, 3560, 3479, 3404, 3331, 3268, 3207, 3146, 3077, 3011,
54     2947, 2894, 2828, 2776, 2727, 2676, 2626, 2589, 2538, 2490, 2442, 2394, 2345, 2302, 2243, 2192,
55     2156, 2115, 2078, 2043, 2004, 1967, 1922, 1893, 1845, 1809, 1772, 1736, 1702, 1674, 1640, 1605,
56     1566, 1536, 1509, 1479, 1452, 1423, 1388, 1362, 1332, 1304, 1289, 1266, 1250, 1228, 1206, 1180,
57     1160, 1134, 1118, 1100, 1080, 1068, 1051, 1034, 1012, 1001, 980, 960, 945, 934, 916, 900, 888,
58     878, 865, 852, 828, 807, 787, 770, 753, 744, 731, 722, 706, 700, 683, 676, 671, 664, 652, 641,
59     634, 627, 613, 604, 591, 582, 568, 560, 552, 540, 534, 529, 519, 509, 495, 484, 474, 467, 460,
60     450, 438, 427, 419, 410, 406, 399, 394, 387, 382, 377, 372, 366, 359, 353, 348, 343, 337, 333,
61     328, 321, 315, 309, 303, 298, 293, 287, 284, 281, 277, 273, 265, 261, 255, 251, 247, 241, 240,
62     235, 229, 218, 217, 213, 212, 208, 206, 197, 193, 191, 189, 185, 184, 180, 177, 172, 170, 170,
63     170, 166, 163, 159, 158, 156, 155, 151, 146, 141, 138, 136, 132, 130, 128, 125, 123, 122, 118,
64     118, 118, 117, 115, 114, 108, 107, 105, 105, 105, 102, 97, 97, 95, 94, 93, 91, 88, 86, 83, 80,
65     80, 79, 79, 79, 78, 76, 75, 72, 72, 71, 70, 68, 65, 63, 61, 61, 59, 59, 59, 58, 56, 55, 54, 54,
66     52, 49, 48, 48, 48, 48, 45, 45, 45, 44, 43, 41, 41, 41, 41, 40, 40, 38, 37, 36, 34, 34, 34, 33,
67     31, 29, 29, 29, 28, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 27, 24, 24, 23, 23, 22, 21, 20, 20,
68     19, 19, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17, 17, 17, 16, 16, 15, 15, 14, 14, 14, 12, 12, 11,
69     9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
70     8, 8, 8, 8, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
71     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 2, 2, 2, 2,
72     2, 1, 1, 1, 1, 1, 1, 1 };
73
74   int move_importance(int ply) { return MoveImportance[Min(ply, 511)]; }
75
76
77   /// Function Prototypes
78
79   int min_time_for_MTG(int myTime, int movesToGo, int currentPly);
80   int max_time_for_MTG(int myTime, int movesToGo, int currentPly);
81 }
82
83
84 ////
85 //// Functions
86 ////
87
88 void get_search_times(int myTime, int myInc, int movesToGo, int currentPly,
89                       int* maxSearchTime, int* absoluteMaxSearchTime)
90 {
91   /* We support four different kind of time controls:
92
93       Inc == 0 && movesToGo == 0 means: x basetime  [sudden death!]
94       Inc == 0 && movesToGo != 0 means: (x moves) / (y minutes)
95       Inc > 0  && movesToGo == 0 means: x basetime + z inc.
96       Inc > 0  && movesToGo != 0 means: (x moves) / (y minutes) + z inc
97
98     Time management is adjusted by following UCI parameters:
99
100       emergencyMoveHorizon :Be prepared to always play at least this many moves
101       emergencyBaseTime    :Always attempt to keep at least this much time (in ms) at clock
102       emergencyMoveTime    :Plus attempt to keep at least this much time for each remaining emergency move
103       minThinkingTime      :No matter what, use at least this much thinking before doing the move
104   */
105
106   int hypMTG, hypMyTime;
107
108   // Read uci parameters
109   int emergencyMoveHorizon = get_option_value_int("Emergency Move Horizon");
110   int emergencyBaseTime    = get_option_value_int("Emergency Base Time");
111   int emergencyMoveTime    = get_option_value_int("Emergency Move Time");
112   int minThinkingTime      = get_option_value_int("Minimum Thinking Time");
113
114   // Initialize variables to maximum values
115   *maxSearchTime = *absoluteMaxSearchTime = myTime;
116
117   // We calculate optimum time usage for different hypothetic "moves to go"-values and choose the
118   // minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values.
119   for (hypMTG = 1; hypMTG <= (movesToGo ? Min(movesToGo, MaxMoveHorizon) : MaxMoveHorizon); hypMTG++)
120   {
121       // Calculate thinking time for hypothetic "moves to go"-value
122       hypMyTime = Max(myTime + (hypMTG - 1) * myInc - emergencyBaseTime - Min(hypMTG, emergencyMoveHorizon) * emergencyMoveTime, 0);
123
124       *maxSearchTime = Min(*maxSearchTime, minThinkingTime + min_time_for_MTG(hypMyTime, hypMTG, currentPly));
125       *absoluteMaxSearchTime = Min(*absoluteMaxSearchTime, minThinkingTime + max_time_for_MTG(hypMyTime, hypMTG, currentPly));
126   }
127
128   // Make sure that maxSearchTime is not over absoluteMaxSearchTime
129   *maxSearchTime = Min(*maxSearchTime, *absoluteMaxSearchTime);
130 }
131
132 ////
133 //// Local functions
134 ////
135
136 namespace {
137
138   int min_time_for_MTG(int myTime, int movesToGo, int currentPly)
139   {
140     float thisMoveImportance = move_importance(currentPly);
141     float otherMovesImportance = 0;
142
143     for (int i = 1; i < movesToGo; i++)
144         otherMovesImportance += move_importance(currentPly + 2 * i);
145
146     float ratio = thisMoveImportance / (thisMoveImportance + otherMovesImportance);
147
148     return int(floor(myTime * ratio));
149   }
150
151   int max_time_for_MTG(int myTime, int movesToGo, int currentPly)
152   {
153     float thisMoveImportance = move_importance(currentPly);
154     float otherMovesImportance = 0;
155
156     for (int i = 1; i < movesToGo; i++)
157         otherMovesImportance += move_importance(currentPly + 2 * i);
158
159     float ratio1 = (MaxRatio * thisMoveImportance) / (MaxRatio * thisMoveImportance + otherMovesImportance);
160     float ratio2 = (thisMoveImportance + MaxStealRatio * otherMovesImportance) / (thisMoveImportance + otherMovesImportance);
161
162     return int(floor(myTime * Min(ratio1, ratio2)));
163   }
164 }
165