Add depth based time limit

This commit is contained in:
stefiosif
2025-01-27 22:20:45 +02:00
parent a01310c7b7
commit b4f61f6504
4 changed files with 12 additions and 8 deletions

View File

@@ -17,7 +17,7 @@ pub fn iterative_deepening(
let mut best_move = None; let mut best_move = None;
for depth in 1..=max_depth { for depth in 1..=max_depth {
if time.exceed_hard_limit() { if time.exceed_soft_limit() {
return Ok(best_move); return Ok(best_move);
} }

View File

@@ -9,6 +9,6 @@ pub mod transposition_table;
pub const MAX_DEPTH: u8 = 50; pub const MAX_DEPTH: u8 = 50;
pub const TIME: u128 = 1000; pub const TIME: u128 = 1000;
pub const INC: u128 = 1000; pub const INC: u128 = 1000;
pub const HARD_LIMIT_DIVISION: u128 = 10; pub const HARD_LIMIT_DIVISION: u128 = 3;
pub const SOFT_LIMIT_DIVISION: u128 = HARD_LIMIT_DIVISION * 2; pub const SOFT_LIMIT_DIVISION: u128 = 20;
pub const MAX_TT_SIZE: u64 = 750000; pub const MAX_TT_SIZE: u64 = 3000000;

View File

@@ -9,9 +9,9 @@ pub fn quiescence(
mut alpha: i32, mut alpha: i32,
beta: i32, beta: i32,
time: &TimeInfo, time: &TimeInfo,
total_nodes_searched: &mut u64, nodes: &mut u64,
) -> Result<i32> { ) -> Result<i32> {
if time.exceed_hard_limit() { if *nodes & 1024 == 0 && time.exceed_hard_limit() {
bail!("Hard limit exceeded in quiescence"); bail!("Hard limit exceeded in quiescence");
} }
@@ -52,7 +52,7 @@ pub fn quiescence(
game.unmake_move(); game.unmake_move();
continue; continue;
} }
*total_nodes_searched += 1; *nodes += 1;
let score = -quiescence(game, -beta, -alpha, time, &mut 0)?; let score = -quiescence(game, -beta, -alpha, time, &mut 0)?;
game.unmake_move(); game.unmake_move();

View File

@@ -1,6 +1,6 @@
use std::time::Instant; use std::time::Instant;
use super::HARD_LIMIT_DIVISION; use super::{HARD_LIMIT_DIVISION, SOFT_LIMIT_DIVISION};
pub struct TimeInfo { pub struct TimeInfo {
pub instant: Instant, pub instant: Instant,
@@ -20,4 +20,8 @@ impl TimeInfo {
pub fn exceed_hard_limit(&self) -> bool { pub fn exceed_hard_limit(&self) -> bool {
self.instant.elapsed().as_millis() >= self.time / HARD_LIMIT_DIVISION + self.inc / 2 self.instant.elapsed().as_millis() >= self.time / HARD_LIMIT_DIVISION + self.inc / 2
} }
pub fn exceed_soft_limit(&self) -> bool {
self.instant.elapsed().as_millis() >= self.time / SOFT_LIMIT_DIVISION + self.inc / 2
}
} }