]> git.sesse.net Git - stockfish/commitdiff
Add support for playing in 'nodes as time' mode
authorMarco Costalba <mcostalba@gmail.com>
Sun, 22 Mar 2015 20:15:44 +0000 (21:15 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 3 Apr 2015 02:40:55 +0000 (04:40 +0200)
When running more games in parallel, or simply when running a game
with a background process, due to how OS scheduling works, there is no
guarantee that the CPU resources allocated evenly between the two
players. This introduces noise in the result that leads to unreliable
result and in the worst cases can even invalidate the result. For
instance in SF test framework we avoid running from clouds virtual
machines because are a known source of very unstable CPU speed.

To overcome this issue, without requiring changes to the GUI, the idea
is to use searched nodes instead of time, and to convert time to
available nodes upfront, at the beginning of the game.

When nodestime UCI option is set at a given nodes per milliseconds
(npmsec), at the beginning of the game (and only once), the engine
reads the available time to think, sent by the GUI with 'go wtime x'
UCI command. Then it translates time in available nodes (nodes =
npmsec * x), then feeds available nodes instead of time to the time
management logic and starts the search. During the search the engine
checks the searched nodes against the available ones in such a way
that all the time management logic still fully applies, and the game
mimics a real one played on real time. When the search finishes,
before returning best move, the total available nodes are updated,
subtracting the real searched nodes. After the first move, the time
information sent by the GUI is ignored, and the engine fully relies on
the updated total available nodes to feed time management.

To avoid time losses, the speed of the engine (npms) must be set to a
value lower than real speed so that if the real TC is for instance 30
secs, and npms is half of the real speed, the game will last on
average 15 secs, so much less than the TC limit, providing for a
safety 'time buffer'.

There are 2 main limitations with this mode.

1. Engine speed should be the same for both players, and this limits
the approach to mainly parameter tuning patches.

2. Because npms is fixed while, in real engines, the speed increases
toward endgame, this introduces an artifact that is equivalent to an
altered time management. Namely it is like the time management gives
less available time than what should be in standard case.

May be the second limitation could be mitigated in a future with a
smarter 'dynamic npms' approach.

Tests shows that the standard deviation of the results with 'nodestime'
is lower than in standard TC, as is expected because now all the introduced
noise due the random speed variability of the engines during the game is
fully removed.

Original NIT idea by Michael Hoffman that shows how to play in NIT mode
without requiring changes to the GUI. This implementation goes a bit
further, the key difference is that we read TC from GUI only once upfront
instead of re-reading after every move as in Michael's implementation.

No functional change.

src/search.cpp
src/search.h
src/timeman.cpp
src/timeman.h
src/uci.cpp
src/ucioption.cpp

index 4590bd4a1905c6f3e0baa5931f3284487565fe68..f0c1bdd978e14fdc661c2a4041bb220fce343055 100644 (file)
@@ -129,7 +129,6 @@ namespace {
   };
 
   size_t PVIdx;
-  TimeManagement Time;
   EasyMoveManager EasyMove;
   double BestMoveChanges;
   Value DrawValue[COLOR_NB];
@@ -218,11 +217,12 @@ template uint64_t Search::perft<true>(Position& pos, Depth depth);
 
 void Search::think() {
 
-  Time.init(Limits, RootPos.side_to_move(), RootPos.game_ply(), now());
+  Color us = RootPos.side_to_move();
+  Time.init(Limits, us, RootPos.game_ply(), now());
 
   int contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns
-  DrawValue[ RootPos.side_to_move()] = VALUE_DRAW - Value(contempt);
-  DrawValue[~RootPos.side_to_move()] = VALUE_DRAW + Value(contempt);
+  DrawValue[ us] = VALUE_DRAW - Value(contempt);
+  DrawValue[~us] = VALUE_DRAW + Value(contempt);
 
   TB::Hits = 0;
   TB::RootInTB = false;
@@ -291,6 +291,11 @@ void Search::think() {
       Threads.timer->run = false;
   }
 
+  // When playing in 'nodes as time' mode, subtract the searched nodes from
+  // the available ones before to exit.
+  if (Limits.npmsec)
+      Time.availableNodes += Limits.inc[us] - RootPos.nodes_searched();
+
   // When we reach the maximum depth, we can arrive here without a raise of
   // Signals.stop. However, if we are pondering or in an infinite search,
   // the UCI protocol states that we shouldn't print the best move before the
index ed2c1e1897e93304cebd70fec9f166b3b4c9c07f..5e17b82928b60435fba05cff072d59fac72dc639 100644 (file)
@@ -76,7 +76,7 @@ typedef std::vector<RootMove> RootMoveVector;
 struct LimitsType {
 
   LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC
-    nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = movestogo =
+    nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movestogo =
     depth = movetime = mate = infinite = ponder = 0;
   }
 
@@ -85,7 +85,7 @@ struct LimitsType {
   }
 
   std::vector<Move> searchmoves;
-  int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, movetime, mate, infinite, ponder;
+  int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder;
   int64_t nodes;
 };
 
index 4763671a845edf6805e03a62a668816dc59732b1..7a5db255142e91b6d696f516ccd58e4945e948d9 100644 (file)
@@ -25,6 +25,8 @@
 #include "timeman.h"
 #include "uci.h"
 
+TimeManagement Time; // Our global time management object
+
 namespace {
 
   enum TimeType { OptimumTime, MaxTime };
@@ -78,11 +80,27 @@ namespace {
 ///  inc >  0 && movestogo == 0 means: x basetime + z increment
 ///  inc >  0 && movestogo != 0 means: x moves in y minutes + z increment
 
-void TimeManagement::init(const Search::LimitsType& limits, Color us, int ply, TimePoint now)
+void TimeManagement::init(Search::LimitsType& limits, Color us, int ply, TimePoint now)
 {
   int minThinkingTime = Options["Minimum Thinking Time"];
   int moveOverhead    = Options["Move Overhead"];
   int slowMover       = Options["Slow Mover"];
+  int npmsec          = Options["nodestime"];
+
+  // If we have to play in 'nodes as time' mode, then convert from time
+  // to nodes, and use resulting values in time management formulas.
+  // WARNING: Given npms (nodes per millisecond) must be much lower then
+  // real engine speed to avoid time losses.
+  if (npmsec)
+  {
+      if (!availableNodes) // Only once at game start
+          availableNodes = npmsec * limits.time[us]; // Time is in msec
+
+      // Convert from millisecs to nodes
+      limits.time[us] = (int)availableNodes;
+      limits.inc[us] *= npmsec;
+      limits.npmsec = npmsec;
+  }
 
   start = now;
   unstablePvFactor = 1;
index 3cb59bbad30b6447c26738aa0a46e26c761fdd2d..f2c3663b1eb2d1c68676c53aa2c1fbad1c6c71e5 100644 (file)
 
 class TimeManagement {
 public:
-  void init(const Search::LimitsType& limits, Color us, int ply, TimePoint now);
+  void init(Search::LimitsType& limits, Color us, int ply, TimePoint now);
   void pv_instability(double bestMoveChanges) { unstablePvFactor = 1 + bestMoveChanges; }
   int available() const { return int(optimumTime * unstablePvFactor * 0.76); }
   int maximum() const { return maximumTime; }
   int elapsed() const { return now() - start; }
 
+  int64_t availableNodes; // When in 'nodes as time' mode
+
 private:
   TimePoint start;
   int optimumTime;
@@ -40,4 +42,6 @@ private:
   double unstablePvFactor;
 };
 
+extern TimeManagement Time;
+
 #endif // #ifndef TIMEMAN_H_INCLUDED
index 2c07807c1b678b2ee0bfb26325439b3853781ffa..a9fe4f207e856d715b0a6dc9bb23c7238c9cd28a 100644 (file)
@@ -26,6 +26,7 @@
 #include "position.h"
 #include "search.h"
 #include "thread.h"
+#include "timeman.h"
 #include "tt.h"
 #include "uci.h"
 
@@ -178,8 +179,12 @@ void UCI::loop(int argc, char* argv[]) {
                     << "\n"       << Options
                     << "\nuciok"  << sync_endl;
 
+      else if (token == "ucinewgame")
+      {
+          TT.clear();
+          Time.availableNodes = 0;
+      }
       else if (token == "isready")    sync_cout << "readyok" << sync_endl;
-      else if (token == "ucinewgame") TT.clear();
       else if (token == "go")         go(pos, is);
       else if (token == "position")   position(pos, is);
       else if (token == "setoption")  setoption(is);
index 06641cf5081908aa04c804b4ba2e75c022cc2db2..35c907ae52fb7b368a0feeea36108619adab598e 100644 (file)
@@ -67,6 +67,7 @@ void init(OptionsMap& o) {
   o["Move Overhead"]         << Option(30, 0, 5000);
   o["Minimum Thinking Time"] << Option(20, 0, 5000);
   o["Slow Mover"]            << Option(80, 10, 1000);
+  o["nodestime"]             << Option(0, 0, 10000);
   o["UCI_Chess960"]          << Option(false);
   o["SyzygyPath"]            << Option("<empty>", on_tb_path);
   o["SyzygyProbeDepth"]      << Option(1, 1, 100);