]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Do not use evaluate() for razoring
[stockfish] / src / search.cpp
index 80b907aa7d8450c8fa75899545c898ddf4ce88f4..c2974dcfeebffdc2671955a17d5a3cb19c1e43ac 100644 (file)
@@ -22,7 +22,6 @@
 ////
 
 #include <cassert>
-#include <cstdio>
 #include <fstream>
 #include <iostream>
 #include <sstream>
@@ -181,7 +180,7 @@ namespace {
   // Time managment variables
   int SearchStartTime;
   int MaxNodes, MaxDepth;
-  int MaxSearchTime, AbsoluteMaxSearchTime, ExtraSearchTime;
+  int MaxSearchTime, AbsoluteMaxSearchTime, ExtraSearchTime, TimeAdvantage;
   Move BestRootMove, PonderMove, EasyMove;
   int RootMoveNumber;
   bool InfiniteSearch;
@@ -300,9 +299,9 @@ History H;  // Should be made local?
 /// the program receives the UCI 'go' command.  It initializes various
 /// search-related global variables, and calls root_search()
 
-void think(const Position &pos, bool infinite, bool ponder, int time,
-           int increment, int movesToGo, int maxDepth, int maxNodes,
-           int maxTime, Move searchMoves[]) {
+void think(const Position &pos, bool infinite, bool ponder, int side_to_move,
+           int time[], int increment[], int movesToGo, int maxDepth,
+           int maxNodes, int maxTime, Move searchMoves[]) {
 
   // Look for a book move:
   if(!infinite && !ponder && get_option_value_bool("OwnBook")) {
@@ -342,7 +341,7 @@ void think(const Position &pos, bool infinite, bool ponder, int time,
   TT.set_size(get_option_value_int("Hash"));
   if(button_was_pressed("Clear Hash"))
     TT.clear();
-  PonderingEnabled = get_option_value_int("Ponder");
+  PonderingEnabled = get_option_value_bool("Ponder");
   MultiPV = get_option_value_int("MultiPV");
 
   CheckExtension[1] = Depth(get_option_value_int("Check Extension (PV nodes)"));
@@ -407,14 +406,6 @@ void think(const Position &pos, bool infinite, bool ponder, int time,
     init_eval(ActiveThreads);
   }
 
-  // Write information to search log file:
-  if(UseLogFile) {
-    LogFile << "Searching: " << pos.to_fen() << '\n';
-    LogFile << "infinite: " << infinite << " ponder: " << ponder
-            << " time: " << time << " increment: " << increment
-            << " moves to go: " << movesToGo << '\n';
-  }
-
   // Wake up sleeping threads:
   wake_sleeping_threads();
 
@@ -422,24 +413,30 @@ void think(const Position &pos, bool infinite, bool ponder, int time,
     assert(thread_is_available(i, 0));
 
   // Set thinking time:
+  int myTime = time[side_to_move];
+  int myIncrement = increment[side_to_move];
+  int oppTime = time[1 - side_to_move];
+  
+  TimeAdvantage = myTime - oppTime;
+
   if(!movesToGo) { // Sudden death time control
     if(increment) {
-      MaxSearchTime = time / 30 + increment;
-      AbsoluteMaxSearchTime = Max(time / 4, increment - 100);
+      MaxSearchTime = myTime / 30 + myIncrement;
+      AbsoluteMaxSearchTime = Max(myTime / 4, myIncrement - 100);
     }
     else { // Blitz game without increment
-      MaxSearchTime = time / 40;
-      AbsoluteMaxSearchTime = time / 8;
+      MaxSearchTime = myTime / 40;
+      AbsoluteMaxSearchTime = myTime / 8;
     }
   }
   else { // (x moves) / (y minutes)
     if(movesToGo == 1) {
-      MaxSearchTime = time / 2;
-      AbsoluteMaxSearchTime = Min(time / 2, time - 500);
+      MaxSearchTime = myTime / 2;
+      AbsoluteMaxSearchTime = Min(myTime / 2, myTime - 500);
     }
     else {
-      MaxSearchTime = time / Min(movesToGo, 20);
-      AbsoluteMaxSearchTime = Min((4 * time) / movesToGo, time / 3);
+      MaxSearchTime = myTime / Min(movesToGo, 20);
+      AbsoluteMaxSearchTime = Min((4 * myTime) / movesToGo, myTime / 3);
     }
   }
   if(PonderingEnabled) {
@@ -460,6 +457,15 @@ void think(const Position &pos, bool infinite, bool ponder, int time,
   else
     NodesBetweenPolls = 30000;
 
+
+  // Write information to search log file:
+  if(UseLogFile) {
+    LogFile << "Searching: " << pos.to_fen() << '\n';
+    LogFile << "infinite: " << infinite << " ponder: " << ponder
+            << " time: " << myTime << " increment: " << myIncrement
+            << " moves to go: " << movesToGo << '\n';
+  }
+
   // We're ready to start thinking.  Call the iterative deepening loop
   // function:
   id_loop(pos, searchMoves);
@@ -635,6 +641,10 @@ namespace {
             BestMoveChangesByIteration[Iteration] * (MaxSearchTime / 2) +
             BestMoveChangesByIteration[Iteration-1] * (MaxSearchTime / 3);
 
+        // If we need some more and we are in time advantage take it.
+        if (ExtraSearchTime > 0 && TimeAdvantage > 2 * MaxSearchTime)
+            ExtraSearchTime += MaxSearchTime / 2;
+
         // Stop search if most of MaxSearchTime is consumed at the end of the
         // iteration.  We probably don't have enough time to search the first
         // move at the next iteration anyway.
@@ -1112,9 +1122,7 @@ namespace {
         }
     }
     // Null move search not allowed, try razoring
-    else if (   depth < RazorDepth
-             && approximateEval < beta - RazorMargin
-             && evaluate(pos, ei, threadID) < beta - RazorMargin)
+    else if (depth < RazorDepth && approximateEval < beta - RazorMargin)
     {
         Value v = qsearch(pos, ss, beta-1, beta, Depth(0), ply, threadID);
         if (v < beta)
@@ -1516,8 +1524,8 @@ namespace {
     // If this is the master thread and we have been asked to stop because of
     // a beta cutoff higher up in the tree, stop all slave threads:
     if (sp->master == threadID && thread_should_stop(threadID))
-        for(int i = 0; i < ActiveThreads; i++)
-            if(sp->slaves[i])
+        for (int i = 0; i < ActiveThreads; i++)
+            if (sp->slaves[i])
                 Threads[i].stop = true;
 
     sp->cpus--;
@@ -1536,6 +1544,7 @@ namespace {
   // after we return from the split point.
 
   void sp_search_pv(SplitPoint *sp, int threadID) {
+
     assert(threadID >= 0 && threadID < ActiveThreads);
     assert(ActiveThreads > 1);
 
@@ -1543,12 +1552,11 @@ namespace {
     SearchStack *ss = sp->sstack[threadID];
     Value value;
     Move move;
-    int moveCount = sp->moves;
 
-    while(sp->alpha < sp->beta && !thread_should_stop(threadID)
-          && (move = sp->mp->get_next_move(sp->lock)) != MOVE_NONE) {
-      UndoInfo u;
-      Depth ext, newDepth;
+    while (    sp->alpha < sp->beta
+           && !thread_should_stop(threadID)
+           && (move = sp->mp->get_next_move(sp->lock)) != MOVE_NONE)
+    {
       bool moveIsCheck = pos.move_is_check(move, sp->dcCandidates);
       bool moveIsCapture = pos.move_is_capture(move);
       bool moveIsPassedPawnPush = pos.move_is_passed_pawn_push(move);
@@ -1559,74 +1567,88 @@ namespace {
         PawnValueMidgame : pos.midgame_value_of_piece_on(move_to(move));
 
       lock_grab(&(sp->lock));
-      sp->moves++;
-      moveCount = sp->moves;
+      int moveCount = ++sp->moves;
       lock_release(&(sp->lock));
 
       ss[sp->ply].currentMove = move;
 
       // Decide the new search depth.
-      ext = extension(pos, move, true, moveIsCheck, false, false);
-      newDepth = sp->depth - OnePly + ext;
+      Depth ext = extension(pos, move, true, moveIsCheck, false, false);
+      Depth newDepth = sp->depth - OnePly + ext;
 
       // Make and search the move.
+      UndoInfo u;
       pos.do_move(move, u, sp->dcCandidates);
-      if(ext == Depth(0) && moveCount >= LMRPVMoves && !moveIsCapture
-         && !move_promotion(move) && !moveIsPassedPawnPush
-         && !move_is_castle(move)
-         && move != ss[sp->ply].killer1 && move != ss[sp->ply].killer2) {
-        ss[sp->ply].reduction = OnePly;
-        value = -search(pos, ss, -sp->alpha, newDepth - OnePly, sp->ply+1,
-                        true, threadID);
+
+      // Try to reduce non-pv search depth by one ply if move seems not problematic,
+      // if the move fails high will be re-searched at full depth.
+      if (    ext == Depth(0)
+          &&  moveCount >= LMRPVMoves
+          && !moveIsCapture
+          && !moveIsPassedPawnPush
+          && !move_promotion(move)
+          && !move_is_castle(move)
+          &&  move != ss[sp->ply].killer1
+          &&  move != ss[sp->ply].killer2)
+      {
+          ss[sp->ply].reduction = OnePly;
+          value = -search(pos, ss, -sp->alpha, newDepth - OnePly, sp->ply+1, true, threadID);
       }
       else
-        value = sp->alpha + 1;
-      if(value > sp->alpha) {
-        ss[sp->ply].reduction = Depth(0);
-        value = -search(pos, ss, -sp->alpha, newDepth, sp->ply+1, true,
-                        threadID);
-        if(value > sp->alpha && value < sp->beta) {
-          if(sp->ply == 1 && RootMoveNumber == 1)
-            // When the search fails high at ply 1 while searching the first
-            // move at the root, set the flag failHighPly1.  This is used for
-            // time managment:  We don't want to stop the search early in
-            // such cases, because resolving the fail high at ply 1 could
-            // result in a big drop in score at the root.
-            Threads[threadID].failHighPly1 = true;
-          value = -search_pv(pos, ss, -sp->beta, -sp->alpha, newDepth,
-                             sp->ply+1, threadID);
-          Threads[threadID].failHighPly1 = false;
+          value = sp->alpha + 1; // Just to trigger next condition
+
+      if (value > sp->alpha) // Go with full depth non-pv search
+      {
+          ss[sp->ply].reduction = Depth(0);
+          value = -search(pos, ss, -sp->alpha, newDepth, sp->ply+1, true, threadID);
+
+          if (value > sp->alpha && value < sp->beta)
+          {
+              // When the search fails high at ply 1 while searching the first
+              // move at the root, set the flag failHighPly1.  This is used for
+              // time managment:  We don't want to stop the search early in
+              // such cases, because resolving the fail high at ply 1 could
+              // result in a big drop in score at the root.
+              if (sp->ply == 1 && RootMoveNumber == 1)
+                  Threads[threadID].failHighPly1 = true;
+
+              value = -search_pv(pos, ss, -sp->beta, -sp->alpha, newDepth, sp->ply+1, threadID);
+              Threads[threadID].failHighPly1 = false;
         }
       }
       pos.undo_move(move, u);
 
       assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
 
-      if(thread_should_stop(threadID))
-        break;
+      if (thread_should_stop(threadID))
+          break;
 
       // New best move?
       lock_grab(&(sp->lock));
-      if(value > sp->bestValue && !thread_should_stop(threadID)) {
-        sp->bestValue = value;
-        if(value > sp->alpha) {
-          sp->alpha = value;
-          sp_update_pv(sp->parentSstack, ss, sp->ply);
-          if(value == value_mate_in(sp->ply + 1))
-            ss[sp->ply].mateKiller = move;
-          if(value >= sp->beta) {
-            for(int i = 0; i < ActiveThreads; i++)
-              if(i != threadID && (i == sp->master || sp->slaves[i]))
-                Threads[i].stop = true;
-            sp->finished = true;
-          }
+      if (value > sp->bestValue && !thread_should_stop(threadID))
+      {
+          sp->bestValue = value;
+          if (value > sp->alpha)
+          {
+              sp->alpha = value;
+              sp_update_pv(sp->parentSstack, ss, sp->ply);
+              if (value == value_mate_in(sp->ply + 1))
+                  ss[sp->ply].mateKiller = move;
+
+              if(value >= sp->beta)
+              {
+                  for(int i = 0; i < ActiveThreads; i++)
+                      if(i != threadID && (i == sp->master || sp->slaves[i]))
+                          Threads[i].stop = true;
+
+                  sp->finished = true;
+              }
         }
         // If we are at ply 1, and we are searching the first root move at
         // ply 0, set the 'Problem' variable if the score has dropped a lot
         // (from the computer's point of view) since the previous iteration:
-        if(Iteration >= 2 &&
-           -value <= ValueByIteration[Iteration-1] - ProblemMargin)
-          Problem = true;
+        if (Iteration >= 2 && -value <= ValueByIteration[Iteration-1] - ProblemMargin)
+            Problem = true;
       }
       lock_release(&(sp->lock));
     }
@@ -1635,10 +1657,10 @@ namespace {
 
     // If this is the master thread and we have been asked to stop because of
     // a beta cutoff higher up in the tree, stop all slave threads:
-    if(sp->master == threadID && thread_should_stop(threadID))
-      for(int i = 0; i < ActiveThreads; i++)
-        if(sp->slaves[i])
-          Threads[i].stop = true;
+    if (sp->master == threadID && thread_should_stop(threadID))
+        for (int i = 0; i < ActiveThreads; i++)
+            if (sp->slaves[i])
+                Threads[i].stop = true;
 
     sp->cpus--;
     sp->slaves[threadID] = 0;
@@ -2111,62 +2133,64 @@ namespace {
   // search.
 
   void poll() {
-    int t, data;
-    static int lastInfoTime;
 
-    t = current_search_time();
+    static int lastInfoTime;
+    int t = current_search_time();
 
     //  Poll for input
-    data = Bioskey();
-    if(data) {
-      char input[256];
-      if(fgets(input, 255, stdin) == NULL)
-        strcpy(input, "quit\n");
-      if(strncmp(input, "quit", 4) == 0) {
-        AbortSearch = true;
-        PonderSearch = false;
-        Quit = true;
-      }
-      else if(strncmp(input, "stop", 4) == 0) {
-        AbortSearch = true;
-        PonderSearch = false;
-      }
-      else if(strncmp(input, "ponderhit", 9) == 0)
-        ponderhit();
-    }
+    if (Bioskey())
+    {
+        // We are line oriented, don't read single chars
+        std::string command;
+        if (!std::getline(std::cin, command))
+            command = "quit";
 
-    // Print search information
-    if(t < 1000)
-      lastInfoTime = 0;
-    else if(lastInfoTime > t)
-      // HACK: Must be a new search where we searched less than
-      // NodesBetweenPolls nodes during the first second of search.
-      lastInfoTime = 0;
-    else if(t - lastInfoTime >= 1000) {
-      lastInfoTime = t;
-      lock_grab(&IOLock);
-      std::cout << "info nodes " << nodes_searched() << " nps " << nps()
-                << " time " << t << " hashfull " << TT.full() << std::endl;
-      lock_release(&IOLock);
-      if(ShowCurrentLine)
-        Threads[0].printCurrentLine = true;
+        if (command == "quit")
+        {
+            AbortSearch = true;
+            PonderSearch = false;
+            Quit = true;
+        }
+        else if(command == "stop")
+        {
+            AbortSearch = true;
+            PonderSearch = false;
+        }
+        else if(command == "ponderhit")
+            ponderhit();
     }
+    // Print search information
+    if (t < 1000)
+        lastInfoTime = 0;
+
+    else if (lastInfoTime > t)
+        // HACK: Must be a new search where we searched less than
+        // NodesBetweenPolls nodes during the first second of search.
+        lastInfoTime = 0;
 
+    else if (t - lastInfoTime >= 1000)
+    {
+        lastInfoTime = t;
+        lock_grab(&IOLock);
+        std::cout << "info nodes " << nodes_searched() << " nps " << nps()
+                  << " time " << t << " hashfull " << TT.full() << std::endl;
+        lock_release(&IOLock);
+        if (ShowCurrentLine)
+            Threads[0].printCurrentLine = true;
+    }
     // Should we stop the search?
-    if(!PonderSearch && Iteration >= 2 &&
-       (!InfiniteSearch && (t > AbsoluteMaxSearchTime ||
-                            (RootMoveNumber == 1 &&
-                             t > MaxSearchTime + ExtraSearchTime) ||
-                            (!FailHigh && !fail_high_ply_1() && !Problem &&
-                             t > 6*(MaxSearchTime + ExtraSearchTime)))))
-      AbortSearch = true;
+    if (PonderSearch)
+        return;
 
-    if(!PonderSearch && ExactMaxTime && t >= ExactMaxTime)
-      AbortSearch = true;
+    bool overTime =     t > AbsoluteMaxSearchTime
+                     || (RootMoveNumber == 1 && t > MaxSearchTime + ExtraSearchTime)
+                     || (  !FailHigh && !fail_high_ply_1() && !Problem 
+                         && t > 6*(MaxSearchTime + ExtraSearchTime));
 
-    if(!PonderSearch && Iteration >= 3 && MaxNodes
-       && nodes_searched() >= MaxNodes)
-      AbortSearch = true;
+    if (   (Iteration >= 2 && (!InfiniteSearch && overTime))
+        || (ExactMaxTime && t >= ExactMaxTime)
+        || (Iteration >= 3 && MaxNodes && nodes_searched() >= MaxNodes))
+        AbortSearch = true;
   }