-
-
Notifications
You must be signed in to change notification settings - Fork 3
Evaluation.md
This page explains how MicroChess evaluates chess board positions to select the best moves, focusing on the evaluation functions implemented in chessutil.cpp and pieces.cpp. The evaluation process is critical for the minimax algorithm with alpha-beta pruning, enabling the engine to assess positions within Arduino’s memory constraints (less than 2K RAM).
The evaluation function assigns a numerical score to a board position, reflecting its quality from the perspective of the current player. A positive score favors white, a negative score favors black, and zero indicates a balanced position. MicroChess’s evaluation balances accuracy and efficiency to operate on resource-constrained Arduino boards.
The evaluation function, primarily in chessutil.cpp and supported by pieces.cpp, considers several factors:
- Material Balance: The total value of pieces on the board for each side.
- Piece Activity: The mobility and positioning of pieces (e.g., centralized pieces score higher).
- King Safety: The vulnerability of each player’s king (e.g., exposure to checks).
- Pawn Structure: Factors like doubled pawns, isolated pawns, or passed pawns.
- Control of Key Squares: Influence over the center (e.g., d4, d5, e4, e5).
Each piece is assigned a base value (in centipawns, where 100 = 1 pawn):
- Pawn: 100
- Knight: 300
- Bishop: 300
- Rook: 500
- Queen: 900
- King: Not scored for material (as it cannot be captured), but considered for safety.
These values are defined in pieces.cpp and used to compute the material balance by summing the values of each player’s pieces.
The evaluation function (in chessutil.cpp) works as follows:
-
Material Score:
- Sum the values of all pieces for white and black.
- Compute the difference:
material_score = white_value - black_value.
-
Positional Score:
- Add bonuses for piece activity (e.g., knights on central squares like e4 score higher).
- Apply penalties for weaknesses (e.g., doubled pawns, exposed king).
- Example: A knight on d5 might gain a +20 bonus, while a pawn on a2 with no adjacent pawns loses -10.
-
King Safety:
- Penalize kings exposed to checks or lacking pawn cover (e.g., -50 for a king on an open file).
- Reward pawn shields (e.g., +30 for three pawns in front of the king).
-
Pawn Structure:
- Reward passed pawns (e.g., +50 for a pawn with no opposing pawns blocking its path).
- Penalize doubled pawns (-20) or isolated pawns (-15).
-
Final Score:
- Combine all factors:
score = material_score + positional_score + king_safety + pawn_structure. - Return the score from the perspective of the current player (negated if black’s turn).
- Combine all factors:
To fit within 2K RAM:
- Simplified Heuristics: Uses lightweight positional bonuses/penalties instead of complex calculations.
-
Precomputed Tables: Stores piece-square tables (in
pieces.cpp) to assign positional scores based on square location (e.g., knight on e4 vs. a1). - Incremental Updates: Updates the evaluation incrementally during move application to avoid rescanning the board.
-
Bit Fields: Relies on
board_t’s compact representation to access piece data efficiently.
The evaluation function interacts with:
-
Board Representation (
board.h/cpp): Reads theboard_tinstance to access piece positions and types. -
Move Generation (
move.h/cpp): Assigns scores to generated moves for ranking in the minimax algorithm. -
Game Logic (
game.h/cpp): Provides scores for current and hypothetical board states during move evaluation. -
Search Algorithm (
MicroChess.ino): Supplies scores to the minimax algorithm to determine the best move.
For a position after 1. e4 e5:
- Material: White and black each have 8 pawns (800), 2 knights (600), 2 bishops (600), 2 rooks (1000), 1 queen (900) = 3900 points each. Material score = 0.
-
Positional:
- White pawn on e4: +10 (central control).
- Black pawn on e5: +10 (central control).
- Knights and bishops: Neutral (still on starting squares).
- King Safety: Both kings are safe (+0).
- Pawn Structure: No weaknesses (+0).
- Total Score: 0 (balanced position).
If white plays 2. Nf3, attacking e5, a small bonus (+5) might be added for piece activity, tilting the score slightly in white’s favor.
Evaluation affects the engine’s move choices, visible in:
- LED Strip: Shows the board after evaluated moves (see MicroChessSmall.gif).
- Serial Console: Displays moves chosen based on evaluation (see MicroChessConsole2.gif). More details are in the Visuals page.
- Explore Search Algorithm for how evaluations drive move selection.
- See Game Logic for game state management.
- Check Code Structure for an overview of all files.
MicroChess | GitHub Repository | License: MIT | Contributing
© 2025 Trent M. Wyatt. All rights reserved.