feat: exact movetime based time management

This commit is contained in:
dogeystamp 2024-12-28 14:29:05 -05:00
parent caa3bc454c
commit fc8eab4d4b
No known key found for this signature in database
2 changed files with 33 additions and 18 deletions

View File

@ -111,51 +111,56 @@ fn cmd_position(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState
/// Play the game. /// Play the game.
fn cmd_go(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState) { fn cmd_go(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState) {
let mut wtime = 0; let mut wtime: Option<u64> = None;
let mut btime = 0; let mut btime: Option<u64> = None;
let mut movetime: Option<u64> = None;
macro_rules! set_time { macro_rules! set_time {
($color: expr, $var: ident) => { ($var: ident) => {
if let Some(time) = tokens.next() { if let Some(time) = tokens.next() {
if let Ok(time) = time.parse::<u64>() { $var = time.parse::<u64>().ok();
$var = time;
}
} }
}; };
} }
while let Some(token) = tokens.next() { while let Some(token) = tokens.next() {
match token { match token {
"wtime" => { "wtime" => {
set_time!(Color::White, wtime) set_time!(wtime)
} }
"btime" => { "btime" => {
set_time!(Color::Black, btime) set_time!(btime)
}
"movetime" => {
set_time!(movetime)
} }
_ => ignore!(), _ => ignore!(),
} }
} }
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) (wtime, btime)
} else { } else {
(btime, wtime) (btime, wtime)
}; };
if ourtime_ms == 0 { let time_lims = if let Some(movetime) = movetime {
ourtime_ms = 300_000 TimeLimits::from_movetime(movetime)
} } else {
TimeLimits::from_ourtime_theirtime(
ourtime_ms.unwrap_or(300_000),
theirtime_ms.unwrap_or(300_000),
eval_metrics(&state.board),
)
};
state state
.tx_engine .tx_engine
.send(MsgToEngine::Go(Box::new(GoMessage { .send(MsgToEngine::Go(Box::new(GoMessage {
board: state.board, board: state.board,
config: state.config, config: state.config,
time_lims: TimeLimits::from_ourtime_theirtime( time_lims,
ourtime_ms,
theirtime_ms,
eval_metrics(&state.board),
),
}))) })))
.unwrap(); .unwrap();
} }

View File

@ -454,7 +454,7 @@ impl TimeLimits {
ourtime_ms: u64, ourtime_ms: u64,
_theirtime_ms: u64, _theirtime_ms: u64,
eval: EvalMetrics, eval: EvalMetrics,
) -> TimeLimits { ) -> Self {
// hard timeout (max) // hard timeout (max)
let mut hard_ms = 100_000; let mut hard_ms = 100_000;
// soft timeout (default max) // soft timeout (default max)
@ -488,6 +488,16 @@ impl TimeLimits {
soft: Some(soft_limit), soft: Some(soft_limit),
} }
} }
/// Make time limit based on an exact hard limit.
pub fn from_movetime(movetime_ms: u64) -> Self {
let hard_limit = Instant::now() + Duration::from_millis(movetime_ms);
TimeLimits {
hard: Some(hard_limit),
soft: None,
}
}
} }
/// Helper type to avoid retyping the same arguments into every function prototype. /// Helper type to avoid retyping the same arguments into every function prototype.