Line data Source code
1 : #include "egt.hpp"
2 :
3 : #ifdef WITH_SYZYGY
4 :
5 : #include "dynamicConfig.hpp"
6 : #include "logging.hpp"
7 : #include "position.hpp"
8 : #include "searcher.hpp"
9 : #include "tools.hpp"
10 :
11 : extern "C" {
12 : #include "tbprobe.h"
13 : }
14 :
15 : namespace SyzygyTb {
16 :
17 : int MAX_TB_MEN = -1;
18 :
19 : const array1d<ScoreType,5> valueMap = {-TB_WIN_SCORE, -TB_CURSED_SCORE, 0, TB_CURSED_SCORE, TB_WIN_SCORE};
20 : const array1d<ScoreType,5> valueMapNo50 = {-TB_WIN_SCORE, -TB_WIN_SCORE, 0, TB_WIN_SCORE, TB_WIN_SCORE};
21 :
22 0 : MType getMoveType(const Position &p, unsigned res) {
23 0 : const bool isCap = (p.board_const(TB_GET_TO(res)) != P_none);
24 0 : switch (TB_GET_PROMOTES(res)) {
25 0 : case TB_PROMOTES_QUEEN: return isCap ? T_cappromq : T_promq;
26 0 : case TB_PROMOTES_ROOK: return isCap ? T_cappromr : T_promr;
27 0 : case TB_PROMOTES_BISHOP: return isCap ? T_cappromb : T_promb;
28 0 : case TB_PROMOTES_KNIGHT: return isCap ? T_cappromn : T_promn;
29 0 : default: return isCap ? T_capture : T_std;
30 : }
31 : }
32 :
33 0 : Move getMove(const Position &p, unsigned res) {
34 0 : return ToMove(TB_GET_FROM(res), TB_GET_TO(res), TB_GET_EP(res) ? T_ep : getMoveType(p, res));
35 : } // Note: castling not possible
36 :
37 22 : bool initTB() {
38 22 : if (DynamicConfig::syzygyPath.empty()) {
39 22 : MAX_TB_MEN = -1;
40 22 : return false;
41 : }
42 0 : Logging::LogIt(Logging::logInfo) << "Init tb from path " << DynamicConfig::syzygyPath;
43 0 : if (!tb_init(DynamicConfig::syzygyPath.c_str())){
44 0 : MAX_TB_MEN = 0;
45 0 : return false;
46 : }
47 : else
48 0 : MAX_TB_MEN = static_cast<int>(TB_LARGEST);
49 0 : Logging::LogIt(Logging::logInfo) << "MAX_TB_MEN: " << MAX_TB_MEN;
50 0 : return true;
51 : }
52 :
53 0 : int probe_root(Searcher &/*context*/, const Position &p, ScoreType &score, MoveList &rootMoves) {
54 0 : if (MAX_TB_MEN <= 0) return -1;
55 0 : score = 0;
56 : unsigned results[TB_MAX_MOVES];
57 0 : debug_king_cap(p);
58 0 : unsigned result = tb_probe_root(p.allPieces[Co_White],
59 : p.allPieces[Co_Black],
60 : p.allKing(),
61 : p.allQueen(),
62 : p.allRook(),
63 : p.allBishop(),
64 : p.allKnight(),
65 : p.allPawn(),
66 0 : p.fifty,
67 0 : p.castling != C_none,
68 0 : p.ep == INVALIDSQUARE ? 0 : p.ep,
69 0 : p.c == Co_White,
70 : results);
71 0 : if (result == TB_RESULT_FAILED) return -1;
72 0 : const unsigned wdl = TB_GET_WDL(result);
73 0 : assert(wdl < 5);
74 0 : score = valueMap[wdl];
75 0 : rootMoves.push_back(getMove(p, result));
76 : /*
77 : if (context.isRep(p, false)) rootMoves.push_back(getMove(p, result));
78 : else {
79 : unsigned res;
80 : for (int i = 0; (res = results[i]) != TB_RESULT_FAILED; i++) {
81 : if (TB_GET_WDL(res) >= wdl) rootMoves.push_back(getMove(p, res));
82 : }
83 : }
84 : */
85 0 : return TB_GET_DTZ(result);
86 : }
87 :
88 0 : int probe_wdl(const Position &p, ScoreType &score, const bool use50MoveRule) {
89 0 : if (MAX_TB_MEN <= 0) return -1;
90 0 : score = 0;
91 0 : debug_king_cap(p);
92 0 : unsigned result = tb_probe_wdl(p.allPieces[Co_White],
93 : p.allPieces[Co_Black],
94 : p.allKing(),
95 : p.allQueen(),
96 : p.allRook(),
97 : p.allBishop(),
98 : p.allKnight(),
99 : p.allPawn(),
100 0 : p.fifty,
101 0 : p.castling != C_none,
102 0 : p.ep == INVALIDSQUARE ? 0 : p.ep,
103 0 : p.c == Co_White);
104 0 : if (result == TB_RESULT_FAILED) return 0;
105 0 : unsigned wdl = TB_GET_WDL(result);
106 0 : assert(wdl < 5);
107 0 : if (use50MoveRule) score = valueMap[wdl];
108 : else
109 0 : score = valueMapNo50[wdl];
110 : return 1;
111 : }
112 :
113 : } // namespace SyzygyTb
114 : #endif
|