1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Reformat time manager code

In particular clarify that 'sd'
parameter is used only in !movesToGo
case.

Verified with Ivan's check tool it is
equivalent to original code.

No functional change.
This commit is contained in:
Marco Costalba 2017-08-18 08:44:37 -07:00
parent daf0fe1f57
commit e10255339f

View file

@ -31,17 +31,15 @@ namespace {
enum TimeType { OptimumTime, MaxTime }; enum TimeType { OptimumTime, MaxTime };
int remaining(int myTime, int myInc, int moveOverhead, int movesToGo, int remaining(int myTime, int myInc, int moveOverhead, int movesToGo,
int ply, bool ponder, TimeType type) { int moveNum, bool ponder, TimeType type) {
if (myTime <= 0) if (myTime <= 0)
return 0; return 0;
int moveNumber = (ply + 1) / 2; double ratio; // Which ratio of myTime we are going to use
double ratio; // Which ratio of myTime we are going to use. It's <= 1 if not ponder
double sd = 8.5;
// Usage of increment follows quadratic distribution with the maximum at move 25 // Usage of increment follows quadratic distribution with the maximum at move 25
double inc = myInc * std::max(55.0, 120.0 - 0.12 * (moveNumber - 25) * (moveNumber - 25)); double inc = myInc * std::max(55.0, 120 - 0.12 * (moveNum - 25) * (moveNum - 25));
// In moves-to-go we distribute time according to a quadratic function with // In moves-to-go we distribute time according to a quadratic function with
// the maximum around move 20 for 40 moves in y time case. // the maximum around move 20 for 40 moves in y time case.
@ -49,28 +47,26 @@ namespace {
{ {
ratio = (type == OptimumTime ? 1.0 : 6.0) / std::min(50, movesToGo); ratio = (type == OptimumTime ? 1.0 : 6.0) / std::min(50, movesToGo);
if (moveNumber <= 40) if (moveNum <= 40)
ratio *= 1.1 - 0.001 * (moveNumber - 20) * (moveNumber - 20); ratio *= 1.1 - 0.001 * (moveNum - 20) * (moveNum - 20);
else else
ratio *= 1.5; ratio *= 1.5;
ratio *= 1 + inc / (myTime * 8.5);
} }
// Otherwise we increase usage of remaining time as the game goes on // Otherwise we increase usage of remaining time as the game goes on
else else
{ {
sd = 1 + 20 * moveNumber / (500.0 + moveNumber); double k = 1 + 20 * moveNum / (500.0 + moveNum);
ratio = (type == OptimumTime ? 0.017 : 0.07) * sd; ratio = (type == OptimumTime ? 0.017 : 0.07) * (k + inc / myTime);
} }
ratio = std::min(1.0, ratio * (1 + inc / (myTime * sd))); int time = int(std::min(1.0, ratio) * std::max(0, myTime - moveOverhead));
assert(ratio <= 1); if (type == OptimumTime && ponder)
time *= 1.25;
if (ponder && type == OptimumTime) if (type == MaxTime)
ratio *= 1.25;
int time = int(ratio * (myTime - moveOverhead));
if (type == OptimumTime)
time -= 10; // Keep always at least 10 millisecs on the clock time -= 10; // Keep always at least 10 millisecs on the clock
return std::max(0, time); return std::max(0, time);
@ -109,9 +105,11 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply)
limits.npmsec = npmsec; limits.npmsec = npmsec;
} }
int moveNum = (ply + 1) / 2;
startTime = limits.startTime; startTime = limits.startTime;
optimumTime = remaining(limits.time[us], limits.inc[us], moveOverhead, optimumTime = remaining(limits.time[us], limits.inc[us], moveOverhead,
limits.movestogo, ply, ponder, OptimumTime); limits.movestogo, moveNum, ponder, OptimumTime);
maximumTime = remaining(limits.time[us], limits.inc[us], moveOverhead, maximumTime = remaining(limits.time[us], limits.inc[us], moveOverhead,
limits.movestogo, ply, ponder, MaxTime); limits.movestogo, moveNum, ponder, MaxTime);
} }