stub: hard time limit
This commit is contained in:
parent
b25d21f462
commit
82847e32be
37
src/main.rs
37
src/main.rs
@ -34,12 +34,10 @@ Copyright © 2024 dogeystamp <dogeystamp@disroot.org>
|
|||||||
//! "receive at Main" respectively. These names would be used for one channel.
|
//! "receive at Main" respectively. These names would be used for one channel.
|
||||||
|
|
||||||
use chess_inator::prelude::*;
|
use chess_inator::prelude::*;
|
||||||
use std::cmp::min;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
/// UCI protocol says to ignore any unknown words.
|
/// UCI protocol says to ignore any unknown words.
|
||||||
///
|
///
|
||||||
@ -112,21 +110,14 @@ 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) {
|
||||||
// hard timeout
|
let mut wtime = 0;
|
||||||
let mut hard_ms = 100_000;
|
let mut btime = 0;
|
||||||
// soft timeout
|
|
||||||
let mut soft_ms = 1_200;
|
|
||||||
|
|
||||||
macro_rules! set_time {
|
macro_rules! set_time {
|
||||||
() => {
|
($color: expr, $var: ident) => {
|
||||||
if state.board.get_turn() == Color::White {
|
if let Some(time) = tokens.next() {
|
||||||
if let Some(time) = tokens.next() {
|
if let Ok(time) = time.parse::<u64>() {
|
||||||
if let Ok(time) = time.parse::<u64>() {
|
$var = time;
|
||||||
let factor = if (time > 20_000) { 10 } else { 40 };
|
|
||||||
hard_ms = min(time / factor, hard_ms);
|
|
||||||
|
|
||||||
soft_ms = min(time / 50, soft_ms);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -135,27 +126,27 @@ fn cmd_go(mut tokens: std::str::SplitWhitespace<'_>, state: &mut MainState) {
|
|||||||
while let Some(token) = tokens.next() {
|
while let Some(token) = tokens.next() {
|
||||||
match token {
|
match token {
|
||||||
"wtime" => {
|
"wtime" => {
|
||||||
set_time!()
|
set_time!(Color::White, wtime)
|
||||||
}
|
}
|
||||||
"btime" => {
|
"btime" => {
|
||||||
set_time!()
|
set_time!(Color::Black, btime)
|
||||||
}
|
}
|
||||||
_ => ignore!(),
|
_ => ignore!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let hard_limit = Instant::now() + Duration::from_millis(hard_ms);
|
let (ourtime_ms, theirtime_ms) = if state.board.get_turn() == Color::White {
|
||||||
let soft_limit = Instant::now() + Duration::from_millis(soft_ms);
|
(wtime, btime)
|
||||||
|
} else {
|
||||||
|
(btime, wtime)
|
||||||
|
};
|
||||||
|
|
||||||
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 {
|
time_lims: TimeLimits::from_ourtime_theirtime(ourtime_ms, theirtime_ms),
|
||||||
hard: None,
|
|
||||||
soft: Some(soft_limit),
|
|
||||||
},
|
|
||||||
})))
|
})))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,9 @@ use crate::eval::{Eval, EvalInt};
|
|||||||
use crate::hash::ZobristTable;
|
use crate::hash::ZobristTable;
|
||||||
use crate::movegen::{Move, MoveGen};
|
use crate::movegen::{Move, MoveGen};
|
||||||
use crate::{Board, Piece};
|
use crate::{Board, Piece};
|
||||||
use std::cmp::max;
|
use std::cmp::{max, min};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::time::Instant;
|
use std::time::{Instant, Duration};
|
||||||
|
|
||||||
// min can't be represented as positive
|
// min can't be represented as positive
|
||||||
const EVAL_WORST: EvalInt = -(EvalInt::MAX);
|
const EVAL_WORST: EvalInt = -(EvalInt::MAX);
|
||||||
@ -343,6 +343,28 @@ pub struct TimeLimits {
|
|||||||
pub soft: Option<Instant>,
|
pub soft: Option<Instant>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TimeLimits {
|
||||||
|
/// Make time limits based on wtime, btime (but color-independent).
|
||||||
|
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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
let hard_limit = Instant::now() + Duration::from_millis(hard_ms);
|
||||||
|
let soft_limit = Instant::now() + Duration::from_millis(soft_ms);
|
||||||
|
|
||||||
|
TimeLimits {
|
||||||
|
hard: Some(hard_limit),
|
||||||
|
soft: Some(soft_limit),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Helper type to avoid retyping the same arguments into every function prototype.
|
/// Helper type to avoid retyping the same arguments into every function prototype.
|
||||||
///
|
///
|
||||||
/// This should be owned outside the actual thinking part so that the engine can remember state
|
/// This should be owned outside the actual thinking part so that the engine can remember state
|
||||||
|
Loading…
Reference in New Issue
Block a user