11#include " eval.h"
22
33// Initialize static weight map
4+ /* *
5+ * @brief Static map of evaluation weights used for chess position scoring
6+ *
7+ * This map contains predefined integer weights for various chess evaluation criteria,
8+ * including pawn structure, piece development, board control, and tactical considerations.
9+ *
10+ * The weights are used to calculate a comprehensive evaluation score for a chess position,
11+ * covering aspects such as:
12+ * - Pawn structure (doubled, backward, isolated pawns)
13+ * - Piece development and positioning
14+ * - King safety
15+ * - Tactical opportunities (pins, forks, discovered attacks)
16+ * - Piece values
17+ *
18+ * Weights are defined as static const to ensure consistent evaluation across the chess engine.
19+ */
420const std::unordered_map<EvalKey, int > EvalWeights::weights = {
521 {EvalKey::DOUBLED, 10 }, {EvalKey::BACKWARD, 8 }, {EvalKey::BLOCKED, 5 },
622 {EvalKey::ISLANDED, 6 }, {EvalKey::ISOLATED, 12 }, {EvalKey::DBLISOLATED, 20 },
@@ -17,9 +33,8 @@ const std::unordered_map<EvalKey, int> EvalWeights::weights = {
1733 {EvalKey::TEMPO_FREEDOM_WEIGHT, 8 }, {EvalKey::TEMPO_OPPONENT_MOBILITY_PENALTY, 6 },
1834 {EvalKey::UNDERPROMOTE, 5 }, {EvalKey::PAWN, 100 }, {EvalKey::KNIGHT, 300 },
1935 {EvalKey::BISHOP, 300 }, {EvalKey::ROOK, 500 }, {EvalKey::QUEEN, 900 },
20- {EvalKey::COUNTER, 1000 }, {EvalKey::PV_MOVE, MATE ( 0 )}
36+ {EvalKey::COUNTER, 1000 }
2137};
22- // std::unordered_map<U64, int> transposition; // Faster lookup
2338
2439// Declare only missing functions
2540int evaluatePawnStructure (const chess::Position&);
@@ -110,7 +125,6 @@ std::vector<Square> scan_reversed(chess::Bitboard bb)
110125 }
111126 return iter;
112127}
113- #ifdef DBG
114128// **1. Evaluate Bad Bishops**
115129int evaluateBadBishops (const chess::Position& board) {
116130 auto myBishops = board.pieces (chess::PieceType::underlying::BISHOP, board.sideToMove ());
@@ -126,7 +140,6 @@ int evaluateBadBishops(const chess::Position& board) {
126140 }
127141 return score;
128142}
129- #endif
130143int evaluateKingSafety (const chess::Position& board) {
131144 // 1) One-time fetches
132145 const int side = board.sideToMove ();
@@ -175,8 +188,6 @@ int evaluateKingSafety(const chess::Position& board) {
175188 int p = phase (board); // 1=open, 2=mid, 3=end
176189 return score * phaseWeight[p];
177190}
178- #ifdef DBG
179-
180191// **4. Evaluate Rooks on Open/Semi-Open Files**
181192int evaluateRooksOnFiles (const chess::Position& board) {
182193 auto myRooks = board.pieces (chess::PieceType::underlying::ROOK, board.sideToMove ());
@@ -926,7 +937,6 @@ int underpromote(chess::Position &board) {
926937
927938 return (lastMove.promotionType ().internal () != chess::PieceType::QUEEN) ? reward : 0 ;
928939}
929- #endif
930940
931941// PAWN
932942const int PAWN_PSQT[2 ][64 ] = {
@@ -1110,16 +1120,14 @@ int psqt_eval(const chess::Position& board) {
11101120 }
11111121 return score; // Negate for Black
11121122}
1123+ TranspositionTable evaltt (5 );
11131124unsigned long long evalhits=0 ;
11141125int eval (chess::Position &board) {
1115- /* U64 hash = board.hash();
1116- // Faster lookup & insertion
1117- auto [it, inserted] = transposition.emplace(hash, 0);
1118- if (!inserted){
1119- ++evalhits;
1120- return it->second; // Already exists, return stored evaluation
1121- }*/
1122-
1126+ U64 hash = board.hash ();
1127+ if (TTEntry* entry=evaltt.probe (hash)){
1128+ evalhits++;
1129+ return entry->score ();
1130+ }
11231131 // Precompute piece counts (avoid multiple function calls)
11241132 int pieceCount[10 ] = {
11251133 board.pieces (chess::PieceType::PAWN, chess::Color::WHITE).count (),
@@ -1143,7 +1151,6 @@ int eval(chess::Position &board) {
11431151
11441152 // Evaluate positional aspects
11451153 eval += evaluateKingSafety (board);
1146- #ifdef DBG
11471154 eval += evaluatePawnStructure (board);
11481155 eval += evaluatePieces (board);
11491156 eval += evaluateTactics (board);
@@ -1153,12 +1160,10 @@ int eval(chess::Position &board) {
11531160 eval += center_control (board);
11541161 eval += key_center_squares_control (board);
11551162 eval += evaluatePieceActivity (board);
1156- #endif
11571163 eval += psqt_eval (board);
11581164 board.makeNullMove ();
11591165 // Evaluate positional aspects
11601166 eval -= evaluateKingSafety (board);
1161- #ifdef DBG
11621167 eval -= evaluatePawnStructure (board);
11631168 eval -= evaluatePieces (board);
11641169 eval -= evaluateTactics (board);
@@ -1168,14 +1173,11 @@ int eval(chess::Position &board) {
11681173 eval -= center_control (board);
11691174 eval -= key_center_squares_control (board);
11701175 eval -= evaluatePieceActivity (board);
1171- #endif
11721176 eval -= psqt_eval (board);
11731177 board.unmakeNullMove ();
1174-
1175- // it->second = eval; // Store result
1178+ evaltt.store (hash, chess::Move (), eval, 0 , TTFlag::EXACT);
11761179 return eval;
11771180}
1178- #ifdef DBG
11791181// Use `const chess::Position&` to avoid copies
11801182int evaluatePawnStructure (const chess::Position& board) {
11811183 chess::Position b=board;
@@ -1189,18 +1191,4 @@ int evaluatePawnStructure(const chess::Position& board) {
11891191int evaluatePieces (const chess::Position& board) {
11901192 return -(evaluateBadBishops (board) + evaluateTrappedPieces (board) + evaluateKnightForks (board)) +
11911193 (evaluateFianchetto (board) + evaluateRooksOnFiles (board));
1192- }
1193- #endif
1194- int piece_value (chess::PieceType piece) {
1195- static const int lookup[] = {
1196- 0 , // NONE
1197- EvalWeights::getWeight (EvalKey::PAWN),
1198- EvalWeights::getWeight (EvalKey::KNIGHT),
1199- EvalWeights::getWeight (EvalKey::BISHOP),
1200- EvalWeights::getWeight (EvalKey::ROOK),
1201- EvalWeights::getWeight (EvalKey::QUEEN),
1202- 0 // KING or others → 0
1203- };
1204- int idx = static_cast <int >(piece);
1205- return (idx >= 0 && idx < 7 ) ? lookup[idx] : 0 ;
1206- }
1194+ }
0 commit comments