Compare commits

..

No commits in common. "b0a662460a35c4cc8e9df15fcc741199c5b6a1ad" and "e44cc0586e14de138e74d0cf095f8e8a9a1b982d" have entirely different histories.

4 changed files with 35 additions and 63 deletions

View File

@ -69,7 +69,7 @@ impl From<Color> for char {
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Piece {
enum Piece {
Rook,
Bishop,
Knight,

View File

@ -135,26 +135,18 @@ fn cmd_go(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState) {
}
}
let (mut ourtime_ms, theirtime_ms) = if state.board.get_turn() == Color::White {
let (ourtime_ms, theirtime_ms) = if state.board.get_turn() == Color::White {
(wtime, btime)
} else {
(btime, wtime)
};
if ourtime_ms == 0 {
ourtime_ms = 300_000
}
state
.tx_engine
.send(MsgToEngine::Go(Box::new(GoMessage {
board: state.board,
config: state.config,
time_lims: TimeLimits::from_ourtime_theirtime(
ourtime_ms,
theirtime_ms,
eval_metrics(&state.board),
),
time_lims: TimeLimits::from_ourtime_theirtime(ourtime_ms, theirtime_ms),
})))
.unwrap();
}
@ -225,7 +217,7 @@ fn outp_bestmove(bestmove: MsgBestmove) {
);
match bestmove.eval {
SearchEval::Checkmate(n) => println!("info score mate {}", n / 2),
SearchEval::Exact(eval) | SearchEval::Lower(eval) | SearchEval::Upper(eval) => {
SearchEval::Centipawns(eval) => {
println!("info score cp {}", eval,)
}
SearchEval::Stopped => {

View File

@ -13,9 +13,9 @@ Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
//! Prelude that you can import entirely to use the library conveniently.
pub use crate::eval::{eval_metrics, EvalMetrics, EvalInt, Eval};
pub use crate::eval::{eval_metrics, EvalMetrics};
pub use crate::fen::{FromFen, ToFen};
pub use crate::movegen::{FromUCIAlgebraic, Move, MoveGen, ToUCIAlgebraic};
pub use crate::search::{best_line, best_move, SearchEval, TranspositionTable, EngineState, SearchConfig, TimeLimits};
pub use crate::{Board, Color, BOARD_HEIGHT, BOARD_WIDTH, N_COLORS, N_PIECES, N_SQUARES, ColPiece, Piece};
pub use crate::{Board, Color, BOARD_HEIGHT, BOARD_WIDTH, N_COLORS, N_PIECES, N_SQUARES};
pub use crate::coordination::{UCIMode, UCIModeTransition, UCIModeMachine, MsgBestmove, MsgToMain, MsgToEngine, GoMessage};

View File

@ -13,11 +13,14 @@ Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
//! Game-tree search.
use crate::coordination::MsgToEngine;
use crate::eval::{Eval, EvalInt};
use crate::hash::ZobristTable;
use crate::prelude::*;
use crate::movegen::{Move, MoveGen};
use crate::{Board, Piece};
use std::cmp::{max, min};
use std::sync::mpsc;
use std::time::{Duration, Instant};
use std::time::{Instant, Duration};
// min can't be represented as positive
const EVAL_WORST: EvalInt = -(EvalInt::MAX);
@ -40,12 +43,8 @@ mod test_eval_int {
pub enum SearchEval {
/// Mate in |n| - 1 half moves, negative for own mate.
Checkmate(i8),
/// Centipawn score (exact).
Exact(EvalInt),
/// Centipawn score (lower bound).
Lower(EvalInt),
/// Centipawn score (upper bound).
Upper(EvalInt),
/// Centipawn score.
Centipawns(EvalInt),
/// Search was hard-stopped.
Stopped,
}
@ -62,9 +61,7 @@ impl SearchEval {
Self::Checkmate(-(n + 1))
}
}
SearchEval::Exact(eval) => Self::Exact(-eval),
SearchEval::Lower(eval) => Self::Upper(-eval),
SearchEval::Upper(eval) => Self::Lower(-eval),
SearchEval::Centipawns(eval) => Self::Centipawns(-eval),
SearchEval::Stopped => SearchEval::Stopped,
}
}
@ -81,9 +78,7 @@ impl From<SearchEval> for EvalInt {
EVAL_BEST - EvalInt::from(n)
}
}
SearchEval::Exact(eval) => eval,
SearchEval::Lower(eval) => eval,
SearchEval::Upper(eval) => eval,
SearchEval::Centipawns(eval) => eval,
SearchEval::Stopped => panic!("Attempted to evaluate a halted search"),
}
}
@ -128,6 +123,14 @@ impl Default for SearchConfig {
}
}
/// 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];
@ -135,23 +138,14 @@ fn lvv_mva_eval(src_pc: Piece, cap_pc: Piece) -> EvalInt {
}
/// Assign a priority to a move based on how promising it is.
fn move_priority(board: &mut Board, mv: &Move, state: &mut EngineState) -> EvalInt {
fn move_priority(board: &mut Board, mv: &Move) -> EvalInt {
// move eval
let mut eval: EvalInt = 0;
let src_pc = board.get_piece(mv.src).unwrap();
let anti_mv = mv.make(board);
if state.config.enable_trans_table {
if let Some(entry) = &state.cache[board.zobrist] {
eval = entry.eval.into();
}
} else if let Some(cap_pc) = anti_mv.cap {
if let Some((src_pc, cap_pc)) = move_get_capture(board, mv) {
// least valuable victim, most valuable attacker
eval += lvv_mva_eval(src_pc.into(), cap_pc)
eval += lvv_mva_eval(src_pc, cap_pc)
}
anti_mv.unmake(board);
eval
}
@ -206,7 +200,7 @@ fn minmax(
if depth == 0 {
let eval = board.eval() * EvalInt::from(board.turn.sign());
return (Vec::new(), SearchEval::Exact(eval));
return (Vec::new(), SearchEval::Centipawns(eval));
}
let mut mvs: Vec<_> = board
@ -214,17 +208,17 @@ fn minmax(
.into_iter()
.collect::<Vec<_>>()
.into_iter()
.map(|mv| (move_priority(board, &mv, state), mv))
.map(|mv| (move_priority(board, &mv), mv))
.collect();
// get transposition table entry
if state.config.enable_trans_table {
if let Some(entry) = &state.cache[board.zobrist] {
// 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 {
if let SearchEval::Exact(_) | SearchEval::Upper(_) = entry.eval {
// no point looking for a better move
return (vec![entry.best_move], entry.eval);
}
// we don't save PV line in transposition table, so no information on that
return (vec![entry.best_move], entry.eval);
}
mvs.push((EVAL_BEST, entry.best_move));
}
@ -233,7 +227,7 @@ fn minmax(
// sort moves by decreasing priority
mvs.sort_unstable_by_key(|mv| -mv.0);
let mut abs_best = SearchEval::Exact(EVAL_WORST);
let mut abs_best = SearchEval::Centipawns(EVAL_WORST);
let mut best_move: Option<Move> = None;
let mut best_continuation: Vec<Move> = Vec::new();
@ -242,7 +236,7 @@ fn minmax(
return (Vec::new(), SearchEval::Checkmate(-1));
} else {
// stalemate
return (Vec::new(), SearchEval::Exact(0));
return (Vec::new(), SearchEval::Centipawns(0));
}
}
@ -270,9 +264,6 @@ fn minmax(
// (different moves in the parent node). Alpha > beta means the eval here is _worse_
// for the other player, so they will never make the move that leads into this branch.
// Therefore, we stop evaluating this branch at all.
if let SearchEval::Upper(eval) | SearchEval::Exact(eval) = abs_best {
abs_best = SearchEval::Lower(eval);
}
break;
}
}
@ -354,23 +345,12 @@ pub struct TimeLimits {
impl TimeLimits {
/// Make time limits based on wtime, btime (but color-independent).
///
/// Also takes in eval metrics, for instance to avoid wasting too much time in the opening.
pub fn from_ourtime_theirtime(
ourtime_ms: u64,
_theirtime_ms: u64,
eval: EvalMetrics,
) -> TimeLimits {
pub fn from_ourtime_theirtime(ourtime_ms: u64, _theirtime_ms: u64) -> TimeLimits {
// hard timeout (max)
let mut hard_ms = 100_000;
// soft timeout (max)
let mut soft_ms = 1_200;
// if we have more than 5 minutes, and we're out of the opening, we can afford to think longer
if ourtime_ms > 300_000 && eval.phase <= 13 {
soft_ms = 4_500
}
let factor = if ourtime_ms > 5_000 { 10 } else { 40 };
hard_ms = min(ourtime_ms / factor, hard_ms);
soft_ms = min(ourtime_ms / 50, soft_ms);