2011-11-12 19:44:06 +00:00
|
|
|
/*
|
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2015-02-01 00:46:09 +00:00
|
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
2020-01-18 08:00:03 +01:00
|
|
|
Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
2011-11-12 19:44:06 +00:00
|
|
|
|
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-01-01 00:52:19 +00:00
|
|
|
#include <algorithm>
|
2018-02-01 18:45:14 +01:00
|
|
|
#include <cfloat>
|
|
|
|
|
#include <cmath>
|
2011-11-12 19:44:06 +00:00
|
|
|
|
|
|
|
|
#include "search.h"
|
|
|
|
|
#include "timeman.h"
|
2015-02-01 00:46:09 +00:00
|
|
|
#include "uci.h"
|
2011-11-12 19:44:06 +00:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
TimeManagement Time; // Our global time management object
|
|
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
/// init() is called at the beginning of the search and calculates the bounds
|
|
|
|
|
/// of time allowed for the current game ply. We currently support:
|
|
|
|
|
// 1) x basetime (+z increment)
|
|
|
|
|
// 2) x moves in y seconds (+z increment)
|
2011-11-12 19:44:06 +00:00
|
|
|
|
2018-02-01 18:45:14 +01:00
|
|
|
void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
|
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
TimePoint minThinkingTime = TimePoint(Options["Minimum Thinking Time"]);
|
|
|
|
|
TimePoint moveOverhead = TimePoint(Options["Move Overhead"]);
|
|
|
|
|
TimePoint slowMover = TimePoint(Options["Slow Mover"]);
|
|
|
|
|
TimePoint npmsec = TimePoint(Options["nodestime"]);
|
|
|
|
|
|
|
|
|
|
// opt_scale is a percentage of available time to use for the current move.
|
|
|
|
|
// max_scale is a multiplier applied to optimumTime.
|
|
|
|
|
double opt_scale, max_scale;
|
2015-10-23 22:58:14 +02:00
|
|
|
|
|
|
|
|
// If we have to play in 'nodes as time' mode, then convert from time
|
|
|
|
|
// to nodes, and use resulting values in time management formulas.
|
2018-12-03 20:38:40 +01:00
|
|
|
// WARNING: to avoid time losses, the given npmsec (nodes per millisecond)
|
|
|
|
|
// must be much lower than the real engine speed.
|
2015-10-23 22:58:14 +02:00
|
|
|
if (npmsec)
|
|
|
|
|
{
|
|
|
|
|
if (!availableNodes) // Only once at game start
|
|
|
|
|
availableNodes = npmsec * limits.time[us]; // Time is in msec
|
|
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
// Convert from milliseconds to nodes
|
|
|
|
|
limits.time[us] = TimePoint(availableNodes);
|
2015-10-23 22:58:14 +02:00
|
|
|
limits.inc[us] *= npmsec;
|
|
|
|
|
limits.npmsec = npmsec;
|
|
|
|
|
}
|
2011-11-12 19:44:06 +00:00
|
|
|
|
2017-09-10 10:30:09 +02:00
|
|
|
startTime = limits.startTime;
|
2018-02-01 18:45:14 +01:00
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
//Maximum move horizon of 50 moves
|
|
|
|
|
int mtg = limits.movestogo ? std::min(limits.movestogo, 50) : 50;
|
2018-02-01 18:45:14 +01:00
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
// Make sure timeLeft is > 0 since we may use it as a divisor
|
|
|
|
|
TimePoint timeLeft = std::max(TimePoint(1),
|
|
|
|
|
limits.time[us] + limits.inc[us] * (mtg - 1) - moveOverhead * (2 + mtg));
|
2018-02-01 18:45:14 +01:00
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
// A user may scale time usage by setting UCI option "Slow Mover"
|
|
|
|
|
// Default is 100 and changing this value will probably lose elo.
|
|
|
|
|
timeLeft = slowMover * timeLeft / 100;
|
2018-02-01 18:45:14 +01:00
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
// x basetime (+ z increment)
|
|
|
|
|
// If there is a healthy increment, timeLeft can exceed actual available
|
|
|
|
|
// game time for the current move, so also cap to 20% of available game time.
|
|
|
|
|
if (limits.movestogo == 0)
|
|
|
|
|
{
|
|
|
|
|
opt_scale = std::min(0.008 + std::pow(ply + 3.0, 0.5) / 250.0,
|
|
|
|
|
0.2 * limits.time[us] / double(timeLeft));
|
|
|
|
|
max_scale = 4 + std::min(36, ply) / 12.0;
|
|
|
|
|
}
|
2018-02-01 18:45:14 +01:00
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
// x moves in y seconds (+ z increment)
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
opt_scale = std::min((0.8 + ply / 128.0) / mtg,
|
|
|
|
|
0.8 * limits.time[us] / double(timeLeft));
|
|
|
|
|
max_scale = std::min(6.3, 1.5 + 0.11 * mtg);
|
2018-02-01 18:45:14 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-19 11:49:56 +02:00
|
|
|
// Never use more than 80% of the available time for this move
|
|
|
|
|
optimumTime = std::max(minThinkingTime, TimePoint(opt_scale * timeLeft));
|
|
|
|
|
maximumTime = TimePoint(std::min(0.8 * limits.time[us] - moveOverhead, max_scale * optimumTime));
|
|
|
|
|
|
2018-02-01 18:45:14 +01:00
|
|
|
if (Options["Ponder"])
|
|
|
|
|
optimumTime += optimumTime / 4;
|
2011-11-12 19:44:06 +00:00
|
|
|
}
|