improve evenness of FutureMoves tree

This commit is contained in:
Simon Gardling 2025-02-28 10:29:02 -05:00
parent 00238fc1f1
commit 10389ad8ee
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 38 additions and 9 deletions

View File

@ -42,3 +42,7 @@ criterion = { version = "0.5", features = [ "html_reports" ] }
[[bench]]
name = "future_children"
harness = false
[lints.rust]
# fix weird warnings about `test` not being expected
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(test)'] }

View File

@ -59,10 +59,38 @@ impl FutureMoves {
self.arena.len()
}
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)
.filter(|&idx| {
let got = &self.arena[idx];
!got.is_trimmed && !got.tried_children && got.winner == Winner::None
})
.filter(|&idx| self.is_connected_to_root(idx))
.collect::<Vec<usize>>();
// we want to try and make the tree even in depth
// by first generating children for younger moves
indexes.sort_by_key(|&x| self.depth_of(x));
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> {
// leaf_moves is sorted from min to max depth
self.leaf_moves().first().map(|&i| self.depth_of(i))
}
/// Generate children for all children of `nodes`
/// only `pub` for the sake of benchmarking
pub fn extend_layers(&mut self) {
for _ in (self.current_depth + 1)..=self.config.max_depth {
// recover from partial tree extention
if let Some(current_depth) = self.determine_current_depth() {
self.current_depth = current_depth;
}
for _ in self.current_depth..self.config.max_depth {
let pstyle_inner = if cfg!(test) {
""
} else {
@ -73,13 +101,10 @@ impl FutureMoves {
)
};
let cf = (0..self.arena.len())
// we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated)
.filter(|&idx| {
let got = &self.arena[idx];
!got.is_trimmed && !got.tried_children && got.winner == Winner::None
})
.filter(|&idx| self.is_connected_to_root(idx))
let cf = self
.leaf_moves()
.into_iter()
.filter(|&i| self.depth_of(i) == self.current_depth)
.collect::<Vec<usize>>()
.into_iter()
.progress_with_style(ProgressStyle::with_template(pstyle_inner).unwrap())
@ -198,7 +223,7 @@ impl FutureMoves {
let by_depth_vec = self.by_depth(indexes);
// reversed so we build up the value of the closest (in time) moves from the future
for (depth, nodes) in by_depth_vec.into_iter().rev() {
for (_, nodes) in by_depth_vec.into_iter().rev() {
for idx in nodes {
let children_value = self.arena[idx]
.children