summaryrefslogtreecommitdiff
path: root/engine/src/search.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2023-12-28 10:26:06 -0500
committerMicha White <botahamec@outlook.com>2023-12-28 10:26:06 -0500
commit923aeb11d61b6e20ad33598ef7f44d0a6dfa6c15 (patch)
tree03c2139625f32257b2ac3c246ae623e8fdfce16f /engine/src/search.rs
parentef59ee8eb6562dfd724c1cb0bbd37aebc7d798ad (diff)
Changes made to support the eventual C API
Diffstat (limited to 'engine/src/search.rs')
-rw-r--r--engine/src/search.rs52
1 files changed, 32 insertions, 20 deletions
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<EvaluationTask>, frontend: &dyn Frontend) -> Evaluation {
+pub fn search(
+ task: Arc<EvaluationTask>,
+ frontend: &dyn Frontend,
+ cancel: Option<&AtomicBool>,
+) -> (Evaluation, Option<Move>) {
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<EvaluationTask>, 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<EvaluationTask>, 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<EvaluationTask>, 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<EvaluationTask>, 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<EvaluationTask>, frontend: &dyn Frontend) -> Evaluation
}
}
- eval
+ (eval, best_move)
}