Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug in psqt #34

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions engine/src/psqt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,16 @@ impl Psqt {

/// Helper to initialize the tables
/// See <https://www.chessprogramming.org/PeSTO%27s_Evaluation_Function>
///
/// Here white is 0 and black is 1, see [`Side`]. The PSQT tables are from white's perspective, so we need to flip
/// the board for white and not for black.
fn initialize_tables(&mut self) {
for (p, pc) in (0..6).zip((0..12).step_by(2)) {
for sq in 0..64 {
self.mg_table[pc][sq] = MG_VALUE[p] + MG_PESTO_TABLE[p][sq];
self.eg_table[pc][sq] = EG_VALUE[p] + EG_PESTO_TABLE[p][sq];
self.mg_table[pc + 1][sq] = MG_VALUE[p] + MG_PESTO_TABLE[p][FLIP(sq)];
self.eg_table[pc + 1][sq] = EG_VALUE[p] + EG_PESTO_TABLE[p][FLIP(sq)];
self.mg_table[pc][sq] = MG_VALUE[p] + MG_PESTO_TABLE[p][FLIP(sq)];
self.eg_table[pc][sq] = EG_VALUE[p] + EG_PESTO_TABLE[p][FLIP(sq)];
self.mg_table[pc + 1][sq] = MG_VALUE[p] + MG_PESTO_TABLE[p][sq];
self.eg_table[pc + 1][sq] = EG_VALUE[p] + EG_PESTO_TABLE[p][sq];
}
}
}
Expand Down Expand Up @@ -300,7 +303,7 @@ impl Psqt {
mod tests {
use chess::board::Board;

use crate::psqt::Psqt;
use crate::{psqt::Psqt, score::Score};

#[test]
fn default_position_is_equal() {
Expand All @@ -309,4 +312,22 @@ mod tests {
let score = psqt.evaluate(&board);
assert_eq!(score, super::Score::new(0));
}

#[test]
fn white_ahead() {
let fens = [
"4k3/8/8/8/8/8/PPPPPPPP/4K3 w - - 0 1",
"4k3/8/8/8/8/8/NNNNNNNN/4K3 w - - 0 1",
"4k3/8/8/8/8/8/BBBBBBBB/4K3 w - - 0 1",
"4k3/8/8/8/8/8/RRRR1RRR/4K3 w - - 0 1",
"4k3/8/8/8/8/8/QQQQ1QQQ/4K3 w - - 0 1",
"4k3/1QQR1RQQ/1QQQKQQ1/1BBNN3/8/8/8/8 w - - 0 1",
];

for fen in fens {
let pos = Board::from_fen(fen).unwrap();
let eval = Psqt::new().evaluate(&pos);
assert!(eval > Score::new(0));
}
}
}
2 changes: 1 addition & 1 deletion engine/src/score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use uci_parser::UciScore;

pub(crate) type ScoreType = i16;
/// Represents a score in centipawns.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Score(pub ScoreType);

impl Score {
Expand Down