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.
fn cmd_go(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState) {
let mut wtime = 0;
let mut btime = 0;
let mut wtime: Option<u64> = None;
let mut btime: Option<u64> = None;
let mut movetime: Option<u64> = None;
macro_rules! set_time {
($color: expr, $var: ident) => {
($var: ident) => {
if let Some(time) = tokens.next() {
if let Ok(time) = time.parse::<u64>() {
$var = time;
}
$var = time.parse::<u64>().ok();
}
};
}
while let Some(token) = tokens.next() {
match token {
"wtime" => {
set_time!(Color::White, wtime)
set_time!(wtime)
}
"btime" => {
set_time!(Color::Black, btime)
set_time!(btime)
}
"movetime" => {
set_time!(movetime)
}
_ => 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)
} else {
(btime, wtime)
};
if ourtime_ms == 0 {
ourtime_ms = 300_000
}
let time_lims = if let Some(movetime) = movetime {
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
.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,
})))
.unwrap();
}

View File

@ -454,7 +454,7 @@ impl TimeLimits {
ourtime_ms: u64,
_theirtime_ms: u64,
eval: EvalMetrics,
) -> TimeLimits {
) -> Self {
// hard timeout (max)
let mut hard_ms = 100_000;
// soft timeout (default max)
@ -488,6 +488,16 @@ impl TimeLimits {
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.