From fedc8a28e7b82aa1fc1e1a6659fb884608a4ca79 Mon Sep 17 00:00:00 2001 From: Hans Binderup Date: Wed, 5 Mar 2025 12:52:03 +0100 Subject: [PATCH 1/4] evaluator: improve time handling This commit clean up time handling when playing eg. lichess games where there is no max moves rule. This is definitely not final, but it's better than before. Also removed some commented out code. Sorry.. Signed-off-by: Hans Binderup --- src/evaluation/evaluator.h | 60 ++++++++++++++++++++++++++------------ src/uci_handler.h | 5 ---- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/evaluation/evaluator.h b/src/evaluation/evaluator.h index f0cc8e4..d113a4d 100644 --- a/src/evaluation/evaluator.h +++ b/src/evaluation/evaluator.h @@ -148,9 +148,11 @@ class Evaluator { { using namespace std::chrono; + /* allow some extra time for processing etc */ + constexpr auto buffer = 5ms; + const auto timeLeft = board.player == PlayerWhite ? milliseconds(m_whiteTime) : milliseconds(m_blackTime); const auto timeInc = board.player == PlayerWhite ? milliseconds(m_whiteMoveInc) : milliseconds(m_blackMoveInc); - const auto buffer = timeBuffer(board); if (m_moveTime) { m_endTime = start + milliseconds(m_moveTime.value()) - buffer + timeInc; @@ -158,14 +160,26 @@ class Evaluator { const auto time = timeLeft / m_movesToGo; m_endTime = start + time - buffer + timeInc; } else { - /* TODO: how long should we search if no time controls are set? */ - const auto time = timeLeft / (s_defaultAmountMoves - board.fullMoves); - m_endTime = start + time - buffer + timeInc; + /* Dynamic time allocation based on game phase */ + constexpr uint32_t openingMoves = 20; + constexpr uint32_t lateGameMoves = 50; + + /* Estimate remaining moves */ + uint32_t movesRemaining = std::clamp(s_defaultAmountMoves - board.fullMoves, openingMoves, lateGameMoves); + + /* Allocate time proportionally */ + const auto time = timeLeft / movesRemaining; + + /* Adjust based on game phase (early = slightly faster, late = slightly deeper) */ + const float phaseFactor = 1.0 + (static_cast(board.fullMoves) / s_defaultAmountMoves) * 0.5; + const auto adjustedTime = duration_cast(time * phaseFactor); + + m_endTime = start + adjustedTime - buffer + timeInc; } /* just to make sure that we actually search something - better to run out of time than to not search any moves.. */ if (m_endTime <= start) { - m_endTime = start + buffer + timeInc; + m_endTime = start + milliseconds(250) + timeInc; } }; @@ -185,6 +199,29 @@ class Evaluator { if (m_isStopped) break; + /* always allow full scan on first move - will be good for the hash table :) */ + if (board.fullMoves > 0) { + const auto now = std::chrono::system_clock::now(); + const auto timeLeft = m_endTime - now; + const auto timeSpent = now - startTime; + + /* factor is "little less than half" meaning that we give juuust about half the time we spent to search a new depth + * might need tweaking - will do when game phases are implemented */ + const auto timeLimit = timeSpent / 1.9; + + /* uncommment for debugging */ + + /* m_logger.log("d: {}, timeLeft: {}, timeSpent: {}, timeLimit: {}", d, */ + /* std::chrono::duration_cast(timeLeft), */ + /* std::chrono::duration_cast(timeSpent), */ + /* std::chrono::duration_cast(timeLimit)); */ + + if (timeLeft < timeLimit) { + /* m_logger.log("Stopped early; saved: {}", std::chrono::duration_cast(timeLeft)); */ + break; + } + } + m_scoring.pvTable().setIsFollowing(true); int32_t score = negamax(d, board, alpha, beta); @@ -473,19 +510,6 @@ class Evaluator { } } - constexpr std::chrono::milliseconds timeBuffer(const BitBoard& board) - { - using namespace std::chrono_literals; - - if (board.fullMoves < 4) { - return 500ms; - } else if (board.fullMoves < 6) { - return 250ms; - } else { - return 5ms; - } - } - struct MoveResult { uint64_t hash; BitBoard board; diff --git a/src/uci_handler.h b/src/uci_handler.h index 6e1c6da..e3e8dc9 100644 --- a/src/uci_handler.h +++ b/src/uci_handler.h @@ -24,10 +24,6 @@ class UciHandler { engine::TtHashTable::clear(); s_evaluator.reset(); - /* const auto board = parsing::FenParser::parse("pk6/8/8/6Bb/8/PPPP4/8/PK6 w - - 0 0"); */ - /* engine::printBoardDebug(s_fileLogger, board.value()); */ - /* s_evaluator.printEvaluation(board.value(), 5); */ - std::array buffer; while (s_isRunning && std::cin.getline(buffer.data(), buffer.size())) { processInput(std::string_view(buffer.data())); @@ -38,7 +34,6 @@ class UciHandler { static bool processInput(std::string_view input) { const auto [command, args] = parsing::split_sv_by_space(input); - /* s_fileLogger.log("processInput, command: {}, args: {}", command, args); */ if (command == "uci") { return handleUci(); From c9ee9df92049f4246a609eaaca0ff4fcd12ef330 Mon Sep 17 00:00:00 2001 From: Hans Binderup Date: Wed, 5 Mar 2025 22:33:44 +0100 Subject: [PATCH 2/4] pv_table: increase capacity to avoid overflow The PV table would previously have the same size as the max search depth. This is problematic as the copy of moves will be into n+1. Therefore we need an extra layer hence increasing the size of the tables by one. Without this the tables will overflow when reaching the max search depth. Signed-off-by: Hans Binderup --- src/evaluation/evaluator.h | 4 ---- src/evaluation/pv_table.h | 11 ++++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/evaluation/evaluator.h b/src/evaluation/evaluator.h index d113a4d..b865af8 100644 --- a/src/evaluation/evaluator.h +++ b/src/evaluation/evaluator.h @@ -250,10 +250,6 @@ class Evaluator { { using namespace std::chrono; - /* if we're stopped to early and no PV was found - don't print empty eval */ - if (m_scoring.pvTable().size() == 0) - return; - const auto endTime = system_clock::now(); const auto timeDiff = duration_cast(endTime - startTime).count(); diff --git a/src/evaluation/pv_table.h b/src/evaluation/pv_table.h index 07152de..39144b3 100644 --- a/src/evaluation/pv_table.h +++ b/src/evaluation/pv_table.h @@ -106,10 +106,15 @@ class PVTable { movement::Move& operator[](int i) { return m_pvTable.at(0).at(i); } private: - using PVNode = std::array; - std::array m_pvTable {}; - std::array m_pvLength {}; + /* NOTE: tables are +1 as we'll need space to copy next layer of ply - + * EVEN if we're at the max level */ + constexpr static inline std::size_t s_pvTableSize { s_maxSearchDepth + 1 }; + using PVNode = std::array; + std::array m_pvTable {}; + std::array m_pvLength {}; + + /* TODO: use single state instead of two bools - will be racy when we start multi threading */ bool m_isScoring; bool m_isFollowing; }; From 4112e7634b8da7c14c29ad46abcfd15347b6185d Mon Sep 17 00:00:00 2001 From: Hans Binderup Date: Wed, 5 Mar 2025 23:52:11 +0100 Subject: [PATCH 3/4] evaluation: use tapered pesto evaluation Instead of our simple eval it was time to implement a more sophisticated material evaluation. A common evaluation method is called "pesto". Read more here: https://www.chessprogramming.org/PeSTO%27s_Evaluation_Function Signed-off-by: Hans Binderup --- src/engine/tt_hash_table.h | 2 +- src/evaluation/evaluator.h | 5 + src/evaluation/material_scoring.h | 37 +-- src/evaluation/pesto_tables.h | 385 ++++++++++++++++++++++++++++++ src/evaluation/position_tables.h | 121 +--------- src/evaluation/positioning.h | 41 ---- 6 files changed, 400 insertions(+), 191 deletions(-) create mode 100644 src/evaluation/pesto_tables.h diff --git a/src/engine/tt_hash_table.h b/src/engine/tt_hash_table.h index ebad4c4..c77409e 100644 --- a/src/engine/tt_hash_table.h +++ b/src/engine/tt_hash_table.h @@ -105,7 +105,7 @@ class TtHashTable { } private: - constexpr static inline std::size_t s_hashTableSizeMb { 64 }; + constexpr static inline std::size_t s_hashTableSizeMb { 128 }; constexpr static inline std::size_t s_ttHashSize { (s_hashTableSizeMb * 1024 * 1024) / sizeof(TtHashEntry) }; constexpr static inline uint8_t s_generationMask { 0xF }; /* 4-bit wrapping generation (max 16 generations allowed) */ diff --git a/src/evaluation/evaluator.h b/src/evaluation/evaluator.h index b865af8..f23088d 100644 --- a/src/evaluation/evaluator.h +++ b/src/evaluation/evaluator.h @@ -248,6 +248,11 @@ class Evaluator { constexpr void printScoreInfo(std::chrono::time_point startTime, int32_t score, uint8_t currentDepth) { + /* most likely means that we've been stopped early / before a pv line was found - no need to print it */ + if (m_scoring.pvTable().size() == 0) { + return; + } + using namespace std::chrono; const auto endTime = system_clock::now(); diff --git a/src/evaluation/material_scoring.h b/src/evaluation/material_scoring.h index d0e4292..16f6a1d 100644 --- a/src/evaluation/material_scoring.h +++ b/src/evaluation/material_scoring.h @@ -1,6 +1,7 @@ #pragma once #include "src/bit_board.h" +#include "src/evaluation/pesto_tables.h" #include "src/evaluation/positioning.h" #include @@ -13,49 +14,28 @@ namespace evaluation { -namespace { - -/* material score for each piece - lookup based on Piece enum */ -constexpr static inline auto s_pieceScoring = std::to_array({ - 100, /* White Pawn */ - 300, /* White Knight */ - 350, /* White Bishop */ - 500, /* White Rook */ - 1000, /* White Queen */ - 10000, /* White King */ - -100, /* Black Pawn */ - -300, /* Black Knight */ - -350, /* Black Bishop */ - -500, /* Black Rook */ - -1000, /* Black Queen */ - -10000, /* Black King */ -}); - -} - constexpr int32_t materialScore(const BitBoard& board) { - int32_t score = 0; + /* we first evaluate based on "pesto score" + * then we add mobility, double pawn etc. */ + int32_t score = pesto::evaluate(board); // Material scoring for (const auto piece : magic_enum::enum_values()) { - score += std::popcount(board.pieces[piece]) * s_pieceScoring[piece]; - switch (piece) { case WhitePawn: score += position::getPawnScore(board, board.pieces[piece]); break; case WhiteKnight: - score += position::getKnightScore(board.pieces[piece]); break; case WhiteBishop: - score += position::getBishopScore(board, board.pieces[piece]); + score += position::getBishopScore(board, board.pieces[piece]); break; case WhiteRook: score += position::getRookScore(board, board.pieces[piece]); break; case WhiteQueen: - score += position::getQueenScore(board, board.pieces[piece]); + score += position::getQueenScore(board, board.pieces[piece]); break; case WhiteKing: score += position::getKingScore(board, board.pieces[piece]); @@ -67,16 +47,15 @@ constexpr int32_t materialScore(const BitBoard& board) score -= position::getPawnScore(board, board.pieces[piece]); break; case BlackKnight: - score -= position::getKnightScore(board.pieces[piece]); break; case BlackBishop: - score -= position::getBishopScore(board, board.pieces[piece]); + score -= position::getBishopScore(board, board.pieces[piece]); break; case BlackRook: score -= position::getRookScore(board, board.pieces[piece]); break; case BlackQueen: - score -= position::getQueenScore(board, board.pieces[piece]); + score -= position::getQueenScore(board, board.pieces[piece]); break; case BlackKing: score -= position::getKingScore(board, board.pieces[piece]); diff --git a/src/evaluation/pesto_tables.h b/src/evaluation/pesto_tables.h new file mode 100644 index 0000000..ef3475a --- /dev/null +++ b/src/evaluation/pesto_tables.h @@ -0,0 +1,385 @@ +#pragma once + +#include "magic_enum/magic_enum.hpp" +#include "src/bit_board.h" +#include "src/board_defs.h" +#include "src/helpers/bit_operations.h" +#include +#include + +/* Tapered evaluation score + * based on https://www.chessprogramming.org/PeSTO%27s_Evaluation_Function + */ + +namespace evaluation::pesto { + +using PestoTable = std::array; + +namespace { + +namespace mg { + +constexpr PestoTable s_whitePawnTable { + 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 +}; + +constexpr PestoTable s_whiteKnightTable { + -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 +}; + +constexpr PestoTable s_whiteBishopTable { + -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 +}; + +constexpr PestoTable s_whiteRookTable { + -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 +}; + +constexpr PestoTable s_whiteQueenTable { + -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 +}; + +constexpr PestoTable s_whiteKingTable { + -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 +}; + +constexpr PestoTable s_blackPawnTable { + 0, 0, 0, 0, 0, 0, 0, 0, + 98, 134, 61, 95, 68, 126, 34, -11, + -6, 7, 26, 31, 65, 56, 25, -20, + -14, 13, 6, 21, 23, 12, 17, -23, + -27, -2, -5, 12, 17, 6, 10, -25, + -26, -4, -4, -10, 3, 3, 33, -12, + -35, -1, -20, -23, -15, 24, 38, -22, + 0, 0, 0, 0, 0, 0, 0, 0 +}; + +constexpr PestoTable s_blackKnightTable { + -167, -89, -34, -49, 61, -97, -15, -107, + -73, -41, 72, 36, 23, 62, 7, -17, + -47, 60, 37, 65, 84, 129, 73, 44, + -9, 17, 19, 53, 37, 69, 18, 22, + -13, 4, 16, 13, 28, 19, 21, -8, + -23, -9, 12, 10, 19, 17, 25, -16, + -29, -53, -12, -3, -1, 18, -14, -19, + -105, -21, -58, -33, -17, -28, -19, -23 +}; + +constexpr PestoTable s_blackBishopTable { + -29, 4, -82, -37, -25, -42, 7, -8, + -26, 16, -18, -13, 30, 59, 18, -47, + -16, 37, 43, 40, 35, 50, 37, -2, + -4, 5, 19, 50, 37, 37, 7, -2, + -6, 13, 13, 26, 34, 12, 10, 4, + 0, 15, 15, 15, 14, 27, 18, 10, + 4, 15, 16, 0, 7, 21, 33, 1, + -33, -3, -14, -21, -13, -12, -39, -21 +}; + +constexpr PestoTable s_blackRookTable { + 32, 42, 32, 51, 63, 9, 31, 43, + 27, 32, 58, 62, 80, 67, 26, 44, + -5, 19, 26, 36, 17, 45, 61, 16, + -24, -11, 7, 26, 24, 35, -8, -20, + -36, -26, -12, -1, 9, -7, 6, -23, + -45, -25, -16, -17, 3, 0, -5, -33, + -44, -16, -20, -9, -1, 11, -6, -71, + -19, -13, 1, 17, 16, 7, -37, -26 +}; + +constexpr PestoTable s_blackQueenTable { + -28, 0, 29, 12, 59, 44, 43, 45, + -24, -39, -5, 1, -16, 57, 28, 54, + -13, -17, 7, 8, 29, 56, 47, 57, + -27, -27, -16, -16, -1, 17, -2, 1, + -9, -26, -9, -10, -2, -4, 3, -3, + -14, 2, -11, -2, -5, 2, 14, 5, + -35, -8, 11, 2, 8, 15, -3, 1, + -1, -18, -9, 10, -15, -25, -31, -50 +}; + +constexpr PestoTable s_blackKingTable { + -65, 23, 16, -15, -56, -34, 2, 13, + 29, -1, -20, -7, -8, -4, -38, -29, + -9, 24, 2, -16, -20, 6, 22, -22, + -17, -20, -12, -27, -30, -25, -14, -36, + -49, -1, -27, -39, -46, -44, -33, -51, + -14, -14, -22, -46, -44, -30, -15, -27, + 1, 7, -8, -64, -43, -16, 9, 8, + -15, 36, 12, -54, 8, -28, 24, 14 +}; + +constexpr std::array()> s_pieceValue = { + 82, 337, 365, 477, 1025, 0, /* white pieces */ + 82, 337, 365, 477, 1025, 0, /* black pieces */ +}; + +constexpr auto generatePestoTable() +{ + std::array()> tables; + + for (BoardPosition p : magic_enum::enum_values()) { + tables[WhitePawn][p] = s_whitePawnTable[p] + s_pieceValue[WhitePawn]; + tables[WhiteKnight][p] = s_whiteKnightTable[p] + s_pieceValue[WhiteKnight]; + tables[WhiteBishop][p] = s_whiteBishopTable[p] + s_pieceValue[WhiteBishop]; + tables[WhiteRook][p] = s_whiteRookTable[p] + s_pieceValue[WhiteRook]; + tables[WhiteQueen][p] = s_whiteQueenTable[p] + s_pieceValue[WhiteQueen]; + tables[WhiteKing][p] = s_whiteKingTable[p] + s_pieceValue[WhiteKing]; + + tables[BlackPawn][p] = s_blackPawnTable[p] + s_pieceValue[BlackPawn]; + tables[BlackKnight][p] = s_blackKnightTable[p] + s_pieceValue[BlackKnight]; + tables[BlackBishop][p] = s_blackBishopTable[p] + s_pieceValue[BlackBishop]; + tables[BlackRook][p] = s_blackRookTable[p] + s_pieceValue[BlackRook]; + tables[BlackQueen][p] = s_blackQueenTable[p] + s_pieceValue[BlackQueen]; + tables[BlackKing][p] = s_blackKingTable[p] + s_pieceValue[BlackKing]; + } + + return tables; +} + +} + +namespace eg { + +constexpr PestoTable s_whitePawnTable { + 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 +}; + +constexpr PestoTable s_whiteKnightTable { + -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 +}; + +constexpr PestoTable s_whiteBishopTable { + -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 +}; + +constexpr PestoTable s_whiteRookTable { + -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 +}; + +constexpr PestoTable s_whiteQueenTable { + -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 +}; + +constexpr PestoTable s_whiteKingTable { + -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 +}; + +constexpr PestoTable s_blackPawnTable { + 0, 0, 0, 0, 0, 0, 0, 0, + 178, 173, 158, 134, 147, 132, 165, 187, + 94, 100, 85, 67, 56, 53, 82, 84, + 32, 24, 13, 5, -2, 4, 17, 17, + 13, 9, -3, -7, -7, -8, 3, -1, + 4, 7, -6, 1, 0, -5, -1, -8, + 13, 8, 8, 10, 13, 0, 2, -7, + 0, 0, 0, 0, 0, 0, 0, 0 +}; + +constexpr PestoTable s_blackKnightTable { + -58, -38, -13, -28, -31, -27, -63, -99, + -25, -8, -25, -2, -9, -25, -24, -52, + -24, -20, 10, 9, -1, -9, -19, -41, + -17, 3, 22, 22, 22, 11, 8, -18, + -18, -6, 16, 25, 16, 17, 4, -18, + -23, -3, -1, 15, 10, -3, -20, -22, + -42, -20, -10, -5, -2, -20, -23, -44, + -29, -51, -23, -15, -22, -18, -50, -64 +}; + +constexpr PestoTable s_blackBishopTable { + -14, -21, -11, -8, -7, -9, -17, -24, + -8, -4, 7, -12, -3, -13, -4, -14, + 2, -8, 0, -1, -2, 6, 0, 4, + -3, 9, 12, 9, 14, 10, 3, 2, + -6, 3, 13, 19, 7, 10, -3, -9, + -12, -3, 8, 10, 13, 3, -7, -15, + -14, -18, -7, -1, 4, -9, -15, -27, + -23, -9, -23, -5, -9, -16, -5, -17 +}; + +constexpr PestoTable s_blackRookTable { + 13, 10, 18, 15, 12, 12, 8, 5, + 11, 13, 13, 11, -3, 3, 8, 3, + 7, 7, 7, 5, 4, -3, -5, -3, + 4, 3, 13, 1, 2, 1, -1, 2, + 3, 5, 8, 4, -5, -6, -8, -11, + -4, 0, -5, -1, -7, -12, -8, -16, + -6, -6, 0, 2, -9, -9, -11, -3, + -9, 2, 3, -1, -5, -13, 4, -20 +}; + +constexpr PestoTable s_blackQueenTable { + -9, 22, 22, 27, 27, 19, 10, 20, + -17, 20, 32, 41, 58, 25, 30, 0, + -20, 6, 9, 49, 47, 35, 19, 9, + 3, 22, 24, 45, 57, 40, 57, 36, + -18, 28, 19, 47, 31, 34, 39, 23, + -16, -27, 15, 6, 9, 17, 10, 5, + -22, -23, -30, -16, -16, -23, -36, -32, + -33, -28, -22, -43, -5, -32, -20, -41 +}; + +constexpr PestoTable s_blackKingTable { + -74, -35, -18, -18, -11, 15, 4, -17, + -12, 17, 14, 17, 17, 38, 23, 11, + 10, 17, 23, 15, 20, 45, 44, 13, + -8, 22, 24, 27, 26, 33, 26, 3, + -18, -4, 21, 24, 27, 23, 9, -11, + -19, -3, 11, 21, 23, 16, 7, -9, + -27, -11, 4, 13, 14, 4, -5, -17, + -53, -34, -21, -11, -28, -14, -24, -43 +}; + +constexpr std::array()> s_pieceValue = { + 94, 281, 297, 512, 936, 0, /* white pieces */ + 94, 281, 297, 512, 936, 0, /* black pieces */ +}; + +constexpr auto generatePestoTable() +{ + std::array()> tables; + + for (BoardPosition p : magic_enum::enum_values()) { + tables[WhitePawn][p] = s_whitePawnTable[p] + s_pieceValue[WhitePawn]; + tables[WhiteKnight][p] = s_whiteKnightTable[p] + s_pieceValue[WhiteKnight]; + tables[WhiteBishop][p] = s_whiteBishopTable[p] + s_pieceValue[WhiteBishop]; + tables[WhiteRook][p] = s_whiteRookTable[p] + s_pieceValue[WhiteRook]; + tables[WhiteQueen][p] = s_whiteQueenTable[p] + s_pieceValue[WhiteQueen]; + tables[WhiteKing][p] = s_whiteKingTable[p] + s_pieceValue[WhiteKing]; + + tables[BlackPawn][p] = s_blackPawnTable[p] + s_pieceValue[BlackPawn]; + tables[BlackKnight][p] = s_blackKnightTable[p] + s_pieceValue[BlackKnight]; + tables[BlackBishop][p] = s_blackBishopTable[p] + s_pieceValue[BlackBishop]; + tables[BlackRook][p] = s_blackRookTable[p] + s_pieceValue[BlackRook]; + tables[BlackQueen][p] = s_blackQueenTable[p] + s_pieceValue[BlackQueen]; + tables[BlackKing][p] = s_blackKingTable[p] + s_pieceValue[BlackKing]; + } + + return tables; +} +} +} + +constexpr std::array()> s_gamePhaseScore = { + 0, 1, 1, 2, 4, 0, /* white pieces */ + 0, 1, 1, 2, 4, 0 /* black pieces */ +}; + +constexpr auto s_mgTables = mg::generatePestoTable(); +constexpr auto s_egTables = eg::generatePestoTable(); + +constexpr int32_t evaluate(const BitBoard& board) +{ + int32_t mgScore {}; + int32_t egScore {}; + uint8_t gamePhase {}; + + for (uint8_t wp = WhitePawn; wp <= WhiteKing; wp++) { + helper::bitIterate(board.pieces[wp], [&mgScore, &egScore, &gamePhase, wp](BoardPosition pos) { + mgScore += s_mgTables[wp][pos]; + egScore += s_egTables[wp][pos]; + gamePhase += s_gamePhaseScore[wp]; + }); + } + + for (uint8_t bp = BlackPawn; bp <= BlackKing; bp++) { + helper::bitIterate(board.pieces[bp], [&mgScore, &egScore, &gamePhase, bp](BoardPosition pos) { + mgScore -= s_mgTables[bp][pos]; + egScore -= s_egTables[bp][pos]; + gamePhase += s_gamePhaseScore[bp]; + }); + } + + constexpr uint8_t mgLimit { 24 }; + const uint8_t mgGamePhase = std::min(gamePhase, mgLimit); /* 'min' in case of early promotion */ + const uint8_t egGamePhase = mgLimit - mgGamePhase; + + return (mgScore * mgGamePhase + egScore * egGamePhase) / mgLimit; +} + +} diff --git a/src/evaluation/position_tables.h b/src/evaluation/position_tables.h index 9a9d0db..de60651 100644 --- a/src/evaluation/position_tables.h +++ b/src/evaluation/position_tables.h @@ -188,124 +188,5 @@ constexpr auto s_isolationMaskTable = generateIsolationMaskTable(); constexpr auto s_whitePassedPawnMaskTable = generateWhitePassedPawnMaskTable(); constexpr auto s_blackPassedPawnMaskTable = generateBlackPassedPawnMaskTable(); -constexpr auto s_whitePawnTable = std::to_array( - { 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 }); - -constexpr auto s_whiteKnightTable = std::to_array( - { -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 }); - -constexpr auto s_whiteBishopTable = std::to_array( - { 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 }); - -constexpr auto s_whiteRookTable = std::to_array( - { 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 }); - -constexpr auto s_whiteQueenTable = std::to_array( - { -20, -10, -10, -5, -5, -10, -10, -20, - -10, 0, 5, 0, 0, 0, 0, -10, - -10, 5, 5, 5, 5, 5, 0, -10, - 0, 0, 5, 5, 5, 5, 0, -5, - -5, 0, 5, 5, 5, 5, 0, -5, - -10, 0, 5, 5, 5, 5, 0, -10, - -10, 0, 0, 0, 0, 0, 0, -10, - -20, -10, -10, -5, -5, -10, -10, -20 }); - -constexpr auto s_whiteKingTable = std::to_array( - { 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 }); - -constexpr auto s_blackPawnTable = std::to_array( - { 90, 90, 90, 90, 90, 90, 90, 90, - 30, 30, 30, 40, 40, 30, 30, 30, - 20, 20, 20, 30, 30, 30, 20, 20, - 10, 10, 10, 20, 20, 10, 10, 10, - 5, 5, 10, 20, 20, 5, 5, 5, - 0, 0, 0, 5, 5, 0, 0, 0, - 0, 0, 0, -10, -10, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 }); - -constexpr auto s_blackKnightTable = std::to_array( - { -5, 0, 0, 0, 0, 0, 0, -5, - -5, 0, 0, 10, 10, 0, 0, -5, - -5, 5, 20, 20, 20, 20, 5, -5, - -5, 10, 20, 30, 30, 20, 10, -5, - -5, 10, 20, 30, 30, 20, 10, -5, - -5, 5, 20, 10, 10, 20, 5, -5, - -5, 0, 0, 0, 0, 0, 0, -5, - -5, -10, 0, 0, 0, 0, -10, -5 }); - -constexpr auto s_blackBishopTable = std::to_array( - { 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 10, 10, 0, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 0, 10, 20, 20, 10, 0, 0, - 0, 10, 0, 0, 0, 0, 10, 0, - 0, 30, 0, 0, 0, 0, 30, 0, - 0, 0, -10, 0, 0, -10, 0, 0 }); - -constexpr auto s_blackRookTable = std::to_array( - { 50, 50, 50, 50, 50, 50, 50, 50, - 50, 50, 50, 50, 50, 50, 50, 50, - 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, - 0, 0, 0, 20, 20, 0, 0, 0 }); - -constexpr auto s_blackQueenTable = std::to_array( - { -20, -10, -10, -5, -5, -10, -10, -20, - -10, 0, 0, 0, 0, 0, 0, -10, - -10, 0, 5, 5, 5, 5, 0, -10, - -5, 0, 5, 5, 5, 5, 0, -5, - 0, 0, 5, 5, 5, 5, 0, -5, - -10, 5, 5, 5, 5, 5, 0, -10, - -10, 0, 5, 0, 0, 0, 0, -10, - -20, -10, -10, -5, -5, -10, -10, -20 }); - -constexpr auto s_blackKingTable = std::to_array( - { 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 5, 5, 5, 5, 0, 0, - 0, 5, 5, 10, 10, 5, 5, 0, - 0, 5, 10, 20, 20, 10, 5, 0, - 0, 5, 10, 20, 20, 10, 5, 0, - 0, 0, 5, 10, 10, 5, 0, 0, - 0, 5, 5, -5, -5, 0, 5, 0, - 0, 0, 5, 0, -15, 0, 10, 0 }); - } + diff --git a/src/evaluation/positioning.h b/src/evaluation/positioning.h index f31707f..122547d 100644 --- a/src/evaluation/positioning.h +++ b/src/evaluation/positioning.h @@ -11,11 +11,6 @@ #include "src/movement/kings.h" #include "src/movement/rooks.h" -/* - * Based on the tables from: - * https://www.chessprogramming.org/Simplified_Evaluation_Function - */ - namespace evaluation { namespace { @@ -48,13 +43,11 @@ constexpr static inline int32_t getPawnScore(const BitBoard& board, const uint64 score += s_isolatedPawnPenalty; if constexpr (player == PlayerWhite) { - score += s_whitePawnTable[pos]; if ((board.pieces[BlackPawn] & s_whitePassedPawnMaskTable[pos]) == 0) { const uint8_t row = (pos / 8); score += s_passedPawnBonus[row]; } } else { - score += s_blackPawnTable[pos]; if ((board.pieces[WhitePawn] & s_blackPassedPawnMaskTable[pos]) == 0) { const uint8_t row = (pos / 8); score += s_passedPawnBonus[7 - row]; /* invert table for black */ @@ -65,23 +58,6 @@ constexpr static inline int32_t getPawnScore(const BitBoard& board, const uint64 return score; } -template -constexpr static inline int32_t getKnightScore(const uint64_t knights) -{ - int32_t score = 0; - - helper::bitIterate(knights, [&score](BoardPosition pos) { - if constexpr (player == PlayerWhite) { - score += s_whiteKnightTable[pos]; - } else { - score += s_blackKnightTable[pos]; - } - }); - - return score; -} - -template constexpr static inline int32_t getBishopScore(const BitBoard& board, const uint64_t bishops) { int32_t score = 0; @@ -89,12 +65,6 @@ constexpr static inline int32_t getBishopScore(const BitBoard& board, const uint helper::bitIterate(bishops, [&score, &board](BoardPosition pos) { const uint64_t moves = movement::getBishopAttacks(pos, board.occupation[Both]); score += std::popcount(moves) * s_bishopMobilityScore; - - if constexpr (player == PlayerWhite) { - score += s_whiteBishopTable[pos]; - } else { - score += s_blackBishopTable[pos]; - } }); return score; @@ -113,12 +83,10 @@ constexpr static inline int32_t getRookScore(const BitBoard& board, const uint64 score += s_openFileScore; if constexpr (player == PlayerWhite) { - score += s_whiteRookTable[pos]; if ((whitePawns & s_fileMaskTable[pos]) == 0) score += s_semiOpenFileScore; } else { - score += s_blackRookTable[pos]; if ((blackPawns & s_isolationMaskTable[pos]) == 0) score += s_semiOpenFileScore; } @@ -127,7 +95,6 @@ constexpr static inline int32_t getRookScore(const BitBoard& board, const uint64 return score; } -template constexpr static inline int32_t getQueenScore(const BitBoard& board, const uint64_t queens) { int32_t score = 0; @@ -138,12 +105,6 @@ constexpr static inline int32_t getQueenScore(const BitBoard& board, const uint6 | movement::getRookAttacks(pos, board.occupation[Both]); score += std::popcount(moves) * s_queenMobilityScore; - - if constexpr (player == PlayerWhite) { - score += s_whiteQueenTable[pos]; - } else { - score += s_blackQueenTable[pos]; - } }); return score; @@ -163,14 +124,12 @@ constexpr static inline int32_t getKingScore(const BitBoard& board, const uint64 score -= s_openFileScore; if constexpr (player == PlayerWhite) { - score += s_whiteKingTable[pos]; if ((whitePawns & s_fileMaskTable[pos]) == 0) score -= s_semiOpenFileScore; const uint64_t kingShields = movement::getKingAttacks(pos) & board.occupation[PlayerWhite]; score += std::popcount(kingShields) * s_kingShieldScore; } else { - score += s_blackKingTable[pos]; if ((blackPawns & s_fileMaskTable[pos]) == 0) score -= s_semiOpenFileScore; From 51084c93194b8425b7f1b6deb74d36ed1d81b563 Mon Sep 17 00:00:00 2001 From: Hans Binderup Date: Thu, 6 Mar 2025 15:44:30 +0100 Subject: [PATCH 4/4] move_scoring: add promotion scoring Promotion moves are usually quite good, so let's try them first as well. Signed-off-by: Hans Binderup --- src/evaluation/move_scoring.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/evaluation/move_scoring.h b/src/evaluation/move_scoring.h index ecc5706..f5fcd98 100644 --- a/src/evaluation/move_scoring.h +++ b/src/evaluation/move_scoring.h @@ -43,6 +43,19 @@ class MoveScoring { return 20000; } + switch (move.promotionType()) { + case PromotionNone: + break; + case PromotionQueen: + return 19000; + case PromotionKnight: + return 18000; + case PromotionBishop: + return 16000; + case PromotionRook: + return 17000; + } + const auto attacker = board.getPieceAtSquare(move.fromSquare()); const auto victim = board.getPieceAtSquare(move.toSquare());