Skip to content

Evaluation.md

Trent M. Wyatt edited this page Apr 21, 2025 · 1 revision

Evaluation

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).

Overview

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.

Evaluation Components

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).

Piece Values

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.

Evaluation Function

The evaluation function (in chessutil.cpp) works as follows:

  1. Material Score:
    • Sum the values of all pieces for white and black.
    • Compute the difference: material_score = white_value - black_value.
  2. 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.
  3. 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).
  4. 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).
  5. 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).

Optimization for Arduino

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.

Integration with Other Components

The evaluation function interacts with:

  • Board Representation (board.h/cpp): Reads the board_t instance 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.

Example

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.

Visual Aids

Evaluation affects the engine’s move choices, visible in:

Next Steps


Back to Home | Next: Search Algorithm

Clone this wiki locally