|
| 1 | +// Part of the byte-knight project. |
| 2 | +// Author: Paul Tsouchlos (ptsouchlos) (developer.paul.123@gmail.com) |
| 3 | +// GNU General Public License v3.0 or later |
| 4 | +// https://www.gnu.org/licenses/gpl-3.0-standalone.html |
| 5 | + |
| 6 | +use chess::{moves::Move, pieces::Piece}; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + history::{types::PieceToHistory, util::gravity}, |
| 10 | + score::LargeScoreType, |
| 11 | + utils::{self, boxed_and_zeroed}, |
| 12 | +}; |
| 13 | + |
| 14 | +pub struct ContinuationHistory { |
| 15 | + single_ply_entries: Box<PieceToHistory<PieceToHistory<i32>>>, |
| 16 | +} |
| 17 | + |
| 18 | +impl ContinuationHistory { |
| 19 | + const MAX: i32 = 16384; |
| 20 | + const BONUS_MAX: i32 = Self::MAX / 4; |
| 21 | + |
| 22 | + pub(crate) fn get(&self, prev_mv: Move, prev_pc: Piece, mv: Move, pc: Piece) -> i32 { |
| 23 | + self.single_ply_entries[prev_pc.index()][prev_mv.to().index()][pc.index()][mv.to().index()] |
| 24 | + } |
| 25 | + |
| 26 | + pub(crate) fn update( |
| 27 | + &mut self, |
| 28 | + prev_mv: Move, |
| 29 | + prev_pc: Piece, |
| 30 | + mv: Move, |
| 31 | + pc: Piece, |
| 32 | + bonus: LargeScoreType, |
| 33 | + ) { |
| 34 | + let bonus = bonus.clamp(-Self::BONUS_MAX, Self::BONUS_MAX); |
| 35 | + let entry = &mut self.single_ply_entries[prev_pc.index()][prev_mv.to().index()][pc.index()] |
| 36 | + [mv.to().index()]; |
| 37 | + *entry = gravity(*entry, bonus, Self::MAX); |
| 38 | + } |
| 39 | + |
| 40 | + pub(crate) fn clear(&mut self) { |
| 41 | + self.single_ply_entries = unsafe { boxed_and_zeroed() }; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl Default for ContinuationHistory { |
| 46 | + fn default() -> Self { |
| 47 | + Self { |
| 48 | + single_ply_entries: unsafe { utils::boxed_and_zeroed() }, |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use chess::{ |
| 56 | + moves::{Move, MoveFlag}, |
| 57 | + pieces::Piece, |
| 58 | + square::Square, |
| 59 | + }; |
| 60 | + |
| 61 | + use crate::history::continuation_history::ContinuationHistory; |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn clear_table() { |
| 65 | + // This test is mostly here to validate that we don't overflow the stack when clearing the table. |
| 66 | + let mut cont_hist = ContinuationHistory::default(); |
| 67 | + let prev_mv = Move::new(Square::B2, Square::B4, MoveFlag::DoublePush); |
| 68 | + let mv = Move::new(Square::B4, Square::B5, MoveFlag::Standard); |
| 69 | + let bonus = 300; |
| 70 | + let pc = Piece::Pawn; |
| 71 | + // Update the score |
| 72 | + cont_hist.update(prev_mv, pc, mv, pc, bonus); |
| 73 | + // Ensure it's non-zero |
| 74 | + let score = cont_hist.get(prev_mv, pc, mv, pc); |
| 75 | + assert!(score > 0); |
| 76 | + |
| 77 | + // Clear the table |
| 78 | + cont_hist.clear(); |
| 79 | + // Now the score should be 0 |
| 80 | + let score = cont_hist.get(prev_mv, pc, mv, pc); |
| 81 | + assert!(score == 0); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn score_never_exceeds_max() { |
| 86 | + let mut cont_hist = ContinuationHistory::default(); |
| 87 | + let prev_mv = Move::new(Square::B2, Square::B4, MoveFlag::DoublePush); |
| 88 | + let mv = Move::new(Square::B4, Square::B5, MoveFlag::Standard); |
| 89 | + let pc = Piece::Pawn; |
| 90 | + |
| 91 | + // Hammer the same cell with maximal bonuses to try to force it past MAX - |
| 92 | + // a saturated entry must never be able to sort above KILLER_BONUS in the move picker |
| 93 | + // once combined with quiet history (see move_picker::tests::combined_history_score_never_exceeds_killer_bonus). |
| 94 | + for _ in 0..10_000 { |
| 95 | + cont_hist.update(prev_mv, pc, mv, pc, i32::MAX); |
| 96 | + } |
| 97 | + |
| 98 | + let score = cont_hist.get(prev_mv, pc, mv, pc); |
| 99 | + assert!( |
| 100 | + score <= ContinuationHistory::MAX, |
| 101 | + "saturated continuation history entry ({score}) must not exceed MAX ({})", |
| 102 | + ContinuationHistory::MAX |
| 103 | + ); |
| 104 | + |
| 105 | + // Same check in the negative direction. |
| 106 | + for _ in 0..10_000 { |
| 107 | + cont_hist.update(prev_mv, pc, mv, pc, i32::MIN); |
| 108 | + } |
| 109 | + |
| 110 | + let score = cont_hist.get(prev_mv, pc, mv, pc); |
| 111 | + assert!( |
| 112 | + score >= -ContinuationHistory::MAX, |
| 113 | + "saturated continuation history entry ({score}) must not exceed -MAX ({})", |
| 114 | + -ContinuationHistory::MAX |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments