Skip to content

Commit

Permalink
chore: lots of rearranging
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Jun 6, 2024
1 parent c4901c8 commit 9a7d74e
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 274 deletions.
7 changes: 6 additions & 1 deletion src/commands/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ use std::str::FromStr;

use ataxx::Position;

use crate::mcts::{self, Searcher};

pub struct Context {
pub position: Position,
pub searcher: mcts::Searcher,
}

impl Default for Context {
fn default() -> Self {
let position = Position::from_str("x5o/7/7/7/7/7/o5x x 0 1").unwrap();
Context {
position: Position::from_str("x5o/7/7/7/7/7/o5x x 0 1").unwrap(),
position,
searcher: Searcher::new(position, mcts::policy::handcrafted, mcts::value::material),
}
}
}
108 changes: 19 additions & 89 deletions src/commands/go.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{mem, time};

use uxi::{error, Bundle, Command, Flag, RunError};

use super::Context;
use crate::mcts::{self, Node};
use crate::mcts;

pub fn go() -> Command<Context> {
Command::new(|bundle: Bundle<Context>| {
Expand All @@ -30,19 +28,20 @@ pub fn go() -> Command<Context> {

let ctx = bundle.lock();
let position = ctx.position;
let mut searcher = ctx.searcher.clone();
drop(ctx);

let limits = Limits {
nodes: match bundle.get_single_flag("nodes") {
Some(nodes) => nodes.parse()?,
None => usize::MAX,
let limits = mcts::Limits {
maxnodes: match bundle.get_single_flag("nodes") {
Some(nodes) => Some(nodes.parse()?),
None => None,
},
depth: match bundle.get_single_flag("depth") {
Some(depth) => depth.parse()?,
None => usize::MAX,
maxdepth: match bundle.get_single_flag("depth") {
Some(depth) => Some(depth.parse()?),
None => None,
},
movetime: {
let movetime = match bundle.get_single_flag("movetime") {
let mov_time = match bundle.get_single_flag("movetime") {
Some(movetime) => movetime.parse()?,
None => u128::MAX,
};
Expand All @@ -68,9 +67,11 @@ pub fn go() -> Command<Context> {

let usable_time = (our_time / 20 + our_inc / 2).max(1);

usable_time.min(movetime)
Some(usable_time.min(mov_time))
} else if movetime {
Some(mov_time)
} else {
movetime
None
}
},

Expand All @@ -81,10 +82,14 @@ pub fn go() -> Command<Context> {
};

let mut nodes = 0;
let bestmove = search(position, limits, &mut nodes);
let bestmove = searcher.search(limits, &mut nodes);

println!("bestmove {}", bestmove);

let mut ctx = bundle.lock();
ctx.searcher = searcher;
drop(ctx);

Ok(())
})
// Flags for reporting the current time situation.
Expand All @@ -104,78 +109,3 @@ pub fn go() -> Command<Context> {
// can still respond to and run other Commands while this one is running.
.parallelize(true)
}

#[derive(Debug)]
pub struct Limits {
pub depth: usize,
pub nodes: usize,
pub movetime: u128,
pub movestogo: Option<usize>,
}

pub fn search(position: ataxx::Position, limits: Limits, total_nodes: &mut u64) -> ataxx::Move {
let mut tree = mcts::Tree::new(position);
let start = time::Instant::now();

let mut depth = 0;
let mut seldepth = 0;
let mut cumulative_depth = 0;

loop {
let mut new_depth = 0;
let node = tree.playout(&mut new_depth);

cumulative_depth += new_depth;
if new_depth > seldepth {
seldepth = new_depth;
}

let avg_depth = cumulative_depth / tree.playouts();
if avg_depth > depth {
depth = avg_depth;

let node = tree.node(node);

// Make a new info report.
println!(
"info depth {} seldepth {} score cp {:.0} nodes {} nps {}",
depth,
seldepth,
node.q() * 100.0,
tree.playouts(),
tree.nodes() * 1000 / start.elapsed().as_millis().max(1) as usize
);
}

if tree.playouts() & 127 == 0 {
if start.elapsed().as_millis() >= limits.movetime
|| depth >= limits.depth
|| tree.nodes() >= limits.nodes
{
break;
}

// Hard memory limit to prevent overuse.
// TODO: Fix this by removing old nodes and stuff.
if tree.nodes() > 2_000_000_000 / mem::size_of::<Node>() {
break;
}
}
}

*total_nodes += tree.nodes() as u64;

println!(
"info depth {} seldepth {} score cp {:.0} nodes {} nps {}",
cumulative_depth / tree.playouts(),
seldepth,
100.0,
tree.playouts(),
tree.nodes() * 1000 / start.elapsed().as_millis().max(1) as usize
);

// Verify the tree.
debug_assert!(tree.verify().is_ok());

tree.best_move()
}
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{env, str::FromStr, time};

use commands::{search, Limits};
use uxi::Client;

mod commands;
Expand Down Expand Up @@ -76,14 +74,15 @@ fn main() {
for (i, fen) in BENCH_FENS.iter().enumerate() {
println!("[#{}] {}", i + 1, fen);
let position = ataxx::Position::from_str(fen).unwrap();
let tc = Limits {
nodes: 50000,
depth: 10,
movetime: u128::MAX,
let mut searcher = mcts::Searcher::new(position, mcts::policy::handcrafted, mcts::value::material);
let limits = mcts::Limits {
maxnodes: Some(50000),
maxdepth: Some(10),
movetime: None,
movestogo: None,
};

search(position, tc, &mut total_nodes);
searcher.search(limits, &mut total_nodes);
}
let elapsed = start.elapsed().as_millis();

Expand Down
Loading

0 comments on commit 9a7d74e

Please sign in to comment.