]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Use see() instead of see_sign() in previous patch
[stockfish] / src / search.cpp
index dfe690b4d6498ec65c016c200696acb39175ca15..9a6bd86386807019e73d2eaba746fb6e3a0d0b44 100644 (file)
 #include <iostream>
 #include <sstream>
 
-#include "book.h"
 #include "evaluate.h"
 #include "movegen.h"
 #include "movepick.h"
 #include "notation.h"
+#include "rkiss.h"
 #include "search.h"
 #include "timeman.h"
 #include "thread.h"
@@ -180,8 +180,6 @@ uint64_t Search::perft(Position& pos, Depth depth) {
 
 void Search::think() {
 
-  static PolyglotBook book; // Defined static to initialize the PRNG only once
-
   RootColor = RootPos.side_to_move();
   TimeMgr.init(Limits, RootPos.game_ply(), RootColor);
 
@@ -199,17 +197,6 @@ void Search::think() {
       goto finalize;
   }
 
-  if (Options["OwnBook"] && !Limits.infinite && !Limits.mate)
-  {
-      Move bookMove = book.probe(RootPos, Options["Book File"], Options["Best Book Move"]);
-
-      if (bookMove && std::count(RootMoves.begin(), RootMoves.end(), bookMove))
-      {
-          std::swap(RootMoves[0], *std::find(RootMoves.begin(), RootMoves.end(), bookMove));
-          goto finalize;
-      }
-  }
-
   if (Options["Write Search Log"])
   {
       Log log(Options["Search Log Filename"]);
@@ -582,6 +569,10 @@ namespace {
         &&  abs(beta) < VALUE_MATE_IN_MAX_PLY
         && !pos.pawn_on_7th(pos.side_to_move()))
     {
+        if (   depth <= ONE_PLY
+            && eval + razor_margin(3 * ONE_PLY) <= alpha)
+            return qsearch<NonPV, false>(pos, ss, alpha, beta, DEPTH_ZERO);
+
         Value ralpha = alpha - razor_margin(depth);
         Value v = qsearch<NonPV, false>(pos, ss, ralpha, ralpha+1, DEPTH_ZERO);
         if (v <= ralpha)
@@ -877,6 +868,13 @@ moves_loop: // When in check and at SpNode search starts from here
           if (move == countermoves[0] || move == countermoves[1])
               ss->reduction = std::max(DEPTH_ZERO, ss->reduction - ONE_PLY);
 
+          // Decrease reduction for moves that escape a capture
+          if (   ss->reduction
+              && type_of(move) == NORMAL
+              && type_of(pos.piece_on(to_sq(move))) != PAWN
+              && pos.see(make_move(to_sq(move), from_sq(move))) < 0)
+              ss->reduction = std::max(DEPTH_ZERO, ss->reduction - ONE_PLY);
+
           Depth d = std::max(newDepth - ss->reduction, ONE_PLY);
           if (SpNode)
               alpha = splitPoint->alpha;
@@ -1017,18 +1015,18 @@ moves_loop: // When in check and at SpNode search starts from here
     // must be mate or stalemate. If we are in a singular extension search then
     // return a fail low score.
     if (!moveCount)
-        return  excludedMove ? alpha
-              : inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
+        bestValue = excludedMove ? alpha
+                   :     inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
+
+    // Quiet best move: update killers, history, countermoves and followupmoves
+    else if (bestValue >= beta && !pos.capture_or_promotion(bestMove) && !inCheck)
+        update_stats(pos, ss, bestMove, depth, quietsSearched, quietCount - 1);
 
     TT.store(posKey, value_to_tt(bestValue, ss->ply),
              bestValue >= beta  ? BOUND_LOWER :
              PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER,
              depth, bestMove, ss->staticEval);
 
-    // Quiet best move: update killers, history, countermoves and followupmoves
-    if (bestValue >= beta && !pos.capture_or_promotion(bestMove) && !inCheck)
-        update_stats(pos, ss, bestMove, depth, quietsSearched, quietCount - 1);
-
     assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
 
     return bestValue;
@@ -1395,28 +1393,31 @@ void RootMove::extract_pv_from_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_6], *st = state;
   const TTEntry* tte;
-  int ply = 0;
-  Move m = pv[0];
+  int ply = 1;    // At root ply is 1...
+  Move m = pv[0]; // ...instead pv[] array starts from 0
+  Value expectedScore = score;
 
   pv.clear();
 
   do {
       pv.push_back(m);
 
-      assert(MoveList<LEGAL>(pos).contains(pv[ply]));
+      assert(MoveList<LEGAL>(pos).contains(pv[ply - 1]));
 
-      pos.do_move(pv[ply++], *st++);
+      pos.do_move(pv[ply++ - 1], *st++);
       tte = TT.probe(pos.key());
+      expectedScore = -expectedScore;
 
   } while (   tte
+           && expectedScore == value_from_tt(tte->value(), ply)
            && pos.pseudo_legal(m = tte->move()) // Local copy, TT could change
            && pos.legal(m, pos.pinned_pieces(pos.side_to_move()))
            && ply < MAX_PLY
-           && (!pos.is_draw() || ply < 2));
+           && (!pos.is_draw() || ply <= 2));
 
   pv.push_back(MOVE_NONE); // Must be zero-terminating
 
-  while (ply) pos.undo_move(pv[--ply]);
+  while (--ply) pos.undo_move(pv[ply - 1]);
 }
 
 
@@ -1428,21 +1429,21 @@ void RootMove::insert_pv_in_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_6], *st = state;
   const TTEntry* tte;
-  int ply = 0;
+  int idx = 0; // Ply starts from 1, we need to start from 0
 
   do {
       tte = TT.probe(pos.key());
 
-      if (!tte || tte->move() != pv[ply]) // Don't overwrite correct entries
-          TT.store(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[ply], VALUE_NONE);
+      if (!tte || tte->move() != pv[idx]) // Don't overwrite correct entries
+          TT.store(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[idx], VALUE_NONE);
 
-      assert(MoveList<LEGAL>(pos).contains(pv[ply]));
+      assert(MoveList<LEGAL>(pos).contains(pv[idx]));
 
-      pos.do_move(pv[ply++], *st++);
+      pos.do_move(pv[idx++], *st++);
 
-  } while (pv[ply] != MOVE_NONE);
+  } while (pv[idx] != MOVE_NONE);
 
-  while (ply) pos.undo_move(pv[--ply]);
+  while (idx) pos.undo_move(pv[--idx]);
 }
 
 
@@ -1552,7 +1553,7 @@ void Thread::idle_loop() {
           if (Threads.size() > 2)
               for (size_t i = 0; i < Threads.size(); ++i)
               {
-                  int size = Threads[i]->splitPointsSize; // Local copy
+                  const int size = Threads[i]->splitPointsSize; // Local copy
                   sp = size ? &Threads[i]->splitPoints[size - 1] : NULL;
 
                   if (   sp