stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

commit 120c077931942c5cc5308bf9ccc62cc83cfd6c82
parent d519ce0daa0a384b31511d5a730900697f6c3fe6
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 12 Sep 2023 15:43:07 +0200

New evaluations, better interpolation, different ttable

Diffstat:
MCMakeLists.txt | 2+-
Msrc/engine/engine.cpp | 19+++++++++++++++----
Msrc/engine/evaluate.cpp | 70++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Msrc/engine/score.hpp | 213++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
4 files changed, 204 insertions(+), 100 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project( Stellar - VERSION 1.1.0 + VERSION 9.0.2 DESCRIPTION "Chess engine written in C++" HOMEPAGE_URL https://git.dimitrijedobrota.com/stellar.git LANGUAGES CXX diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp @@ -117,10 +117,21 @@ static U64 nodes; static uint8_t ply; U32 inline move_list_score(Move move) { + static constexpr const uint16_t capture[6][6] = { + // clang-format off + {105, 205, 305, 405, 505, 605}, + {104, 204, 304, 404, 504, 604}, + {103, 203, 303, 403, 503, 603}, + {102, 202, 302, 402, 502, 602}, + {101, 201, 301, 401, 501, 601}, + {100, 200, 300, 400, 500, 600}, + // clang-format on + }; + const piece::Type type = board.get_square_piece_type(move.source()); if (move.is_capture()) { const piece::Type captured = board.get_square_piece_type(move.target()); - return score::get(type, captured) + 10000; + return capture[to_underlying(type)][to_underlying(captured)] + 10000; } if (killer[0][ply] == move) return 9000; if (killer[1][ply] == move) return 8000; @@ -188,11 +199,11 @@ void stats_move_unmake(Board &copy, const Move move) { } int16_t quiescence(int16_t alpha, int16_t beta) { + pvtable.start(ply); if ((nodes & 2047) == 0) { uci::communicate(settings); if (settings->stopped) return 0; } - pvtable.start(ply); nodes++; int score = evaluate::score_position(board); @@ -227,11 +238,11 @@ int16_t negamax(int16_t alpha, int16_t beta, uint8_t depth, bool null) { Move bestMove; Board copy; + pvtable.start(ply); if ((nodes & 2047) == 0) { uci::communicate(settings); if (settings->stopped) return 0; } - pvtable.start(ply); // && fifty >= 100 if (ply && rtable.is_repetition(board.get_hash())) return 0; @@ -378,7 +389,7 @@ Move search_position(const uci::Settings &settingsr) { settings = &settingsr; if (settings->newgame) { - ttable = TTable(C64(0x1000000)); + ttable = TTable(C64(0x2FB4377)); } rtable.clear(); diff --git a/src/engine/evaluate.cpp b/src/engine/evaluate.cpp @@ -71,9 +71,22 @@ using piece::Type::PAWN; using piece::Type::QUEEN; using piece::Type::ROOK; -int16_t score_position_side(const Board &board, const color::Color side) { - U64 bitboard; +using score::Phase::ENDGAME; +using score::Phase::OPENING; + +uint16_t score_game_phase(const Board &board) { int16_t total = 0; + for (int type_i = KNIGHT; type_i < KING; type_i++) { + const piece::Type type = static_cast<piece::Type>(type_i); + total += bit::count(board.get_bitboard_piece(type)) * score::get(type); + } + return total; +} + +int16_t score_position_side(const Board &board, const color::Color side, const uint16_t phase_score) { + U64 bitboard; + + int16_t total = 0, opening = 0, endgame = 0; int8_t square_i; const uint8_t side_i = to_underlying(side); @@ -84,66 +97,79 @@ int16_t score_position_side(const Board &board, const color::Color side) { bitboard = board.get_bitboard_piece(PAWN, side); bitboard_for_each_bit(square_i, bitboard) { const square::Square square = static_cast<square::Square>(square_i); - total += score::get(PAWN, side, square); - total += score::get(PAWN); + opening += score::get(PAWN, side, square, OPENING) + score::get(PAWN, OPENING); + endgame += score::get(PAWN, side, square, ENDGAME) + score::get(PAWN, ENDGAME); // check isolated, doubled and passed pawns const uint8_t file = square::file(square), rank = square::rank(square); - if (!(mask_isolated[file] & pawnsS)) total -= score::pawn_isolated; - if (bit::count(pawnsS & mask_file[file]) > 1) total -= score::pawn_double; + if (!(mask_isolated[file] & pawnsS)) { + opening -= score::pawn_isolated_opening; + endgame -= score::pawn_isolated_endgame; + } + + if (bit::count(pawnsS & mask_file[file]) > 1) { + opening -= score::pawn_double_opening; + endgame -= score::pawn_double_endgame; + } + if (!(pawnsO & mask_passed[side_i][square_i])) total += score::pawn_passed[side_i][rank]; } bitboard = board.get_bitboard_piece(KNIGHT, side); bitboard_for_each_bit(square_i, bitboard) { const square::Square square = static_cast<square::Square>(square_i); - total += score::get(KNIGHT, side, square); - total += score::get(KNIGHT); + opening += score::get(KNIGHT, side, square, OPENING) + score::get(KNIGHT, OPENING); + endgame += score::get(KNIGHT, side, square, ENDGAME) + score::get(KNIGHT, ENDGAME); } bitboard = board.get_bitboard_piece(BISHOP, side); bitboard_for_each_bit(square_i, bitboard) { const square::Square square = static_cast<square::Square>(square_i); - total += score::get(BISHOP, side, square); - total += score::get(BISHOP); + opening += score::get(BISHOP, side, square, OPENING) + score::get(BISHOP, OPENING); + endgame += score::get(BISHOP, side, square, ENDGAME) + score::get(BISHOP, ENDGAME); } bitboard = board.get_bitboard_piece(ROOK, side); bitboard_for_each_bit(square_i, bitboard) { const square::Square square = static_cast<square::Square>(square_i); - total += score::get(ROOK, side, square); - total += score::get(ROOK); + opening += score::get(ROOK, side, square, OPENING) + score::get(ROOK, OPENING); + endgame += score::get(ROOK, side, square, ENDGAME) + score::get(ROOK, ENDGAME); // rook on open and semi-open files const uint8_t file = square::file(square); - if (!(pawns & mask_file[file])) total += score::score_open; - if (!(pawnsS & mask_file[file])) total += score::score_open_semi; + if (!(pawns & mask_file[file])) total += score::file_open; + if (!(pawnsS & mask_file[file])) total += score::file_open_semi; } bitboard = board.get_bitboard_piece(QUEEN, side); bitboard_for_each_bit(square_i, bitboard) { const square::Square square = static_cast<square::Square>(square_i); - total += score::get(QUEEN, side, square); - total += score::get(QUEEN); + opening += score::get(QUEEN, side, square, OPENING) + score::get(QUEEN, OPENING); + endgame += score::get(QUEEN, side, square, ENDGAME) + score::get(QUEEN, ENDGAME); } bitboard = board.get_bitboard_piece(KING, side); bitboard_for_each_bit(square_i, bitboard) { const square::Square square = static_cast<square::Square>(square_i); - total += score::get(KING, side, square); - total += score::get(KING); + opening += score::get(KING, side, square, OPENING) + score::get(KING, OPENING); + endgame += score::get(KING, side, square, ENDGAME) + score::get(KING, ENDGAME); // king on open and semi-open files const uint8_t file = square::file(square); - if (!(pawns & mask_file[file])) total -= score::score_open; - if (!(pawnsS & mask_file[file])) total -= score::score_open_semi; + if (!(pawns & mask_file[file])) total -= score::file_open; + if (!(pawnsS & mask_file[file])) total -= score::file_open_semi; } - return total; + opening += total, endgame += total; + if (phase_score > score::phase_opening) return opening; + if (phase_score < score::phase_endgame) return endgame; + return score::interpolate(phase_score, opening, endgame); } int16_t score_position(const Board &board) { - const int16_t score = score_position_side(board, color::WHITE) - score_position_side(board, color::BLACK); + const uint16_t phase_score = score_game_phase(board); + const int16_t score = score_position_side(board, color::WHITE, phase_score) - + score_position_side(board, color::BLACK, phase_score); return board.get_side() == color::WHITE ? score : -score; } diff --git a/src/engine/score.hpp b/src/engine/score.hpp @@ -1,6 +1,7 @@ #ifndef STELLAR_SCORE_H #define STELLAR_SCORE_H +#include "utils.hpp" #define MAX_PLY 64 #define SCORE_INFINITY 32000 @@ -9,95 +10,161 @@ namespace score { -inline constexpr const uint16_t value[6] = {100, 300, 350, 500, 1000, 10000}; -inline constexpr const uint16_t capture[6][6] = { - // clang-format off - {105, 205, 305, 405, 505, 605}, - {104, 204, 304, 404, 504, 604}, - {103, 203, 303, 403, 503, 603}, - {102, 202, 302, 402, 502, 602}, - {101, 201, 301, 401, 501, 601}, - {100, 200, 300, 400, 500, 600}, - // clang-format on +inline constexpr const uint16_t value[2][6] = { + {82, 337, 365, 477, 1025, 12000}, + {94, 201, 297, 512, 936, 12000}, }; -inline constexpr const int8_t position[6][64] = { +inline constexpr const int16_t position[2][6][64] = { // clang-format off { - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, -10, -10, 0, 0, 0, - 0, 0, 0, 5, 5, 0, 0, 0, - 5, 5, 10, 20, 20, 5, 5, 5, - 10, 10, 10, 20, 20, 10, 10, 10, - 20, 20, 20, 30, 30, 30, 20, 20, - 30, 30, 30, 40, 40, 30, 30, 30, - 90, 90, 90, 90, 90, 90, 90, 90 - }, { - -5, -10 , 0, 0, 0, 0, -10, -5, - -5, 0, 0, 0, 0, 0, 0, -5, - -5, 5, 20, 10, 10, 20, 5, -5, - -5, 10, 20, 30, 30, 20, 10, -5, - -5, 10, 20, 30, 30, 20, 10, -5, - -5, 5, 20, 20, 20, 20, 5, -5, - -5, 0, 0, 10, 10, 0, 0, -5, - -5, 0, 0, 0, 0, 0, 0, -5 - }, { - 0, 0, -10, 0, 0, -10, 0, 0, - 0, 30, 0, 0, 0, 0, 30, 0, - 0, 10, 0, 0, 0, 0, 10, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 0, 0, 10, 10, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 - }, { - 0, 0, 0, 20, 20, 0, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 50, 50, 50, 50, 50, 50, 50, 50, - 50, 50, 50, 50, 50, 50, 50, 50 - }, { - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 + { + 0, 0, 0, 0, 0, 0, 0, 0, + -35, -1, -20, -23, -15, 24, 38, -22, + -26, -4, -4, -10, 3, 3, 33, -12, + -27, -2, -5, 12, 17, 6, 10, -25, + -14, 13, 6, 21, 23, 12, 17, -23, + -6, 7, 26, 31, 65, 56, 25, -20, + 98, 134, 61, 95, 68, 126, 34, -11, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + -105, -21, -58, -33, -17, -28, -19, -23, + -29, -53, -12, -3, -1, 18, -14, -19, + -23, -9, 12, 10, 19, 17, 25, -16, + -13, 4, 16, 13, 28, 19, 21, -8, + -9, 17, 19, 53, 37, 69, 18, 22, + -47, 60, 37, 65, 84, 129, 73, 44, + -73, -41, 72, 36, 23, 62, 7, -17, + -167, -89, -34, -49, 61, -97, -15, -107, + }, { + -33, -3, -14, -21, -13, -12, -39, -21, + 4, 15, 16, 0, 7, 21, 33, 1, + 0, 15, 15, 15, 14, 27, 18, 10, + -6, 13, 13, 26, 34, 12, 10, 4, + -4, 5, 19, 50, 37, 37, 7, -2, + -16, 37, 43, 40, 35, 50, 37, -2, + -26, 16, -18, -13, 30, 59, 18, -47, + -29, 4, -82, -37, -25, -42, 7, -8, + }, { + -19, -13, 1, 17, 16, 7, -37, -26, + -44, -16, -20, -9, -1, 11, -6, -71, + -45, -25, -16, -17, 3, 0, -5, -33, + -36, -26, -12, -1, 9, -7, 6, -23, + -24, -11, 7, 26, 24, 35, -8, -20, + -5, 19, 26, 36, 17, 45, 61, 16, + 27, 32, 58, 62, 80, 67, 26, 44, + 32, 42, 32, 51, 63, 9, 31, 43, + }, { + -1, -18, -9, 10, -15, -25, -31, -50, + -35, -8, 11, 2, 8, 15, -3, 1, + -14, 2, -11, -2, -5, 2, 14, 5, + -9, -26, -9, -10, -2, -4, 3, -3, + -27, -27, -16, -16, -1, 17, -2, 1, + -13, -17, 7, 8, 29, 56, 47, 57, + -24, -39, -5, 1, -16, 57, 28, 54, + -28, 0, 29, 12, 59, 44, 43, 45, + }, { + -15, 36, 12, -54, 8, -28, 24, 14, + 1, 7, -8, -64, -43, -16, 9, 8, + -14, -14, -22, -46, -44, -30, -15, -27, + -49, -1, -27, -39, -46, -44, -33, -51, + -17, -20, -12, -27, -30, -25, -14, -36, + -9, 24, 2, -16, -20, 6, 22, -22, + 29, -1, -20, -7, -8, -4, -38, -29, + -65, 23, 16, -15, -56, -34, 2, 13, + } }, { - 0, 0, 5, 0, -15, 0, 10, 0, - 0, 5, 5, -5, -5, 0, 5, 0, - 0, 0, 5, 10, 10, 5, 0, 0, - 0, 5, 10, 20, 20, 10, 5, 0, - 0, 5, 10, 20, 20, 10, 5, 0, - 0, 5, 5, 10, 10, 5, 5, 0, - 0, 0, 5, 5, 5, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 - }, + { + 0, 0, 0, 0, 0, 0, 0, 0, + 13, 8, 8, 10, 13, 0, 2, -7, + 4, 7, -6, 1, 0, -5, -1, -8, + 13, 9, -3, -7, -7, -8, 3, -1, + 32, 24, 13, 5, -2, 4, 17, 17, + 94, 100, 85, 67, 56, 53, 82, 84, + 178, 173, 158, 134, 147, 132, 165, 187, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + -29, -51, -23, -15, -22, -18, -50, -64, + -42, -20, -10, -5, -2, -20, -23, -44, + -23, -3, -1, 15, 10, -3, -20, -22, + -18, -6, 16, 25, 16, 17, 4, -18, + -17, 3, 22, 22, 22, 11, 8, -18, + -24, -20, 10, 9, -1, -9, -19, -41, + -25, -8, -25, -2, -9, -25, -24, -52, + -58, -38, -13, -28, -31, -27, -63, -99, + }, { + -23, -9, -23, -5, -9, -16, -5, -17, + -14, -18, -7, -1, 4, -9, -15, -27, + -12, -3, 8, 10, 13, 3, -7, -15, + -6, 3, 13, 19, 7, 10, -3, -9, + -3, 9, 12, 9, 14, 10, 3, 2, + 2, -8, 0, -1, -2, 6, 0, 4, + -8, -4, 7, -12, -3, -13, -4, -14, + -14, -21, -11, -8, -7, -9, -17, -24, + }, { + -9, 2, 3, -1, -5, -13, 4, -20, + -6, -6, 0, 2, -9, -9, -11, -3, + -4, 0, -5, -1, -7, -12, -8, -16, + 3, 5, 8, 4, -5, -6, -8, -11, + 4, 3, 13, 1, 2, 1, -1, 2, + 7, 7, 7, 5, 4, -3, -5, -3, + 11, 13, 13, 11, -3, 3, 8, 3, + 13, 10, 18, 15, 12, 12, 8, 5, + }, { + -33, -28, -22, -43, -5, -32, -20, -41, + -22, -23, -30, -16, -16, -23, -36, -32, + -16, -27, 15, 6, 9, 17, 10, 5, + -18, 28, 19, 47, 31, 34, 39, 23, + 3, 22, 24, 45, 57, 40, 57, 36, + -20, 6, 9, 49, 47, 35, 19, 9, + -17, 20, 32, 41, 58, 25, 30, 0, + -9, 22, 22, 27, 27, 19, 10, 20, + }, { + -53, -34, -21, -11, -28, -14, -24, -43, + -27, -11, 4, 13, 14, 4, -5, -17, + -19, -3, 11, 21, 23, 16, 7, -9, + -18, -4, 21, 24, 27, 23, 9, -11, + -8, 22, 24, 27, 26, 33, 26, 3, + 10, 17, 23, 15, 20, 45, 44, 13, + -12, 17, 14, 17, 17, 38, 23, 11, + -74, -35, -18, -18, -11, 15, 4, -17, + } + } // clang-format on }; -inline constexpr uint16_t get(const piece::Type piece) { return value[to_underlying(piece)]; } -inline constexpr uint16_t get(const piece::Type piece, const piece::Type captured) { - return capture[to_underlying(piece)][to_underlying(captured)]; +inline constexpr const uint16_t phase_opening = 6192; +inline constexpr const uint16_t phase_endgame = 518; + +inline constexpr int16_t interpolate(int16_t phase_score, int16_t opening, int16_t endgame) { + return (opening * phase_score + endgame * (phase_opening - phase_score)) / phase_opening; +} + +enum Phase { + OPENING, + ENDGAME, +}; + +inline constexpr int16_t get(const piece::Type piece, const Phase phase = OPENING) { + return value[to_underlying(phase)][to_underlying(piece)]; } -inline constexpr int8_t get(const piece::Type type, const color::Color color, const square::Square square) { +inline constexpr int16_t get(const piece::Type piece, const color::Color color, const square::Square square, + const Phase phase = ENDGAME) { uint8_t square_i = to_underlying(color == color::WHITE ? square : square::mirror(square)); - return position[to_underlying(type)][square_i]; + return position[to_underlying(phase)][to_underlying(piece)][square_i]; } -inline constexpr const uint8_t pawn_double = 10; -inline constexpr const uint8_t pawn_isolated = 10; +inline constexpr const uint8_t pawn_double_opening = 5; +inline constexpr const uint8_t pawn_double_endgame = 10; +inline constexpr const uint8_t pawn_isolated_opening = 5; +inline constexpr const uint8_t pawn_isolated_endgame = 10; + inline constexpr const std::array<std::array<int16_t, 8>, 2> pawn_passed = { {{0, 10, 30, 50, 75, 100, 150, 200}, {200, 150, 100, 75, 50, 30, 10, 0}}}; -inline constexpr const uint8_t score_open_semi = 10; -inline constexpr const uint8_t score_open = 15; +inline constexpr const uint8_t file_open_semi = 10; +inline constexpr const uint8_t file_open = 15; } // namespace score