2024-10-26 19:53:20 -04:00
|
|
|
/*
|
|
|
|
|
|
|
|
This file is part of chess_inator.
|
|
|
|
|
|
|
|
chess_inator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
chess_inator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with chess_inator. If not, see https://www.gnu.org/licenses/.
|
|
|
|
|
|
|
|
Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Game-tree search.
|
|
|
|
|
|
|
|
use crate::eval::{Eval, EvalInt};
|
2024-11-17 14:02:47 -05:00
|
|
|
use crate::hash::ZobristTable;
|
2024-11-16 21:56:53 -05:00
|
|
|
use crate::movegen::{Move, MoveGen};
|
2024-11-04 14:08:29 -05:00
|
|
|
use crate::{Board, Piece};
|
2024-10-26 19:53:20 -04:00
|
|
|
use std::cmp::max;
|
2024-11-16 15:57:01 -05:00
|
|
|
use std::sync::mpsc;
|
2024-10-26 19:53:20 -04:00
|
|
|
|
2024-10-26 21:05:51 -04:00
|
|
|
// min can't be represented as positive
|
|
|
|
const EVAL_WORST: EvalInt = -(EvalInt::MAX);
|
2024-10-27 17:34:41 -04:00
|
|
|
const EVAL_BEST: EvalInt = EvalInt::MAX;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_eval_int {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eval_worst_best_symm() {
|
|
|
|
// int limits will bite you if you don't test this
|
|
|
|
assert_eq!(EVAL_WORST, -EVAL_BEST);
|
|
|
|
assert_eq!(-EVAL_WORST, EVAL_BEST);
|
|
|
|
}
|
|
|
|
}
|
2024-10-26 21:05:51 -04:00
|
|
|
|
2024-11-02 16:05:04 -04:00
|
|
|
/// Eval in the context of search.
|
2024-11-02 19:33:29 -04:00
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
|
|
pub enum SearchEval {
|
2024-11-02 16:05:04 -04:00
|
|
|
/// Mate in |n| - 1 half moves, negative for own mate.
|
|
|
|
Checkmate(i8),
|
|
|
|
/// Centipawn score.
|
|
|
|
Centipawns(EvalInt),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SearchEval {
|
|
|
|
/// Flip side, and increment the "mate in n" counter.
|
|
|
|
fn increment(self) -> Self {
|
|
|
|
match self {
|
|
|
|
SearchEval::Checkmate(n) => {
|
|
|
|
debug_assert_ne!(n, 0);
|
|
|
|
if n < 0 {
|
|
|
|
Self::Checkmate(-(n - 1))
|
|
|
|
} else {
|
|
|
|
Self::Checkmate(-(n + 1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SearchEval::Centipawns(eval) => Self::Centipawns(-eval),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SearchEval> for EvalInt {
|
|
|
|
fn from(value: SearchEval) -> Self {
|
|
|
|
match value {
|
|
|
|
SearchEval::Checkmate(n) => {
|
|
|
|
debug_assert_ne!(n, 0);
|
|
|
|
if n < 0 {
|
2024-11-02 19:33:29 -04:00
|
|
|
EVAL_WORST - EvalInt::from(n)
|
2024-11-02 16:05:04 -04:00
|
|
|
} else {
|
|
|
|
EVAL_BEST - EvalInt::from(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SearchEval::Centipawns(eval) => eval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for SearchEval {
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
let e1 = EvalInt::from(*self);
|
|
|
|
let e2 = EvalInt::from(*other);
|
|
|
|
e1.cmp(&e2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for SearchEval {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 21:36:54 -04:00
|
|
|
/// Configuration for the gametree search.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct SearchConfig {
|
|
|
|
/// Enable alpha-beta pruning.
|
|
|
|
alpha_beta_on: bool,
|
2024-11-04 14:08:29 -05:00
|
|
|
/// Limit regular search depth
|
2024-11-02 21:36:54 -04:00
|
|
|
depth: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SearchConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
SearchConfig {
|
|
|
|
alpha_beta_on: true,
|
2024-11-23 21:17:20 -05:00
|
|
|
// try to make this even to be more conservative and avoid horizon problem
|
2024-11-16 15:57:01 -05:00
|
|
|
depth: 10,
|
2024-11-02 21:36:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-04 14:08:29 -05:00
|
|
|
/// If a move is a capture, return which piece is capturing what.
|
|
|
|
fn move_get_capture(board: &mut Board, mv: &Move) -> Option<(Piece, Piece)> {
|
|
|
|
// TODO: en passant
|
|
|
|
board
|
|
|
|
.get_piece(mv.dest)
|
|
|
|
.map(|cap_pc| (board.get_piece(mv.src).unwrap().into(), cap_pc.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Least valuable victim, most valuable attacker heuristic for captures.
|
|
|
|
fn lvv_mva_eval(src_pc: Piece, cap_pc: Piece) -> EvalInt {
|
|
|
|
let pc_values = [500, 300, 300, 20000, 900, 100];
|
|
|
|
pc_values[cap_pc as usize] - pc_values[src_pc as usize]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assign a priority to a move based on how promising it is.
|
|
|
|
fn move_priority(board: &mut Board, mv: &Move) -> EvalInt {
|
|
|
|
// move eval
|
|
|
|
let mut eval: EvalInt = 0;
|
|
|
|
if let Some((src_pc, cap_pc)) = move_get_capture(board, mv) {
|
|
|
|
// least valuable victim, most valuable attacker
|
|
|
|
eval += lvv_mva_eval(src_pc, cap_pc)
|
|
|
|
}
|
|
|
|
|
|
|
|
eval
|
|
|
|
}
|
|
|
|
|
2024-11-02 16:05:04 -04:00
|
|
|
/// Search the game tree to find the absolute (positive good) move and corresponding eval for the
|
|
|
|
/// current player.
|
2024-10-27 17:34:41 -04:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * board: board position to analyze.
|
|
|
|
/// * depth: how deep to analyze the game tree.
|
|
|
|
/// * alpha: best score (absolute, from current player perspective) guaranteed for current player.
|
|
|
|
/// * beta: best score (absolute, from current player perspective) guaranteed for other player.
|
2024-11-02 16:05:04 -04:00
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
2024-11-02 19:33:29 -04:00
|
|
|
/// The best line (in reverse move order), and its corresponding absolute eval for the current player.
|
2024-11-02 16:05:04 -04:00
|
|
|
fn minmax(
|
|
|
|
board: &mut Board,
|
2024-11-02 21:36:54 -04:00
|
|
|
config: &SearchConfig,
|
2024-11-02 16:05:04 -04:00
|
|
|
depth: usize,
|
|
|
|
alpha: Option<EvalInt>,
|
|
|
|
beta: Option<EvalInt>,
|
2024-11-17 14:02:47 -05:00
|
|
|
cache: &mut TranspositionTableOpt,
|
2024-11-02 19:33:29 -04:00
|
|
|
) -> (Vec<Move>, SearchEval) {
|
2024-10-27 17:34:41 -04:00
|
|
|
// default to worst, then gradually improve
|
|
|
|
let mut alpha = alpha.unwrap_or(EVAL_WORST);
|
|
|
|
// our best is their worst
|
|
|
|
let beta = beta.unwrap_or(EVAL_BEST);
|
|
|
|
|
2024-10-26 19:53:20 -04:00
|
|
|
if depth == 0 {
|
2024-11-15 21:59:15 -05:00
|
|
|
let eval = board.eval() * EvalInt::from(board.turn.sign());
|
2024-11-04 14:08:29 -05:00
|
|
|
return (Vec::new(), SearchEval::Centipawns(eval));
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|
|
|
|
|
2024-11-04 14:08:29 -05:00
|
|
|
let mut mvs: Vec<_> = board
|
|
|
|
.gen_moves()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into_iter()
|
|
|
|
.map(|mv| (move_priority(board, &mv), mv))
|
|
|
|
.collect();
|
2024-11-17 14:02:47 -05:00
|
|
|
|
2024-11-23 19:13:02 -05:00
|
|
|
// get transposition table entry
|
2024-11-17 14:02:47 -05:00
|
|
|
if let Some(cache) = cache {
|
|
|
|
if let Some(entry) = &cache[board.zobrist] {
|
2024-11-23 19:13:02 -05:00
|
|
|
// the entry has a deeper knowledge than we do, so follow its best move exactly instead of
|
|
|
|
// just prioritizing what it thinks is best
|
|
|
|
if entry.depth > depth {
|
|
|
|
mvs.clear();
|
|
|
|
}
|
2024-11-17 14:02:47 -05:00
|
|
|
mvs.push((EVAL_BEST, entry.best_move));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort moves by decreasing priority
|
2024-11-04 14:08:29 -05:00
|
|
|
mvs.sort_unstable_by_key(|mv| -mv.0);
|
2024-10-26 19:53:20 -04:00
|
|
|
|
2024-11-02 16:05:04 -04:00
|
|
|
let mut abs_best = SearchEval::Centipawns(EVAL_WORST);
|
|
|
|
let mut best_move: Option<Move> = None;
|
2024-11-02 19:33:29 -04:00
|
|
|
let mut best_continuation: Vec<Move> = Vec::new();
|
2024-10-26 21:05:51 -04:00
|
|
|
|
|
|
|
if mvs.is_empty() {
|
|
|
|
if board.is_check(board.turn) {
|
2024-11-02 19:33:29 -04:00
|
|
|
return (Vec::new(), SearchEval::Checkmate(-1));
|
2024-10-26 21:05:51 -04:00
|
|
|
} else {
|
|
|
|
// stalemate
|
2024-11-02 19:33:29 -04:00
|
|
|
return (Vec::new(), SearchEval::Centipawns(0));
|
2024-10-26 21:05:51 -04:00
|
|
|
}
|
|
|
|
}
|
2024-10-26 19:53:20 -04:00
|
|
|
|
2024-11-04 14:08:29 -05:00
|
|
|
for (_priority, mv) in mvs {
|
2024-10-26 19:53:20 -04:00
|
|
|
let anti_mv = mv.make(board);
|
2024-11-17 14:02:47 -05:00
|
|
|
let (continuation, score) =
|
|
|
|
minmax(board, config, depth - 1, Some(-beta), Some(-alpha), cache);
|
2024-11-02 16:05:04 -04:00
|
|
|
let abs_score = score.increment();
|
2024-11-02 19:33:29 -04:00
|
|
|
if abs_score > abs_best {
|
2024-11-02 16:05:04 -04:00
|
|
|
abs_best = abs_score;
|
|
|
|
best_move = Some(mv);
|
2024-11-02 19:33:29 -04:00
|
|
|
best_continuation = continuation;
|
2024-11-02 16:05:04 -04:00
|
|
|
}
|
|
|
|
alpha = max(alpha, abs_best.into());
|
2024-10-26 19:53:20 -04:00
|
|
|
anti_mv.unmake(board);
|
2024-11-02 21:36:54 -04:00
|
|
|
if alpha >= beta && config.alpha_beta_on {
|
2024-10-27 17:34:41 -04:00
|
|
|
// alpha-beta prune.
|
|
|
|
//
|
|
|
|
// Beta represents the best eval that the other player can get in sibling branches
|
2024-11-02 19:33:29 -04:00
|
|
|
// (different moves in the parent node). Alpha > beta means the eval here is _worse_
|
2024-10-27 17:34:41 -04:00
|
|
|
// for the other player, so they will never make the move that leads into this branch.
|
|
|
|
// Therefore, we stop evaluating this branch at all.
|
|
|
|
break;
|
|
|
|
}
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|
|
|
|
|
2024-11-17 14:02:47 -05:00
|
|
|
if let Some(best_move) = best_move {
|
|
|
|
best_continuation.push(best_move);
|
|
|
|
if let Some(cache) = cache {
|
2024-11-23 19:13:02 -05:00
|
|
|
cache[board.zobrist] = Some(TranspositionEntry {
|
|
|
|
best_move,
|
|
|
|
eval: abs_best,
|
|
|
|
depth,
|
|
|
|
});
|
2024-11-17 14:02:47 -05:00
|
|
|
}
|
2024-11-02 19:33:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
(best_continuation, abs_best)
|
|
|
|
}
|
|
|
|
|
2024-11-16 15:57:01 -05:00
|
|
|
/// Messages from the interface to the search thread.
|
|
|
|
pub enum InterfaceMsg {
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
|
|
|
type InterfaceRx = mpsc::Receiver<InterfaceMsg>;
|
|
|
|
|
2024-11-17 14:02:47 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct TranspositionEntry {
|
2024-11-23 19:13:02 -05:00
|
|
|
/// best move found last time
|
2024-11-17 14:02:47 -05:00
|
|
|
best_move: Move,
|
2024-11-23 19:13:02 -05:00
|
|
|
/// last time's eval
|
|
|
|
eval: SearchEval,
|
|
|
|
/// depth of this entry
|
|
|
|
depth: usize,
|
2024-11-17 14:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type TranspositionTable = ZobristTable<TranspositionEntry>;
|
|
|
|
type TranspositionTableOpt = Option<TranspositionTable>;
|
|
|
|
|
2024-11-16 15:57:01 -05:00
|
|
|
/// Iteratively deepen search until it is stopped.
|
|
|
|
fn iter_deep(
|
|
|
|
board: &mut Board,
|
|
|
|
config: &SearchConfig,
|
|
|
|
interface: Option<InterfaceRx>,
|
2024-11-17 14:02:47 -05:00
|
|
|
cache: &mut TranspositionTableOpt,
|
2024-11-16 15:57:01 -05:00
|
|
|
) -> (Vec<Move>, SearchEval) {
|
2024-11-23 21:17:20 -05:00
|
|
|
let (mut prev_line, mut prev_eval) = minmax(board, config, 1, None, None, cache);
|
|
|
|
for depth in 2..=config.depth {
|
2024-11-17 14:02:47 -05:00
|
|
|
let (line, eval) = minmax(board, config, depth, None, None, cache);
|
2024-11-23 21:17:20 -05:00
|
|
|
|
2024-11-16 15:57:01 -05:00
|
|
|
if let Some(ref rx) = interface {
|
2024-11-20 12:19:09 -05:00
|
|
|
// don't interrupt a depth 1 search so that there's at least a move to be played
|
|
|
|
if depth != 1 {
|
|
|
|
match rx.try_recv() {
|
|
|
|
Ok(msg) => match msg {
|
2024-11-23 21:17:20 -05:00
|
|
|
InterfaceMsg::Stop => {
|
|
|
|
if depth & 1 == 0 && (EvalInt::from(eval) - EvalInt::from(prev_eval) > 300) {
|
|
|
|
// be skeptical if we move last and we suddenly earn a lot of
|
|
|
|
// centipawns. this may be a sign of horizon problem
|
|
|
|
return (line, eval)
|
|
|
|
} else {
|
|
|
|
return (prev_line, prev_eval)
|
|
|
|
}
|
|
|
|
},
|
2024-11-20 12:19:09 -05:00
|
|
|
},
|
|
|
|
Err(e) => match e {
|
|
|
|
mpsc::TryRecvError::Empty => {}
|
|
|
|
mpsc::TryRecvError::Disconnected => panic!("interface thread stopped"),
|
|
|
|
},
|
|
|
|
}
|
2024-11-16 15:57:01 -05:00
|
|
|
}
|
2024-11-16 21:53:32 -05:00
|
|
|
} else if depth == config.depth - 1 {
|
2024-11-16 21:55:56 -05:00
|
|
|
return (line, eval);
|
2024-11-16 15:57:01 -05:00
|
|
|
}
|
2024-11-23 21:17:20 -05:00
|
|
|
(prev_line, prev_eval) = (line, eval);
|
2024-11-16 15:57:01 -05:00
|
|
|
}
|
2024-11-23 21:17:20 -05:00
|
|
|
(prev_line, prev_eval)
|
2024-11-16 15:57:01 -05:00
|
|
|
}
|
|
|
|
|
2024-11-02 19:33:29 -04:00
|
|
|
/// Find the best line (in reverse order) and its evaluation.
|
2024-11-16 15:57:01 -05:00
|
|
|
pub fn best_line(
|
|
|
|
board: &mut Board,
|
|
|
|
config: Option<SearchConfig>,
|
|
|
|
interface: Option<InterfaceRx>,
|
2024-11-17 14:02:47 -05:00
|
|
|
cache: &mut TranspositionTableOpt,
|
2024-11-16 15:57:01 -05:00
|
|
|
) -> (Vec<Move>, SearchEval) {
|
2024-11-02 21:36:54 -04:00
|
|
|
let config = config.unwrap_or_default();
|
2024-11-17 14:02:47 -05:00
|
|
|
let (line, eval) = iter_deep(board, &config, interface, cache);
|
2024-11-02 19:33:29 -04:00
|
|
|
(line, eval)
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the best move.
|
2024-11-16 15:57:01 -05:00
|
|
|
pub fn best_move(
|
|
|
|
board: &mut Board,
|
|
|
|
config: Option<SearchConfig>,
|
|
|
|
interface: Option<InterfaceRx>,
|
2024-11-17 14:02:47 -05:00
|
|
|
cache: &mut TranspositionTableOpt,
|
2024-11-16 15:57:01 -05:00
|
|
|
) -> Option<Move> {
|
2024-11-17 14:02:47 -05:00
|
|
|
let (line, _eval) = best_line(board, Some(config.unwrap_or_default()), interface, cache);
|
2024-11-02 19:33:29 -04:00
|
|
|
line.last().copied()
|
2024-10-26 19:53:20 -04:00
|
|
|
}
|
2024-11-02 21:36:54 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::fen::{FromFen, ToFen};
|
|
|
|
use crate::movegen::ToUCIAlgebraic;
|
|
|
|
|
|
|
|
/// Theoretically, alpha-beta pruning should not affect the result of minmax.
|
|
|
|
#[test]
|
|
|
|
fn alpha_beta_same_result() {
|
|
|
|
let test_cases = [
|
|
|
|
"r2q1rk1/1bp1pp1p/p2p2p1/1p1P2P1/2n1P3/3Q1P2/PbPBN2P/3RKB1R b K - 5 15",
|
|
|
|
"r1b1k2r/p1qpppbp/1p4pn/2B3N1/1PP1P3/2P5/P4PPP/RN1QR1K1 w kq - 0 14",
|
|
|
|
];
|
|
|
|
for fen in test_cases {
|
|
|
|
let mut board = Board::from_fen(fen).unwrap();
|
|
|
|
let mv_no_prune = best_move(
|
|
|
|
&mut board,
|
|
|
|
Some(SearchConfig {
|
|
|
|
alpha_beta_on: false,
|
|
|
|
depth: 3,
|
|
|
|
}),
|
2024-11-16 15:57:01 -05:00
|
|
|
None,
|
2024-11-17 14:02:47 -05:00
|
|
|
&mut None,
|
2024-11-02 21:36:54 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(board.to_fen(), fen);
|
|
|
|
|
|
|
|
let mv_with_prune = best_move(
|
|
|
|
&mut board,
|
|
|
|
Some(SearchConfig {
|
|
|
|
alpha_beta_on: true,
|
|
|
|
depth: 3,
|
|
|
|
}),
|
2024-11-16 15:57:01 -05:00
|
|
|
None,
|
2024-11-17 14:02:47 -05:00
|
|
|
&mut None,
|
2024-11-02 21:36:54 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(board.to_fen(), fen);
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"without ab prune got {}, otherwise {}, fen {}",
|
|
|
|
mv_no_prune.to_uci_algebraic(),
|
|
|
|
mv_with_prune.to_uci_algebraic(),
|
|
|
|
fen
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(mv_no_prune, mv_with_prune);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|