This commit is contained in:
Simon Gardling 2025-02-28 17:50:04 -05:00
parent 2388ee8c75
commit 1b195b0f91
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -59,6 +59,7 @@ impl FutureMoves {
self.arena.len()
}
/// Returns indexes of leaf [`Move`]s, sorted by their depth (increasing order)
fn leaf_moves(&self) -> Vec<usize> {
let mut indexes = (0..self.arena.len())
// we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated)
@ -122,7 +123,6 @@ impl FutureMoves {
self.prune_bad_children();
self.current_depth += 1;
if cf.is_break() {
// println!("extend_layers: early break");
return;
}
}
@ -231,6 +231,7 @@ impl FutureMoves {
.map(|&child| self.arena[child].value.expect("child has no value??"))
// average value of children
.sum::<i128>()
// divide in order to calculate average value of all children
.checked_div(self.arena[idx].children.len() as i128)
.unwrap_or(0);
@ -350,17 +351,16 @@ impl FutureMoves {
));
}
}
if !self.is_connected_to_root(idx) {
errors.push(format!("{}: not connected to root in any way", idx));
}
}
errors
}
fn prune_bad_children(&mut self) {
if self
.config
.max_depth
.saturating_sub(self.config.min_arena_depth_sub)
> self.current_depth
{
if self.config.max_depth > self.current_depth + self.config.min_arena_depth_sub {
return;
}
@ -425,23 +425,23 @@ impl FutureMoves {
let mut index_map = vec![None; self.arena.len()];
let new_start: Vec<(usize, usize, Move)> = retain
let (indexes, moves): (Vec<(usize, usize)>, Vec<Move>) = retain
.into_iter()
.enumerate() // old_idx
.zip(self.arena.drain(..))
.filter(|&((_, keep), _)| keep) // filter out un-related nodes
.map(|((old_idx, _), node)| (old_idx, node))
.enumerate() // new_idx
.map(|(a, (b, c))| (a, b, c))
.collect();
.map(|(new_idx, (old_idx, node))| ((new_idx, old_idx), node))
.unzip();
for &(new_idx, old_idx, _) in &new_start {
for (new_idx, old_idx) in indexes {
index_map[old_idx] = Some(new_idx);
}
self.arena = new_start
self.arena = moves
.into_iter()
.map(|(_, _, mut node)| {
.map(|mut node| {
if let Some(parent) = node.parent.as_mut() {
if let Some(new_parent) = index_map[*parent] {
*parent = new_parent;
@ -452,8 +452,9 @@ impl FutureMoves {
}
for c in node.children.as_mut_slice() {
debug_assert!(
index_map.get(*c).unwrap().is_some(),
debug_assert_ne!(
index_map.get(*c).and_then(|&x| x),
None,
"index_map should contain the child's index"
);
*c = unsafe { index_map.get_unchecked(*c).unwrap_unchecked() };