Use increment from uci winc/binc in time management

This commit is contained in:
stefiosif
2025-01-26 11:00:55 +02:00
parent 2d9076cd1d
commit 7667f7b5d2
8 changed files with 82 additions and 134 deletions

View File

@@ -1,7 +1,6 @@
use std::{
io::{BufRead, Write},
str::SplitWhitespace,
time::Instant,
};
use anyhow::{anyhow, bail};
@@ -13,8 +12,8 @@ use crate::{
},
movegen::r#move::Move,
search::{
iterative_deepening, transposition_table::TranspositionTable, MAX_DEPTH, MAX_TT_SIZE,
REMAINING_TIME_DEFAULT,
iterative_deepening, time::TimeInfo, transposition_table::TranspositionTable, INC,
MAX_DEPTH, MAX_TT_SIZE, TIME,
},
};
@@ -70,28 +69,26 @@ impl fmt::Display for Response {
#[derive(Debug)]
struct UciParameters {
movetime: Option<usize>,
depth: Option<u8>,
game: Option<Game>,
wtime: Option<u128>,
btime: Option<u128>,
winc: Option<u128>,
binc: Option<u128>,
}
impl UciParameters {
const fn new() -> Self {
Self {
movetime: None,
depth: None,
game: None,
wtime: None,
btime: None,
winc: None,
binc: None,
}
}
fn add_movetime(&mut self, movetime: usize) {
self.movetime = Some(movetime);
}
fn add_depth(&mut self, depth: u8) {
self.depth = Some(depth);
}
@@ -104,6 +101,14 @@ impl UciParameters {
self.btime = Some(btime);
}
fn add_winc(&mut self, winc: u128) {
self.winc = Some(winc);
}
fn add_binc(&mut self, binc: u128) {
self.binc = Some(binc);
}
fn add_game(&mut self, game: Game) {
self.game = Some(game);
}
@@ -148,24 +153,24 @@ pub fn uci_go(go_iter: &mut SplitWhitespace, game: &mut Game) -> anyhow::Result<
match subcommand {
"wtime" => params.add_wtime(parse_next(go_iter, "wtime")?),
"btime" => params.add_btime(parse_next(go_iter, "btime")?),
"winc" => params.add_winc(parse_next(go_iter, "winc")?),
"binc" => params.add_binc(parse_next(go_iter, "binc")?),
"depth" => params.add_depth(parse_next(go_iter, "depth")?),
"movetime" => params.add_movetime(parse_next(go_iter, "movetime")?),
_ => (),
}
}
let time = Instant::now();
let remaining_time = match game.current_player() {
Color::White => params.wtime.unwrap_or(REMAINING_TIME_DEFAULT),
Color::Black => params.btime.unwrap_or(REMAINING_TIME_DEFAULT),
let (time, inc) = match game.current_player() {
Color::White => (params.wtime.unwrap_or(TIME), params.winc.unwrap_or(INC)),
Color::Black => (params.btime.unwrap_or(TIME), params.binc.unwrap_or(INC)),
};
iterative_deepening::iterative_deepening(game, MAX_DEPTH, remaining_time)?.ok_or_else(|| {
anyhow!(
"No stored best move found. Time: {}",
remaining_time - time.elapsed().as_millis()
)
})
iterative_deepening::iterative_deepening(
game,
params.depth.unwrap_or(MAX_DEPTH),
&TimeInfo::new(std::time::Instant::now(), time, inc),
)?
.ok_or_else(|| anyhow!("No stored best move found"))
}
fn parse_next<T: std::str::FromStr>(go_iter: &mut SplitWhitespace, val: &str) -> anyhow::Result<T> {
@@ -295,7 +300,6 @@ mod tests {
id author stefiosif\n\
uciok\n\
Clear cache\n\
Initialized position\n\
bestmove e3f2\n";
let actual_response = String::from_utf8(output).expect("Invalid UTF-8 in output");
@@ -303,30 +307,4 @@ mod tests {
Ok(())
}
#[test]
fn test_cute_chess_bug() -> anyhow::Result<()> {
init_attacks();
let commands = "uci\n\
ucinewgame\n\
position startpos moves b1c3 g7g6 d2d4 f8g7 e2e4 f7f5 e4f5 e7e6 f5g6 h7g6 g1f3 b8c6 f1c4 d7d6 c1e3 e6e5 d4d5 c6e7 c3b5 c7c6 d5c6 b7c6 b5d6 d8d6 d1d6 c8d7 f3e5 d7e6 d6e6 g8f6 e5g6 f6g8 e6f7 e8d7 f7g7 g8f6 g7e7 d7c8 g6h8 c8b8 e7d8 b8b7 d8f6 b7c7 e3a7 a8a7 h8g6 c7b6 g6e5 a7c7 f6c6 b6a7
go\n\
quit";
let input = Cursor::new(commands);
let mut output: Vec<_> = vec![];
uci_loop(input, &mut output)?;
let expected_response = "id name zeal\n\
id author stefiosif\n\
uciok\n\
Clear cache\n\
Initialized position\n\
bestmove c6c7\n";
let actual_response = String::from_utf8(output).expect("Invalid UTF-8 in output");
assert_eq!(expected_response, actual_response);
Ok(())
}
}