fix compute_values

This commit is contained in:
Simon Gardling 2025-02-20 22:09:40 -05:00
parent cb129a96cf
commit de0eafecfa
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -1,3 +1,5 @@
use std::collections::HashMap;
use indicatif::{ProgressIterator, ProgressStyle};
use crate::{
@ -167,14 +169,21 @@ impl FutureMoves {
// PERF! pre-organize all indexes based on what depth they're at
// previously, I did a lookup map based on if a node was visited, still resulted in a full
// O(n) iteration each depth
let mut by_depth: Vec<Vec<usize>> = (0..=self.max_depth + 2).map(|_| Vec::new()).collect();
let mut by_depth: HashMap<usize, Vec<usize>> = HashMap::new();
for idx in indexes {
let depth = self.depth_of(idx);
// -1 because `depth_of` is one-indexed
by_depth[depth - 1].push(idx);
if let Some(got) = by_depth.get_mut(&depth) {
got.push(idx);
} else {
by_depth.insert(depth, vec![idx]);
}
}
for (depth, nodes) in by_depth.into_iter().enumerate().rev() {
let mut by_depth_vec: Vec<(usize, Vec<usize>)> = by_depth.into_iter().collect();
by_depth_vec.sort_by_key(|x| x.0);
for (depth, nodes) in by_depth_vec {
for idx in nodes {
// TODO! impl dynamic sorting based on children's states, maybe it propegates
// upwards using the `parent` field
@ -192,7 +201,7 @@ impl FutureMoves {
.rev() // rev then reverse so we get an index starting from the back
.enumerate()
// since children are sorted by value, we should weight the first one more
.map(|(i, &child)| self.arena[child].value * (i as i128 + 1))
.map(|(i, &child)| self.arena[child].value * (i as i128))
.sum::<i128>();
self.arena[idx].value =