use search_core::Budget;
use crate::alpha_beta::{Node, Run, Window};
use crate::incremental::{Borrowed, Place};
use crate::table::Memory;
use crate::{Algorithm, Bound, Game, SearchResult, Searcher, Stats};
const MAX_ATTEMPTS: u32 = 8;
pub trait Deepening<M, S> {
fn proceed(&mut self, result: &SearchResult<M, S>) -> bool;
fn window(&mut self, previous: &SearchResult<M, S>, attempt: u32) -> Window<S> {
let _ = (previous, attempt);
Window::FULL
}
}
impl<M, S, F: FnMut(&SearchResult<M, S>) -> bool> Deepening<M, S> for F {
fn proceed(&mut self, result: &SearchResult<M, S>) -> bool {
self(result)
}
}
#[must_use]
pub fn deepen<G: Game>(
game: &G,
position: &G::Position,
max_depth: u32,
budget: impl Budget<Stats>,
) -> SearchResult<G::Move, G::Score> {
Searcher::new().deepen(game, position, max_depth, budget)
}
impl<G: Game, T: Memory<G>> Searcher<G, T> {
#[must_use]
pub fn deepen(
&mut self,
game: &G,
position: &G::Position,
max_depth: u32,
budget: impl Budget<Stats>,
) -> SearchResult<G::Move, G::Score> {
let always = |_: &SearchResult<G::Move, G::Score>| true;
self.deepen_with(game, position, max_depth, budget, always)
}
#[must_use]
pub fn deepen_with(
&mut self,
game: &G,
position: &G::Position,
max_depth: u32,
budget: impl Budget<Stats>,
deepening: impl Deepening<G::Move, G::Score>,
) -> SearchResult<G::Move, G::Score> {
self.deepen_from(game, &mut Borrowed(position), max_depth, budget, deepening)
}
pub(crate) fn deepen_from(
&mut self,
game: &G,
here: &mut impl Place<G>,
max_depth: u32,
mut budget: impl Budget<Stats>,
mut deepening: impl Deepening<G::Move, G::Score>,
) -> SearchResult<G::Move, G::Score> {
self.stats = Stats::ZERO;
self.hint.clear();
self.guess = None;
let first_depth = (1 + self.lead).min(max_depth);
let mut run = Run {
game,
budget: &mut budget,
};
let node = if self.budget_first {
self.iterate(&mut run, here, first_depth, Window::FULL)
} else {
let mut unlimited = |_: &Stats| false;
let mut first = Run {
game,
budget: &mut unlimited,
};
self.iterate(&mut first, here, first_depth, Window::FULL)
};
self.answered = !self.aborted;
let mut best = self.result(&node, first_depth);
for depth in first_depth + 1..=max_depth {
if best.proven || run.budget.exhausted(&self.stats) || !deepening.proceed(&best) {
break;
}
self.hint.clear();
self.hint.extend(best.pv.iter().map(|step| step.mv.clone()));
self.guess = Some(best.score);
let node = self.aspirate(&mut run, here, depth, (&best, &mut deepening));
if self.aborted {
break;
}
best = self.result(&node, depth);
}
best.stats = self.stats;
best
}
fn aspirate<B: Budget<Stats>, H: Place<G>>(
&mut self,
run: &mut Run<'_, G, B>,
here: &mut H,
depth: u32,
(previous, deepening): (
&SearchResult<G::Move, G::Score>,
&mut impl Deepening<G::Move, G::Score>,
),
) -> Node<G::Score> {
let mut attempt = 0;
loop {
let window = if attempt < MAX_ATTEMPTS && self.algorithm != Algorithm::Mtdf {
deepening.window(previous, attempt)
} else {
Window::FULL
};
let node = self.iterate(run, here, depth, window);
if self.aborted || node.bound == Bound::Exact || window.is_full() {
return node;
}
self.stats.widenings += 1;
attempt += 1;
}
}
}