]> git.sesse.net Git - stockfish/blobdiff - src/tt.cpp
Fixed a bug in PV extraction from the transposition table: The
[stockfish] / src / tt.cpp
index fe222106f9a67f59caedcbcc66a80e13293a90c3..1e98aa24901549c888a3704576b5215619f6a6bf 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 Marco Costalba
+  Copyright (C) 2008-2009 Marco Costalba
 
   Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -26,6 +26,7 @@
 #include <cmath>
 #include <cstring>
 
+#include "movegen.h"
 #include "tt.h"
 
 
@@ -69,7 +70,7 @@ void TranspositionTable::set_size(unsigned mbSize) {
       {
           std::cerr << "Failed to allocate " << mbSize
                     << " MB for transposition table." << std::endl;
-          exit(EXIT_FAILURE);
+          Application::exit_with_failure();
       }
       clear();
   }
@@ -97,14 +98,14 @@ void TranspositionTable::clear() {
 /// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
 /// never replaces another entry for the same position.
 
-void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d, Move m) {
+void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m) {
 
   TTEntry *tte, *replace;
 
-  tte = replace = first_entry(p);
+  tte = replace = first_entry(posKey);
   for (int i = 0; i < 4; i++, tte++)
   {
-      if (!tte->key() || tte->key() == p.get_key()) // empty or overwrite old
+      if (!tte->key() || tte->key() == posKey) // empty or overwrite old
       {
           // Do not overwrite when new type is VALUE_TYPE_EVAL
           if (tte->key() && t == VALUE_TYPE_EVAL)
@@ -113,7 +114,7 @@ void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d,
           if (m == MOVE_NONE)
               m = tte->move();
 
-          *tte = TTEntry(p.get_key(), v, t, d, m, generation);
+          *tte = TTEntry(posKey, v, t, d, m, generation);
           return;
       }
       else if (i == 0)  // replace would be a no-op in this common case
@@ -126,7 +127,7 @@ void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d,
       if (c1 + c2 + c3 > 0)
           replace = tte;
   }
-  *replace = TTEntry(p.get_key(), v, t, d, m, generation);
+  *replace = TTEntry(posKey, v, t, d, m, generation);
   writes++;
 }
 
@@ -135,12 +136,12 @@ void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d,
 /// transposition table. Returns a pointer to the TTEntry or NULL
 /// if position is not found.
 
-TTEntry* TranspositionTable::retrieve(const Position& pos) const {
+TTEntry* TranspositionTable::retrieve(const Key posKey) const {
 
-  TTEntry *tte = first_entry(pos);
+  TTEntry *tte = first_entry(posKey);
 
   for (int i = 0; i < 4; i++, tte++)
-      if (tte->key() == pos.get_key())
+      if (tte->key() == posKey)
           return tte;
 
   return NULL;
@@ -150,9 +151,9 @@ TTEntry* TranspositionTable::retrieve(const Position& pos) const {
 /// TranspositionTable::first_entry returns a pointer to the first
 /// entry of a cluster given a position.
 
-inline TTEntry* TranspositionTable::first_entry(const Position& pos) const {
+inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
 
-  return entries + (int(pos.get_key() & (size - 1)) << 2);
+  return entries + (int(posKey & (size - 1)) << 2);
 }
 
 /// TranspositionTable::new_search() is called at the beginning of every new
@@ -179,12 +180,44 @@ void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
 
   for (int i = 0; pv[i] != MOVE_NONE; i++)
   {
-      store(p, VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
+      store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
       p.do_move(pv[i], st);
   }
 }
 
 
+/// TranspositionTable::extract_pv() extends a PV by adding moves from the
+/// transposition table at the end. This should ensure that the PV is almost
+/// always at least two plies long, which is important, because otherwise we
+/// will often get single-move PVs when the search stops while failing high,
+/// and a single-move PV means that we don't have a ponder move.
+
+void TranspositionTable::extract_pv(const Position& pos, Move pv[]) {
+
+  int ply;
+  Position p(pos);
+  StateInfo st[100];
+
+  for (ply = 0; pv[ply] != MOVE_NONE; ply++)
+      p.do_move(pv[ply], st[ply]);
+
+  bool stop;
+  const TTEntry* tte;
+  for (stop = false, tte = retrieve(p.get_key());
+       tte && tte->move() != MOVE_NONE && !stop;
+       tte = retrieve(p.get_key()), ply++)
+  {
+      if (!move_is_legal(p, tte->move()))
+          break;
+      pv[ply] = tte->move();
+      p.do_move(pv[ply], st[ply]);
+      for (int j = 0; j < ply; j++)
+          if (st[j].key == p.get_key()) stop = true;
+  }
+  pv[ply] = MOVE_NONE;
+}
+
+
 /// TranspositionTable::full() returns the permill of all transposition table
 /// entries which have received at least one write during the current search.
 /// It is used to display the "info hashfull ..." information in UCI.