From 1de3779add579440f7795f4a44002c8702cfbe70 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 6 Mar 2025 07:58:57 +0100 Subject: [PATCH] implemented tapered eval --- source/evaluate.cpp | 27 +++++++++++--- source/evaluate.h | 85 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 16 deletions(-) diff --git a/source/evaluate.cpp b/source/evaluate.cpp index 8244c2d..6c7cb31 100644 --- a/source/evaluate.cpp +++ b/source/evaluate.cpp @@ -1,5 +1,16 @@ #include "evaluate.h" +static int GetPhaseWeight(Board &board, int color) { + int phase = 0; + + // For all piece types other than pawn and king + for (int piece = Knight; piece <= Queen; piece++) { + phase += board.pieces[piece].PopCount() * phaseValues[piece - 1]; + } + + return phase; +} + static int GetMaterialScore(Board &board, int type, int color) { int side = color ? -1 : 1; @@ -8,7 +19,11 @@ static int GetMaterialScore(Board &board, int type, int color) { } static int GetPositionalScore(Board &board, int type, int color) { - int score = 0; + int middleEval = 0; + int endEval = 0; + + int phaseWeight = GetPhaseWeight(board, color); + int side = color ? -1 : 1; Bitboard pieces = board.pieces[type] & board.colors[color]; @@ -18,15 +33,19 @@ static int GetPositionalScore(Board &board, int type, int color) { // White if (!color) { - score += pieceSquareScores[type][scoreSquares[square]] * side; + middleEval += middlePieceScores[type][scoreSquares[square]] * side; + endEval += endPieceSquares[type][scoreSquares[square]] * side; } else { - score += pieceSquareScores[type][square] * side; + middleEval += middlePieceScores[type][square] * side; + endEval += endPieceSquares[type][square] * side; } pieces.PopBit(square); } - return score; + int finalEval = (middleEval * phaseWeight + endEval * (phaseMax - phaseWeight)) / phaseMax; + + return finalEval; } int Evaluate(Board &board) { diff --git a/source/evaluate.h b/source/evaluate.h index 8a03a01..a6b889a 100644 --- a/source/evaluate.h +++ b/source/evaluate.h @@ -1,6 +1,15 @@ #pragma once #include "board.h" +constexpr int phaseMax = 24; + +constexpr int phaseValues[4] { + 1, // Knight + 1, // Bishop + 2, // Rook + 4 // Queen +}; + constexpr int materialValues[6] { 100, // Pawn 300, // Knight @@ -10,7 +19,7 @@ constexpr int materialValues[6] { 10000 // King }; -constexpr int pieceSquareScores[7][64] { +constexpr int middlePieceScores[6][64] { // Pawn {0, 0, 0, 0, 0, 0, 0, 0, 50, 50, 50, 50, 50, 50, 50, 50, @@ -61,7 +70,7 @@ constexpr int pieceSquareScores[7][64] { -10, 0, 5, 0, 0, 0, 0,-10, -20,-10,-10, -5, -5,-10,-10,-20}, - // King middle game + // King {-30,-40,-40,-50,-50,-40,-40,-30, -30,-40,-40,-50,-50,-40,-40,-30, -30,-40,-40,-50,-50,-40,-40,-30, @@ -70,16 +79,68 @@ constexpr int pieceSquareScores[7][64] { -10,-20,-20,-20,-20,-20,-20,-10, 20, 20, 0, 0, 0, 0, 20, 20, 20, 30, 10, 0, 0, 10, 30, 20}, - - // King end game - {-30,-40,-40,-50,-50,-40,-40,-30, - -30,-40,-40,-50,-50,-40,-40,-30, - -30,-40,-40,-50,-50,-40,-40,-30, - -30,-40,-40,-50,-50,-40,-40,-30, - -20,-30,-30,-40,-40,-30,-30,-20, - -10,-20,-20,-20,-20,-20,-20,-10, - 20, 20, 0, 0, 0, 0, 20, 20, - 20, 30, 10, 0, 0, 10, 30, 20} +}; + +constexpr int endPieceSquares[6][64] { + // Pawn + {0, 0, 0, 0, 0, 0, 0, 0, + 50, 50, 50, 50, 50, 50, 50, 50, + 10, 10, 20, 30, 30, 20, 10, 10, + 5, 5, 10, 25, 25, 10, 5, 5, + 0, 0, 0, 20, 20, 0, 0, 0, + 5, -5,-10, 30, 30,-10, -5, 5, + 5, 10, 10,-20,-20, 10, 10, 5, + 0, 0, 0, 0, 0, 0, 0, 0}, + + // Knight + {-50,-40,-30,-30,-30,-30,-40,-50, + -40,-20, 0, 0, 0, 0,-20,-40, + -30, 0, 5, 10, 10, 5, 0,-30, + -30, 5, 10, 15, 15, 10, 5,-30, + -30, 0, 10, 15, 15, 10, 0,-30, + -30, 5, 5, 10, 10, 5, 5,-30, + -40,-20, 0, 5, 5, 0,-20,-40, + -50,-40,-30,-30,-30,-30,-40,-50}, + + // Bishop + {-20,-10,-10,-10,-10,-10,-10,-20, + -10, 0, 0, 0, 0, 0, 0,-10, + -10, 0, 5, 10, 10, 5, 0,-10, + -10, 5, 5, 15, 15, 5, 5,-10, + -10, 0, 10, 15, 15, 10, 0,-10, + -10, 10, 10, 15, 15, 10, 10,-10, + -10, 5, 0, 5, 5, 0, 5,-10, + -20,-10,-10,-10,-10,-10,-10,-20}, + + // Rook + {0, 0, 0, 5, 5, 0, 0, 0, + 5, 10, 10, 10, 10, 10, 10, 5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 5, 5, 0, 0, -5, + -5, 0, 0, 5, 5, 0, 0, -5, + -5, 0, 0, 5, 5, 0, 0, -5, + -5, 0, 0, 5, 5, 0, 0, -5, + 0, 0, 5, 10, 10, 5, 0, 0}, + + // Queen + {-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}, + + // King + {-50,-40,-30,-20,-20,-30,-40,-50, + -30,-20,-10, 0, 0,-10,-20,-30, + -30,-10, 20, 30, 30, 20,-10,-30, + -30,-10, 30, 40, 40, 30,-10,-30, + -30,-10, 30, 40, 40, 30,-10,-30, + -30,-10, 20, 30, 30, 20,-10,-30, + -30,-20,-10, 0, 0,-10,-20,-30, + -50,-40,-30,-20,-20,-30,-40,-50}, }; constexpr int scoreSquares[64] {