Compare commits

..

1 commit

Author SHA1 Message Date
c22d4f14d7 Change A* heuristic to be a proper estimate
This makes it:
- Actually find the path with the fewest number of moves, and
- Not be so prone to getting stuck exploring space that seems "close to
  a solution" but is actually a dead end.
2025-09-13 16:55:44 -07:00

View file

@ -322,22 +322,8 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
hasher.finish()
};
#[derive(PartialEq)]
struct Ordf32(f32);
impl Eq for Ordf32 {}
impl PartialOrd for Ordf32 {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Ordf32 {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.total_cmp(&other.0)
}
}
let mut states = Vec::new();
let mut todo = BinaryHeap::<(Ordf32, usize, usize)>::new();
let mut todo = BinaryHeap::<((i64, i64), i64, usize)>::new();
let mut had = HashSet::new();
let mut ctr = 0;
@ -346,7 +332,7 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
parent_index: 0,
m: Move::new(0, 0),
});
todo.push((Ordf32(0.0), 1000000000, 0));
todo.push(((0, 0), 1000000000, 0));
while let Some((score, depth, state_index)) = todo.pop() {
ctr += 1;
@ -387,8 +373,36 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
working_instance.towers = state.towers.clone();
working_instance.make_move(m).unwrap();
let num_solved = working_instance.num_solved();
let brs = working_instance.burried_ring_score();
let brs: i64 = {
let this = &working_instance;
let mut seen_bottom_rings = HashSet::new();
this.towers
.iter()
.map(|i| {
let this = &i;
let mut ring_type = None;
let mut burried_score = 0;
for ring in this.rings.iter().rev().flatten() {
if let Some(existing_ring_type) = ring_type
&& existing_ring_type != *ring
{
burried_score += 1;
}
ring_type = Some(*ring);
}
if let Some(Some(ring)) = this.rings.last() {
if seen_bottom_rings.contains(ring) {
burried_score += 1;
} else {
seen_bottom_rings.insert(ring);
}
}
burried_score
})
.sum()
};
let new_state_hash = hash_state(&working_instance.towers);
if had.contains(&new_state_hash) {
@ -402,9 +416,8 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
m,
});
let score = (num_solved as f32 * 10.0) + (-(brs as f32) * 100.0);
todo.push((Ordf32(score), depth - 1, new_state_index));
let score = (depth) - (brs);
todo.push(((score, -depth), depth - 1, new_state_index));
}
}