Skip to content

feat: add reverse futility pruning #67

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 2 commits into from
Mar 25, 2025
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
45 changes: 44 additions & 1 deletion engine/src/search.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) ([email protected])
* -----
* Last Modified: Wed Dec 18 2024
* Last Modified: Tue Mar 25 2025
* -----
* Copyright (c) 2024 Paul Tsouchlos (DeveloperPaul123)
* GNU General Public License v3.0 or later
Expand Down Expand Up @@ -33,6 +33,7 @@ use crate::{
score::{LargeScoreType, Score, ScoreType},
traits::Eval,
ttable::{self, TranspositionTableEntry},
tuneable::{MAX_RFP_DEPTH, RFP_MARGIN},
};
use ttable::TranspositionTable;

Expand Down Expand Up @@ -345,6 +346,11 @@ impl<'a> Search<'a> {
}
}

// can we prune the current node with something other than TT?
if let Some(score) = self.pruned_score(board, depth, ply, alpha, beta) {
return score;
}

// get all legal moves
let mut move_list = MoveList::new();
self.move_gen.generate_legal_moves(board, &mut move_list);
Expand Down Expand Up @@ -462,6 +468,43 @@ impl<'a> Search<'a> {
best_score
}

/// Checks to see if the current node can be pruned. If it can, returns the score. Otherwise returns None.
///
/// # Arguments
///
/// - `board` - The current board state.
/// - `depth` - The current depth.
/// - `beta` - The current beta value.
///
/// # Returns
///
/// The score of the position if it can be pruned, otherwise None.
fn pruned_score(
&self,
board: &Board,
depth: i16,
ply: i16,
alpha: Score,
beta: Score,
) -> Option<Score> {
let is_pv_node = ply == 0 || beta - alpha > Score::new(1);

// no pruning if we are in check or if we are in a PV node
if board.is_in_check(&self.move_gen) || is_pv_node {
return None;
}

let static_eval = self.eval.eval(board);
// Reverse futility pruning
// https://cosmo.tardis.ac/files/2023-02-20-viri-wiki.html
// https://www.chessprogramming.org/Reverse_Futility_Pruning
// If the static evaluation is very high and beats beta by a depth-dependent margin, we can prune the move.
if depth <= MAX_RFP_DEPTH && static_eval - RFP_MARGIN * depth > beta {
return Some(static_eval);
}
None
}

/// Implements [quiescence search](https://www.chessprogramming.org/Quiescence_Search).
/// We use this to avoid the horizon effect. The idea is to evaluate quiet moves where there are no tactical moves to make.
///
Expand Down
5 changes: 4 additions & 1 deletion engine/src/tuneable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: Wednesday, December 11th 2024
* Author: Paul Tsouchlos (DeveloperPaul123) ([email protected])
* -----
* Last Modified: Thu Dec 12 2024
* Last Modified: Tue Mar 25 2025
* -----
* Copyright (c) 2024 Paul Tsouchlos (DeveloperPaul123)
* GNU General Public License v3.0 or later
Expand All @@ -16,3 +16,6 @@ use crate::score::ScoreType;

pub(crate) const MIN_ASPIRATION_DEPTH: ScoreType = 1;
pub(crate) const ASPIRATION_WINDOW: ScoreType = 50;

pub(crate) const MAX_RFP_DEPTH: ScoreType = 4;
pub(crate) const RFP_MARGIN: ScoreType = 82;