Skip to content

Commit

Permalink
Merge pull request #17 from dsekercioglu/abort_fix
Browse files Browse the repository at this point in the history
Do proper search abort
  • Loading branch information
jnlt3 authored Dec 6, 2021
2 parents fb6f3d5 + 84bec80 commit 46707da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/bm/bm_runner/ab_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub struct LocalContext {
killer_moves: Vec<MoveEntry<{ SEARCH_PARAMS.get_k_move_cnt() }>>,
threat_moves: Vec<MoveEntry<{ SEARCH_PARAMS.get_threat_move_cnt() }>>,
nodes: u32,
abort: bool,
}

impl SharedContext {
Expand Down Expand Up @@ -297,6 +298,14 @@ impl LocalContext {
pub fn nodes(&mut self) -> &mut u32 {
&mut self.nodes
}

pub fn trigger_abort(&mut self) {
self.abort = true;
}

pub fn abort(&self) -> bool {
self.abort
}
}

pub struct AbRunner {
Expand Down Expand Up @@ -451,6 +460,7 @@ impl AbRunner {
skip_moves: vec![],
sel_depth: 0,
nodes: 0,
abort: false,
},
position,
}
Expand Down
17 changes: 5 additions & 12 deletions src/bm/bm_search/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pub fn search<Search: SearchType>(
) -> (Option<ChessMove>, Evaluation) {
let depth = target_ply.saturating_sub(ply);
if ply != 0 && shared_context.abort_absolute(depth, *local_context.nodes()) {
return (None, Evaluation::max());
local_context.trigger_abort();
return (None, Evaluation::min());
}

if ply != 0 && position.forced_draw(ply) {
Expand Down Expand Up @@ -102,7 +103,6 @@ pub fn search<Search: SearchType>(

/*
Transposition Table
If we get a TT hit and the search is deep enough,
we can use the score from TT to cause an early cutoff
We also use the best move from the transposition table
Expand Down Expand Up @@ -153,7 +153,6 @@ pub fn search<Search: SearchType>(
if !Search::PV && !in_check && skip_move.is_none() {
/*
Reverse Futility Pruning:
If in a non PV node and evaluation is higher than beta + a depth dependent margin
we assume we can at least achieve beta
*/
Expand All @@ -167,10 +166,8 @@ pub fn search<Search: SearchType>(

/*
Null Move Pruning:
If in a non PV node and we can still achieve beta at a reduced depth after
giving the opponent the side to move we can prune this node and return the evaluation
While doing null move pruning, we also get the "best move" for the opponent in case
This is seen as the major threat in the current position and can be used in
move ordering for the next ply
Expand Down Expand Up @@ -214,10 +211,8 @@ pub fn search<Search: SearchType>(

/*
Internal Iterative Deepening
In PV nodes, if we don't have a move from the transposition table, we do a reduced
depth search to get a good estimation on what the best move is
This is currently disabled
*/
let do_iid = SEARCH_PARAMS.do_iid(depth) && Search::PV && !in_check;
Expand Down Expand Up @@ -297,7 +292,6 @@ pub fn search<Search: SearchType>(
if moves_seen == 0 {
/*
Singular Extensions:
If a move can't be beaten by any other move, we assume the move
is singular (only solution) and extend in order to get a more accurate
estimation of best move/eval
Expand Down Expand Up @@ -329,7 +323,6 @@ pub fn search<Search: SearchType>(
} else if s_beta >= beta {
/*
Multi-cut:
If a move isn't singular and the move that disproves the singularity
our singular beta is above beta, we assume the move is good enough to beat beta
*/
Expand Down Expand Up @@ -494,7 +487,7 @@ pub fn search<Search: SearchType>(
}
if score > alpha {
if score >= beta {
if skip_move.is_none() {
if skip_move.is_none() && !local_context.abort() {
if !is_capture {
let killer_table = local_context.get_k_table();
killer_table[ply as usize].push(make_move);
Expand Down Expand Up @@ -533,7 +526,7 @@ pub fn search<Search: SearchType>(
}
let highest_score = highest_score.unwrap();

if skip_move.is_none() {
if skip_move.is_none() && !local_context.abort() {
if let Some(final_move) = &best_move {
let entry_type = if highest_score > initial_alpha {
Exact
Expand Down Expand Up @@ -657,4 +650,4 @@ pub fn q_search(
shared_context.get_t_table().set(position.board(), analysis);
}
highest_score.unwrap_or(alpha)
}
}

0 comments on commit 46707da

Please sign in to comment.