]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Retire RootMove::nodes
[stockfish] / src / search.cpp
index 4cf8f47122e5143bed143abbf9c1e876a38844d7..baae494b9e6073f656a2ca715d7d91bf63cf843b 100644 (file)
@@ -1,7 +1,7 @@
 /*
   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
-  Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
+  Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
 
   Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -67,7 +67,6 @@ namespace {
 
     RootMove(){}
     RootMove(Move m) {
-      nodes = 0;
       score = prevScore = -VALUE_INFINITE;
       pv.push_back(m);
       pv.push_back(MOVE_NONE);
@@ -79,7 +78,6 @@ namespace {
     void extract_pv_from_tt(Position& pos);
     void insert_pv_in_tt(Position& pos);
 
-    int64_t nodes;
     Value score;
     Value prevScore;
     std::vector<Move> pv;
@@ -296,12 +294,12 @@ void Search::think() {
           || count(SearchMoves.begin(), SearchMoves.end(), ml.move()))
           RootMoves.push_back(RootMove(ml.move()));
 
-  if (Options["OwnBook"].value<bool>())
+  if (Options["OwnBook"])
   {
-      if (Options["Book File"].value<string>() != book.name())
-          book.open(Options["Book File"].value<string>());
+      if (book.name() != (string)Options["Book File"])
+          book.open(Options["Book File"]);
 
-      Move bookMove = book.probe(pos, Options["Best Book Move"].value<bool>());
+      Move bookMove = book.probe(pos, Options["Best Book Move"]);
 
       if (   bookMove != MOVE_NONE
           && count(RootMoves.begin(), RootMoves.end(), bookMove))
@@ -315,24 +313,24 @@ void Search::think() {
   read_evaluation_uci_options(pos.side_to_move());
   Threads.read_uci_options();
 
-  TT.set_size(Options["Hash"].value<int>());
-  if (Options["Clear Hash"].value<bool>())
+  TT.set_size(Options["Hash"]);
+  if (Options["Clear Hash"])
   {
-      Options["Clear Hash"].set_value("false");
+      Options["Clear Hash"] = false;
       TT.clear();
   }
 
-  UCIMultiPV = Options["MultiPV"].value<size_t>();
-  SkillLevel = Options["Skill Level"].value<int>();
+  UCIMultiPV = Options["MultiPV"];
+  SkillLevel = Options["Skill Level"];
 
   // Do we have to play with skill handicap? In this case enable MultiPV that
   // we will use behind the scenes to retrieve a set of possible moves.
   SkillLevelEnabled = (SkillLevel < 20);
   MultiPV = (SkillLevelEnabled ? std::max(UCIMultiPV, (size_t)4) : UCIMultiPV);
 
-  if (Options["Use Search Log"].value<bool>())
+  if (Options["Use Search Log"])
   {
-      Log log(Options["Search Log Filename"].value<string>());
+      Log log(Options["Search Log Filename"]);
       log << "\nSearching: "  << pos.to_fen()
           << "\ninfinite: "   << Limits.infinite
           << " ponder: "      << Limits.ponder
@@ -362,11 +360,11 @@ void Search::think() {
   Threads.set_timer(0);
   Threads.set_size(1);
 
-  if (Options["Use Search Log"].value<bool>())
+  if (Options["Use Search Log"])
   {
       int e = elapsed_time();
 
-      Log log(Options["Search Log Filename"].value<string>());
+      Log log(Options["Search Log Filename"]);
       log << "Nodes: "          << pos.nodes_searched()
           << "\nNodes/second: " << (e > 0 ? pos.nodes_searched() * 1000 / e : 0)
           << "\nBest move: "    << move_to_san(pos, RootMoves[0].pv[0]);
@@ -410,10 +408,10 @@ namespace {
     bestValue = delta = -VALUE_INFINITE;
     ss->currentMove = MOVE_NULL; // Hack to skip update gains
 
-    // Handle the special case of a mate/stalemate position
+    // Handle the special case of a mated/stalemate position
     if (RootMoves.empty())
     {
-        cout << "info depth 0"
+        cout << "info depth 0 score "
              << score_to_uci(pos.in_check() ? -VALUE_MATE : VALUE_DRAW) << endl;
 
         RootMoves.push_back(MOVE_NONE);
@@ -512,7 +510,7 @@ namespace {
         if (SkillLevelEnabled && depth == 1 + SkillLevel)
             skillBest = do_skill_level();
 
-        if (Options["Use Search Log"].value<bool>())
+        if (Options["Use Search Log"])
              pv_info_to_log(pos, depth, bestValue, elapsed_time(), &RootMoves[0].pv[0]);
 
         // Filter out startup noise when monitoring best move stability
@@ -588,13 +586,12 @@ namespace {
     const bool SpNode   = (NT == SplitPointPV || NT == SplitPointNonPV || NT == SplitPointRoot);
     const bool RootNode = (NT == Root || NT == SplitPointRoot);
 
-    assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
-    assert(beta > alpha && beta <= VALUE_INFINITE);
-    assert(PvNode || alpha == beta - 1);
+    assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
+    assert(PvNode == (alpha != beta - 1));
+    assert(depth > DEPTH_ZERO);
     assert(pos.thread() >= 0 && pos.thread() < Threads.size());
 
     Move movesSearched[MAX_MOVES];
-    int64_t nodes;
     StateInfo st;
     const TTEntry *tte;
     Key posKey;
@@ -640,11 +637,16 @@ namespace {
          || ss->ply > PLY_MAX) && !RootNode)
         return VALUE_DRAW;
 
-    // Step 3. Mate distance pruning
+    // Step 3. Mate distance pruning. Even if we mate at the next move our score
+    // would be at best mate_in(ss->ply+1), but if alpha is already bigger because
+    // a shorter mate was found upward in the tree then there is no need to search
+    // further, we will never beat current alpha. Same logic but with reversed signs
+    // applies also in the opposite condition of being mated instead of giving mate,
+    // in this case return a fail-high score.
     if (!RootNode)
     {
-        alpha = std::max(value_mated_in(ss->ply), alpha);
-        beta = std::min(value_mate_in(ss->ply+1), beta);
+        alpha = std::max(mated_in(ss->ply), alpha);
+        beta = std::min(mate_in(ss->ply+1), beta);
         if (alpha >= beta)
             return alpha;
     }
@@ -701,7 +703,7 @@ namespace {
     if (   (move = (ss-1)->currentMove) != MOVE_NULL
         && (ss-1)->eval != VALUE_NONE
         && ss->eval != VALUE_NONE
-        && pos.captured_piece_type() == PIECE_TYPE_NONE
+        && pos.captured_piece_type() == NO_PIECE_TYPE
         && !is_special(move))
     {
         Square to = move_to(move);
@@ -896,7 +898,6 @@ split_point_start: // At split points actual search starts from here
       if (RootNode)
       {
           Signals.firstRootMove = (moveCount == 1);
-          nodes = pos.nodes_searched();
 
           if (pos.thread() == 0 && elapsed_time() > 2000)
               cout << "info depth " << depth / ONE_PLY
@@ -1063,7 +1064,6 @@ split_point_start: // At split points actual search starts from here
       if (RootNode && !Signals.stop)
       {
           RootMove& rm = *find(RootMoves.begin(), RootMoves.end(), move);
-          rm.nodes += pos.nodes_searched() - nodes;
 
           // PV move or new best move ?
           if (isPvMove || value > alpha)
@@ -1122,7 +1122,7 @@ split_point_start: // At split points actual search starts from here
     // harmless because return value is discarded anyhow in the parent nodes.
     // If we are in a singular extension search then return a fail low score.
     if (!moveCount)
-        return excludedMove ? oldAlpha : inCheck ? value_mated_in(ss->ply) : VALUE_DRAW;
+        return excludedMove ? oldAlpha : inCheck ? mated_in(ss->ply) : VALUE_DRAW;
 
     // If we have pruned all the moves without searching return a fail-low score
     if (bestValue == -VALUE_INFINITE)
@@ -1190,10 +1190,9 @@ split_point_start: // At split points actual search starts from here
     const bool PvNode = (NT == PV);
 
     assert(NT == PV || NT == NonPV);
-    assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
-    assert(beta >= -VALUE_INFINITE && beta <= VALUE_INFINITE);
-    assert(PvNode || alpha == beta - 1);
-    assert(depth <= 0);
+    assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
+    assert(PvNode == (alpha != beta - 1));
+    assert(depth <= DEPTH_ZERO);
     assert(pos.thread() >= 0 && pos.thread() < Threads.size());
 
     StateInfo st;
@@ -1366,7 +1365,7 @@ split_point_start: // At split points actual search starts from here
     // All legal moves have been searched. A special case: If we're in check
     // and no legal moves were found, it is checkmate.
     if (inCheck && bestValue == -VALUE_INFINITE)
-        return value_mated_in(ss->ply);
+        return mated_in(ss->ply); // Plies to mate from the root
 
     // Update transposition table
     move = bestValue <= oldAlpha ? MOVE_NONE : ss->bestMove;
@@ -1491,8 +1490,8 @@ split_point_start: // At split points actual search starts from here
 
 
   // value_to_tt() adjusts a mate score from "plies to mate from the root" to
-  // "plies to mate from the current ply". Non-mate scores are unchanged. The
-  // function is called before storing a value to the transposition table.
+  // "plies to mate from the current position". Non-mate scores are unchanged.
+  // The function is called before storing a value to the transposition table.
 
   Value value_to_tt(Value v, int ply) {
 
@@ -1506,8 +1505,9 @@ split_point_start: // At split points actual search starts from here
   }
 
 
-  // value_from_tt() is the inverse of value_to_tt(): It adjusts a mate score from
-  // the transposition table to a mate score corrected for the current ply.
+  // value_from_tt() is the inverse of value_to_tt(): It adjusts a mate score
+  // from the transposition table (where refers to the plies to mate/be mated
+  // from current position) to "plies to mate/be mated from the root".
 
   Value value_from_tt(Value v, int ply) {
 
@@ -1619,10 +1619,10 @@ split_point_start: // At split points actual search starts from here
 
     std::stringstream s;
 
-    if (abs(v) < VALUE_MATE - PLY_MAX * ONE_PLY)
-        s << " score cp " << int(v) * 100 / int(PawnValueMidgame); // Scale to centipawns
+    if (abs(v) < VALUE_MATE_IN_PLY_MAX)
+        s << "cp " << v * 100 / int(PawnValueMidgame);
     else
-        s << " score mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
+        s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
 
     s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
 
@@ -1659,7 +1659,7 @@ split_point_start: // At split points actual search starts from here
 
         cout << "info depth " << d
              << " seldepth " << selDepth
-             << (i == PVIdx ? score_to_uci(v, alpha, beta) : score_to_uci(v))
+             << " score " << (i == PVIdx ? score_to_uci(v, alpha, beta) : score_to_uci(v))
              << " nodes " << pos.nodes_searched()
              << " nps " << (t > 0 ? pos.nodes_searched() * 1000 / t : 0)
              << " time " << t
@@ -1753,7 +1753,7 @@ split_point_start: // At split points actual search starts from here
     while (m != pv)
         pos.undo_move(*--m);
 
-    Log l(Options["Search Log Filename"].value<string>());
+    Log l(Options["Search Log Filename"]);
     l << s.str() << endl;
   }