Skip to content

Commit a3529fb

Browse files
committed
Added first depth multi threading to the bot
1 parent 26a7c7a commit a3529fb

File tree

17 files changed

+537
-702
lines changed

17 files changed

+537
-702
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,4 @@ tmp/
193193
*.csv
194194
*.html
195195
*zip
196+
pgn/

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ once_cell = "1.21.3"
2828
tar = "0.4.44"
2929
rand = "*"
3030
lru = "*"
31+
rayon = "*"

bots/mystic-bot

1010 KB
Binary file not shown.

homemade.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(self, *args, **kwargs):
110110
self.timeout = 60 # Not used
111111
self.timeRemaining = 0
112112
self.fenVals = []
113-
self.server_url = "http://localhost:8080"
113+
self.server_url = "http://localhost:2832"
114114

115115
def set_chess_board(self, board: chess.Board):
116116
self.chessBoard = board

notebooks/benchmark.ipynb

Lines changed: 140 additions & 87 deletions
Large diffs are not rendered by default.

notebooks/botBattle.ipynb

Lines changed: 0 additions & 334 deletions
This file was deleted.

src/api/get/get_eval.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::{ extract::{ State, Json }, response::IntoResponse, http::StatusCode };
22
use serde::{ Deserialize, Serialize };
3-
use std::{ str::FromStr, sync::Arc};
3+
use std::{ str::FromStr, sync::Arc, time::Instant };
44
use chess::Board;
55
use crate::bot::{
66
algorithm::root::search,
@@ -17,30 +17,32 @@ use crate::bot::{
1717
pub struct EvalRequest {
1818
pub current_fen: String,
1919
pub history: Vec<String>,
20-
time_left_ms: u128,
21-
time_limit_ms: Option<u128>,
20+
pub time_left_ms: u128,
21+
pub time_limit_ms: Option<u128>,
2222
}
2323

2424
#[derive(Debug, Serialize)]
2525
pub struct BestMoveResponse {
26-
pub best_move: Option<String>,
27-
pub eval: i32,
28-
pub nodes: u64,
29-
pub time: u128,
30-
pub depth: u8,
26+
pub best_move: String,
27+
pub line: Vec<String>, // Full principal variation in UCI format
28+
pub eval: i32, // Evaluation score
29+
pub nodes: u64, // Total nodes searched
30+
pub time: u128, // Time taken in milliseconds
31+
pub depth: u8, // Maximum search depth reached
3132
}
3233

3334
pub async fn eval_position_handler(
3435
State(state): State<ServerState>,
3536
Json(payload): Json<EvalRequest>
3637
) -> impl IntoResponse {
3738
let current_board = match Board::from_str(&payload.current_fen) {
38-
Ok(b) => b,
39+
Ok(board) => board,
3940
Err(_) => {
4041
return (
4142
StatusCode::BAD_REQUEST,
4243
Json(BestMoveResponse {
43-
best_move: None,
44+
best_move: String::new(),
45+
line: vec![],
4446
eval: 0,
4547
nodes: 0,
4648
time: 0,
@@ -50,10 +52,11 @@ pub async fn eval_position_handler(
5052
}
5153
};
5254

55+
// Reconstruct repetition history
5356
let mut history = RepetitionHistory::new();
5457
for fen in &payload.history {
55-
if let Ok(board) = Board::from_str(fen) {
56-
let hash = board.get_hash();
58+
if let Ok(past_board) = Board::from_str(fen) {
59+
let hash = past_board.get_hash();
5760
history.increment(hash);
5861
}
5962
}
@@ -69,18 +72,26 @@ pub async fn eval_position_handler(
6972
transposition_table,
7073
};
7174

75+
let start_time = Instant::now();
7276
let board = engine.current_board.clone();
73-
let (best_move, nodes, time_taken_ms, eval, depth) = search(
77+
let (line, nodes, _, eval, depth) = search(
7478
payload.time_left_ms,
7579
payload.time_limit_ms,
7680
&board,
7781
&mut engine
7882
);
83+
let time_taken_ms = start_time.elapsed().as_millis();
84+
85+
let best_move_str = line.first().map_or(String::new(), |m| m.to_string());
7986

8087
(
8188
StatusCode::OK,
8289
Json(BestMoveResponse {
83-
best_move: best_move.map(|m| m.to_string()),
90+
best_move: best_move_str,
91+
line: line
92+
.iter()
93+
.map(|m| m.to_string())
94+
.collect(),
8495
eval,
8596
nodes,
8697
time: time_taken_ms,

src/api/post/best_move.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
use axum::{ extract::State, http::StatusCode, response::IntoResponse, Json };
1+
use axum::{ extract::{ State, Json }, http::StatusCode, response::IntoResponse };
22
use serde::{ Deserialize, Serialize };
3-
use std::{ time::Instant };
3+
use std::time::Instant;
44
use crate::bot::{ algorithm::root::search, include::types::{ ServerState, Statistics } };
55

66
#[derive(Debug, Deserialize)]
77
pub struct BestMoveQuery {
8-
game_id: String,
9-
time_left_ms: u128,
10-
time_limit_ms: Option<u128>,
11-
update_state: Option<bool>,
8+
pub game_id: String,
9+
pub time_left_ms: u128,
10+
pub time_limit_ms: Option<u128>,
11+
pub update_state: Option<bool>,
1212
}
1313

1414
#[derive(Debug, Serialize)]
1515
pub struct BestMoveResponse {
16-
best_move: Option<String>,
17-
eval: i32,
18-
nodes: u64,
19-
time: u128,
20-
depth: u8,
21-
new_position: String,
16+
pub best_move: String,
17+
pub line: Vec<String>, // Full principal variation
18+
pub eval: i32,
19+
pub nodes: u64,
20+
pub time: u128,
21+
pub depth: u8,
22+
pub new_position: String,
2223
}
2324

2425
pub async fn best_move_handler(
@@ -29,7 +30,8 @@ pub async fn best_move_handler(
2930
return (
3031
StatusCode::NOT_FOUND,
3132
Json(BestMoveResponse {
32-
best_move: None,
33+
best_move: String::new(),
34+
line: vec![],
3335
eval: 0,
3436
nodes: 0,
3537
time: 0,
@@ -42,27 +44,28 @@ pub async fn best_move_handler(
4244
let now = Instant::now();
4345
let board = engine.current_board.clone();
4446

45-
let (best_move, nodes, time, eval, depth) = search(
47+
// search returns (Vec<ChessMove>, u64, u128, i32, u8)
48+
let (line, nodes, _, eval, depth) = search(
4649
params.time_left_ms,
4750
params.time_limit_ms,
4851
&board,
4952
&mut engine
5053
);
51-
5254
let time_taken_ms = now.elapsed().as_millis();
5355

5456
let mut new_position = engine.current_board.to_string();
55-
// Update engine state statistics if requested
57+
58+
let best_move_str = line.first().map_or(String::new(), |m| m.to_string());
59+
5660
if params.update_state.unwrap_or(false) {
57-
// Update cumulative statistics under a fixed key (e.g., 0)
5861
let key = engine.current_board.get_hash();
5962
engine.statistics.entry(key).or_insert(Statistics {
6063
nodes_explored: nodes,
61-
time_taken_ms: time_taken_ms,
64+
time_taken_ms,
6265
});
63-
if let Some(best) = best_move {
64-
// Update the current board with the selected move
65-
let new_board = engine.current_board.make_move_new(best);
66+
67+
if let Some(first_move) = line.first() {
68+
let new_board = engine.current_board.make_move_new(*first_move);
6669
engine.current_board = new_board;
6770
new_position = engine.current_board.to_string();
6871
}
@@ -71,10 +74,14 @@ pub async fn best_move_handler(
7174
(
7275
StatusCode::OK,
7376
Json(BestMoveResponse {
74-
best_move: best_move.map(|m| m.to_string()),
77+
best_move: best_move_str,
78+
line: line
79+
.iter()
80+
.map(|m| m.to_string())
81+
.collect(),
7582
eval,
7683
nodes,
77-
time,
84+
time: time_taken_ms,
7885
depth,
7986
new_position,
8087
}),

0 commit comments

Comments
 (0)