Use labels to break from outer loop, group tunable constants

This commit is contained in:
stefiosif
2025-03-02 01:11:29 +02:00
parent 91345848c6
commit 478ed3511a
3 changed files with 34 additions and 27 deletions

View File

@@ -5,7 +5,10 @@ use crate::{
movegen::r#move::Move,
};
use super::{negamax, time::TimeInfo};
use super::{
negamax, time::TimeInfo, ASPIRATION_WINDOW_DEPTH_THRESHOLD, ASPIRATION_WINDOW_EXPANSION,
ASPIRATION_WINDOW_INITIAL,
};
pub fn iterative_deepening(
game: &mut Game,
@@ -15,51 +18,44 @@ pub fn iterative_deepening(
let mut best_move = None;
let mut best_score = MIN_SCORE;
for depth in 1..=max_depth {
'iterative_deepening: for depth in 1..=max_depth {
if time.exceed_soft_limit() {
println!("Soft limit exceeded in negamax");
return Ok(best_move);
}
let mut nodes = 0;
let mut score;
if depth < 4 {
if depth < ASPIRATION_WINDOW_DEPTH_THRESHOLD {
score = negamax::negamax(game, MIN_SCORE, MAX_SCORE, depth, 0, time, &mut nodes, true);
} else {
let mut window_size = 20;
let mut window_size = ASPIRATION_WINDOW_INITIAL;
let mut alpha = (best_score - window_size).max(MIN_SCORE);
let mut beta = (best_score + window_size).min(MAX_SCORE);
let mut break_from_aw = true;
loop {
'aspiration_windows: loop {
score = negamax::negamax(game, alpha, beta, depth, 0, time, &mut nodes, true);
let score = match score {
Ok(score) => score,
Err(ref e) => {
println!("Error: {}", Response::Info(e.to_string()));
break_from_aw = false;
break;
break 'iterative_deepening;
}
};
if score >= beta {
beta = MAX_SCORE.min(beta.saturating_add(window_size));
window_size = window_size.saturating_mul(2);
window_size = window_size.saturating_mul(ASPIRATION_WINDOW_EXPANSION);
} else if score <= alpha {
alpha = MIN_SCORE.max(alpha.saturating_sub(window_size));
window_size = window_size.saturating_mul(2);
window_size = window_size.saturating_mul(ASPIRATION_WINDOW_EXPANSION);
} else {
best_score = score;
break;
break 'aspiration_windows;
}
}
if !break_from_aw {
break;
}
}
if let Err(e) = score {