1919*/
2020
2121#include < algorithm>
22+ #include < cfloat>
23+ #include < cmath>
2224
2325#include " search.h"
2426#include " timeman.h"
@@ -30,46 +32,41 @@ namespace {
3032
3133 enum TimeType { OptimumTime, MaxTime };
3234
33- int remaining (int myTime, int myInc, int moveOverhead, int movesToGo,
34- int moveNum, bool ponder, TimeType type) {
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
3538
36- if (myTime <= 0 )
37- return 0 ;
3839
39- double ratio; // Which ratio of myTime we are going to use
40+ // move_importance() is a skew-logistic function based on naive statistical
41+ // analysis of "how many games are still undecided after n half-moves". Game
42+ // is considered "undecided" as long as neither side has >275cp advantage.
43+ // Data was extracted from the CCRL game database with some simple filtering criteria.
4044
41- // Usage of increment follows quadratic distribution with the maximum at move 25
42- double inc = myInc * std::max (55.0 , 120 - 0.12 * (moveNum - 25 ) * (moveNum - 25 ));
45+ double move_importance (int ply) {
4346
44- // In moves-to-go we distribute time according to a quadratic function with
45- // the maximum around move 20 for 40 moves in y time case.
46- if (movesToGo)
47- {
48- ratio = (type == OptimumTime ? 1.0 : 6.0 ) / std::min (50 , movesToGo);
47+ const double XScale = 7.64 ;
48+ const double XShift = 58.4 ;
49+ const double Skew = 0.183 ;
4950
50- if (moveNum <= 40 )
51- ratio *= 1.1 - 0.001 * (moveNum - 20 ) * (moveNum - 20 );
52- else
53- ratio *= 1.5 ;
51+ return pow ((1 + exp ((ply - XShift) / XScale)), -Skew) + DBL_MIN; // Ensure non-zero
52+ }
53+
54+ template <TimeType T>
55+ int remaining (int myTime, int movesToGo, int ply, int slowMover) {
5456
55- if (movesToGo > 1 )
56- ratio = std::min ( 0.75 , ratio );
57+ const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
58+ const double TStealRatio = (T == OptimumTime ? 0 : StealRatio );
5759
58- ratio *= 1 + inc / (myTime * 8.5 );
59- }
60- // Otherwise we increase usage of remaining time as the game goes on
61- else
62- {
63- double k = 1 + 20 * moveNum / (500.0 + moveNum);
64- ratio = (type == OptimumTime ? 0.017 : 0.07 ) * (k + inc / myTime);
65- }
60+ double moveImportance = (move_importance (ply) * slowMover) / 100 ;
61+ double otherMovesImportance = 0 ;
6662
67- int time = int (std::min (1.0 , ratio) * std::max (0 , myTime - moveOverhead));
63+ for (int i = 1 ; i < movesToGo; ++i)
64+ otherMovesImportance += move_importance (ply + 2 * i);
6865
69- if (type == OptimumTime && ponder)
70- time = 5 * time / 4 ;
66+ double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance);
67+ double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance) ;
7168
72- return time;
69+ return int (myTime * std::min (ratio1, ratio2)); // Intel C++ asks for an explicit cast
7370 }
7471
7572} // namespace
@@ -84,11 +81,12 @@ namespace {
8481// / inc > 0 && movestogo == 0 means: x basetime + z increment
8582// / inc > 0 && movestogo != 0 means: x moves in y minutes + z increment
8683
87- void TimeManagement::init (Search::LimitsType& limits, Color us, int ply)
88- {
89- int moveOverhead = Options[" Move Overhead" ];
90- int npmsec = Options[" nodestime" ];
91- bool ponder = Options[" Ponder" ];
84+ void TimeManagement::init (Search::LimitsType& limits, Color us, int ply) {
85+
86+ int minThinkingTime = Options[" Minimum Thinking Time" ];
87+ int moveOverhead = Options[" Move Overhead" ];
88+ int slowMover = Options[" Slow Mover" ];
89+ int npmsec = Options[" nodestime" ];
9290
9391 // If we have to play in 'nodes as time' mode, then convert from time
9492 // to nodes, and use resulting values in time management formulas.
@@ -105,11 +103,30 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply)
105103 limits.npmsec = npmsec;
106104 }
107105
108- int moveNum = (ply + 1 ) / 2 ;
109-
110106 startTime = limits.startTime ;
111- optimumTime = remaining (limits.time [us], limits.inc [us], moveOverhead,
112- limits.movestogo , moveNum, ponder, OptimumTime);
113- maximumTime = remaining (limits.time [us], limits.inc [us], moveOverhead,
114- limits.movestogo , moveNum, ponder, MaxTime);
107+ optimumTime = maximumTime = std::max (limits.time [us], minThinkingTime);
108+
109+ const int MaxMTG = limits.movestogo ? std::min (limits.movestogo , MoveHorizon) : MoveHorizon;
110+
111+ // We calculate optimum time usage for different hypothetical "moves to go"-values
112+ // and choose the minimum of calculated search time values. Usually the greatest
113+ // hypMTG gives the minimum values.
114+ for (int hypMTG = 1 ; hypMTG <= MaxMTG; ++hypMTG)
115+ {
116+ // Calculate thinking time for hypothetical "moves to go"-value
117+ int hypMyTime = limits.time [us]
118+ + limits.inc [us] * (hypMTG - 1 )
119+ - moveOverhead * (2 + std::min (hypMTG, 40 ));
120+
121+ hypMyTime = std::max (hypMyTime, 0 );
122+
123+ int t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, ply, slowMover);
124+ int t2 = minThinkingTime + remaining<MaxTime >(hypMyTime, hypMTG, ply, slowMover);
125+
126+ optimumTime = std::min (t1, optimumTime);
127+ maximumTime = std::min (t2, maximumTime);
128+ }
129+
130+ if (Options[" Ponder" ])
131+ optimumTime += optimumTime / 4 ;
115132}
0 commit comments