change handling of current_depth

This commit is contained in:
Simon Gardling 2025-04-22 15:49:26 -04:00
parent 0d81dca8df
commit cf21f981e5
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -24,9 +24,6 @@ pub struct FutureMoves {
/// Index of the [`Move`] tree's root node
current_root: Option<usize>,
/// Current generated depth of the Arena
current_depth: usize,
/// Color w.r.t
agent_color: Piece,
@ -110,7 +107,6 @@ impl FutureMoves {
Self {
arena: Vec::new(),
current_root: None,
current_depth: 0,
agent_color,
config,
board: Board::new(),
@ -139,30 +135,27 @@ impl FutureMoves {
indexes
}
/// Find the current depth of the arena by
/// looking at leaf moves and finding the smallest value
fn determine_current_depth(&self) -> Option<usize> {
/// Return the current depth of the tree
fn current_depth(&self) -> usize {
// leaf_moves is sorted from min to max depth
self.leaf_moves().first().map(|&i| self.depth_of(i))
self.leaf_moves()
.first()
.map(|&i| self.depth_of(i))
.unwrap_or(0) // handle empty trees
}
/// Generate children for all children of `nodes`
/// only `pub` for the sake of benchmarking
pub fn extend_layers(&mut self) {
// recover from partial tree extention
if let Some(current_depth) = self.determine_current_depth() {
self.current_depth = current_depth;
}
let mut leafs = self.leaf_moves().into_iter().collect::<Vec<usize>>();
for _ in self.current_depth..self.config.max_depth {
for _ in self.current_depth()..self.config.max_depth {
let pstyle_inner = if cfg!(test) || !self.config.print {
""
} else {
&format!(
"Generating children (depth: {}/{}): ({{pos}}/{{len}}) {{per_sec}}",
self.current_depth + 1,
self.current_depth() + 1,
self.config.max_depth
)
};
@ -197,7 +190,6 @@ impl FutureMoves {
let got_len = curr_size.load(Ordering::Acquire);
self.prune_bad_children();
self.current_depth += 1;
if got_len == allowed_size {
// arena has hit the upper limit of size permitted
break;
@ -430,7 +422,6 @@ impl FutureMoves {
fn rebuild_from_board(&mut self, board: Board) {
self.arena = vec![self.create_move(None, board, !self.agent_color)];
self.current_root = Some(0);
self.current_depth = 0;
self.board = board;
}
@ -438,7 +429,6 @@ impl FutureMoves {
let board = self
.get_board_from_idx(idx)
.expect("unable to get board at idx");
self.current_depth -= self.depth_of(idx);
self.current_root = Some(idx);
self.board = board;
self.refocus_tree();
@ -490,7 +480,7 @@ impl FutureMoves {
}
fn prune_bad_children(&mut self) {
if self.current_depth < self.config.min_arena_depth || !self.config.do_prune {
if self.current_depth() < self.config.min_arena_depth || !self.config.do_prune {
return;
}
@ -499,7 +489,7 @@ impl FutureMoves {
for (depth, indexes) in self.by_depth(0..self.arena.len()) {
// TODO! maybe update by_depth every iteration or something?
if depth > self.current_depth.saturating_sub(self.config.up_to_minus) {
if depth > self.current_depth().saturating_sub(self.config.up_to_minus) {
return;
}