Skip to content

feat: use history gravity formula for history bonus #51

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

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions engine/src/history_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chess::{
side::Side,
};

use crate::score::MoveOrderScoreType;
use crate::score::{MoveOrderScoreType, Score};

pub struct HistoryTable {
table: [[[MoveOrderScoreType; NumberOf::SQUARES]; NumberOf::PIECE_TYPES]; NumberOf::SIDES],
Expand All @@ -30,7 +30,11 @@ impl HistoryTable {
bonus: MoveOrderScoreType,
) {
assert!(side != Side::Both, "Side cannot be Both");
self.table[side as usize][piece as usize][square as usize] += bonus;
let current_value = self.table[side as usize][piece as usize][square as usize];
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;
}

pub(crate) fn clear(&mut self) {
Expand Down
25 changes: 20 additions & 5 deletions engine/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ impl<'a> Search<'a> {
let mut best_move = None;

// loop through all moves
for (i, mv) in sorted_moves.enumerate() {
// TODO(PT): Not a fan of this clone() call, but we needed it (for now) for the history malus update later on.
// This will likely be a non-issue once we implement a move picker
for (i, mv) in sorted_moves.clone().enumerate() {
// make the move
board.make_move_unchecked(mv).unwrap();
let score : Score =
Expand Down Expand Up @@ -391,11 +393,24 @@ impl<'a> Search<'a> {
// update history table for quiets
if mv.is_quiet() {
// calculate history bonus
let bonus = (depth * depth) as MoveOrderScoreType;
self.history_table
.update(board.side_to_move(), mv.piece(), mv.to(), bonus);
let bonus = 300 * depth - 250;
self.history_table.update(
board.side_to_move(),
mv.piece(),
mv.to(),
bonus as MoveOrderScoreType,
);

// apply a penalty to all quiets searched so far
for mv in sorted_moves.take(i).filter(|mv| mv.is_quiet()) {
self.history_table.update(
board.side_to_move(),
mv.piece(),
mv.to(),
-bonus as MoveOrderScoreType,
);
}
}

break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/byte-knight/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: Thursday, November 21st 2024
* Author: Paul Tsouchlos (DeveloperPaul123) (developer.paul.123@gmail.com)
* -----
* Last Modified: Sat Nov 30 2024
* Last Modified: Wed Dec 11 2024
* -----
* Copyright (c) 2024 Paul Tsouchlos (DeveloperPaul123)
* GNU General Public License v3.0 or later
Expand Down Expand Up @@ -166,7 +166,7 @@ pub(crate) fn bench(depth: u8, epd_file: &Option<String>) {
let mut search = Search::new(&config, &mut tt, &mut hist);

for bench in benchmark_strings {
let fen: &str = bench.split(";").next().unwrap();
let fen: &str = bench.split(';').next().unwrap();
let mut board = Board::from_fen(fen).unwrap();

let result = search.search(&mut board, None);
Expand Down
4 changes: 2 additions & 2 deletions src/bin/perft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ fn process_epd_file(path: &str, move_generation: &MoveGenerator) {
lines
.par_iter()
.map(|line| {
let parts: Vec<&str> = line.split(";").collect();
let parts: Vec<&str> = line.split(';').collect();
let fen = parts[0];
let mut failures = Vec::new();
for part in parts.iter().skip(1) {
let parts = part.split_whitespace().collect::<Vec<&str>>();
let depth = parts[0].replace("D", "").parse::<usize>().unwrap();
let depth = parts[0].replace('D', "").parse::<usize>().unwrap();
let expected_nodes = parts[1].parse::<u64>().unwrap();
let mut board = Board::from_fen(fen).unwrap();
let nodes = perft::perft(&mut board, move_generation, depth, false).unwrap();
Expand Down