]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Introduce Null Threat extension
[stockfish] / src / search.cpp
index 98f3e3fdf0ca44553b279ff9e024cd7c7cb746ed..c05a714578be3ce0da1b7d4fb0d3fca841b6d96d 100644 (file)
@@ -188,7 +188,7 @@ void Search::think() {
   {
       RootMoves.push_back(MOVE_NONE);
       sync_cout << "info depth 0 score "
-                << score_to_uci(RootPos.in_check() ? -VALUE_MATE : VALUE_DRAW)
+                << score_to_uci(RootPos.checkers() ? -VALUE_MATE : VALUE_DRAW)
                 << sync_endl;
 
       goto finalize;
@@ -208,7 +208,7 @@ void Search::think() {
   if (Options["Contempt Factor"] && !Options["UCI_AnalyseMode"])
   {
       int cf = Options["Contempt Factor"] * PawnValueMg / 100; // From centipawns
-      cf = cf * MaterialTable::game_phase(RootPos) / PHASE_MIDGAME; // Scale down with phase
+      cf = cf * Material::game_phase(RootPos) / PHASE_MIDGAME; // Scale down with phase
       DrawValue[ RootColor] = VALUE_DRAW - Value(cf);
       DrawValue[~RootColor] = VALUE_DRAW + Value(cf);
   }
@@ -482,16 +482,17 @@ namespace {
     Key posKey;
     Move ttMove, move, excludedMove, bestMove, threatMove;
     Depth ext, newDepth;
-    Value bestValue, value, ttValue, ttValueUpper;
+    Value bestValue, value, ttValue;
     Value eval, nullValue, futilityValue;
     bool inCheck, givesCheck, pvMove, singularExtensionNode;
-    bool captureOrPromotion, dangerous, doFullDepthSearch;
+    bool captureOrPromotion, dangerous, doFullDepthSearch, threatExtension;
     int moveCount, playedMoveCount;
 
     // Step 1. Initialize node
     Thread* thisThread = pos.this_thread();
     moveCount = playedMoveCount = 0;
-    inCheck = pos.in_check();
+    threatExtension = false;
+    inCheck = pos.checkers();
 
     if (SpNode)
     {
@@ -544,43 +545,31 @@ namespace {
     tte = TT.probe(posKey);
     ttMove = RootNode ? RootMoves[PVIdx].pv[0] : tte ? tte->move() : MOVE_NONE;
     ttValue = tte ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
-    ttValueUpper = tte ? value_from_tt(tte->value_upper(), ss->ply) : VALUE_NONE;
 
     // At PV nodes we check for exact scores, while at non-PV nodes we check for
     // a fail high/low. Biggest advantage at probing at PV nodes is to have a
     // smooth experience in analysis mode. We don't probe at Root nodes otherwise
     // we should also update RootMoveList to avoid bogus output.
-    if (!RootNode && tte)
+    if (   !RootNode
+        && tte
+        && tte->depth() >= depth
+        && ttValue != VALUE_NONE // Only in case of TT access race
+        && (           PvNode ?  tte->type() == BOUND_EXACT
+            : ttValue >= beta ? (tte->type() & BOUND_LOWER)
+                              : (tte->type() & BOUND_UPPER)))
     {
-        // Fail High
-        if (  (tte->type() & BOUND_LOWER)
-            && ttValue >= beta
-            && tte->depth() >= depth
-            && ttValue != VALUE_NONE) // Only in case of TT access race
-        {
-            // Update killers, we assume ttMove caused a cut-off
-            if (    ttMove
-                && !pos.is_capture_or_promotion(ttMove)
-                &&  ttMove != ss->killers[0])
-            {
-                ss->killers[1] = ss->killers[0];
-                ss->killers[0] = ttMove;
-            }
-            TT.refresh(tte);
-            ss->currentMove = ttMove; // Can be MOVE_NONE
-            return ttValue;
-        }
+        TT.refresh(tte);
+        ss->currentMove = ttMove; // Can be MOVE_NONE
 
-        // Fail Low
-        if (  (tte->type() & BOUND_UPPER)
-            && ttValueUpper <= alpha
-            && tte->depth_upper() >= depth
-            && ttValueUpper != VALUE_NONE) // Only in case of TT access race
+        if (    ttValue >= beta
+            &&  ttMove
+            && !pos.is_capture_or_promotion(ttMove)
+            &&  ttMove != ss->killers[0])
         {
-            TT.refresh(tte);
-            ss->currentMove = ttMove; // Can be MOVE_NONE
-            return ttValueUpper;
+            ss->killers[1] = ss->killers[0];
+            ss->killers[0] = ttMove;
         }
+        return ttValue;
     }
 
     // Step 5. Evaluate the position statically and update parent's gain statistics
@@ -687,16 +676,15 @@ namespace {
             // The null move failed low, which means that we may be faced with
             // some kind of threat. If the previous move was reduced, check if
             // the move that refuted the null move was somehow connected to the
-            // move which was reduced. If a connection is found, return a fail
-            // low score (which will cause the reduced move to fail high in the
-            // parent node, which will trigger a re-search with full depth).
+            // move which was reduced. If a connection is found extend moves that
+            // defend against threat.
             threatMove = (ss+1)->currentMove;
 
             if (   depth < 5 * ONE_PLY
                 && (ss-1)->reduction
                 && threatMove != MOVE_NONE
                 && yields_to_threat(pos, (ss-1)->currentMove, threatMove))
-                return beta - 1;
+                threatExtension = true;
         }
     }
 
@@ -814,6 +802,9 @@ split_point_start: // At split points actual search starts from here
       if (PvNode && dangerous)
           ext = ONE_PLY;
 
+      else if (threatExtension && prevents_threat(pos, move, threatMove))
+          ext = ONE_PLY;
+
       else if (givesCheck && pos.see_sign(move) >= 0)
           ext = ONE_PLY / 2;
 
@@ -1015,9 +1006,10 @@ split_point_start: // At split points actual search starts from here
       // Step 19. Check for splitting the search
       if (   !SpNode
           &&  depth >= Threads.min_split_depth()
-          &&  bestValue < beta
           &&  Threads.available_slave_exists(thisThread))
       {
+          assert(bestValue < beta);
+
           bestValue = Threads.split<FakeSplit>(pos, ss, alpha, beta, bestValue, &bestMove,
                                                depth, threatMove, moveCount, mp, NT);
           if (bestValue >= beta)
@@ -1092,7 +1084,7 @@ split_point_start: // At split points actual search starts from here
     const bool PvNode = (NT == PV);
 
     assert(NT == PV || NT == NonPV);
-    assert(InCheck == pos.in_check());
+    assert(InCheck == !!pos.checkers());
     assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
     assert(PvNode || (alpha == beta - 1));
     assert(depth <= DEPTH_ZERO);
@@ -1101,7 +1093,7 @@ split_point_start: // At split points actual search starts from here
     const TTEntry* tte;
     Key posKey;
     Move ttMove, move, bestMove;
-    Value bestValue, value, ttValue, ttValueUpper, futilityValue, futilityBase, oldAlpha;
+    Value bestValue, value, ttValue, futilityValue, futilityBase, oldAlpha;
     bool givesCheck, enoughMaterial, evasionPrunable, fromNull;
     Depth ttDepth;
 
@@ -1123,34 +1115,21 @@ split_point_start: // At split points actual search starts from here
     tte = TT.probe(posKey);
     ttMove = tte ? tte->move() : MOVE_NONE;
     ttValue = tte ? value_from_tt(tte->value(),ss->ply) : VALUE_NONE;
-    ttValueUpper = tte ? value_from_tt(tte->value_upper(),ss->ply) : VALUE_NONE;
 
     // Decide whether or not to include checks, this fixes also the type of
     // TT entry depth that we are going to use. Note that in qsearch we use
     // only two types of depth in TT: DEPTH_QS_CHECKS or DEPTH_QS_NO_CHECKS.
     ttDepth = InCheck || depth >= DEPTH_QS_CHECKS ? DEPTH_QS_CHECKS
                                                   : DEPTH_QS_NO_CHECKS;
-    if (tte)
+    if (   tte
+        && tte->depth() >= ttDepth
+        && ttValue != VALUE_NONE // Only in case of TT access race
+        && (           PvNode ?  tte->type() == BOUND_EXACT
+            : ttValue >= beta ? (tte->type() & BOUND_LOWER)
+                              : (tte->type() & BOUND_UPPER)))
     {
-        // Fail High
-        if (  (tte->type() & BOUND_LOWER)
-            && ttValue >= beta
-            && tte->depth() >= ttDepth
-            && ttValue != VALUE_NONE) // Only in case of TT access race
-        {
-            ss->currentMove = ttMove; // Can be MOVE_NONE
-            return ttValue;
-        }
-
-        // Fail Low
-        if (  (tte->type() & BOUND_UPPER)
-            && ttValueUpper <= alpha
-            && tte->depth_upper() >= ttDepth
-            && ttValueUpper != VALUE_NONE) // Only in case of TT access race
-        {
-            ss->currentMove = ttMove; // Can be MOVE_NONE
-            return ttValueUpper;
-        }
+        ss->currentMove = ttMove; // Can be MOVE_NONE
+        return ttValue;
     }
 
     // Evaluate the position statically
@@ -1564,7 +1543,8 @@ void RootMove::extract_pv_from_tt(Position& pos) {
   do {
       pv.push_back(m);
 
-      assert(pos.move_is_legal(pv[ply]));
+      assert(MoveList<LEGAL>(pos).contains(pv[ply]));
+
       pos.do_move(pv[ply++], *st++);
       tte = TT.probe(pos.key());
 
@@ -1596,7 +1576,8 @@ void RootMove::insert_pv_in_tt(Position& pos) {
       if (!tte || tte->move() != pv[ply]) // Don't overwrite correct entries
           TT.store(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[ply]);
 
-      assert(pos.move_is_legal(pv[ply]));
+      assert(MoveList<LEGAL>(pos).contains(pv[ply]));
+
       pos.do_move(pv[ply++], *st++);
 
   } while (pv[ply] != MOVE_NONE);