Skip to content

Commit

Permalink
transposition table fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
archishou committed Feb 3, 2023
1 parent eee67ff commit f77f5ce
Show file tree
Hide file tree
Showing 17 changed files with 6,004 additions and 20 deletions.
5,853 changes: 5,853 additions & 0 deletions ChessEngine/ChessEngine_lib/engine_uci_inputs_diagnostics.txt

Large diffs are not rendered by default.

34 changes: 23 additions & 11 deletions ChessEngine/ChessEngine_lib/move_generation/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ std::string Position::fen() const {
if (i > 0) fen << '/';
}

std::string castling_rights;
const Bitboard set_castling_state = castling_state();
if ((set_castling_state >> 3) & 0b1) castling_rights += "K";
if ((set_castling_state >> 2) & 0b1) castling_rights += "Q";
if ((set_castling_state >> 1) & 0b1) castling_rights += "k";
if (set_castling_state & 0b1) castling_rights += "q";
if (set_castling_state == 0) castling_rights = "-";

fen << (side_to_play == WHITE ? " w " : " b ")
<< (history[game_ply].entry & WHITE_OO_MASK ? "" : "K")
<< (history[game_ply].entry & WHITE_OOO_MASK ? "" : "Q")
<< (history[game_ply].entry & BLACK_OO_MASK ? "" : "k")
<< (history[game_ply].entry & BLACK_OOO_MASK ? "" : "q")
<< (history[game_ply].entry & ALL_CASTLING_MASK ? "-" : "")
<< castling_rights
<< (history[game_ply].epsq == NO_SQUARE ? " -" : " " + std::string(SQSTR[history[game_ply].epsq]))
<< " 0 1";

Expand Down Expand Up @@ -132,7 +136,9 @@ void Position::set(const std::string& fen, Position& p) {
p.half_move_clock = std::stoi(half_move_clock);
p.full_move_clock = std::stoi(full_move_clock);

p.update_hash_board_features();
if (p.side_to_play == BLACK) p.hash ^= zobrist::zobrist_color_key;
p.hash ^= zobrist::zobrist_castling_rights_table[p.castling_state()];
p.hash ^= zobrist::zobrist_ep_file_table[p.ep_file()];
}

//Moves a piece to a (possibly empty) square on the board and updates the hash
Expand All @@ -154,12 +160,18 @@ void Position::move_piece_quiet(Square from, Square to) {
board[from] = NO_PIECE;
}

void Position::update_hash_board_features() {
void Position::update_hash_board_features(Bitboard original_castle_state, int original_ep_file) {
hash ^= zobrist::zobrist_color_key;
hash ^= zobrist::zobrist_castling_rights_table[castling_state()];
int ep_file = NFILES;
if (ep_square() != NO_SQUARE) ep_file = file_of(ep_square());
hash ^= zobrist::zobrist_ep_file_table[ep_file];
Bitboard new_castling_state = castling_state();
if (new_castling_state != original_castle_state) {
hash ^= zobrist::zobrist_castling_rights_table[original_castle_state];
hash ^= zobrist::zobrist_castling_rights_table[new_castling_state];
}
int new_ep_file = ep_file();
if (original_ep_file != new_ep_file) {
hash ^= zobrist::zobrist_ep_file_table[original_ep_file];
hash ^= zobrist::zobrist_ep_file_table[new_ep_file];
}
}

void Position::clear() {
Expand Down
14 changes: 9 additions & 5 deletions ChessEngine/ChessEngine_lib/move_generation/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,13 @@ class Position {
void move_piece(Square from, Square to);
void move_piece_quiet(Square from, Square to);


friend std::ostream& operator<<(std::ostream& os, const Position& p);
std::string fen() const;

Position& operator=(const Position&) = delete;
inline bool operator==(const Position& other) const { return hash == other.hash; }

Bitboard castling_state() {
const Bitboard castling_state() const {
Bitboard entry = history[game_ply].entry;
int white_oo = !((bool) (entry & WHITE_OO_MASK)) << 3;
int white_ooo = !((bool) (entry & WHITE_OOO_MASK)) << 2;
Expand All @@ -159,8 +158,9 @@ class Position {
inline int ply() const { return game_ply; }

inline Square ep_square() const { return history[game_ply].epsq; }
short ep_file() const { return ep_square() == NO_SQUARE ? NFILES : file_of(ep_square()); }

void update_hash_board_features();
void update_hash_board_features(Bitboard original_castle_state, int original_ep_file);
inline zobrist_hash get_hash() const { return hash; }

template<Color C> inline Bitboard diagonal_sliders() const;
Expand Down Expand Up @@ -221,6 +221,8 @@ inline Bitboard Position::attackers_from(Square s, Bitboard occ) const {
template<Color C>
void Position::play(const Move m) {
side_to_play = ~side_to_play;
Bitboard original_castle_state = castling_state();
int original_ep_file = ep_file();
++game_ply;
history[game_ply] = UndoInfo(history[game_ply - 1]);

Expand Down Expand Up @@ -311,14 +313,16 @@ void Position::play(const Move m) {

break;
}
update_hash_board_features();
update_hash_board_features(original_castle_state, original_ep_file);
hash_history.push_back(hash);
}

//Undos a move in the current position, rolling it back to the previous position
template<Color C>
void Position::undo(const Move m) {
MoveFlag type = m.flag();
Bitboard current_castling_state = castling_state();
int current_ep_file = ep_file();
switch (type) {
case QUIET:
move_piece_quiet(m.to(), m.from());
Expand Down Expand Up @@ -370,8 +374,8 @@ void Position::undo(const Move m) {
}
hash_history.pop_back();
side_to_play = ~side_to_play;
update_hash_board_features();
--game_ply;
update_hash_board_features(current_castling_state, current_ep_file);
}

template<Color Us>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using namespace std;

struct ReadUCIParameters {
bool debug_info;
bool debug_info = true;
};

void initialize_uci(Position& p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ void perft_hash(Position& p, unsigned int depth) {
if (depth == 1) return;
for (Move move : list) {
zobrist_hash initial_hash = p.get_hash();
Position b2;
Position::set(p.fen(), b2);
EXPECT_EQ(p.get_hash(), b2.get_hash());
if (initial_hash != b2.get_hash()) {
std::cout << "Failure to set position! " << std::endl;
}
p.play<Us>(move);
perft_hash<~Us>(p, depth - 1);
p.undo<Us>(move);
EXPECT_EQ(initial_hash, p.get_hash());
EXPECT_EQ(initial_hash, b2.get_hash());
}
}

Expand Down Expand Up @@ -64,3 +71,112 @@ TEST_F(MoveGenerationZobristHashTests, TestConsitentHashKiwiPete){
TEST_F(MoveGenerationZobristHashTests, TestConsitentHashTalkchess){
test_perft_hash(TALKCHESS_FEN, 5);
}

TEST_F(MoveGenerationZobristHashTests, TestTraiangulation){
Position p;
Position::set("8/k7/3p4/p2P1p2/P2P1P2/8/8/K7 w - - 0 1", p);
zobrist_hash initial_hash = p.get_hash();

Move m1 = Move(a1, a2, QUIET);
Move m2 = Move(a7, b7, QUIET);
Move m3 = Move(a2, b2, QUIET);
Move m4 = Move(b7, a7, QUIET);
Move m5 = Move(b2, a1, QUIET);

p.play<WHITE>(m1);
p.play<BLACK>(m2);
p.play<WHITE>(m3);
p.play<BLACK>(m4);
p.play<WHITE>(m5);
EXPECT_NE(p.get_hash(), initial_hash);

p.undo<WHITE>(m5);
p.undo<BLACK>(m4);
p.undo<WHITE>(m3);
p.undo<BLACK>(m2);
p.undo<WHITE>(m1);
EXPECT_EQ(p.get_hash(), initial_hash);
}

TEST_F(MoveGenerationZobristHashTests, TestCastling) {
Position p;
Position::set(INITIAL_BOARD_FEN, p);
zobrist_hash initial_hash = p.get_hash();

Move m1 = Move(e2, e4, DOUBLE_PUSH);
Move m2 = Move(e7, e5, DOUBLE_PUSH);
Move m3 = Move(g1, f3);
Move m4 = Move(g8, f6);
Move m5 = Move(f1, d3);
Move m6 = Move(f8, d6);
Move m7 = Move(e1, g1, OO);

p.play<WHITE>(m1);
p.play<BLACK>(m2);
p.play<WHITE>(m3);
p.play<BLACK>(m4);
p.play<WHITE>(m5);
p.play<BLACK>(m6);
p.play<WHITE>(m7);

Position p2;
Position::set(p.fen(), p2);

EXPECT_EQ(p.get_hash(), p2.get_hash());

p.undo<WHITE>(m7);
p.undo<BLACK>(m6);
p.undo<WHITE>(m5);
p.undo<BLACK>(m4);
p.undo<WHITE>(m3);
p.undo<BLACK>(m2);
p.undo<WHITE>(m1);

Position p3;
Position::set(p.fen(), p3);
EXPECT_EQ(p.get_hash(), initial_hash);
EXPECT_EQ(p3.get_hash(), initial_hash);
}

TEST_F(MoveGenerationZobristHashTests, TestEPFile) {
Position p;
Position::set("rnbqkbnr/1ppppppp/8/pP6/8/8/P1PPPPPP/RNBQKBNR w KQkq a6 0 1", p);
EXPECT_EQ(p.ep_file(), 0);
Position::set("rnbqkbnr/p1pppppp/8/1pP5/8/8/PP1PPPPP/RNBQKBNR w KQkq b6 0 1", p);
EXPECT_EQ(p.ep_file(), 1);
Position::set("rnbqkbnr/pp1ppppp/8/2pP4/8/8/PPP1PPPP/RNBQKBNR w KQkq c6 0 1", p);
EXPECT_EQ(p.ep_file(), 2);
Position::set("rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 1", p);
EXPECT_EQ(p.ep_file(), 3);
Position::set("rnbqkbnr/pppp1ppp/8/4pP2/8/8/PPPPP1PP/RNBQKBNR w KQkq e6 0 1", p);
EXPECT_EQ(p.ep_file(), 4);
Position::set("rnbqkbnr/ppppp1pp/8/5pP1/8/8/PPPPPP1P/RNBQKBNR w KQkq f6 0 1", p);
EXPECT_EQ(p.ep_file(), 5);
Position::set("rnbqkbnr/pppppp1p/8/6pP/8/8/PPPPPPP1/RNBQKBNR w KQkq g6 0 1", p);
EXPECT_EQ(p.ep_file(), 6);
Position::set("rnbqkbnr/ppppppp1/8/6Pp/8/8/PPPPPP1P/RNBQKBNR w KQkq h6 0 1", p);
EXPECT_EQ(p.ep_file(), 7);
Position::set(INITIAL_BOARD_FEN, p);
EXPECT_EQ(p.ep_file(), 8);


Position::set(INITIAL_BOARD_FEN, p);
zobrist_hash initial_hash = p.get_hash();
Move m1 = Move(e2, e4, DOUBLE_PUSH);
Move m2 = Move(d7, d5, DOUBLE_PUSH);
Move m3 = Move(e4, e5);
Move m4 = Move(f7, f5, DOUBLE_PUSH);
p.play<WHITE>(m1);
p.play<BLACK>(m2);
p.play<WHITE>(m3);
p.play<BLACK>(m4);
Position p2;
Position::set(p.fen(), p2);
EXPECT_EQ(p2.get_hash(), p.get_hash());

p.undo<BLACK>(m4);
p.undo<WHITE>(m3);
p.undo<BLACK>(m2);
p.undo<WHITE>(m1);
EXPECT_EQ(p.get_hash(), initial_hash);
}
1 change: 0 additions & 1 deletion ChessEngine/ChessEngine_tests/MoveSearchTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "gtest/gtest.h"
#include "move_generation/position.h"
#include "move_search/search.h"
#include "constants.h"

class MoveSearchFixture : public ::testing::Test {
protected:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified ChessEngine/cmake-build-debug/ChessEngine_run
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions ChessEngine/cmake-build-debug/Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Start testing: Feb 02 00:20 EST
Start testing: Feb 02 19:23 EST
----------------------------------------------------------
End testing: Feb 02 00:20 EST
End testing: Feb 02 19:23 EST

0 comments on commit f77f5ce

Please sign in to comment.