Make mvv_lva to map pieces to their material score instead of abritrary matrix
Add time limits, use anyhow crate to improve error handling, remove depth limit on quiescence search
This commit is contained in:
@@ -7,8 +7,8 @@ use crate::{
|
||||
};
|
||||
|
||||
use super::{
|
||||
negamax, transposition_table::TranspositionTable, HARD_LIMIT_DIVISION, SOFT_EVAL_THRESHOLD,
|
||||
SOFT_LIMIT_DIVISION,
|
||||
negamax, time::TimeInfo, transposition_table::TranspositionTable, HARD_LIMIT_DIVISION,
|
||||
SOFT_EVAL_THRESHOLD, SOFT_LIMIT_DIVISION,
|
||||
};
|
||||
|
||||
pub fn iterative_deepening(
|
||||
@@ -16,30 +16,47 @@ pub fn iterative_deepening(
|
||||
max_depth: u8,
|
||||
remaining_time: u128,
|
||||
tt: &mut TranspositionTable,
|
||||
) -> Option<Move> {
|
||||
let (mut best_move, mut best_eval): (Option<Move>, i32) = (None, MIN_SCORE);
|
||||
let time_now = std::time::Instant::now();
|
||||
) -> anyhow::Result<Option<Move>> {
|
||||
let (mut best_move, mut best_score) = (None, MIN_SCORE);
|
||||
let time = std::time::Instant::now();
|
||||
|
||||
for depth in 1..=max_depth {
|
||||
if time_limit_reached(&time_now, remaining_time, best_eval) {
|
||||
return best_move;
|
||||
if time_limit_reached(&time, remaining_time, best_score) {
|
||||
return Ok(best_move);
|
||||
}
|
||||
|
||||
(best_move, best_eval) = negamax::negamax(game, MIN_SCORE, MAX_SCORE, depth, 0, tt);
|
||||
let search_result = negamax::negamax(
|
||||
game,
|
||||
MIN_SCORE,
|
||||
MAX_SCORE,
|
||||
depth,
|
||||
0,
|
||||
&TimeInfo::new(time, remaining_time),
|
||||
tt,
|
||||
);
|
||||
|
||||
if let Ok(search_result) = search_result {
|
||||
if search_result.best_move.is_some() {
|
||||
best_move = search_result.best_move;
|
||||
}
|
||||
best_score = search_result.best_score;
|
||||
} else {
|
||||
return Ok(best_move);
|
||||
}
|
||||
}
|
||||
|
||||
best_move
|
||||
Ok(best_move)
|
||||
}
|
||||
|
||||
fn time_limit_reached(time: &Instant, remaining_time: u128, eval: i32) -> bool {
|
||||
hard_limit(time, remaining_time) || soft_limit(time, remaining_time, eval)
|
||||
}
|
||||
|
||||
fn hard_limit(time_now: &Instant, remaining_time: u128) -> bool {
|
||||
pub fn hard_limit(time_now: &Instant, remaining_time: u128) -> bool {
|
||||
time_now.elapsed().as_millis() >= remaining_time / HARD_LIMIT_DIVISION
|
||||
}
|
||||
|
||||
fn soft_limit(time: &Instant, remaining_time: u128, eval: i32) -> bool {
|
||||
pub fn soft_limit(time: &Instant, remaining_time: u128, eval: i32) -> bool {
|
||||
time.elapsed().as_millis() >= remaining_time / SOFT_LIMIT_DIVISION && eval > SOFT_EVAL_THRESHOLD
|
||||
}
|
||||
|
||||
@@ -57,9 +74,9 @@ mod tests {
|
||||
const FEN: &str = "1r2k2r/2P1pq1p/2npb3/1p3ppP/p3P3/P2B1Q2/1P1PNPP1/R3K2R w KQk g6 0 1";
|
||||
|
||||
#[test]
|
||||
fn test_iterative_deepening() -> Result<(), String> {
|
||||
fn test_iterative_deepening() -> anyhow::Result<()> {
|
||||
init_attacks();
|
||||
let mut game = from_fen(FEN)?;
|
||||
let mut game = from_fen(FEN).unwrap();
|
||||
let mut tt = TranspositionTable::new(MAX_TT_SIZE);
|
||||
let time_now = std::time::Instant::now();
|
||||
|
||||
@@ -68,7 +85,7 @@ mod tests {
|
||||
MAX_DEPTH,
|
||||
REMAINING_TIME_DEFAULT,
|
||||
&mut tt,
|
||||
);
|
||||
)?;
|
||||
|
||||
dbg!(time_now.elapsed());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user