]> git.sesse.net Git - stockfish/blobdiff - src/book.cpp
Drop a magic in book.cpp
[stockfish] / src / book.cpp
index fcc8c0d412919aaec0a8d325b35a7bf7bdc6a52f..1c2d30e0a41df95358a23cd9bdf1b8ace8944315 100644 (file)
@@ -1,57 +1,52 @@
 /*
-  Glaurung, a UCI chess playing engine.
-  Copyright (C) 2004-2008 Tord Romstad
+  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+  Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
+  Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
 
-  Glaurung is free software: you can redistribute it and/or modify
+  Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
-  
-  Glaurung is distributed in the hope that it will be useful,
+
+  Stockfish is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
-  
+
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-
 /*
   The code in this file is based on the opening book code in PolyGlot
-  by Fabien Letouzey.  PolyGlot is available under the GNU General
+  by Fabien Letouzey. PolyGlot is available under the GNU General
   Public License, and can be downloaded from http://wbec-ridderkerk.nl
 */
 
-
-////
-//// Includes
-////
-
+#include <algorithm>
 #include <cassert>
-#include <cstdio>
+#include <iostream>
 
 #include "book.h"
-#include "mersenne.h"
+#include "misc.h"
 #include "movegen.h"
 
-
-////
-//// Global variables
-////
-
-Book OpeningBook;
-
-
-////
-//// Local definitions
-////
+using namespace std;
 
 namespace {
 
-  /// Random numbers from PolyGlot, used to compute book hash keys.
+  // A Polyglot book is a series of "entries" of 16 bytes. All integers are
+  // stored in big-endian format, with highest byte first (regardless of size).
+  // The entries are ordered according to the key in ascending order.
+  struct BookEntry {
+    uint64_t key;
+    uint16_t move;
+    uint16_t count;
+    uint32_t learn;
+  };
 
-  const uint64_t Random64[781] = {
+  // Random numbers from PolyGlot, used to compute book hash keys
+  const Key PolyGlotRandoms[781] = {
     0x9D39247E33776D41ULL, 0x2AF7398005AAA5C7ULL, 0x44DB015024623547ULL,
     0x9C15F73E62A76AE2ULL, 0x75834465489C0C89ULL, 0x3290AC3A203001BFULL,
     0x0FBBAD1F61042279ULL, 0xE83A908FF2FB60CAULL, 0x0D7E765D58755C10ULL,
@@ -315,272 +310,172 @@ namespace {
     0xF8D626AAAF278509ULL
   };
 
+  // Offsets to the PolyGlotRandoms[] array of zobrist keys
+  const Key* ZobPiece     = PolyGlotRandoms;
+  const Key* ZobCastle    = ZobPiece     + 12 * 64; // Pieces * squares
+  const Key* ZobEnPassant = ZobCastle    + 4;       // Castle flags
+  const Key* ZobTurn      = ZobEnPassant + 8;       // Number of files
+
+  // book_key() returns the PolyGlot hash key of the given position
+  uint64_t book_key(const Position& pos) {
+
+    uint64_t key = 0;
+    Bitboard b = pos.pieces();
+
+    while (b)
+    {
+        // In PolyGlotRandoms[] pieces are stored in the following sequence:
+        // BP = 0, WP = 1, BN = 2, WN = 3, ... BK = 10, WK = 11
+        Square s = pop_lsb(&b);
+        Piece p = pos.piece_on(s);
+        int pieceOfs = 2 * (type_of(p) - 1) + (color_of(p) == WHITE);
+        key ^= ZobPiece[64 * pieceOfs + s];
+    }
 
-  /// Indices to the Random64[] array
-  
-  const int RandomPiece = 0;
-  const int RandomCastle = 768;
-  const int RandomEnPassant = 772;
-  const int RandomTurn = 780;
-
-  
-  /// Convert pieces to the range 0..1
-  
-  const int PieceTo12[] = {
-    0, 0, 2, 4, 6, 8, 10, 0, 0, 1, 3, 5, 7, 9, 11
-  };
-
-
-  /// Prototypes
-  
-  uint64_t book_key(const Position &pos);
-  uint64_t book_piece_key(Piece p, Square s);
-  uint64_t book_castle_key(const Position &pos);
-  uint64_t book_ep_key(const Position &pos);
-  uint64_t book_color_key(const Position &pos);
-
-  uint64_t read_integer(FILE *file, int size);
-  uint16_t read_small_integer(FILE *file, int size);
-}
-
-
-////
-//// Functions
-////
-
-
-/// Constructor
+    b = pos.can_castle(ALL_CASTLES);
 
-Book::Book() {
-  bookFile = NULL;
-  bookSize = 0;
-}
+    while (b)
+        key ^= ZobCastle[pop_lsb(&b)];
 
+    if (pos.ep_square() != SQ_NONE)
+        key ^= ZobEnPassant[file_of(pos.ep_square())];
 
-/// Book::open() opens a book file with a given file name.
+    if (pos.side_to_move() == WHITE)
+        key ^= ZobTurn[0];
 
-void Book::open(const std::string &fName) {
-  fileName = fName;
-  bookFile = fopen(fileName.c_str(), "rb");
-  if(bookFile != NULL) {
-    if(fseek(bookFile, 0, SEEK_END) == -1) {
-      std::cerr << "Failed to open book file " << fileName << std::endl;
-      exit(EXIT_FAILURE);
-    }
-    bookSize = ftell(bookFile) / 16;
-    if(bookSize == -1) {
-      std::cerr << "Failed to open book file " << fileName << std::endl;
-      exit(EXIT_FAILURE);
-    }
+    return key;
   }
-}
 
+} // namespace
 
-/// Book::close() closes the currently open book file.
+PolyglotBook::PolyglotBook() {
 
-void Book::close() {
-  if(bookFile != NULL && fclose(bookFile) == EOF) {
-    std::cerr << "Failed to close book file" << std::endl;
-    exit(EXIT_FAILURE);
-  }
+  for (int i = Time::now() % 10000; i > 0; i--)
+      RKiss.rand<unsigned>(); // Make random number generation less deterministic
 }
 
+PolyglotBook::~PolyglotBook() { if (is_open()) close(); }
 
-/// Book::is_open() tests whether a book file has been opened.
 
-bool Book::is_open() const {
-  return bookFile != NULL && bookSize != 0;
-}
+/// operator>>() reads sizeof(T) chars from the file's binary byte stream and
+/// converts them in a number of type T. A Polyglot book stores numbers in
+/// big-endian format.
 
+template<typename T> PolyglotBook& PolyglotBook::operator>>(T& n) {
 
-/// Book::file_name() returns the file name of the currently active book,
-/// or the empty string if no book is open.
+  n = 0;
+  for (size_t i = 0; i < sizeof(T); i++)
+      n = T((n << 8) + ifstream::get());
 
-const std::string Book::file_name() const {
-  return this->is_open()? fileName : "";
+  return *this;
 }
-  
-
-/// Book::get_move() gets a book move for a given position.  Returns
-/// MOVE_NONE if no book move is found.
-
-Move Book::get_move(const Position &pos) const {
-  if(this->is_open()) {
-    int bestMove = 0, bestScore = 0, move, score;
-    uint64_t key = book_key(pos);
-    BookEntry entry;
-
-    for(int i = this->find_key(key); i < bookSize; i++) {
-      this->read_entry(entry, i);
-      if(entry.key != key)
-        break;
-      move = entry.move;
-      score = entry.count;
-      assert(score > 0);
-
-      bestScore += score;
-      if(int(genrand_int32() % bestScore) < score)
-        bestMove = move;
-    }
 
-    if(bestMove != 0) {
-      MoveStack moves[256];
-      int n, j;
-      n = generate_legal_moves(pos, moves);
-      for(j = 0; j < n; j++)
-        if((int(moves[j].move) & 07777) == bestMove)
-          return moves[j].move;
-    }
-  }
-  return MOVE_NONE;
+template<> PolyglotBook& PolyglotBook::operator>>(BookEntry& e) {
+  return *this >> e.key >> e.move >> e.count >> e.learn;
 }
 
 
-/// Book::find_key() takes a book key as input, and does a binary search
-/// through the book file for the given key.  The index to the first book
-/// entry with the same key as the input is returned.  When the key is not
-/// found in the book file, bookSize is returned.
+/// open() tries to open a book file with the given name after closing any
+/// exsisting one.
 
-int Book::find_key(uint64_t key) const {
-  int left, right, mid;
-  BookEntry entry;
+bool PolyglotBook::open(const char* fName) {
 
-  // Binary search (finds the leftmost entry)
-  left = 0;
-  right = bookSize - 1;
+  if (is_open()) // Cannot close an already closed file
+      close();
 
-  assert(left <= right);
+  ifstream::open(fName, ifstream::in | ifstream::binary);
 
-  while(left < right) {
-    mid = (left + right) / 2;
-    assert(mid >= left && mid < right);
-
-    this->read_entry(entry, mid);
+  fileName = is_open() ? fName : "";
+  ifstream::clear(); // Reset any error flag to allow retry ifstream::open()
+  return !fileName.empty();
+}
 
-    if(key <= entry.key)
-      right = mid;
-    else
-      left = mid + 1;
-  }
 
-  assert(left == right);
+/// probe() tries to find a book move for the given position. If no move is
+/// found returns MOVE_NONE. If pickBest is true returns always the highest
+/// rated move, otherwise randomly chooses one, based on the move score.
 
-  this->read_entry(entry, left);
+Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest) {
 
-  return (entry.key == key)? left : bookSize;
-}
+  if (fileName != fName && !open(fName.c_str()))
+      return MOVE_NONE;
 
+  BookEntry e;
+  uint16_t best = 0;
+  unsigned sum = 0;
+  Move move = MOVE_NONE;
+  uint64_t key = book_key(pos);
 
-/// Book::read_entry() takes a BookEntry reference and an integer index as
-/// input, and looks up the opening book entry at the given index in the book
-/// file.  The book entry is copied to the first input parameter.
+  seekg(find_first(key) * sizeof(BookEntry), ios_base::beg);
 
-void Book::read_entry(BookEntry& entry, int n) const {
-  assert(n >= 0 && n < bookSize);
-  assert(bookFile != NULL);
+  while (*this >> e, e.key == key && good())
+  {
+      best = max(best, e.count);
+      sum += e.count;
 
-  if(fseek(bookFile, n*16, SEEK_SET) == -1) {
-    std::cerr << "Failed to read book entry at index " << n << std::endl;
-    exit(EXIT_FAILURE);
+      // Choose book move according to its score. If a move has a very
+      // high score it has higher probability to be choosen than a move
+      // with lower score. Note that first entry is always chosen.
+      if (   (sum && RKiss.rand<unsigned>() % sum < e.count)
+          || (pickBest && e.count == best))
+          move = Move(e.move);
   }
 
-  entry.key = read_integer(bookFile, 8);
-  entry.move = read_small_integer(bookFile, 2);
-  entry.count = read_small_integer(bookFile, 2);
-  entry.n = read_small_integer(bookFile, 2);
-  entry.sum = read_small_integer(bookFile, 2);
-}
-
+  if (!move)
+      return MOVE_NONE;
+
+  // A PolyGlot book move is encoded as follows:
+  //
+  // bit  0- 5: destination square (from 0 to 63)
+  // bit  6-11: origin square (from 0 to 63)
+  // bit 12-14: promotion piece (from KNIGHT == 1 to QUEEN == 4)
+  //
+  // Castling moves follow "king captures rook" representation. So in case book
+  // move is a promotion we have to convert to our representation, in all the
+  // other cases we can directly compare with a Move after having masked out
+  // the special Move's flags (bit 14-15) that are not supported by PolyGlot.
+  int pt = (move >> 12) & 7;
+  if (pt)
+      move = make<PROMOTION>(from_sq(move), to_sq(move), PieceType(pt + 1));
+
+  // Add 'special move' flags and verify it is legal
+  for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
+      if (move == (ml.move() ^ type_of(ml.move())))
+          return ml.move();
 
-////
-//// Local definitions
-////
+  return MOVE_NONE;
+}
 
-namespace {
 
-  uint64_t book_key(const Position &pos) {
-    uint64_t result = 0ULL;
-
-    for(Color c = WHITE; c <= BLACK; c++) {
-      Bitboard b = pos.pieces_of_color(c);
-      Square s;
-      Piece p;
-      while(b != EmptyBoardBB) {
-        s = pop_1st_bit(&b);
-        p = pos.piece_on(s);
-        assert(piece_is_ok(p));
-        assert(color_of_piece(p) == c);
-
-        result ^= book_piece_key(p, s);
-      }
-    }
+/// find_first() takes a book key as input, and does a binary search through
+/// the book file for the given key. Returns the index of the leftmost book
+/// entry with the same key as the input.
 
-    result ^= book_castle_key(pos);
-    result ^= book_ep_key(pos);
-    result ^= book_color_key(pos);
+size_t PolyglotBook::find_first(uint64_t key) {
 
-    return result;
-  }
-        
+  seekg(0, ios::end); // Move pointer to end, so tellg() gets file's size
 
-  uint64_t book_piece_key(Piece p, Square s) {
-    return Random64[RandomPiece + (PieceTo12[int(p)]^1)*64 + int(s)];
-  }
+  size_t low = 0, mid, high = (size_t)tellg() / sizeof(BookEntry) - 1;
+  BookEntry e;
 
+  assert(low <= high);
 
-  uint64_t book_castle_key(const Position &pos) {
-    uint64_t result = 0ULL;
+  while (low < high && good())
+  {
+      mid = (low + high) / 2;
 
-    if(pos.can_castle_kingside(WHITE))
-      result ^= Random64[RandomCastle+0];
-    if(pos.can_castle_queenside(WHITE))
-      result ^= Random64[RandomCastle+1];
-    if(pos.can_castle_kingside(BLACK))
-      result ^= Random64[RandomCastle+2];
-    if(pos.can_castle_queenside(BLACK))
-      result ^= Random64[RandomCastle+3];
-    return result;
-  }
+      assert(mid >= low && mid < high);
 
-  
-  uint64_t book_ep_key(const Position &pos) {
-    return (pos.ep_square() == SQ_NONE)?
-      0ULL : Random64[RandomEnPassant + square_file(pos.ep_square())];
-  }
+      seekg(mid * sizeof(BookEntry), ios_base::beg);
+      *this >> e;
 
-  
-  uint64_t book_color_key(const Position &pos) {
-    return (pos.side_to_move() == WHITE)? Random64[RandomTurn] : 0ULL;
-  }
-  
-
-  uint64_t read_integer(FILE *file, int size) {
-    uint64_t n = 0ULL;
-    int i;
-    int b;
-
-    assert(file != NULL);
-    assert(size > 0 && size <= 8);
-
-    for(i = 0; i < size; i++) {
-      b = fgetc(file);
-      if(b == EOF) {
-        std::cerr << "Failed to read " << size << " bytes from book file"
-                  << std::endl;
-        exit(EXIT_FAILURE);
-      }
-      assert(b >= 0 && b < 256);
-      n = (n << 8) | b;
-    }
-    return n;
+      if (key <= e.key)
+          high = mid;
+      else
+          low = mid + 1;
   }
 
-  uint16_t read_small_integer(FILE *file, int size) {
-
-      assert(size > 0 && size <= 5); // 16 bit integer
-      uint64_t n = read_integer(file, size);
-      assert(n == (uint16_t)n);
-      return (uint16_t)n;      
-  }
+  assert(low == high);
 
+  return low;
 }