integrated material eval into psqt

This commit is contained in:
Moritz 2025-11-18 20:14:47 +01:00
parent 7b84e8a0ab
commit b69323665c
4 changed files with 127 additions and 120 deletions

Binary file not shown.

View file

@ -29,7 +29,7 @@ fn main() {
let mut engine = Engine::new("Yakari".to_string(), "EiSiMo".to_string()); let mut engine = Engine::new("Yakari".to_string(), "EiSiMo".to_string());
// Set the time limit to 1 second // Set the time limit to 1 second
let time_limit = Duration::from_millis(500); let time_limit = Duration::from_millis(1000);
for test in &sts { for test in &sts {
let fen = &test[0]; let fen = &test[0];
@ -40,7 +40,7 @@ fn main() {
// Record start time // Record start time
let start_time = Instant::now(); let start_time = Instant::now();
let result = engine.search(490_u64); let result = engine.search(990_u64);
// Calculate duration // Calculate duration
let duration = start_time.elapsed(); let duration = start_time.elapsed();

View file

@ -1,72 +1,65 @@
use crate::board::*; use crate::board::*;
use crate::eval::piece_square_tables::PSQT; use crate::eval::piece_square_tables::PSQT;
// Pawn, Knight, Bishop, Rook, Queen
pub const MATERIAL_WEIGHTS: [i32; 5] = [100, 300, 300, 500, 900];
pub const PHASE_WEIGHTS: [i32; 5] = [0, 1, 1, 2, 4];
pub fn evaluate_board(board: &Board) -> i32 { pub fn evaluate_board(board: &Board) -> i32 {
let mut mg_score = 0_i32; let mut mg_score = 0_i32;
let mut eg_score = 0_i32; let mut eg_score = 0_i32;
let mut phase = 0_i32; let mut phase = 0_i32;
// --- WHITE PIECES --- // We use a macro to force loop unrolling.
// Iterating Pawn (0) to Queen (4) for Material + Phase + PSQT // This enables the compiler to use constant offsets for PSQT access
for pt in 0..5 { // instead of calculating addresses at runtime based on a loop variable.
let mut pieces = board.pieces[pt][Color::White as usize]; macro_rules! score_piece {
let count = pieces.count_ones() as i32; ($pt:expr, $phase_weight:expr) => {
// --- WHITE ---
let mut pieces = board.pieces[$pt][Color::White as usize];
if pieces > 0 {
// Phase calculation uses count_ones (POPPCNT) which is very fast
phase += (pieces.count_ones() as i32) * $phase_weight;
mg_score += count * MATERIAL_WEIGHTS[pt]; while pieces > 0 {
eg_score += count * MATERIAL_WEIGHTS[pt]; let sq = pieces.trailing_zeros() as usize;
phase += count * PHASE_WEIGHTS[pt]; pieces &= pieces - 1; // Clear LS1B
while pieces > 0 { // Material is already baked into PSQT, so we just add the table value
let sq = pieces.trailing_zeros() as usize; // Since $pt is a const literal here, this compiles to a direct memory access
pieces &= pieces - 1; // Clear LS1B mg_score += PSQT[$pt][Color::White as usize][0][sq];
eg_score += PSQT[$pt][Color::White as usize][1][sq];
}
}
// Access: [Piece][Color][Phase (0=MG, 1=EG)][Square] // --- BLACK ---
mg_score += PSQT[pt][Color::White as usize][0][sq]; let mut pieces = board.pieces[$pt][Color::Black as usize];
eg_score += PSQT[pt][Color::White as usize][1][sq]; if pieces > 0 {
} phase += (pieces.count_ones() as i32) * $phase_weight;
while pieces > 0 {
let sq = pieces.trailing_zeros() as usize;
pieces &= pieces - 1;
mg_score -= PSQT[$pt][Color::Black as usize][0][sq];
eg_score -= PSQT[$pt][Color::Black as usize][1][sq];
}
}
};
} }
// King (Index 5) - No Material/Phase weight, only PSQT // Explicitly unrolled execution order
let mut white_king = board.pieces[5][Color::White as usize]; // Pawn (0), Weight 0
if white_king > 0 { score_piece!(0, 0);
let sq = white_king.trailing_zeros() as usize; // Knight (1), Weight 1
mg_score += PSQT[5][Color::White as usize][0][sq]; score_piece!(1, 1);
eg_score += PSQT[5][Color::White as usize][1][sq]; // Bishop (2), Weight 1
} score_piece!(2, 1);
// Rook (3), Weight 2
score_piece!(3, 2);
// Queen (4), Weight 4
score_piece!(4, 4);
// King (5), Weight 0 (Phase doesn't change)
score_piece!(5, 0);
// --- BLACK PIECES --- // Tapered Evaluation
// Iterating Pawn (0) to Queen (4) let phase = phase.min(24);
for pt in 0..5 {
let mut pieces = board.pieces[pt][Color::Black as usize];
let count = pieces.count_ones() as i32;
mg_score -= count * MATERIAL_WEIGHTS[pt];
eg_score -= count * MATERIAL_WEIGHTS[pt];
phase += count * PHASE_WEIGHTS[pt];
while pieces > 0 {
let sq = pieces.trailing_zeros() as usize;
pieces &= pieces - 1;
mg_score -= PSQT[pt][Color::Black as usize][0][sq];
eg_score -= PSQT[pt][Color::Black as usize][1][sq];
}
}
// King (Index 5) for Black
let mut black_king = board.pieces[5][Color::Black as usize];
if black_king > 0 {
let sq = black_king.trailing_zeros() as usize;
mg_score -= PSQT[5][Color::Black as usize][0][sq];
eg_score -= PSQT[5][Color::Black as usize][1][sq];
}
// Tapered Evaluation Interpolation
let phase = phase.min(24); // Clamp to 24 max
let mg_phase = phase; let mg_phase = phase;
let eg_phase = 24 - phase; let eg_phase = 24 - phase;

View file

@ -1,33 +1,47 @@
// --- Material Values (PeSTO Standard) ---
const MG_PAWN_VAL: i32 = 82;
const EG_PAWN_VAL: i32 = 94;
const MG_KNIGHT_VAL: i32 = 337;
const EG_KNIGHT_VAL: i32 = 281;
const MG_BISHOP_VAL: i32 = 365;
const EG_BISHOP_VAL: i32 = 297;
const MG_ROOK_VAL: i32 = 477;
const EG_ROOK_VAL: i32 = 512;
const MG_QUEEN_VAL: i32 = 1025;
const EG_QUEEN_VAL: i32 = 936;
const MG_KING_VAL: i32 = 0; // King usually has no material value in eval summation (captured = game over)
const EG_KING_VAL: i32 = 0;
pub const MG_PAWN_TABLE: [i32; 64] = [ pub const MG_PAWN_TABLE: [i32; 64] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98, 134, 61, 95, 68, 126, 34, -11, 98, 134, 61, 95, 68, 126, 34, -11,
-6, 7, 26, 31, 65, 56, 25, -20, -6, 7, 26, 31, 65, 56, 25, -20,
-14, 13, 6, 21, 23, 12, 17, -23, -14, 13, 6, 21, 23, 12, 17, -23,
-27, -2, -5, 12, 17, 6, 10, -25, -27, -2, -5, 12, 17, 6, 10, -25,
-26, -4, -4, -10, 3, 3, 33, -12, -26, -4, -4, -10, 3, 3, 33, -12,
-35, -1, -20, -23, -15, 24, 38, -22, -35, -1, -20, -23, -15, 24, 38, -22,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]; ];
pub const EG_PAWN_TABLE: [i32; 64] = [ pub const EG_PAWN_TABLE: [i32; 64] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178, 173, 158, 134, 147, 132, 165, 187, 178, 173, 158, 134, 147, 132, 165, 187,
94, 100, 85, 67, 56, 53, 82, 84, 94, 100, 85, 67, 56, 53, 82, 84,
32, 24, 13, 5, -2, 4, 17, 17, 32, 24, 13, 5, -2, 4, 17, 17,
13, 9, -3, -7, -7, -8, 3, -1, 13, 9, -3, -7, -7, -8, 3, -1,
4, 7, -6, 1, 0, -5, -1, -8, 4, 7, -6, 1, 0, -5, -1, -8,
13, 8, 8, 10, 13, 0, 2, -7, 13, 8, 8, 10, 13, 0, 2, -7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]; ];
pub const MG_KNIGHT_TABLE: [i32; 64] = [ pub const MG_KNIGHT_TABLE: [i32; 64] = [
-167, -89, -34, -49, 61, -97, -15, -107, -167, -89, -34, -49, 61, -97, -15, -107,
-73, -41, 72, 36, 23, 62, 7, -17, -73, -41, 72, 36, 23, 62, 7, -17,
-47, 60, 37, 65, 84, 129, 73, 44, -47, 60, 37, 65, 84, 129, 73, 44,
-9, 17, 19, 53, 37, 69, 18, 22, -9, 17, 19, 53, 37, 69, 18, 22,
-13, 4, 16, 13, 28, 19, 21, -8, -13, 4, 16, 13, 28, 19, 21, -8,
-23, -9, 12, 10, 19, 17, 25, -16, -23, -9, 12, 10, 19, 17, 25, -16,
-29, -53, -12, -3, -1, 18, -14, -19, -29, -53, -12, -3, -1, 18, -14, -19,
-105, -21, -58, -33, -17, -28, -19, -23, -105, -21, -58, -33, -17, -28, -19, -23,
]; ];
@ -46,28 +60,28 @@ pub const MG_BISHOP_TABLE: [i32; 64] = [
-29, 4, -82, -37, -25, -42, 7, -8, -29, 4, -82, -37, -25, -42, 7, -8,
-26, 16, -18, -13, 30, 59, 18, -47, -26, 16, -18, -13, 30, 59, 18, -47,
-16, 37, 43, 40, 35, 50, 37, -2, -16, 37, 43, 40, 35, 50, 37, -2,
-4, 5, 19, 50, 37, 37, 7, -2, -4, 5, 19, 50, 37, 37, 7, -2,
-6, 13, 13, 26, 34, 12, 10, 4, -6, 13, 13, 26, 34, 12, 10, 4,
0, 15, 15, 15, 14, 27, 18, 10, 0, 15, 15, 15, 14, 27, 18, 10,
4, 15, 16, 0, 7, 21, 33, 1, 4, 15, 16, 0, 7, 21, 33, 1,
-33, -3, -14, -21, -13, -12, -39, -21, -33, -3, -14, -21, -13, -12, -39, -21,
]; ];
pub const EG_BISHOP_TABLE: [i32; 64] = [ pub const EG_BISHOP_TABLE: [i32; 64] = [
-14, -21, -11, -8, -7, -9, -17, -24, -14, -21, -11, -8, -7, -9, -17, -24,
-8, -4, 7, -12, -3, -13, -4, -14, -8, -4, 7, -12, -3, -13, -4, -14,
2, -8, 0, -1, -2, 6, 0, 4, 2, -8, 0, -1, -2, 6, 0, 4,
-3, 9, 12, 9, 14, 10, 3, 2, -3, 9, 12, 9, 14, 10, 3, 2,
-6, 3, 13, 19, 7, 10, -3, -9, -6, 3, 13, 19, 7, 10, -3, -9,
-12, -3, 8, 10, 13, 3, -7, -15, -12, -3, 8, 10, 13, 3, -7, -15,
-14, -18, -7, -1, 4, -9, -15, -27, -14, -18, -7, -1, 4, -9, -15, -27,
-23, -9, -23, -5, -9, -16, -5, -17, -23, -9, -23, -5, -9, -16, -5, -17,
]; ];
pub const MG_ROOK_TABLE: [i32; 64] = [ pub const MG_ROOK_TABLE: [i32; 64] = [
32, 42, 32, 51, 63, 9, 31, 43, 32, 42, 32, 51, 63, 9, 31, 43,
27, 32, 58, 62, 80, 67, 26, 44, 27, 32, 58, 62, 80, 67, 26, 44,
-5, 19, 26, 36, 17, 45, 61, 16, -5, 19, 26, 36, 17, 45, 61, 16,
-24, -11, 7, 26, 24, 35, -8, -20, -24, -11, 7, 26, 24, 35, -8, -20,
-36, -26, -12, -1, 9, -7, 6, -23, -36, -26, -12, -1, 9, -7, 6, -23,
-45, -25, -16, -17, 3, 0, -5, -33, -45, -25, -16, -17, 3, 0, -5, -33,
@ -78,9 +92,9 @@ pub const MG_ROOK_TABLE: [i32; 64] = [
pub const EG_ROOK_TABLE: [i32; 64] = [ pub const EG_ROOK_TABLE: [i32; 64] = [
13, 10, 18, 15, 12, 12, 8, 5, 13, 10, 18, 15, 12, 12, 8, 5,
11, 13, 13, 11, -3, 3, 8, 3, 11, 13, 13, 11, -3, 3, 8, 3,
7, 7, 7, 5, 4, -3, -5, -3, 7, 7, 7, 5, 4, -3, -5, -3,
4, 3, 13, 1, 2, 1, -1, 2, 4, 3, 13, 1, 2, 1, -1, 2,
3, 5, 8, 4, -5, -6, -8, -11, 3, 5, 8, 4, -5, -6, -8, -11,
-4, 0, -5, -1, -7, -12, -8, -16, -4, 0, -5, -1, -7, -12, -8, -16,
-6, -6, 0, 2, -9, -9, -11, -3, -6, -6, 0, 2, -9, -9, -11, -3,
-9, 2, 3, -1, -5, -13, 4, -20, -9, 2, 3, -1, -5, -13, 4, -20,
@ -91,17 +105,17 @@ pub const MG_QUEEN_TABLE: [i32; 64] = [
-24, -39, -5, 1, -16, 57, 28, 54, -24, -39, -5, 1, -16, 57, 28, 54,
-13, -17, 7, 8, 29, 56, 47, 57, -13, -17, 7, 8, 29, 56, 47, 57,
-27, -27, -16, -16, -1, 17, -2, 1, -27, -27, -16, -16, -1, 17, -2, 1,
-9, -26, -9, -10, -2, -4, 3, -3, -9, -26, -9, -10, -2, -4, 3, -3,
-14, 2, -11, -2, -5, 2, 14, 5, -14, 2, -11, -2, -5, 2, 14, 5,
-35, -8, 11, 2, 8, 15, -3, 1, -35, -8, 11, 2, 8, 15, -3, 1,
-1, -18, -9, 10, -15, -25, -31, -50, -1, -18, -9, 10, -15, -25, -31, -50,
]; ];
pub const EG_QUEEN_TABLE: [i32; 64] = [ pub const EG_QUEEN_TABLE: [i32; 64] = [
-9, 22, 22, 27, 27, 19, 10, 20, -9, 22, 22, 27, 27, 19, 10, 20,
-17, 20, 32, 41, 58, 25, 30, 0, -17, 20, 32, 41, 58, 25, 30, 0,
-20, 6, 9, 49, 47, 35, 19, 9, -20, 6, 9, 49, 47, 35, 19, 9,
3, 22, 24, 45, 57, 40, 57, 36, 3, 22, 24, 45, 57, 40, 57, 36,
-18, 28, 19, 47, 31, 34, 39, 23, -18, 28, 19, 47, 31, 34, 39, 23,
-16, -27, 15, 6, 9, 17, 10, 5, -16, -27, 15, 6, 9, 17, 10, 5,
-22, -23, -30, -16, -16, -23, -36, -32, -22, -23, -30, -16, -16, -23, -36, -32,
@ -110,69 +124,69 @@ pub const EG_QUEEN_TABLE: [i32; 64] = [
pub const MG_KING_TABLE: [i32; 64] = [ pub const MG_KING_TABLE: [i32; 64] = [
-65, 23, 16, -15, -56, -34, 2, 13, -65, 23, 16, -15, -56, -34, 2, 13,
29, -1, -20, -7, -8, -4, -38, -29, 29, -1, -20, -7, -8, -4, -38, -29,
-9, 24, 2, -16, -20, 6, 22, -22, -9, 24, 2, -16, -20, 6, 22, -22,
-17, -20, -12, -27, -30, -25, -14, -36, -17, -20, -12, -27, -30, -25, -14, -36,
-49, -1, -27, -39, -46, -44, -33, -51, -49, -1, -27, -39, -46, -44, -33, -51,
-14, -14, -22, -46, -44, -30, -15, -27, -14, -14, -22, -46, -44, -30, -15, -27,
1, 7, -8, -64, -43, -16, 9, 8, 1, 7, -8, -64, -43, -16, 9, 8,
-15, 36, 12, -54, 8, -28, 24, 14, -15, 36, 12, -54, 8, -28, 24, 14,
]; ];
pub const EG_KING_TABLE: [i32; 64] = [ pub const EG_KING_TABLE: [i32; 64] = [
-74, -35, -18, -18, -11, 15, 4, -17, -74, -35, -18, -18, -11, 15, 4, -17,
-12, 17, 14, 17, 17, 38, 23, 11, -12, 17, 14, 17, 17, 38, 23, 11,
10, 17, 23, 15, 20, 45, 44, 13, 10, 17, 23, 15, 20, 45, 44, 13,
-8, 22, 24, 27, 26, 33, 26, 3, -8, 22, 24, 27, 26, 33, 26, 3,
-18, -4, 21, 24, 27, 23, 9, -11, -18, -4, 21, 24, 27, 23, 9, -11,
-19, -3, 11, 21, 23, 16, 7, -9, -19, -3, 11, 21, 23, 16, 7, -9,
-27, -11, 4, 13, 14, 4, -5, -17, -27, -11, 4, 13, 14, 4, -5, -17,
-53, -34, -21, -11, -28, -14, -24, -43 -53, -34, -21, -11, -28, -14, -24, -43
]; ];
/// Helper function to mirror the table vertically at compile time /// Combines logic: mirrors (if needed) AND adds material value.
const fn mirror_psqt(table: [i32; 64]) -> [i32; 64] { /// This happens at compile-time, so no runtime cost.
let mut mirrored = [0; 64]; const fn prepare_table(table: [i32; 64], material: i32, mirror: bool) -> [i32; 64] {
let mut out = [0; 64];
let mut i = 0; let mut i = 0;
while i < 64 { while i < 64 {
// XOR 56 maps rank 1 to 8, 2 to 7 etc, keeping files intact let idx = if mirror { i ^ 56 } else { i };
mirrored[i] = table[i ^ 56]; out[i] = table[idx] + material;
i += 1; i += 1;
} }
mirrored out
} }
// Type signature: [Piece (6)] -> [Color (2)] -> [Phase (2)] -> [Square (64)] // Type signature: [Piece (6)] -> [Color (2)] -> [Phase (2)] -> [Square (64)]
pub const PSQT: [[[[i32; 64]; 2]; 2]; 6] = [ pub const PSQT: [[[[i32; 64]; 2]; 2]; 6] = [
// 1. PAWN // 1. PAWN
[ [
[mirror_psqt(MG_PAWN_TABLE), mirror_psqt(EG_PAWN_TABLE)], // White (Muss geflippt werden!) [prepare_table(MG_PAWN_TABLE, MG_PAWN_VAL, true), prepare_table(EG_PAWN_TABLE, EG_PAWN_VAL, true)], // White (Mirrored + Mat)
[MG_PAWN_TABLE, EG_PAWN_TABLE] // Black (Ist bereits korrekt für schwarze Indices) [prepare_table(MG_PAWN_TABLE, MG_PAWN_VAL, false), prepare_table(EG_PAWN_TABLE, EG_PAWN_VAL, false)] // Black (Normal + Mat)
], ],
// 2. KNIGHT // 2. KNIGHT
[ [
[mirror_psqt(MG_KNIGHT_TABLE), mirror_psqt(EG_KNIGHT_TABLE)], // White [prepare_table(MG_KNIGHT_TABLE, MG_KNIGHT_VAL, true), prepare_table(EG_KNIGHT_TABLE, EG_KNIGHT_VAL, true)],
[MG_KNIGHT_TABLE, EG_KNIGHT_TABLE] // Black [prepare_table(MG_KNIGHT_TABLE, MG_KNIGHT_VAL, false), prepare_table(EG_KNIGHT_TABLE, EG_KNIGHT_VAL, false)]
], ],
// 3. BISHOP // 3. BISHOP
[ [
[mirror_psqt(MG_BISHOP_TABLE), mirror_psqt(EG_BISHOP_TABLE)], // White [prepare_table(MG_BISHOP_TABLE, MG_BISHOP_VAL, true), prepare_table(EG_BISHOP_TABLE, EG_BISHOP_VAL, true)],
[MG_BISHOP_TABLE, EG_BISHOP_TABLE] // Black [prepare_table(MG_BISHOP_TABLE, MG_BISHOP_VAL, false), prepare_table(EG_BISHOP_TABLE, EG_BISHOP_VAL, false)]
], ],
// 4. ROOK // 4. ROOK
[ [
[mirror_psqt(MG_ROOK_TABLE), mirror_psqt(EG_ROOK_TABLE)], // White [prepare_table(MG_ROOK_TABLE, MG_ROOK_VAL, true), prepare_table(EG_ROOK_TABLE, EG_ROOK_VAL, true)],
[MG_ROOK_TABLE, EG_ROOK_TABLE] // Black [prepare_table(MG_ROOK_TABLE, MG_ROOK_VAL, false), prepare_table(EG_ROOK_TABLE, EG_ROOK_VAL, false)]
], ],
// 5. QUEEN // 5. QUEEN
[ [
[mirror_psqt(MG_QUEEN_TABLE), mirror_psqt(EG_QUEEN_TABLE)], // White [prepare_table(MG_QUEEN_TABLE, MG_QUEEN_VAL, true), prepare_table(EG_QUEEN_TABLE, EG_QUEEN_VAL, true)],
[MG_QUEEN_TABLE, EG_QUEEN_TABLE] // Black [prepare_table(MG_QUEEN_TABLE, MG_QUEEN_VAL, false), prepare_table(EG_QUEEN_TABLE, EG_QUEEN_VAL, false)]
], ],
// 6. KING // 6. KING
[ [
[mirror_psqt(MG_KING_TABLE), mirror_psqt(EG_KING_TABLE)], // White [prepare_table(MG_KING_TABLE, MG_KING_VAL, true), prepare_table(EG_KING_TABLE, EG_KING_VAL, true)],
[MG_KING_TABLE, EG_KING_TABLE] // Black [prepare_table(MG_KING_TABLE, MG_KING_VAL, false), prepare_table(EG_KING_TABLE, EG_KING_VAL, false)]
], ],
]; ];