From 6211e5be8963411a88196f0e4778647c55d13616 Mon Sep 17 00:00:00 2001 From: stefiosif Date: Fri, 28 Mar 2025 23:35:02 +0200 Subject: [PATCH] Add history maluses --- src/board/game.rs | 14 ++++++++++++++ src/search/mod.rs | 2 +- src/search/move_ordering.rs | 4 ++-- src/search/negamax.rs | 13 ++++++------- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/board/game.rs b/src/board/game.rs index a57c727..e28f091 100644 --- a/src/board/game.rs +++ b/src/board/game.rs @@ -320,6 +320,20 @@ impl Game { .step_by(2) .any(|mp| mp.zobrist_hash.expect("State without hash.") == self.hash) } + + pub fn update_history(&mut self, color: Color, mv: Move, depth: u8, punish: bool) { + const MAX_HISTORY: i32 = 8192; + let bonus = if punish { + -(depth as i32 * depth as i32) + } else { + depth as i32 * depth as i32 + }; + + let clamped_bonus = bonus.clamp(-MAX_HISTORY, MAX_HISTORY); + let history_value = self.history_heuristic[color][mv.src()][mv.dst()]; + self.history_heuristic[color][mv.src()][mv.dst()] += + clamped_bonus - history_value * clamped_bonus.abs() / MAX_HISTORY; + } } #[derive(Default, Debug, Clone, PartialEq, Eq)] diff --git a/src/search/mod.rs b/src/search/mod.rs index 95c2884..00f1ef8 100644 --- a/src/search/mod.rs +++ b/src/search/mod.rs @@ -12,7 +12,7 @@ pub const INC: u128 = 80; pub const HARD_LIMIT_DIVISION: u128 = 3; pub const SOFT_LIMIT_DIVISION: u128 = 20; pub const TT_SIZE_IN_MB: usize = 64; -pub const MAX_HISTORY: i16 = 8192; +pub const MAX_HISTORY: i32 = 8192; pub const ASPIRATION_WINDOW_INITIAL: i16 = 20; pub const ASPIRATION_WINDOW_DEPTH_THRESHOLD: u8 = 4; pub const ASPIRATION_WINDOW_EXPANSION: i16 = 2; diff --git a/src/search/move_ordering.rs b/src/search/move_ordering.rs index 2361f71..9eb7daa 100644 --- a/src/search/move_ordering.rs +++ b/src/search/move_ordering.rs @@ -11,9 +11,9 @@ pub fn score_move( mv: Move, killer_move: Option, color: Color, - history_heuristic: &[[[i16; 64]; 64]; 2], + history_heuristic: &[[[i32; 64]; 64]; 2], tt_move: Option, -) -> i16 { +) -> i32 { if Some(mv) == tt_move { return -100; } diff --git a/src/search/negamax.rs b/src/search/negamax.rs index 30fcda2..477b866 100644 --- a/src/search/negamax.rs +++ b/src/search/negamax.rs @@ -109,6 +109,7 @@ pub fn negamax( let mut legal_moves = 0; let mut best_move = entry.and_then(|entry| entry.mv); let mut best_score = MIN_SCORE; + let mut quiets_tried: Vec = Vec::new(); for mv in moves { game.make_move(&mv); @@ -166,16 +167,14 @@ pub fn negamax( if score >= beta { if !mv.is_capture() { game.killer[plies as usize] = Some(mv); - - let current_score = game.history_heuristic[color][mv.src()][mv.dst()]; - let bonus = (depth * depth) as i16; - let clamped_bonus = bonus.clamp(-MAX_HISTORY, MAX_HISTORY); - game.history_heuristic[color][mv.src()][mv.dst()] += - clamped_bonus - current_score * clamped_bonus.abs() / MAX_HISTORY; + game.update_history(color, mv, depth, false); + for q in quiets_tried { + game.update_history(color, q, depth, true); + } } break; } else if !mv.is_capture() { - game.history_heuristic[color][mv.src()][mv.dst()] -= 1; + quiets_tried.push(mv); } }