X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Ftimeman.cpp;h=e05c4626f31723c65593c6f995306708414de99a;hp=305084beda2e2c44c7d620cbe84cd5b50ccc444b;hb=7733dadfd7c8781e3bde3cc4e21751fa44ab6ed8;hpb=c0616d773dcb967dad755de74c36937fc5753b63 diff --git a/src/timeman.cpp b/src/timeman.cpp index 305084be..e05c4626 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -25,6 +25,7 @@ #include #include "misc.h" +#include "timeman.h" #include "ucioption.h" //// @@ -33,32 +34,53 @@ namespace { - /// Constants (and their getter functions) + /// Constants - const int MaxMoveHorizon = 50; // Plan time management at most this many moves ahead. - const float MaxRatio = 3.0; // When in trouble, we can step over reserved time with this ratio. - const float MaxStealRatio = 0.33; // However we must not steal time from remaining moves over this ratio. + const int MoveHorizon = 50; // Plan time management at most this many moves ahead + const float MaxRatio = 3.0f; // When in trouble, we can step over reserved time with this ratio + const float StealRatio = 0.33f; // However we must not steal time from remaining moves over this ratio - // Move importance is based on naive statistical analysis of "how many games are still undecided after n half-moves". - // Game is considered "undecided" as long as neither side has >275cp advantage. - // Data was extracted from CCRL game database with some simple filtering criteria. - const int MoveImportance[512] = { 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7778, 7778, 7776, 7776, 7776, 7773, 7770, 7768, 7766, 7763, 7757, 7751, 7743, 7735, 7724, 7713, 7696, 7689, 7670, 7656, 7627, 7605, 7571, 7549, 7522, 7493, 7462, 7425, 7385, 7350, 7308, 7272, 7230, 7180, 7139, 7094, 7055, 7010, 6959, 6902, 6841, 6778, 6705, 6651, 6569, 6508, 6435, 6378, 6323, 6253, 6152, 6085, 5995, 5931, 5859, 5794, 5717, 5646, 5544, 5462, 5364, 5282, 5172, 5078, 4988, 4901, 4831, 4764, 4688, 4609, 4536, 4443, 4365, 4293, 4225, 4155, 4085, 4005, 3927, 3844, 3765, 3693, 3634, 3560, 3479, 3404, 3331, 3268, 3207, 3146, 3077, 3011, 2947, 2894, 2828, 2776, 2727, 2676, 2626, 2589, 2538, 2490, 2442, 2394, 2345, 2302, 2243, 2192, 2156, 2115, 2078, 2043, 2004, 1967, 1922, 1893, 1845, 1809, 1772, 1736, 1702, 1674, 1640, 1605, 1566, 1536, 1509, 1479, 1452, 1423, 1388, 1362, 1332, 1304, 1289, 1266, 1250, 1228, 1206, 1180, 1160, 1134, 1118, 1100, 1080, 1068, 1051, 1034, 1012, 1001, 980, 960, 945, 934, 916, 900, 888, 878, 865, 852, 828, 807, 787, 770, 753, 744, 731, 722, 706, 700, 683, 676, 671, 664, 652, 641, 634, 627, 613, 604, 591, 582, 568, 560, 552, 540, 534, 529, 519, 509, 495, 484, 474, 467, 460, 450, 438, 427, 419, 410, 406, 399, 394, 387, 382, 377, 372, 366, 359, 353, 348, 343, 337, 333, 328, 321, 315, 309, 303, 298, 293, 287, 284, 281, 277, 273, 265, 261, 255, 251, 247, 241, 240, 235, 229, 218, 217, 213, 212, 208, 206, 197, 193, 191, 189, 185, 184, 180, 177, 172, 170, 170, 170, 166, 163, 159, 158, 156, 155, 151, 146, 141, 138, 136, 132, 130, 128, 125, 123, 122, 118, 118, 118, 117, 115, 114, 108, 107, 105, 105, 105, 102, 97, 97, 95, 94, 93, 91, 88, 86, 83, 80, 80, 79, 79, 79, 78, 76, 75, 72, 72, 71, 70, 68, 65, 63, 61, 61, 59, 59, 59, 58, 56, 55, 54, 54, 52, 49, 48, 48, 48, 48, 45, 45, 45, 44, 43, 41, 41, 41, 41, 40, 40, 38, 37, 36, 34, 34, 34, 33, 31, 29, 29, 29, 28, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 27, 24, 24, 23, 23, 22, 21, 20, 20, 19, 19, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17, 17, 17, 16, 16, 15, 15, 14, 14, 14, 12, 12, 11, 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, 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, 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, 2, 1, 1, 1, 1, 1, 1, 1}; + // MoveImportance[] is based on naive statistical analysis of "how many games are still undecided + // after n half-moves". Game is considered "undecided" as long as neither side has >275cp advantage. + // Data was extracted from CCRL game database with some simple filtering criteria. + const int MoveImportance[512] = { + 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, + 7780, 7780, 7780, 7780, 7778, 7778, 7776, 7776, 7776, 7773, 7770, 7768, 7766, 7763, 7757, 7751, + 7743, 7735, 7724, 7713, 7696, 7689, 7670, 7656, 7627, 7605, 7571, 7549, 7522, 7493, 7462, 7425, + 7385, 7350, 7308, 7272, 7230, 7180, 7139, 7094, 7055, 7010, 6959, 6902, 6841, 6778, 6705, 6651, + 6569, 6508, 6435, 6378, 6323, 6253, 6152, 6085, 5995, 5931, 5859, 5794, 5717, 5646, 5544, 5462, + 5364, 5282, 5172, 5078, 4988, 4901, 4831, 4764, 4688, 4609, 4536, 4443, 4365, 4293, 4225, 4155, + 4085, 4005, 3927, 3844, 3765, 3693, 3634, 3560, 3479, 3404, 3331, 3268, 3207, 3146, 3077, 3011, + 2947, 2894, 2828, 2776, 2727, 2676, 2626, 2589, 2538, 2490, 2442, 2394, 2345, 2302, 2243, 2192, + 2156, 2115, 2078, 2043, 2004, 1967, 1922, 1893, 1845, 1809, 1772, 1736, 1702, 1674, 1640, 1605, + 1566, 1536, 1509, 1479, 1452, 1423, 1388, 1362, 1332, 1304, 1289, 1266, 1250, 1228, 1206, 1180, + 1160, 1134, 1118, 1100, 1080, 1068, 1051, 1034, 1012, 1001, 980, 960, 945, 934, 916, 900, 888, + 878, 865, 852, 828, 807, 787, 770, 753, 744, 731, 722, 706, 700, 683, 676, 671, 664, 652, 641, + 634, 627, 613, 604, 591, 582, 568, 560, 552, 540, 534, 529, 519, 509, 495, 484, 474, 467, 460, + 450, 438, 427, 419, 410, 406, 399, 394, 387, 382, 377, 372, 366, 359, 353, 348, 343, 337, 333, + 328, 321, 315, 309, 303, 298, 293, 287, 284, 281, 277, 273, 265, 261, 255, 251, 247, 241, 240, + 235, 229, 218, 217, 213, 212, 208, 206, 197, 193, 191, 189, 185, 184, 180, 177, 172, 170, 170, + 170, 166, 163, 159, 158, 156, 155, 151, 146, 141, 138, 136, 132, 130, 128, 125, 123, 122, 118, + 118, 118, 117, 115, 114, 108, 107, 105, 105, 105, 102, 97, 97, 95, 94, 93, 91, 88, 86, 83, 80, + 80, 79, 79, 79, 78, 76, 75, 72, 72, 71, 70, 68, 65, 63, 61, 61, 59, 59, 59, 58, 56, 55, 54, 54, + 52, 49, 48, 48, 48, 48, 45, 45, 45, 44, 43, 41, 41, 41, 41, 40, 40, 38, 37, 36, 34, 34, 34, 33, + 31, 29, 29, 29, 28, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 27, 24, 24, 23, 23, 22, 21, 20, 20, + 19, 19, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17, 17, 17, 16, 16, 15, 15, 14, 14, 14, 12, 12, 11, + 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, + 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, + 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, + 2, 1, 1, 1, 1, 1, 1, 1 }; int move_importance(int ply) { return MoveImportance[Min(ply, 511)]; } - /// Configurable UCI options - - int EmergencyMoveHorizon; // Be prepared to always play at least this many moves - int EmergencyBaseTime; // Always attempt to keep at least this much time (in ms) at clock - int EmergencyMoveTime; // Plus attempt to keep at least this much time for each remaining emergency move - int MinThinkingTime; // No matter what, use at least this much thinking before doing the move - /// Function Prototypes - int calc_minimum_search_time_for_moves_to_go(const int myTime, const int movesToGo, const int currentPly); - int calc_maximum_search_time_for_moves_to_go(const int myTime, const int movesToGo, const int currentPly); + enum TimeType { OptimumTime, MaxTime }; + + template + int remaining(int myTime, int movesToGo, int currentPly); } @@ -66,39 +88,60 @@ namespace { //// Functions //// -void calc_search_times(const int myTime, const int myInc, const int movesToGo, const int currentPly, int &minimumSearchTime, int &maximumSearchTime) +void TimeManager::pv_instability(int curChanges, int prevChanges) { + + unstablePVExtraTime = curChanges * (optimumSearchTime / 2) + + prevChanges * (optimumSearchTime / 3); +} + +void TimeManager::init(int myTime, int myInc, int movesToGo, int currentPly) { - // We support four different kind of time controls: - // - // Inc == 0 && movesToGo == 0 means: x basetime [sudden death!] - // Inc == 0 && movesToGo != 0 means: (x moves) / (y minutes) - // Inc > 0 && movesToGo == 0 means: x basetime + z inc. - // Inc > 0 && movesToGo != 0 means: (x moves) / (y minutes) + z inc - - // Read uci options - EmergencyMoveHorizon = get_option_value_int("Emergency Move Horizon"); - EmergencyBaseTime = get_option_value_int("Emergency Base Time"); - EmergencyMoveTime = get_option_value_int("Emergency Move Time"); - MinThinkingTime = get_option_value_int("Minimum Thinking Time"); - - // Initialize variables to maximum values - minimumSearchTime = myTime; - maximumSearchTime = myTime; - - // We calculate optimum time usage for different hypothetic "moves to go"-values and - // choose the minimum of calculated search time values. - // Usually the greatest hypMTG gives the minimum values. - for (int hypMTG = 1; hypMTG <= ((movesToGo == 0)? MaxMoveHorizon : Min(movesToGo, MaxMoveHorizon)); hypMTG++) + /* We support four different kind of time controls: + + Inc == 0 && movesToGo == 0 means: x basetime [sudden death!] + Inc == 0 && movesToGo != 0 means: (x moves) / (y minutes) + Inc > 0 && movesToGo == 0 means: x basetime + z inc. + Inc > 0 && movesToGo != 0 means: (x moves) / (y minutes) + z inc + + Time management is adjusted by following UCI parameters: + + emergencyMoveHorizon :Be prepared to always play at least this many moves + emergencyBaseTime :Always attempt to keep at least this much time (in ms) at clock + emergencyMoveTime :Plus attempt to keep at least this much time for each remaining emergency move + minThinkingTime :No matter what, use at least this much thinking before doing the move + */ + + int hypMTG, hypMyTime, t1, t2; + + // Read uci parameters + int emergencyMoveHorizon = get_option_value_int("Emergency Move Horizon"); + int emergencyBaseTime = get_option_value_int("Emergency Base Time"); + int emergencyMoveTime = get_option_value_int("Emergency Move Time"); + int minThinkingTime = get_option_value_int("Minimum Thinking Time"); + + // Initialize to maximum values but unstablePVExtraTime that is reset + unstablePVExtraTime = 0; + optimumSearchTime = maximumSearchTime = myTime; + + // We calculate optimum time usage for different hypothetic "moves to go"-values and choose the + // minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values. + for (hypMTG = 1; hypMTG <= (movesToGo ? Min(movesToGo, MoveHorizon) : MoveHorizon); hypMTG++) { // Calculate thinking time for hypothetic "moves to go"-value - int hypMyTime = Max(myTime + (hypMTG - 1) * myInc - EmergencyBaseTime - Min(hypMTG, EmergencyMoveHorizon) * EmergencyMoveTime, 0); + hypMyTime = Max(myTime + (hypMTG - 1) * myInc - emergencyBaseTime - Min(hypMTG, emergencyMoveHorizon) * emergencyMoveTime, 0); - minimumSearchTime = Min(minimumSearchTime, MinThinkingTime + calc_minimum_search_time_for_moves_to_go(hypMyTime, hypMTG, currentPly)); - maximumSearchTime = Min(maximumSearchTime, MinThinkingTime + calc_maximum_search_time_for_moves_to_go(hypMyTime, hypMTG, currentPly)); + t1 = minThinkingTime + remaining(hypMyTime, hypMTG, currentPly); + t2 = minThinkingTime + remaining(hypMyTime, hypMTG, currentPly); + + optimumSearchTime = Min(optimumSearchTime, t1); + maximumSearchTime = Min(maximumSearchTime, t2); } - // Make sure that miminumSearchTime is not over maximumSearchTime. - minimumSearchTime = Min(minimumSearchTime, maximumSearchTime); + if (get_option_value_bool("Ponder")) + optimumSearchTime += optimumSearchTime / 4; + + // Make sure that maxSearchTime is not over absoluteMaxSearchTime + optimumSearchTime = Min(optimumSearchTime, maximumSearchTime); } //// @@ -107,32 +150,21 @@ void calc_search_times(const int myTime, const int myInc, const int movesToGo, c namespace { - int calc_minimum_search_time_for_moves_to_go(const int myTime, const int movesToGo, const int currentPly) + template + int remaining(int myTime, int movesToGo, int currentPly) { - int thisMoveImportance = move_importance(currentPly); - int otherMovesImportance = 0; + const float TMaxRatio = (T == OptimumTime ? 1 : MaxRatio); + const float TStealRatio = (T == OptimumTime ? 0 : StealRatio); - for (int i = 1; i < movesToGo; i++) - otherMovesImportance += move_importance(currentPly + 2*i); - - float ratio = thisMoveImportance / float(thisMoveImportance + otherMovesImportance); - - return int(floor(myTime * ratio)); - } - - int calc_maximum_search_time_for_moves_to_go(const int myTime, const int movesToGo, const int currentPly) - { int thisMoveImportance = move_importance(currentPly); int otherMovesImportance = 0; for (int i = 1; i < movesToGo; i++) - otherMovesImportance += move_importance(currentPly + 2*i); + otherMovesImportance += move_importance(currentPly + 2 * i); - float ratio1 = (MaxRatio * thisMoveImportance) / float(MaxRatio * thisMoveImportance + otherMovesImportance); - float ratio2 = (thisMoveImportance + MaxStealRatio * otherMovesImportance) / float(thisMoveImportance + otherMovesImportance); - float ratio = Min(ratio1, ratio2); + float ratio1 = (TMaxRatio * thisMoveImportance) / float(TMaxRatio * thisMoveImportance + otherMovesImportance); + float ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / float(thisMoveImportance + otherMovesImportance); - return int(floor(myTime * ratio)); + return int(floor(myTime * Min(ratio1, ratio2))); } } -