WIP: Change A* heuristic to be a proper estimate #5

Draft
computerdruid wants to merge 1 commit from computerdruid/hanoigame:push-svpmwwzovlor into main

View file

@ -322,22 +322,8 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
hasher.finish() 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 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 had = HashSet::new();
let mut ctr = 0; let mut ctr = 0;
@ -346,7 +332,7 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
parent_index: 0, parent_index: 0,
m: Move::new(0, 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() { while let Some((score, depth, state_index)) = todo.pop() {
ctr += 1; ctr += 1;
@ -387,8 +373,36 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
working_instance.towers = state.towers.clone(); working_instance.towers = state.towers.clone();
working_instance.make_move(m).unwrap(); working_instance.make_move(m).unwrap();
let num_solved = working_instance.num_solved(); let brs: i64 = {
let brs = working_instance.burried_ring_score(); 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); let new_state_hash = hash_state(&working_instance.towers);
if had.contains(&new_state_hash) { if had.contains(&new_state_hash) {
@ -402,9 +416,8 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
m, m,
}); });
let score = (num_solved as f32 * 10.0) + (-(brs as f32) * 100.0); let score = (depth) - (brs);
todo.push(((score, -depth), depth - 1, new_state_index));
todo.push((Ordf32(score), depth - 1, new_state_index));
} }
} }