integrated material eval into psqt
This commit is contained in:
parent
7b84e8a0ab
commit
b69323665c
4 changed files with 127 additions and 120 deletions
Binary file not shown.
|
|
@ -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();
|
||||||
|
|
|
||||||
|
|
@ -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 ---
|
||||||
mg_score += count * MATERIAL_WEIGHTS[pt];
|
let mut pieces = board.pieces[$pt][Color::White as usize];
|
||||||
eg_score += count * MATERIAL_WEIGHTS[pt];
|
if pieces > 0 {
|
||||||
phase += count * PHASE_WEIGHTS[pt];
|
// Phase calculation uses count_ones (POPPCNT) which is very fast
|
||||||
|
phase += (pieces.count_ones() as i32) * $phase_weight;
|
||||||
|
|
||||||
while pieces > 0 {
|
while pieces > 0 {
|
||||||
let sq = pieces.trailing_zeros() as usize;
|
let sq = pieces.trailing_zeros() as usize;
|
||||||
pieces &= pieces - 1; // Clear LS1B
|
pieces &= pieces - 1; // Clear LS1B
|
||||||
|
|
||||||
// Access: [Piece][Color][Phase (0=MG, 1=EG)][Square]
|
// Material is already baked into PSQT, so we just add the table value
|
||||||
mg_score += PSQT[pt][Color::White as usize][0][sq];
|
// Since $pt is a const literal here, this compiles to a direct memory access
|
||||||
eg_score += PSQT[pt][Color::White as usize][1][sq];
|
mg_score += PSQT[$pt][Color::White as usize][0][sq];
|
||||||
|
eg_score += PSQT[$pt][Color::White as usize][1][sq];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// King (Index 5) - No Material/Phase weight, only PSQT
|
// --- BLACK ---
|
||||||
let mut white_king = board.pieces[5][Color::White as usize];
|
let mut pieces = board.pieces[$pt][Color::Black as usize];
|
||||||
if white_king > 0 {
|
if pieces > 0 {
|
||||||
let sq = white_king.trailing_zeros() as usize;
|
phase += (pieces.count_ones() as i32) * $phase_weight;
|
||||||
mg_score += PSQT[5][Color::White as usize][0][sq];
|
|
||||||
eg_score += PSQT[5][Color::White as usize][1][sq];
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- BLACK PIECES ---
|
|
||||||
// Iterating Pawn (0) to Queen (4)
|
|
||||||
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 {
|
while pieces > 0 {
|
||||||
let sq = pieces.trailing_zeros() as usize;
|
let sq = pieces.trailing_zeros() as usize;
|
||||||
pieces &= pieces - 1;
|
pieces &= pieces - 1;
|
||||||
|
|
||||||
mg_score -= PSQT[pt][Color::Black as usize][0][sq];
|
mg_score -= PSQT[$pt][Color::Black as usize][0][sq];
|
||||||
eg_score -= PSQT[pt][Color::Black as usize][1][sq];
|
eg_score -= PSQT[$pt][Color::Black as usize][1][sq];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// King (Index 5) for Black
|
// Explicitly unrolled execution order
|
||||||
let mut black_king = board.pieces[5][Color::Black as usize];
|
// Pawn (0), Weight 0
|
||||||
if black_king > 0 {
|
score_piece!(0, 0);
|
||||||
let sq = black_king.trailing_zeros() as usize;
|
// Knight (1), Weight 1
|
||||||
mg_score -= PSQT[5][Color::Black as usize][0][sq];
|
score_piece!(1, 1);
|
||||||
eg_score -= PSQT[5][Color::Black 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);
|
||||||
|
|
||||||
// Tapered Evaluation Interpolation
|
// Tapered Evaluation
|
||||||
let phase = phase.min(24); // Clamp to 24 max
|
let phase = phase.min(24);
|
||||||
let mg_phase = phase;
|
let mg_phase = phase;
|
||||||
let eg_phase = 24 - phase;
|
let eg_phase = 24 - phase;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,17 @@
|
||||||
|
// --- 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,
|
||||||
|
|
@ -130,49 +144,49 @@ pub const EG_KING_TABLE: [i32; 64] = [
|
||||||
-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)]
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue