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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::time::{Duration, Instant};

use crate::eval;
use crate::eval::EvalChessBoard;
use crate::search::negascout::{Scout, SearchContext, SearchResponse};
use crate::search::ordering::EstimatorImpl;
use crate::search::transpositions::TranspositionTable;
use anyhow::{anyhow, Result};
use myopic_board::Move;
use ordering_hints::OrderingHints;
use serde::export::PhantomData;
use serde::ser::SerializeStruct;
use serde::Serializer;
use terminator::SearchTerminator;

pub mod interactive;
pub mod negascout;
mod ordering;
mod ordering_hints;
pub mod terminator;
mod transpositions;

const DEPTH_UPPER_BOUND: usize = 10;
const SHALLOW_EVAL_TRIGGER_DEPTH: usize = 2;
const SHALLOW_EVAL_DEPTH: usize = 1;

/// API function for executing search on the calling thread, we pass a root
/// state and a terminator and compute the best move we can make from this
/// state within the duration constraints implied by the terminator.
pub fn search<B, T>(root: B, parameters: SearchParameters<T>) -> Result<SearchOutcome>
where
    B: EvalChessBoard,
    T: SearchTerminator,
{
    Search {
        root,
        terminator: parameters.terminator,
    }
    .search(parameters.table_size)
}

pub struct SearchParameters<T: SearchTerminator> {
    pub terminator: T,
    pub table_size: usize,
}

/// Data class composing information/result about/of a best move search.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SearchOutcome {
    pub best_move: Move,
    pub eval: i32,
    pub depth: usize,
    pub time: Duration,
    pub optimal_path: Vec<Move>,
}

impl serde::Serialize for SearchOutcome {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("SearchOutcome", 4)?;
        state.serialize_field("bestMove", &self.best_move.uci_format())?;
        state.serialize_field("positionEval", &self.eval)?;
        state.serialize_field("depthSearched", &self.depth)?;
        state.serialize_field("searchDurationMillis", &self.time.as_millis())?;
        state.serialize_field(
            "optimalPath",
            &self
                .optimal_path
                .iter()
                .map(|m| m.uci_format())
                .collect::<Vec<_>>(),
        )?;
        state.end()
    }
}

#[cfg(test)]
mod searchoutcome_serialize_test {
    use super::SearchOutcome;
    use myopic_board::{CastleZone, Move, Piece, Square};
    use serde_json;
    use std::time::Duration;

    #[test]
    fn test_json_serialize() {
        let search_outcome = SearchOutcome {
            best_move: Move::Castle {
                source: 0,
                zone: CastleZone::WK,
            },
            eval: -125,
            depth: 2,
            time: Duration::from_millis(3000),
            optimal_path: vec![
                Move::Castle {
                    source: 0,
                    zone: CastleZone::WK,
                },
                Move::Standard {
                    source: 1,
                    moving: Piece::BP,
                    from: Square::D7,
                    dest: Square::D5,
                    capture: None,
                },
            ],
        };
        assert_eq!(
            r#"{"bestMove":"e1g1","positionEval":-125,"depthSearched":2,"searchDurationMillis":3000,"optimalPath":["e1g1","d7d5"]}"#,
            serde_json::to_string(&search_outcome).expect("Serialization failed")
        );
    }
}

struct Search<B: EvalChessBoard, T: SearchTerminator> {
    root: B,
    terminator: T,
}

struct BestMoveResponse {
    eval: i32,
    best_move: Move,
    path: Vec<Move>,
    depth: usize,
}

impl<B: EvalChessBoard, T: SearchTerminator> Search<B, T> {
    pub fn search(&self, transposition_table_size: usize) -> Result<SearchOutcome> {
        let search_start = Instant::now();
        let mut break_err = anyhow!("Terminated before search began");
        let mut ordering_hints = OrderingHints::new(self.root.clone());
        // TODO inject desired size
        let mut transposition_table = TranspositionTable::new(transposition_table_size)?;
        let mut best_response = None;

        for i in 1..DEPTH_UPPER_BOUND {
            match self.best_move(i, search_start, &ordering_hints, &mut transposition_table) {
                Err(message) => {
                    break_err = anyhow!("{}", message);
                    break;
                }
                Ok(response) => {
                    ordering_hints.add_pv(i, &response.path);
                    best_response = Some(response);
                    // Only fill in the shallow eval when we get deep
                    // enough to male it worthwhile
                    if i == SHALLOW_EVAL_TRIGGER_DEPTH {
                        ordering_hints.populate_shallow_eval(SHALLOW_EVAL_DEPTH);
                    }
                }
            }
        }

        best_response
            .ok_or(break_err)
            .map(|response| SearchOutcome {
                best_move: response.best_move,
                eval: response.eval,
                depth: response.depth,
                time: search_start.elapsed(),
                optimal_path: response.path,
            })
    }

