Add depth based logging
This commit is contained in:
@@ -25,6 +25,7 @@ pub enum Command {
|
|||||||
UciNewGame,
|
UciNewGame,
|
||||||
Position,
|
Position,
|
||||||
Go,
|
Go,
|
||||||
|
Stop,
|
||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +36,7 @@ fn parse_command(parts: &mut SplitWhitespace) -> anyhow::Result<Command> {
|
|||||||
Some("ucinewgame") => Ok(Command::UciNewGame),
|
Some("ucinewgame") => Ok(Command::UciNewGame),
|
||||||
Some("position") => Ok(Command::Position),
|
Some("position") => Ok(Command::Position),
|
||||||
Some("go") => Ok(Command::Go),
|
Some("go") => Ok(Command::Go),
|
||||||
|
Some("stop") => Ok(Command::Stop),
|
||||||
Some("quit") => Ok(Command::Quit),
|
Some("quit") => Ok(Command::Quit),
|
||||||
_ => bail!("Unrecognised command"),
|
_ => bail!("Unrecognised command"),
|
||||||
}
|
}
|
||||||
@@ -47,7 +49,7 @@ pub enum Response {
|
|||||||
Info(String),
|
Info(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_response(handle_out: &mut impl Write, response: &Response) -> anyhow::Result<()> {
|
pub fn write_response(handle_out: &mut impl Write, response: &Response) -> anyhow::Result<()> {
|
||||||
writeln!(handle_out, "{response}").map_err(|e| anyhow!(e))?;
|
writeln!(handle_out, "{response}").map_err(|e| anyhow!(e))?;
|
||||||
handle_out.flush().map_err(|e| anyhow!(e))?;
|
handle_out.flush().map_err(|e| anyhow!(e))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -180,36 +182,43 @@ pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> anyhow::Result
|
|||||||
let line_str = line.unwrap_or_else(|_| "quit".to_string());
|
let line_str = line.unwrap_or_else(|_| "quit".to_string());
|
||||||
let mut parts = line_str.split_whitespace();
|
let mut parts = line_str.split_whitespace();
|
||||||
let command = parse_command(&mut parts)?;
|
let command = parse_command(&mut parts)?;
|
||||||
let response = match command {
|
match command {
|
||||||
Command::Uci => Response::UciOk,
|
Command::Uci => write_response(&mut output, &Response::UciOk)?,
|
||||||
Command::IsReady => Response::ReadyOk,
|
Command::IsReady => write_response(&mut output, &Response::ReadyOk)?,
|
||||||
Command::UciNewGame => {
|
Command::UciNewGame => {
|
||||||
params.game.as_mut().map_or_else(
|
if let Some(game) = params.game.as_mut() {
|
||||||
|| Response::Info("Failed to unwrap from UciParameter".to_string()),
|
game.tt = TranspositionTable::new(MAX_TT_SIZE);
|
||||||
|game| {
|
game.board = Board::startpos();
|
||||||
game.tt = TranspositionTable::new(MAX_TT_SIZE);
|
} else {
|
||||||
game.board = Board::startpos();
|
let game = Game::new();
|
||||||
Response::Info("Initialized new game".to_string())
|
params.add_game(game);
|
||||||
},
|
}
|
||||||
);
|
write_response(&mut output, &Response::Info("Clear cache".to_string()))?;
|
||||||
Response::Info("Clear cache".to_string())
|
|
||||||
}
|
}
|
||||||
Command::Position => {
|
Command::Position => {
|
||||||
|
//TODO: doesnt have to create a new game every time, we can just update the game
|
||||||
params.add_game(uci_position(&mut parts)?);
|
params.add_game(uci_position(&mut parts)?);
|
||||||
Response::Info("Initialized position".to_string())
|
|
||||||
}
|
}
|
||||||
Command::Go => params.game.as_mut().map_or_else(
|
Command::Go => {
|
||||||
|| Response::Info("Failed to unwrap from UciParameter".to_string()),
|
if let Some(game) = params.game.as_mut() {
|
||||||
|game| match uci_go(&mut parts, game) {
|
match uci_go(&mut parts, game) {
|
||||||
Ok(best_move) => Response::BestMove(best_move.parse_into_str()),
|
Ok(best_move) => write_response(
|
||||||
Err(e) => Response::Info(e.to_string()),
|
&mut output,
|
||||||
},
|
&Response::BestMove(best_move.parse_into_str()),
|
||||||
),
|
),
|
||||||
// TODO: Command::Stop => (),
|
Err(e) => write_response(&mut output, &Response::Info(e.to_string())),
|
||||||
|
}?;
|
||||||
|
} else {
|
||||||
|
write_response(
|
||||||
|
&mut output,
|
||||||
|
&Response::Info("Failed to unwrap from UciParameter".to_string()),
|
||||||
|
)?;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Command::Stop => break,
|
||||||
Command::Quit => break,
|
Command::Quit => break,
|
||||||
};
|
};
|
||||||
|
|
||||||
write_response(&mut output, &response)?;
|
|
||||||
output.flush().map_err(|e| anyhow!(e))?
|
output.flush().map_err(|e| anyhow!(e))?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,24 @@ impl fmt::Debug for Move {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Move {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}{}", to_algebraic(self.src), to_algebraic(self.dst))?;
|
||||||
|
|
||||||
|
if let MoveType::Promotion(piece) | MoveType::PromotionCapture(piece) = &self.move_type {
|
||||||
|
let promote_char = match piece {
|
||||||
|
Promote::Knight => 'n',
|
||||||
|
Promote::Bishop => 'b',
|
||||||
|
Promote::Rook => 'r',
|
||||||
|
Promote::Queen => 'q',
|
||||||
|
};
|
||||||
|
write!(f, "{promote_char}")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Move {
|
impl Move {
|
||||||
pub const fn new(src: usize, dst: usize) -> Self {
|
pub const fn new(src: usize, dst: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
board::game::Game,
|
board::game::Game,
|
||||||
evaluation::{MAX_SCORE, MIN_SCORE},
|
evaluation::{MAX_SCORE, MIN_SCORE},
|
||||||
|
interface::uci::{write_response, Response},
|
||||||
movegen::r#move::Move,
|
movegen::r#move::Move,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -22,6 +25,8 @@ pub fn iterative_deepening(
|
|||||||
return Ok(best_move);
|
return Ok(best_move);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut total_nodes_searched = 0;
|
||||||
|
|
||||||
let score = negamax::negamax(
|
let score = negamax::negamax(
|
||||||
game,
|
game,
|
||||||
MIN_SCORE,
|
MIN_SCORE,
|
||||||
@@ -29,6 +34,7 @@ pub fn iterative_deepening(
|
|||||||
depth,
|
depth,
|
||||||
0,
|
0,
|
||||||
&TimeInfo::new(time, remaining_time),
|
&TimeInfo::new(time, remaining_time),
|
||||||
|
&mut total_nodes_searched,
|
||||||
);
|
);
|
||||||
|
|
||||||
if score.is_err() {
|
if score.is_err() {
|
||||||
@@ -37,10 +43,44 @@ pub fn iterative_deepening(
|
|||||||
|
|
||||||
best_score = score?;
|
best_score = score?;
|
||||||
best_move = game.tt.lookup(game.hash).and_then(|entry| entry.mv);
|
best_move = game.tt.lookup(game.hash).and_then(|entry| entry.mv);
|
||||||
|
|
||||||
|
let nps = ((total_nodes_searched * 1_000_000) as u128).div_ceil(time.elapsed().as_micros())
|
||||||
|
as u64;
|
||||||
|
|
||||||
|
write_response(
|
||||||
|
&mut io::stdout(),
|
||||||
|
&search_info(
|
||||||
|
depth,
|
||||||
|
time.elapsed().as_millis() as u64,
|
||||||
|
total_nodes_searched,
|
||||||
|
nps,
|
||||||
|
best_score,
|
||||||
|
best_move,
|
||||||
|
),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
Ok(best_move)
|
Ok(best_move)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn search_info(
|
||||||
|
depth: u8,
|
||||||
|
time: u64,
|
||||||
|
total_nodes_searched: u64,
|
||||||
|
nps: u64,
|
||||||
|
best_score: i32,
|
||||||
|
best_move: Option<Move>,
|
||||||
|
) -> Response {
|
||||||
|
Response::Info(format!(
|
||||||
|
"info depth {} time {} nodes {} nps {} eval {} pv {}",
|
||||||
|
depth,
|
||||||
|
time,
|
||||||
|
total_nodes_searched,
|
||||||
|
nps,
|
||||||
|
best_score,
|
||||||
|
best_move.expect("msg: No best move found")
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ pub fn negamax(
|
|||||||
depth: u8,
|
depth: u8,
|
||||||
plies: u8,
|
plies: u8,
|
||||||
time_info: &TimeInfo,
|
time_info: &TimeInfo,
|
||||||
|
total_nodes_searched: &mut u64,
|
||||||
) -> Result<i32> {
|
) -> Result<i32> {
|
||||||
if hard_limit(&time_info.time, time_info.remaining_time_in_ms) {
|
if hard_limit(&time_info.time, time_info.remaining_time_in_ms) {
|
||||||
bail!("Time is up! In Negamax");
|
bail!("Time is up! In Negamax");
|
||||||
@@ -29,7 +30,8 @@ pub fn negamax(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
let q_score = quiescence(game, alpha, beta, time_info).map_err(|e| anyhow!("{e}"))?;
|
let q_score = quiescence(game, alpha, beta, time_info, total_nodes_searched)
|
||||||
|
.map_err(|e| anyhow!("{e}"))?;
|
||||||
return Ok(q_score);
|
return Ok(q_score);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,15 +51,39 @@ pub fn negamax(
|
|||||||
game.unmake_move();
|
game.unmake_move();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
legal_moves += 1;
|
legal_moves += 1;
|
||||||
|
*total_nodes_searched += 1;
|
||||||
|
|
||||||
let score = if legal_moves == 1 {
|
let score = if legal_moves == 1 {
|
||||||
-negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info)?
|
-negamax(
|
||||||
|
game,
|
||||||
|
-beta,
|
||||||
|
-alpha,
|
||||||
|
depth - 1,
|
||||||
|
plies + 1,
|
||||||
|
time_info,
|
||||||
|
total_nodes_searched,
|
||||||
|
)?
|
||||||
} else {
|
} else {
|
||||||
let mut score = -negamax(game, -alpha - 1, -alpha, depth - 1, plies + 1, time_info)?;
|
let mut score = -negamax(
|
||||||
|
game,
|
||||||
|
-alpha - 1,
|
||||||
|
-alpha,
|
||||||
|
depth - 1,
|
||||||
|
plies + 1,
|
||||||
|
time_info,
|
||||||
|
total_nodes_searched,
|
||||||
|
)?;
|
||||||
if score > alpha && score < beta {
|
if score > alpha && score < beta {
|
||||||
score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info)?;
|
score = -negamax(
|
||||||
|
game,
|
||||||
|
-beta,
|
||||||
|
-alpha,
|
||||||
|
depth - 1,
|
||||||
|
plies + 1,
|
||||||
|
time_info,
|
||||||
|
total_nodes_searched,
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
score
|
score
|
||||||
};
|
};
|
||||||
@@ -110,7 +136,7 @@ mod tests {
|
|||||||
|
|
||||||
let e3f2 = Move::new(Square::E3, Square::F2);
|
let e3f2 = Move::new(Square::E3, Square::F2);
|
||||||
let time_info = TimeInfo::new(std::time::Instant::now(), 1000);
|
let time_info = TimeInfo::new(std::time::Instant::now(), 1000);
|
||||||
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
|
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info, &mut 0)
|
||||||
.expect("Expected a search result");
|
.expect("Expected a search result");
|
||||||
|
|
||||||
assert_eq!(e3f2, game.tt.lookup(game.hash).unwrap().mv.unwrap());
|
assert_eq!(e3f2, game.tt.lookup(game.hash).unwrap().mv.unwrap());
|
||||||
@@ -118,7 +144,7 @@ mod tests {
|
|||||||
let mut game = from_fen(FEN_MATE_IN_1[1]).unwrap();
|
let mut game = from_fen(FEN_MATE_IN_1[1]).unwrap();
|
||||||
|
|
||||||
let e3f2 = Move::new(Square::E3, Square::F2);
|
let e3f2 = Move::new(Square::E3, Square::F2);
|
||||||
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
|
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info, &mut 0)
|
||||||
.expect("Expected a search result");
|
.expect("Expected a search result");
|
||||||
|
|
||||||
assert_eq!(e3f2, game.tt.lookup(game.hash).unwrap().mv.unwrap());
|
assert_eq!(e3f2, game.tt.lookup(game.hash).unwrap().mv.unwrap());
|
||||||
|
|||||||
@@ -7,7 +7,13 @@ use super::{
|
|||||||
time::{hard_limit, TimeInfo},
|
time::{hard_limit, TimeInfo},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32, time_info: &TimeInfo) -> Result<i32> {
|
pub fn quiescence(
|
||||||
|
game: &mut Game,
|
||||||
|
mut alpha: i32,
|
||||||
|
beta: i32,
|
||||||
|
time_info: &TimeInfo,
|
||||||
|
total_nodes_searched: &mut u64,
|
||||||
|
) -> Result<i32> {
|
||||||
if hard_limit(&time_info.time, time_info.remaining_time_in_ms) {
|
if hard_limit(&time_info.time, time_info.remaining_time_in_ms) {
|
||||||
bail!("Time is up! In Quiescence");
|
bail!("Time is up! In Quiescence");
|
||||||
}
|
}
|
||||||
@@ -36,11 +42,11 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32, time_info: &TimeIn
|
|||||||
game.unmake_move();
|
game.unmake_move();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
*total_nodes_searched += 1;
|
||||||
|
|
||||||
let score = -quiescence(game, -beta, -alpha, time_info)?;
|
let score = -quiescence(game, -beta, -alpha, time_info, &mut 0)?;
|
||||||
game.unmake_move();
|
game.unmake_move();
|
||||||
|
|
||||||
|
|
||||||
if score > best_score {
|
if score > best_score {
|
||||||
best_score = score;
|
best_score = score;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ mod tests {
|
|||||||
let mut game = from_fen(FEN).unwrap();
|
let mut game = from_fen(FEN).unwrap();
|
||||||
let time_info = TimeInfo::new(std::time::Instant::now(), 30000);
|
let time_info = TimeInfo::new(std::time::Instant::now(), 30000);
|
||||||
|
|
||||||
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
|
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info, &mut 0)
|
||||||
.expect("Expected a search result");
|
.expect("Expected a search result");
|
||||||
|
|
||||||
dbg!(time_info.time.elapsed());
|
dbg!(time_info.time.elapsed());
|
||||||
|
|||||||
Reference in New Issue
Block a user