Refactor time info, update zobrist to tuple struct, naming and clippy

This commit is contained in:
stefiosif
2025-01-26 20:16:01 +02:00
parent 36aa30ea17
commit 53beda7fe3
11 changed files with 78 additions and 77 deletions

View File

@@ -9,25 +9,26 @@ use crate::{
use super::{
negamax,
time::{hard_limit, TimeInfo},
time::TimeInfo,
};
pub fn iterative_deepening(
game: &mut Game,
max_depth: u8,
time_info: &TimeInfo,
time: &TimeInfo,
) -> anyhow::Result<Option<Move>> {
let mut best_move = None;
for depth in 1..=max_depth {
if hard_limit(time_info.instant, time_info.time, time_info.inc) {
if time.exceed_hard_limit() {
return Ok(best_move);
}
let mut nodes = 0;
let score = negamax::negamax(game, MIN_SCORE, MAX_SCORE, depth, 0, time_info, &mut nodes);
let score = negamax::negamax(game, MIN_SCORE, MAX_SCORE, depth, 0, time, &mut nodes);
if score.is_err() {
if let Err(e) = score {
write_response(&mut io::stdout(), &Response::Info(format!("{e}")))?;
break;
}
@@ -37,9 +38,9 @@ pub fn iterative_deepening(
&mut io::stdout(),
&log_depth_results(
depth,
time_info.instant.elapsed().as_millis() as u64,
time.instant.elapsed().as_millis() as u64,
nodes,
time_info.nps(nodes),
time.nps(nodes),
score?,
best_move,
),

View File

@@ -6,10 +6,9 @@ pub fn score_move(mailbox: &Mailbox, mv: Move, tt_move: Option<Move>) -> i32 {
}
let aggressor = mailbox.piece_at(mv.src).expect("No aggressor found.");
match mailbox.piece_at(mv.dst) {
Some(victim) => aggressor.0.idx() as i32 - (victim.0.idx() * 8) as i32,
None => 100,
}
mailbox.piece_at(mv.dst).map_or(100, |victim| {
aggressor.0.idx() as i32 - (victim.0.idx() * 8) as i32
})
}
#[cfg(test)]
@@ -35,7 +34,6 @@ mod tests {
assert_eq!(moves, vec![pawn_takes_queen, queen_takes_pawn, castle]);
let castle = Move::with_type(Square::E1, Square::C1, MoveType::Castle);
moves.sort_unstable_by_key(|mv| score_move(&game.mailbox, *mv, Some(castle)));
assert_eq!(moves, vec![castle, pawn_takes_queen, queen_takes_pawn]);

View File

@@ -8,7 +8,7 @@ use crate::{
use super::{
move_ordering::score_move,
quiescence::quiescence,
time::{hard_limit, TimeInfo},
time::TimeInfo,
transposition_table::TTEntry,
};
@@ -18,11 +18,11 @@ pub fn negamax(
beta: i32,
depth: u8,
plies: u8,
time_info: &TimeInfo,
time: &TimeInfo,
nodes: &mut u64,
) -> Result<i32> {
if hard_limit(time_info.instant, time_info.time, time_info.inc) {
bail!("Time is up! In Negamax");
if time.exceed_hard_limit() {
bail!("Hard limit exceeded in negamax");
}
if plies != 0 && game.in_repetition() {
@@ -31,7 +31,7 @@ pub fn negamax(
if depth == 0 {
let q_score =
quiescence(game, alpha, beta, time_info, nodes).map_err(|e| anyhow!("{e}"))?;
quiescence(game, alpha, beta, time, nodes).map_err(|e| anyhow!("{e}"))?;
return Ok(q_score);
}
@@ -55,7 +55,7 @@ pub fn negamax(
*nodes += 1;
let score = if legal_moves == 1 {
-negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info, nodes)?
-negamax(game, -beta, -alpha, depth - 1, plies + 1, time, nodes)?
} else {
let mut score = -negamax(
game,
@@ -63,11 +63,11 @@ pub fn negamax(
-alpha,
depth - 1,
plies + 1,
time_info,
time,
nodes,
)?;
if score > alpha && score < beta {
score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info, nodes)?;
score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, time, nodes)?;
}
score
};

View File

@@ -4,18 +4,18 @@ use crate::{board::game::Game, evaluation::pesto::pesto};
use super::{
move_ordering::score_move,
time::{hard_limit, TimeInfo},
time::TimeInfo,
};
pub fn quiescence(
game: &mut Game,
mut alpha: i32,
beta: i32,
time_info: &TimeInfo,
time: &TimeInfo,
total_nodes_searched: &mut u64,
) -> Result<i32> {
if hard_limit(time_info.instant, time_info.time, time_info.inc) {
bail!("Time is up! In Quiescence");
if time.exceed_hard_limit() {
bail!("Hard limit exceeded in quiescence");
}
let stand_pat = pesto().eval(game);
@@ -44,7 +44,7 @@ pub fn quiescence(
}
*total_nodes_searched += 1;
let score = -quiescence(game, -beta, -alpha, time_info, &mut 0)?;
let score = -quiescence(game, -beta, -alpha, time, &mut 0)?;
game.unmake_move();
if score > best_score {

View File

@@ -16,8 +16,8 @@ impl TimeInfo {
pub fn nps(&self, nodes: u64) -> u64 {
((nodes * 1_000_000) as u128).div_ceil(self.instant.elapsed().as_micros()) as u64
}
}
pub fn hard_limit(time_now: Instant, time: u128, inc: u128) -> bool {
time_now.elapsed().as_millis() >= time / HARD_LIMIT_DIVISION + inc / 2
pub fn exceed_hard_limit(&self) -> bool {
self.instant.elapsed().as_millis() >= self.time / HARD_LIMIT_DIVISION + self.inc / 2
}
}

View File

@@ -17,15 +17,15 @@ impl TranspositionTable {
pub fn lookup(&self, zobrist_hash: ZobristHash) -> Option<&TTEntry> {
let entry = self
.positions
.get((zobrist_hash.hash % self.size) as usize)
.get((zobrist_hash.0 % self.size) as usize)
.and_then(|entry| entry.as_ref());
entry
}
pub fn insert(&mut self, tt_entry: TTEntry) {
let index = (tt_entry.hash.hash % self.size) as usize;
self.positions[index] = Some(tt_entry);
let idx = (tt_entry.hash.0 % self.size) as usize;
self.positions[idx] = Some(tt_entry);
}
}