-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add initial implementation of history threats #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DeveloperPaul123
wants to merge
8
commits into
main
Choose a base branch
from
feature/hist-threat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4e856b2
chore(move-gen): make function public
DeveloperPaul123 fd2a2df
feat: add threats to history table
DeveloperPaul123 b803237
chore: add test for bitboard piece get for side
DeveloperPaul123 c5e32f3
fix: use ALL_PIECES for iteration in move gen
DeveloperPaul123 610ebe5
fix: remove dead (commented) code
DeveloperPaul123 0a13307
fix: commenting style
DeveloperPaul123 07f06c1
chore: simplify clear in hist table
DeveloperPaul123 745c184
chore: simplify attacked square calc in search
DeveloperPaul123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,9 @@ use chess::{ | |
| use crate::score::{LargeScoreType, Score}; | ||
|
|
||
| pub struct HistoryTable { | ||
| table: [[[LargeScoreType; NumberOf::SQUARES]; NumberOf::PIECE_TYPES]; NumberOf::SIDES], | ||
| /// The history table is a 5D array indexed by [is_from_attacked][is_to_attacked][side][piece_type][square (to)] | ||
| table: [[[[[LargeScoreType; NumberOf::SQUARES]; NumberOf::PIECE_TYPES]; NumberOf::SIDES]; | ||
| NumberOf::SIDES]; NumberOf::SIDES], | ||
| } | ||
|
|
||
| /// Safe calculation of the bonus applied to quiet moves that are inserted into the history table. | ||
|
|
@@ -28,31 +30,53 @@ pub(crate) fn calculate_bonus_for_depth(depth: i16) -> i16 { | |
|
|
||
| impl HistoryTable { | ||
| pub(crate) fn new() -> Self { | ||
| let table = | ||
| [[[Default::default(); NumberOf::SQUARES]; NumberOf::PIECE_TYPES]; NumberOf::SIDES]; | ||
| let table = [[[[[Default::default(); NumberOf::SQUARES]; NumberOf::PIECE_TYPES]; | ||
| NumberOf::SIDES]; NumberOf::SIDES]; NumberOf::SIDES]; | ||
| Self { table } | ||
| } | ||
|
|
||
| pub(crate) fn get(&self, side: Side, piece: Piece, square: u8) -> LargeScoreType { | ||
| self.table[side as usize][piece as usize][square as usize] | ||
| pub(crate) fn get( | ||
| &self, | ||
| side: Side, | ||
| piece: Piece, | ||
| square: u8, | ||
| is_from_attacked: bool, | ||
| is_to_attacked: bool, | ||
| ) -> LargeScoreType { | ||
| self.table[is_from_attacked as usize][is_to_attacked as usize][side as usize] | ||
| [piece as usize][square as usize] | ||
| } | ||
|
|
||
| pub(crate) fn update(&mut self, side: Side, piece: Piece, square: u8, bonus: LargeScoreType) { | ||
| let current_value = self.table[side as usize][piece as usize][square as usize]; | ||
| fn get_mut( | ||
| &mut self, | ||
| side: Side, | ||
| piece: Piece, | ||
| square: u8, | ||
| is_from_attacked: bool, | ||
| is_to_attacked: bool, | ||
| ) -> &mut LargeScoreType { | ||
| &mut self.table[is_from_attacked as usize][is_to_attacked as usize][side as usize] | ||
| [piece as usize][square as usize] | ||
| } | ||
|
|
||
| pub(crate) fn update( | ||
| &mut self, | ||
| side: Side, | ||
| piece: Piece, | ||
| square: u8, | ||
| is_from_attacked: bool, | ||
| is_to_attacked: bool, | ||
| bonus: LargeScoreType, | ||
| ) { | ||
| let current_value = self.get(side, piece, square, is_from_attacked, is_to_attacked); | ||
| let clamped_bonus = bonus.clamp(-Score::MAX_HISTORY, Score::MAX_HISTORY); | ||
| let new_value = current_value + clamped_bonus | ||
| - current_value * clamped_bonus.abs() / Score::MAX_HISTORY; | ||
| self.table[side as usize][piece as usize][square as usize] = new_value; | ||
| *self.get_mut(side, piece, square, is_from_attacked, is_to_attacked) = new_value; | ||
| } | ||
|
|
||
| pub(crate) fn clear(&mut self) { | ||
| for side in 0..NumberOf::SIDES { | ||
| for piece_type in 0..NumberOf::PIECE_TYPES { | ||
| for square in 0..NumberOf::SQUARES { | ||
| self.table[side][piece_type][square] = Default::default(); | ||
| } | ||
| } | ||
| } | ||
| *self = Self::new(); | ||
| } | ||
|
|
||
| pub(crate) fn print_for_side(&self, side: Side) { | ||
|
|
@@ -63,7 +87,11 @@ impl HistoryTable { | |
| print!("|"); | ||
| for file in 0..NumberOf::FILES { | ||
| let square = file + rank * NumberOf::FILES; | ||
| print!("{:5} ", self.table[side as usize][piece_type][square]); | ||
| let grid = self.table[side as usize][piece_type][square]; | ||
| print!( | ||
| "{:5} | {:5}\n------\n[{:5} | {:5} ]", | ||
| grid[0][0], grid[0][1], grid[1][0], grid[1][1] | ||
| ); | ||
| } | ||
| println!("|"); | ||
| } | ||
|
|
@@ -88,13 +116,23 @@ mod tests { | |
| fn initialize_history_table() { | ||
| let history_table = HistoryTable::new(); | ||
| // loop through all sides, piece types, and squares | ||
| for side in 0..2 { | ||
| for side in 0..2u8 { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the actual enum type here |
||
| for piece_type in 0..6 { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, use |
||
| for square in 0..64 { | ||
| assert_eq!( | ||
| history_table.table[side][piece_type][square], | ||
| Default::default() | ||
| ); | ||
| for is_from_attacked in 0..2 { | ||
| for is_to_attacked in 0..2 { | ||
| assert_eq!( | ||
| history_table.get( | ||
| Side::try_from(side).unwrap(), | ||
| Piece::try_from(piece_type).unwrap(), | ||
| square as u8, | ||
| is_from_attacked != 0, | ||
| is_to_attacked != 0, | ||
| ), | ||
| Default::default() | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -107,10 +145,18 @@ mod tests { | |
| let piece = Piece::Pawn; | ||
| let square = Squares::A1; | ||
| let score = 37; | ||
| history_table.update(side, piece, square, score); | ||
| assert_eq!(history_table.get(side, piece, square), score); | ||
| history_table.update(side, piece, square, score); | ||
| assert_eq!(history_table.get(side, piece, square), score + score); | ||
| let is_from_attacked = true; | ||
| let is_to_attacked = false; | ||
| history_table.update(side, piece, square, is_from_attacked, is_to_attacked, score); | ||
| assert_eq!( | ||
| history_table.get(side, piece, square, is_from_attacked, is_to_attacked), | ||
| score | ||
| ); | ||
| history_table.update(side, piece, square, is_from_attacked, is_to_attacked, score); | ||
| assert_eq!( | ||
| history_table.get(side, piece, square, is_from_attacked, is_to_attacked), | ||
| score + score | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -121,4 +167,24 @@ mod tests { | |
| assert!(bonus as i32 <= i16::MAX.into()); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn update_and_then_clear() { | ||
| let mut history_table = HistoryTable::new(); | ||
| let side = Side::White; | ||
| let piece = Piece::Knight; | ||
| let square = Squares::E4; | ||
| let is_from_attacked = false; | ||
| let is_to_attacked = true; | ||
| let bonus = 50; | ||
|
|
||
| history_table.update(side, piece, square, is_from_attacked, is_to_attacked, bonus); | ||
| let stored_value = history_table.get(side, piece, square, is_from_attacked, is_to_attacked); | ||
| assert_eq!(stored_value, bonus); | ||
|
|
||
| history_table.clear(); | ||
| let cleared_value = | ||
| history_table.get(side, piece, square, is_from_attacked, is_to_attacked); | ||
| assert_eq!(cleared_value, 0); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't actually tested this to see what it looks like - might have broken things.