Skip to content

Commit

Permalink
Update Catch2 and retune evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
konsolas committed Oct 21, 2023
1 parent be2f694 commit d497db6
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 345 deletions.
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ set(SOURCE_FILES
pvs.h pvs.cpp
syzygy/tbconfig.h syzygy/tbconfig.cpp
syzygy/fathom.h syzygy/fathom.cpp)
set(TEST_FILES testing/runner.cpp testing/util.h testing/util.cpp
set(TEST_FILES testing/util.h testing/util.cpp
testing/test_bb.cpp
testing/test_board.cpp
testing/test_perft.cpp
Expand All @@ -39,7 +39,7 @@ Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.7
GIT_TAG v3.4.0
)
FetchContent_Declare(
Fathom
Expand All @@ -53,11 +53,10 @@ include_directories(syzygy ${fathom_SOURCE_DIR}/src)
list(APPEND SOURCE_FILES ${fathom_SOURCE_DIR}/src/tbprobe.c)

# Enable unit tests
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib)
include(CTest)
include(Catch)
add_executable(ToppleTest ${SOURCE_FILES} ${TEST_FILES})
target_link_libraries(ToppleTest Catch2::Catch2)
target_link_libraries(ToppleTest Catch2::Catch2WithMain)
target_compile_options(ToppleTest PUBLIC -march=native -O3)
catch_discover_tests(ToppleTest)

Expand Down
78 changes: 38 additions & 40 deletions bb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,34 @@ namespace bb_sliders {
/*
* Initialisation of various utility bitboards
*/
namespace bb_util {
namespace bb_tables {
U64 between[64][64];
U64 line[64][64];
U64 ray[64][64];
U64 file[8];
U64 king_area[64];

void init_util() {
for (uint8_t a = 0; a < 64; a++) {
// File
{
file[file_index(a)] |= single_bit(a);
}
U64 king_moves[64];
U64 knight_moves[64];
U64 pawn_moves_x1[2][64];
U64 pawn_moves_x2[2][64];
U64 pawn_caps[2][64];

// We use this method (and signed integers) to not generate moves off the edge of the board (or which wrap around)
inline bool valid_square(const int &file, const int &rank) {
return file >= 0 && file < 8 && rank >= 0 && rank < 8;
}

// Check if move is valid and add to lookup table if it is
inline void update_array(U64 *arr, const uint8_t &file, const uint8_t &rank,
int file_offset, int rank_offset) {
if (valid_square(file + file_offset, rank + rank_offset)) {
arr[square_index(file, rank)] |= single_bit(square_index(file + file_offset, rank + rank_offset));
}
}

void init_tables() {
// Utility tables
for (uint8_t a = 0; a < 64; a++) {
for (uint8_t b = 0; b < 64; b++) {
// Between
{
Expand Down Expand Up @@ -272,35 +287,8 @@ namespace bb_util {
}
}
}
}
}

/**
* =====================================================================================================================
* GENERATION FOR NON-SLIDING PIECES
* =====================================================================================================================
*/
namespace bb_normal_moves {
U64 king_moves[64];
U64 knight_moves[64];
U64 pawn_moves_x1[2][64];
U64 pawn_moves_x2[2][64];
U64 pawn_caps[2][64];

// We use this method (and signed integers) to not generate moves off the edge of the board (or which wrap around)
inline bool valid_square(const int &file, const int &rank) {
return file >= 0 && file < 8 && rank >= 0 && rank < 8;
}

// Check if move is valid and add to lookup table if it is
inline void update_array(U64 *arr, const uint8_t &file, const uint8_t &rank,
int file_offset, int rank_offset) {
if (valid_square(file + file_offset, rank + rank_offset)) {
arr[square_index(file, rank)] |= single_bit(square_index(file + file_offset, rank + rank_offset));
}
}

void init_normal_moves() {
// Normal moves
for (uint8_t file_from = 0; file_from < 8; file_from++) {
for (uint8_t rank_from = 0; rank_from < 8; rank_from++) {
// King moves
Expand Down Expand Up @@ -341,13 +329,23 @@ namespace bb_normal_moves {
update_array(pawn_caps[BLACK], file_from, rank_from, -1, -1);
}
}

// King area
for (int file_from = 0; file_from < 8; file_from++) {
for (int rank_from = 0; rank_from < 8; rank_from++) {
king_area[square_index(file_from, rank_from)] = king_moves[square_index(std::clamp(file_from, 1, 6), std::clamp(rank_from, 1, 6))];
}
}
}
}

void init_tables() {
bb_sliders::init_sliders();
bb_util::init_util();
bb_normal_moves::init_normal_moves();
namespace {
struct init_tables {
init_tables() {
bb_sliders::init_sliders();
bb_tables::init_tables();
}
} _;
}

uint8_t to_sq(char file, char rank) {
Expand Down
64 changes: 37 additions & 27 deletions bb.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,29 +327,18 @@ namespace bb_sliders {
/*
* Potentially useful bitboard lookup tables
*/
namespace bb_util {
namespace bb_tables {
extern U64 between[64][64]; // between[a][b] is the bitboard between a and b, or 0 if they are unaligned
extern U64 line[64][64]; // line[a][b] is the bitboard of the line through a and b, or 0 if they are unaligned
extern U64 ray[64][64]; // ray[a][b] is the bitboard of the ray from a through b, or 0 if they are unaligned
extern U64 file[8]; // file[f] is the bitboard of file number f
}

/*
* Non-sliding move generation lookup tables
*/
namespace bb_normal_moves {
extern U64 king_moves[64];
extern U64 knight_moves[64];
extern U64 pawn_moves_x1[2][64]; // Normal pawn advances
extern U64 pawn_moves_x2[2][64]; // Double pawn advances on the 2nd or 7th rank
extern U64 pawn_caps[2][64]; // Pawn captures
extern U64 king_area[64]; // 3x3 area around the king, shifted inwards if the king is on the edge of the board
}

/**
* Initialise bitboard tables. Must be called before use of any other functions in this header.
*/
void init_tables();

/**
* Generate a bitboard of possible moves from the {@code square}, assuming that {@code occupied} is a bitboard which
* represents the occupied square on the board.
Expand All @@ -374,19 +363,19 @@ inline U64 find_moves(Team side, uint8_t square, U64 occupied);
template<>
inline U64 find_moves<PAWN>(Team side, uint8_t square, U64 occupied) {
U64 result = 0;
result |= occupied & bb_normal_moves::pawn_caps[side][square];
if (!(occupied & bb_normal_moves::pawn_moves_x1[side][square])) {
result |= bb_normal_moves::pawn_moves_x1[side][square];
if (!(occupied & bb_normal_moves::pawn_moves_x2[side][square])) {
result |= bb_normal_moves::pawn_moves_x2[side][square];
result |= occupied & bb_tables::pawn_caps[side][square];
if (!(occupied & bb_tables::pawn_moves_x1[side][square])) {
result |= bb_tables::pawn_moves_x1[side][square];
if (!(occupied & bb_tables::pawn_moves_x2[side][square])) {
result |= bb_tables::pawn_moves_x2[side][square];
}
}
return result;
}

template<>
inline U64 find_moves<KNIGHT>(Team side, uint8_t square, U64 occupied) {
return bb_normal_moves::knight_moves[square];
return bb_tables::knight_moves[square];
}

template<>
Expand All @@ -406,7 +395,7 @@ inline U64 find_moves<QUEEN>(Team side, uint8_t square, U64 occupied) {

template<>
inline U64 find_moves<KING>(Team side, uint8_t square, U64 occupied) {
return bb_normal_moves::king_moves[square];
return bb_tables::king_moves[square];
}

/**
Expand Down Expand Up @@ -447,7 +436,7 @@ inline U64 find_moves(Piece type, Team side, uint8_t square, U64 occupied) {
* @return possible captures of the pawn
*/
inline U64 pawn_caps(Team side, uint8_t square) {
return bb_normal_moves::pawn_caps[side][square];
return bb_tables::pawn_caps[side][square];
}

/**
Expand All @@ -470,7 +459,7 @@ inline bool multiple_bits(U64 bb) {
* @return bitboard containing bits between the two squares
*/
inline U64 bits_between(uint8_t a, uint8_t b) {
return bb_util::between[a][b];
return bb_tables::between[a][b];
}

/**
Expand All @@ -483,7 +472,7 @@ inline U64 bits_between(uint8_t a, uint8_t b) {
* @return bitboard containing a line which crosses the two squares
*/
inline U64 line(uint8_t a, uint8_t b) {
return bb_util::line[a][b];
return bb_tables::line[a][b];
}

/**
Expand All @@ -497,7 +486,19 @@ inline U64 line(uint8_t a, uint8_t b) {
* @return bitboard containing the ray
*/
inline U64 ray(uint8_t origin, uint8_t direction) {
return bb_util::ray[origin][direction];
return bb_tables::ray[origin][direction];
}

/**
* Generates a bitboard containing the area of the board immediately around the king. If the king is on the edge of
* the board, this returns a region shifted towards the centre of the board to account for the reduced mobility of
* the king, resulting in a potentially better evaluation of safety.
*
* @param sq king position
* @return bitboard of the king area
*/
inline U64 king_area(uint8_t sq) {
return bb_tables::king_area[sq];
}

/**
Expand All @@ -506,9 +507,18 @@ inline U64 ray(uint8_t origin, uint8_t direction) {
* @param file_index index of file
* @return bitboard of the file
*/

inline U64 file_mask(uint8_t file_index) {
return bb_util::file[file_index];
constexpr U64 file_mask(uint8_t file_index) {
constexpr U64 mask[8] = {
0x0101010101010101,
0x0202020202020202,
0x0404040404040404,
0x0808080808080808,
0x1010101010101010,
0x2020202020202020,
0x4040404040404040,
0x8080808080808080,
};
return mask[file_index];
}

/**
Expand Down
37 changes: 4 additions & 33 deletions eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,6 @@
#include <cstring>
#include <cmath>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Utility tables
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

U64 BB_KING_SQUARE[64] = {};
U64 BB_KING_CIRCLE[64] = {};
U64 BB_PAWN_SHIELD[64] = {};

void evaluator_t::eval_init() {
for (uint8_t sq = 0; sq < 64; sq++) {
BB_KING_SQUARE[sq] = single_bit(sq) | find_moves<KING>(WHITE, sq, 0);
}

for (uint8_t sq = 0; sq < 64; sq++) {
// King circle
BB_KING_CIRCLE[sq] = BB_KING_SQUARE[sq];
if(rank_index(sq) == 0) BB_KING_CIRCLE[sq] |= bb_shifts::shift<D_N>(BB_KING_SQUARE[sq]);
if(rank_index(sq) == 7) BB_KING_CIRCLE[sq] |= bb_shifts::shift<D_S>(BB_KING_SQUARE[sq]);
if(file_index(sq) == 0) BB_KING_CIRCLE[sq] |= bb_shifts::shift<D_E>(BB_KING_SQUARE[sq]);
if(file_index(sq) == 7) BB_KING_CIRCLE[sq] |= bb_shifts::shift<D_W>(BB_KING_SQUARE[sq]);

if(rank_index(sq) <= 1) {
BB_PAWN_SHIELD[sq] = bb_shifts::shift<D_N>(BB_KING_SQUARE[sq]);
} else if(rank_index(sq) >= 6) {
BB_PAWN_SHIELD[sq] = bb_shifts::shift<D_S>(BB_KING_SQUARE[sq]);
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Preprocessing evaluation parameters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -123,8 +94,8 @@ int evaluator_t::evaluate(const board_t &board) {
// Initialise king danger evaluation
data.king_pos[WHITE] = bit_scan(board.pieces(WHITE, KING));
data.king_pos[BLACK] = bit_scan(board.pieces(BLACK, KING));
data.king_circle[WHITE] = BB_KING_CIRCLE[data.king_pos[WHITE]];
data.king_circle[BLACK] = BB_KING_CIRCLE[data.king_pos[BLACK]];
data.king_circle[WHITE] = king_area(data.king_pos[WHITE]);
data.king_circle[BLACK] = king_area(data.king_pos[BLACK]);
data.king_danger[WHITE] = params.kat_zero;
data.king_danger[BLACK] = params.kat_zero;
data.update_attacks(WHITE, KING, find_moves<KING>(WHITE, data.king_pos[WHITE], board.all()));
Expand Down Expand Up @@ -296,8 +267,8 @@ v4si_t evaluator_t::eval_pawns(const board_t &board, eval_data_t &data, float &t
};

// King shield
int pawn_shield_w = std::min(3, pop_count(BB_PAWN_SHIELD[data.king_pos[WHITE]] & board.pieces(WHITE, PAWN)));
int pawn_shield_b = std::min(3, pop_count(BB_PAWN_SHIELD[data.king_pos[BLACK]] & board.pieces(BLACK, PAWN)));
int pawn_shield_w = std::min(3, pop_count(data.king_circle[WHITE] & board.pieces(WHITE, PAWN)));
int pawn_shield_b = std::min(3, pop_count(data.king_circle[BLACK] & board.pieces(BLACK, PAWN)));

score += (blocked_count[WHITE][0] - blocked_count[BLACK][0]) * params.blocked[0];
score += (blocked_count[WHITE][1] - blocked_count[BLACK][1]) * params.blocked[1];
Expand Down
Loading

0 comments on commit d497db6

Please sign in to comment.