arena fixes

This commit is contained in:
Simon Gardling 2025-02-26 10:34:35 -05:00
parent 63a20d875f
commit fd7701cd79
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 21 additions and 17 deletions

View File

@ -46,7 +46,7 @@ impl FutureMoves {
// we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated) // we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated)
.filter(|&idx| { .filter(|&idx| {
let got = &self.arena[idx]; let got = &self.arena[idx];
got.is_lazy || got.children.is_empty() got.is_trimmed || got.children.is_empty()
}) })
.filter(|&idx| self.is_connected_to_root(idx)) .filter(|&idx| self.is_connected_to_root(idx))
.collect::<Vec<usize>>() .collect::<Vec<usize>>()
@ -317,23 +317,25 @@ impl FutureMoves {
// values are needed in order to prune and see what's best // values are needed in order to prune and see what's best
self.compute_values(0..self.arena_len()); self.compute_values(0..self.arena_len());
let by_depth = let by_depth = self.by_depth(0..self.arena.len());
self.by_depth((0..self.arena.len()).filter(|&i| self.is_connected_to_root(i)));
const TOP_K_CHIL: usize = 2; const TOP_K_CHIL: usize = 2;
// the lower the value, the more conservative // the lower the value, the more conservative, what level to stop pruning at?
const UP_TO: usize = 5; // a lower value allows more possible paths
let up_to = self.current_depth - 2;
// start pruning at X depth before self.max_depth // start pruning at X depth before self.max_depth is reached
const START_PRUNING_AT_MINUS: usize = 2; // used for pruning during tree generation before the tree is fully grown
if self.max_depth > self.current_depth + START_PRUNING_AT_MINUS { const START_PRUNING_AT_MINUS: usize = 3;
if self.max_depth.saturating_sub(START_PRUNING_AT_MINUS) > self.current_depth {
return; return;
} }
for (depth, indexes) in by_depth { for (depth, indexes) in by_depth {
// TODO! maybe update by_depth every iteration or something? // TODO! maybe update by_depth every iteration or something?
if depth > UP_TO { if depth > up_to {
return; return;
} }
@ -344,10 +346,10 @@ impl FutureMoves {
for idx in indexes { for idx in indexes {
let mut m = self.arena[idx].clone(); let mut m = self.arena[idx].clone();
if m.is_lazy { if m.is_trimmed {
continue; continue;
} }
m.is_lazy = true; m.is_trimmed = true;
m.sort_children(&mut self.arena); m.sort_children(&mut self.arena);
if m.children.len() > TOP_K_CHIL { if m.children.len() > TOP_K_CHIL {
let drained = m.children.drain(TOP_K_CHIL..); let drained = m.children.drain(TOP_K_CHIL..);
@ -358,6 +360,8 @@ impl FutureMoves {
self.arena[idx] = m; self.arena[idx] = m;
} }
} }
// rebuild tree to exclude the things that were pruned
self.refocus_tree(); self.refocus_tree();
} }
@ -444,7 +448,7 @@ mod tests {
value: None, value: None,
self_value: 0, self_value: 0,
color: Piece::Black, color: Piece::Black,
is_lazy: false, is_trimmed: false,
}); });
futm.update_root_idx_raw(0); futm.update_root_idx_raw(0);
@ -546,7 +550,7 @@ mod tests {
value: None, value: None,
self_value: 0, self_value: 0,
color: Piece::Black, color: Piece::Black,
is_lazy: false, is_trimmed: false,
}); });
futm.update_root_idx_raw(0); futm.update_root_idx_raw(0);
@ -610,7 +614,7 @@ mod tests {
value: None, value: None,
self_value: 0, self_value: 0,
color: Piece::Black, color: Piece::Black,
is_lazy: false, is_trimmed: false,
}); });
futm.update_root_idx_raw(0); futm.update_root_idx_raw(0);

View File

@ -31,8 +31,8 @@ pub struct Move {
/// Which color made a move on this move? /// Which color made a move on this move?
pub color: Piece, pub color: Piece,
/// Was this child lazily created? (it will have delayed child generation) /// Was this move's children previously trimmed?
pub is_lazy: bool, pub is_trimmed: bool,
} }
lazy_static! { lazy_static! {
@ -57,7 +57,7 @@ impl Move {
children: Vec::new(), children: Vec::new(),
value: None, value: None,
color, color,
is_lazy: false, is_trimmed: false,
self_value: 0, self_value: 0,
}; };
m.self_value = m.compute_self_value(agent_color); m.self_value = m.compute_self_value(agent_color);