    fn best_move(
        &self,
        depth: usize,
        search_start: Instant,
        ordering_hints: &OrderingHints<B>,
        transposition_table: &mut TranspositionTable,
    ) -> Result<BestMoveResponse> {
        if depth < 1 {
            return Err(anyhow!("Cannot iteratively deepen with depth 0"));
        }

        let SearchResponse { eval, mut path } = Scout {
            terminator: &self.terminator,
            ordering_hints,
            move_quality_estimator: EstimatorImpl,
            transposition_table,
            board_type: PhantomData,
        }
        .search(
            &mut self.root.clone(),
            SearchContext {
                depth_remaining: depth,
                start_time: search_start,
                alpha: -eval::INFTY,
                beta: eval::INFTY,
                precursors: vec![],
            },
        )?;

        // The path returned from the negamax function is ordered deepest move -> shallowest
        // so we reverse as the shallowest move is the one we make in this position.
        path.reverse();
        // If the path returned is empty then there must be no legal moves in this position
        if path.is_empty() {
            Err(anyhow!(
                "No moves found for position {}",
                self.root.to_fen()
            ))
        } else {
            Ok(BestMoveResponse {
                best_move: path.get(0).unwrap().clone(),
                eval,
                path,
                depth,
            })
        }
    }
}

/// Tests for 'obvious' positions like taking a hanging piece,
/// checkmating or escaping checkmate etc.
#[cfg(test)]
mod test {
    use crate::eval::EvalChessBoard;
    use crate::search::SearchParameters;
    use crate::{eval, EvalBoard, UciMove};
    use myopic_board::{Reflectable, Board};

    const DEPTH: usize = 3;
    const TABLE_SIZE: usize = 10_000;

    fn test(fen_string: &'static str, expected_move_pool: Vec<UciMove>, is_won: bool) {
        let base_board = fen_string.parse::<Board>().unwrap();
        let ref_board = EvalBoard::builder(base_board.reflect()).build();
        let board = EvalBoard::builder(base_board).build();
        let ref_move_pool = expected_move_pool.reflect();
        test_impl(board, expected_move_pool, is_won);
        test_impl(ref_board, ref_move_pool, is_won);
    }

    fn test_impl<B: EvalChessBoard>(board: B, expected_move_pool: Vec<UciMove>, is_won: bool) {
        match super::search(
            board,
            SearchParameters {
                terminator: DEPTH,
                table_size: TABLE_SIZE,
            },
        ) {
            Err(message) => panic!("{}", message),
            Ok(outcome) => {
                assert!(
                    expected_move_pool
                        .contains(&UciMove::new(outcome.best_move.uci_format().as_str()).unwrap()),
                    serde_json::to_string(&outcome).unwrap()
                );
                if is_won {
                    assert_eq!(eval::WIN_VALUE, outcome.eval);
                }
            }
        }
    }

    #[test]
    fn queen_escape_attack() {
        test(
            "r4rk1/5ppp/8/1Bn1p3/Q7/8/5PPP/1R3RK1 w Qq - 5 27",
            vec![
                UciMove::new("a4b4").unwrap(),
                UciMove::new("a4c4").unwrap(),
                UciMove::new("a4g4").unwrap(),
                UciMove::new("a4h4").unwrap(),
                UciMove::new("a4c2").unwrap(),
                UciMove::new("a4d1").unwrap(),
            ],
            false,
        )
    }

    #[test]
    fn mate_0() {
        test(
            "r2r2k1/5ppp/1N2p3/1n6/3Q4/2B5/5PPP/1R3RK1 w Qq - 4 21",
            vec![UciMove::new("d4g7").unwrap()],
            true,
        )
    }

    #[test]
    fn mate_1() {
        test(
            "8/8/8/4Q3/8/6R1/2n1pkBK/8 w - - 0 1",
            vec![UciMove::new("g3d3").unwrap()],
            true,
        )
    }

    #[test]
    fn mate_2() {
        test(
            "8/7B/5Q2/6p1/6k1/8/5K2/8 w - - 0 1",
            vec![UciMove::new("f6h8").unwrap(), UciMove::new("f6f3").unwrap()],
            true,
        )
    }

    #[test]
    fn mate_3() {
        test(
            "3qr2k/1b1p2pp/7N/3Q2b1/4P3/8/5PP1/6K1 w - - 0 1",
            vec![UciMove::new("d5g8").unwrap()],
            true,
        )
    }

    // Mate in 4 moves TODO probably better in benchmark.
    #[ignore]
    #[test]
    fn mate_4() {
        test(
            "r1k2b1r/pp4pp/2p1n3/3NQ1B1/6q1/8/PPP2P1P/2KR4 w - - 4 20",
            vec![UciMove::new("e5c7").unwrap()],
            true,
        )
    }

    #[test]
    fn mate_5() {
        test(
            "r1b1k1nr/p2p1ppp/n2B4/1p1NPN1P/6P1/3P1Q2/P1P1K3/q5b1 w - - 0 30",
            vec![UciMove::new("f5g7").unwrap()],
            true,
        )
    }

    /// A funny one which currently depends on move ordering, at depth 3 the
    /// best move has the same evaluation as another inferior move.
    #[ignore]
    #[test]
    fn tactic_1() {
        test(
            "1r3k2/2R5/1p2p2p/1Q1pPp1q/1P1P2p1/2P1P1P1/6KP/8 b - - 2 31",
            vec![UciMove::new("b8a8").unwrap()],
            false,
        )
    }

    /// This fails at depth 3 but passes at depth 4, should be moved to a
    /// benchmark maybe
    #[ignore]
    #[test]
    fn tactic_2() {
        test(
            "r5k1/pb4pp/1pn1pq2/5B2/2Pr4/B7/PP3RPP/R4QK1 b - - 0 23",
            vec![UciMove::new("e6f5").unwrap()],
            false,
        )
    }
}