Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.     Texel - A UCI chess engine.
  3.     Copyright (C) 2014  Peter Ă–sterlund, peterosterlund2@gmail.com
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. /*
  20.  * tbprobe.hpp
  21.  *
  22.  *  Created on: Jun 2, 2014
  23.  *      Author: petero
  24.  */
  25.  
  26. #ifndef TBPROBE_HPP_
  27. #define TBPROBE_HPP_
  28.  
  29. #include "transpositionTable.hpp"
  30. #include "moveGen.hpp"
  31.  
  32. #include <string>
  33.  
  34.  
  35. class Position;
  36.  
  37. /**
  38.  * Handle tablebase probing.
  39.  */
  40. class TBProbe {
  41.     friend class TBTest;
  42. public:
  43.     /** Initialize tablebases. */
  44.     static void initialize(const std::string& gtbPath, int cacheMB,
  45.                            const std::string& rtbPath);
  46.  
  47.     /** Return true if GTB or RTB probing is enabled. */
  48.     static bool tbEnabled();
  49.  
  50.     /** Probe one or more tablebases to get an exact score or a usable bound.
  51.      * @param pos  The position to probe. The position can be temporarily modified
  52.      *             but is restored to original state before function returns.
  53.      */
  54.     static bool tbProbe(Position& pos, int ply, int alpha, int beta,
  55.                         TranspositionTable::TTEntry& ent);
  56.  
  57.     /** If some TB files are missing, it may be necessary to only search a subset
  58.      * of the root moves in order to make progress. This might happen for example
  59.      * in KPK if the KQK table is missing and search is not able to see the mate
  60.      * after promoting the pawn.
  61.      * @param pos           The root position.
  62.      * @param legalMoves    The set of legal root moves.
  63.      * @param movesToSearch The moves to search.
  64.      * @return True if a subset should be searched, false to search all moves.
  65.      */
  66.     static bool getSearchMoves(Position& pos, const MoveList& legalMoves,
  67.                                std::vector<Move>& movesToSearch);
  68.  
  69.     /** Enhance PV with DTM information from gaviota tablebases. */
  70.     static void extendPV(const Position& rootPos, std::vector<Move>& pv);
  71.  
  72.     /** Probe gaviota DTM tablebases.
  73.      * @param pos  The position to probe. The position can be temporarily modified
  74.      *             but is restored to original state before function returns.
  75.      * @param ply  The ply value used to adjust mate scores.
  76.      * @param score The tablebase score. Only modified for tablebase hits.
  77.      * @return True if pos was found in the tablebases.
  78.      */
  79.     static bool gtbProbeDTM(Position& pos, int ply, int& score);
  80.  
  81.     /**
  82.      * Probe gaviota WDL tablebases.
  83.      * @param pos  The position to probe. The position can be temporarily modified
  84.      *             but is restored to original state before function returns.
  85.      * @param ply  The ply value used to adjust mate scores.
  86.      * @param score The tablebase score. Only modified for tablebase hits.
  87.      *              The returned score is either 0 or a mate bound.
  88.      */
  89.     static bool gtbProbeWDL(Position& pos, int ply, int& score);
  90.  
  91.     /**
  92.      * Probe syzygy DTZ tablebases.
  93.      * @param pos  The position to probe. The position can be temporarily modified
  94.      *             but is restored to original state before function returns.
  95.      * @param ply  The ply value used to adjust mate scores.
  96.      * @param score The tablebase score. Only modified for tablebase hits.
  97.      *              The returned score is either 0 or a mate bound. The bound
  98.      *              is computed by considering the DTZ value and the maximum number
  99.      *              of zeroing moves before mate.
  100.      */
  101.     static bool rtbProbeDTZ(Position& pos, int ply, int& score);
  102.  
  103.     /**
  104.      * Probe syzygy WDL tablebases.
  105.      * @param pos  The position to probe. The position can be temporarily modified
  106.      *             but is restored to original state before function returns.
  107.      * @param ply  The ply value used to adjust mate scores.
  108.      * @param score The tablebase score. Only modified for tablebase hits.
  109.      *              The returned score is either 0 or a mate bound.
  110.      */
  111.     static bool rtbProbeWDL(Position& pos, int ply, int& score);
  112.  
  113. private:
  114.     /** Initialize */
  115.     static void gtbInitialize(const std::string& path, int cacheMB, int wdlFraction);
  116.  
  117.     static void initWDLBounds();
  118.     static int getMaxDTZ(int matId);
  119.     static int getMaxSubMate(const Position& pos);
  120.     static int getMaxSubMate(std::vector<int>& pieces, int pawnMoves);
  121.     static void initMaxDTM();
  122.     static void initMaxDTZ();
  123.  
  124.     struct GtbProbeData {
  125.         unsigned int stm, epsq, castles;
  126.         static const int MAXLEN = 17;
  127.         unsigned int  wSq[MAXLEN];
  128.         unsigned int  bSq[MAXLEN];
  129.         unsigned char wP[MAXLEN];
  130.         unsigned char bP[MAXLEN];
  131.         int materialId;
  132.     };
  133.  
  134.     /** Convert position to GTB probe format. */
  135.     static void getGTBProbeData(const Position& pos, GtbProbeData& gtbData);
  136.  
  137.     static bool gtbProbeDTM(const GtbProbeData& gtbData, int ply, int& score);
  138.  
  139.     static bool gtbProbeWDL(const GtbProbeData& gtbData, int ply, int& score);
  140. };
  141.  
  142.  
  143. #endif /* TBPROBE_HPP_ */
  144.