From 923aeb11d61b6e20ad33598ef7f44d0a6dfa6c15 Mon Sep 17 00:00:00 2001 From: Micha White Date: Thu, 28 Dec 2023 10:26:06 -0500 Subject: Changes made to support the eventual C API --- engine/src/search.rs | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) (limited to 'engine/src/search.rs') diff --git a/engine/src/search.rs b/engine/src/search.rs index b8fd982..9c8ea26 100644 --- a/engine/src/search.rs +++ b/engine/src/search.rs @@ -99,9 +99,13 @@ pub fn negamax( } } -pub fn search(task: Arc, frontend: &dyn Frontend) -> Evaluation { +pub fn search( + task: Arc, + frontend: &dyn Frontend, + cancel: Option<&AtomicBool>, +) -> (Evaluation, Option) { let board = task.position; - let cancel_flag = &task.cancel_flag; + let cancel_flag = cancel.unwrap_or(&task.cancel_flag); let allowed_moves = task.allowed_moves.clone(); let limits = task.limits; @@ -115,25 +119,28 @@ pub fn search(task: Arc, frontend: &dyn Frontend) -> Evaluation let mut eval = Evaluation::DRAW; let mut best_move = None; loop { - if let Some(max_depth) = max_depth { - if depth > max_depth.get() { - break; + // don't leave search is no good moves have been found + if best_move.is_some() { + if let Some(max_depth) = max_depth { + if depth > max_depth.get() { + break; + } } - } - if let Some(max_time) = max_time { - if Instant::now() > max_time { - break; + if let Some(max_time) = max_time { + if Instant::now() > max_time { + break; + } } - } - if let Some(max_nodes) = max_nodes { - if task - .nodes_explored - .load(std::sync::atomic::Ordering::Acquire) - > max_nodes.get() - { - break; + if let Some(max_nodes) = max_nodes { + if task + .nodes_explored + .load(std::sync::atomic::Ordering::Acquire) + > max_nodes.get() + { + break; + } } } @@ -148,7 +155,7 @@ pub fn search(task: Arc, frontend: &dyn Frontend) -> Evaluation ); // prevent incomplete search from overwriting evaluation - if cancel_flag.load(std::sync::atomic::Ordering::Acquire) { + if best_move.is_some() && cancel_flag.load(std::sync::atomic::Ordering::Acquire) { break; } @@ -167,7 +174,7 @@ pub fn search(task: Arc, frontend: &dyn Frontend) -> Evaluation ); // prevent incomplete search from overwriting evaluation - if cancel_flag.load(std::sync::atomic::Ordering::Acquire) { + if best_move.is_some() && cancel_flag.load(std::sync::atomic::Ordering::Acquire) { break; } @@ -193,6 +200,11 @@ pub fn search(task: Arc, frontend: &dyn Frontend) -> Evaluation beta = eval.add_f32(0.125); } + if eval.is_force_sequence() { + // we don't need to search any deeper + return (eval, best_move); + } + depth += 1; } @@ -231,5 +243,5 @@ pub fn search(task: Arc, frontend: &dyn Frontend) -> Evaluation } } - eval + (eval, best_move) } -- cgit v1.2.3