Skip to content

Commit a6950c0

Browse files
Merge pull request #104 from DeveloperPaul123/fix/history-bonus-calc
bench: 2517656
2 parents 179f223 + f4e79fa commit a6950c0

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

engine/src/history_table.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ pub struct HistoryTable {
1010
table: [[[LargeScoreType; NumberOf::SQUARES]; NumberOf::PIECE_TYPES]; NumberOf::SIDES],
1111
}
1212

13+
/// Safe calculation of the bonus applied to quiet moves that are inserted into the history table.
14+
/// This uses `wrappinag_mul` and `wrapping_sub` to safely calculate the value.
15+
///
16+
/// # Arguments
17+
///
18+
/// - depth: The current depth
19+
///
20+
/// # Returns
21+
///
22+
/// The calculated history score.
23+
pub(crate) fn calculate_bonus_for_depth(depth: i16) -> i16 {
24+
depth
25+
.saturating_mul(Score::HISTORY_MULT)
26+
.saturating_sub(Score::HISTORY_OFFSET)
27+
}
28+
1329
impl HistoryTable {
1430
pub(crate) fn new() -> Self {
1531
let table =
@@ -63,7 +79,9 @@ impl Default for HistoryTable {
6379

6480
#[cfg(test)]
6581
mod tests {
66-
use super::HistoryTable;
82+
use crate::defs::MAX_DEPTH;
83+
84+
use super::{HistoryTable, calculate_bonus_for_depth};
6785
use chess::{definitions::Squares, pieces::Piece, side::Side};
6886

6987
#[test]
@@ -94,4 +112,13 @@ mod tests {
94112
history_table.update(side, piece, square, score);
95113
assert_eq!(history_table.get(side, piece, square), score + score);
96114
}
115+
116+
#[test]
117+
fn calculate_bonus_for_any_depth() {
118+
for depth in 1..MAX_DEPTH {
119+
let bonus = calculate_bonus_for_depth(depth as i16);
120+
assert!(bonus > 0);
121+
assert!(bonus as i32 <= i16::MAX.into());
122+
}
123+
}
97124
}

engine/src/score.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ impl Score {
5353
pub const MINIMUM_MATE: Score = Score(Score::MATE.0 - MAX_DEPTH as ScoreType);
5454
pub const INF: Score = Score(ScoreType::MAX as ScoreType);
5555

56-
// Max/min score for history heuristic
57-
// Must be lower then the minimum score for captures in MVV_LVA
56+
/// Multiplier for the history bonus calculation.
57+
pub const HISTORY_MULT: ScoreType = 300;
58+
/// Offset for the history bonus calculation.
59+
pub const HISTORY_OFFSET: ScoreType = 250;
60+
61+
/// Max/min score for history heuristic
62+
/// Must be lower then the minimum score for captures in MVV_LVA
5863
pub const MAX_HISTORY: LargeScoreType = 16_384;
5964

6065
pub fn new(score: ScoreType) -> Score {

engine/src/search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::{
3333
aspiration_window::AspirationWindow,
3434
defs::MAX_DEPTH,
3535
evaluation::ByteKnightEvaluation,
36-
history_table::HistoryTable,
36+
history_table::{self, HistoryTable},
3737
inplace_incremental_sort::InplaceIncrementalSort,
3838
lmr,
3939
move_order::MoveOrder,
@@ -478,7 +478,7 @@ impl<'a> Search<'a> {
478478
// update history table for quiets
479479
if mv.is_quiet() {
480480
// calculate history bonus
481-
let bonus = depth.wrapping_mul(300) - 250;
481+
let bonus = history_table::calculate_bonus_for_depth(depth);
482482
self.history_table.update(
483483
board.side_to_move(),
484484
mv.piece(),

0 commit comments

Comments
 (0)