Add TT lookups in quiescence, move TT inside Game, remove redundant occupancy and time limit functions

This commit is contained in:
stefiosif
2025-01-18 13:01:06 +02:00
parent e9729cf95d
commit 824f8a37b5
9 changed files with 70 additions and 119 deletions

View File

@@ -1,5 +1,6 @@
use crate::{board::zobrist::ZobristHash, movegen::r#move::Move};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TranspositionTable {
positions: Vec<Option<TTEntry>>,
size: u64,
@@ -24,60 +25,29 @@ impl TranspositionTable {
pub fn insert(&mut self, tt_entry: TTEntry) {
let index = (tt_entry.hash.hash % self.size) as usize;
if let Some(stored) = &self.positions[index] {
if tt_entry.depth > stored.depth || matches!(tt_entry.bound, Bound::Exact) {
self.positions[index] = Some(tt_entry);
}
} else {
self.positions[index] = Some(tt_entry);
}
self.positions[index] = Some(tt_entry);
}
}
#[derive(Clone)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TTEntry {
pub hash: ZobristHash,
pub depth: u8,
pub score: i32,
pub mv: Option<Move>,
pub bound: Bound,
}
impl TTEntry {
pub const fn new(
hash: ZobristHash,
depth: u8,
score: i32,
mv: Option<Move>,
bound: Bound,
) -> Self {
Self {
hash,
depth,
score,
mv,
bound,
}
pub const fn new(hash: ZobristHash, mv: Option<Move>) -> Self {
Self { hash, mv }
}
}
#[derive(Clone)]
pub enum Bound {
Exact,
Alpha,
Beta,
}
#[cfg(test)]
mod tests {
use crate::{
board::fen::from_fen,
evaluation::{MAX_SCORE, MIN_SCORE},
movegen::attack_generator::init_attacks,
search::{
negamax::negamax, time::TimeInfo, transposition_table::TranspositionTable, MAX_TT_SIZE,
},
search::{negamax::negamax, time::TimeInfo},
};
const FEN: &str = "1r2k2r/2P1pq1p/2npb3/1p3ppP/p3P3/P2B1Q2/1P1PNPP1/R3K2R w KQk g6 0 1";
@@ -88,10 +58,9 @@ mod tests {
fn test_transposition_table() -> anyhow::Result<()> {
init_attacks();
let mut game = from_fen(FEN).unwrap();
let mut tt = TranspositionTable::new(MAX_TT_SIZE);
let time_info = TimeInfo::new(std::time::Instant::now(), 30000);
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info, &mut tt)
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
.expect("Expected a search result")
.best_move
.expect("Expected a move");
@@ -99,12 +68,12 @@ mod tests {
dbg!(time_info.time.elapsed());
let will_be_hash = from_fen(FEN_POSSIBLE).unwrap().hash;
let tt_entry = tt.lookup(will_be_hash);
let tt_entry = game.tt.lookup(will_be_hash);
assert!(tt_entry.is_some());
let wont_be_hash = from_fen(FEN_IMPOSSIBLE).unwrap().hash;
let tt_entry = tt.lookup(wont_be_hash);
let tt_entry = game.tt.lookup(wont_be_hash);
assert!(tt_entry.is_none());