1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::search::eval;
use crate::search::ordering::MoveQualityEstimator;
use crate::{quiescent, EvalBoard};
use core::cmp;
use myopic_board::{Move, MoveComputeType, Termination};
use std::time::{Duration, Instant};
pub struct SearchContext {
pub start_time: Instant,
pub alpha: i32,
pub beta: i32,
pub depth_remaining: usize,
pub precursors: Vec<Move>,
}
impl SearchContext {
fn next_level(&self, mv: &Move) -> SearchContext {
let mut next_precursors = self.precursors.clone();
next_precursors.push(mv.clone());
SearchContext {
start_time: self.start_time,
alpha: -self.beta,
beta: -self.alpha,
depth_remaining: self.depth_remaining - 1,
precursors: next_precursors,
}
}
}
pub trait SearchTerminator {
fn should_terminate(&self, ctx: &SearchContext) -> bool;
}
pub struct SearchResponse {
pub eval: i32,
pub path: Vec<Move>,
}
pub struct Searcher<'a, T, B, M>
where
T: SearchTerminator,
B: EvalBoard,
M: MoveQualityEstimator<B>,
{
pub terminator: &'a T,
pub principle_variation: &'a Vec<Move>,
pub move_quality_estimator: M,
pub board_type: std::marker::PhantomData<B>,
}
impl<T, B, M> Searcher<'_, T, B, M>
where
T: SearchTerminator,
B: EvalBoard,
M: MoveQualityEstimator<B>,
{
pub fn search(&self, root: &mut B, mut ctx: SearchContext) -> Result<SearchResponse, String> {
if self.terminator.should_terminate(&ctx) {
return Err(format!("Terminated at depth {}", ctx.depth_remaining));
} else if ctx.depth_remaining == 0 || root.termination_status().is_some() {
return Ok(SearchResponse {
eval: match root.termination_status() {
Some(Termination::Loss) => eval::LOSS_VALUE,
Some(Termination::Draw) => eval::DRAW_VALUE,
None => quiescent::search(root, -eval::INFTY, eval::INFTY, -1),
},
path: vec![],
});
}
let mut result = -eval::INFTY;
let mut best_path = vec![];
for evolve in self.compute_moves(root, &ctx.precursors) {
let discards = root.evolve(&evolve);
let SearchResponse { eval, path } = self.search(root, ctx.next_level(&evolve))?;
root.devolve(&evolve, discards);
let negated_eval = -eval;
if negated_eval > result {
result = negated_eval;
best_path = path;
best_path.push(evolve.clone());
}
ctx.alpha = cmp::max(ctx.alpha, result);
if ctx.alpha > ctx.beta {
return Ok(SearchResponse {
eval: ctx.beta,
path: vec![],
});
}
}
return Ok(SearchResponse {
eval: result,
path: best_path,
});
}
fn compute_moves(&self, board: &mut B, precursors: &Vec<Move>) -> Vec<Move> {
let mut moves = board.compute_moves(MoveComputeType::All);
moves.sort_by_cached_key(|m| -self.move_quality_estimator.estimate(board, m));
if self.principle_variation.starts_with(precursors.as_slice()) {
match self.principle_variation.get(precursors.len()) {
None => {}
Some(suggested_move) => {
match moves.iter().position(|m| m == suggested_move) {
None => {}
Some(index) => {
moves.remove(index);
moves.insert(0, suggested_move.clone());
}
}
}
}
}
moves
}
}
impl SearchTerminator for Duration {
fn should_terminate(&self, ctx: &SearchContext) -> bool {
ctx.start_time.elapsed() > *self
}
}
impl SearchTerminator for usize {
fn should_terminate(&self, ctx: &SearchContext) -> bool {
ctx.depth_remaining > *self
}
}
impl SearchTerminator for (Duration, usize) {
fn should_terminate(&self, ctx: &SearchContext) -> bool {
self.0.should_terminate(ctx) || self.1.should_terminate(ctx)
}
}