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

@ -78,12 +78,6 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
tower tower
} }
fn map(self, f: impl FnMut(Option<Ring>) -> Option<Ring>) -> Self {
Self {
rings: self.rings.map(f),
}
}
fn burried_score(&self) -> usize { fn burried_score(&self) -> usize {
let mut ring_type = None; let mut ring_type = None;
let mut same_ring_type_score = 0; let mut same_ring_type_score = 0;
@ -286,13 +280,6 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
continue; continue;
} }
let rings = self.towers[from_tower].rings;
if rings[1..].iter().all(|i| i == &rings[0])
&& self.towers[to_tower].rings[0].is_none()
{
continue;
}
let m = Move::new(from_tower, to_tower); let m = Move::new(from_tower, to_tower);
if self.try_make_move(m, true).is_ok() { if self.try_make_move(m, true).is_ok() {
res.push(m); res.push(m);
@ -325,81 +312,18 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
} }
} }
fn tower_solved<const TOWER_HEIGHT: usize>(tower: &Tower<TOWER_HEIGHT>) -> bool { let hash_state = |s: &[Tower<TOWER_HEIGHT>]| {
tower
.rings
.iter()
.all(|x| x.is_some() && x == &tower.rings[0])
}
let hash_state = |towers: &[Tower<TOWER_HEIGHT>]| {
let mut hasher = DefaultHasher::default(); let mut hasher = DefaultHasher::default();
let num_finished_towers = towers.iter().filter(|i| tower_solved(*i)).count(); for x in s.iter().filter(|i| i.height() != 0).enumerate() {
num_finished_towers.hash(&mut hasher);
let mut filtered_towers = towers
.iter()
.filter(|i| !tower_solved(i) && i.height() != 0)
.enumerate()
.collect::<Vec<_>>();
filtered_towers.sort_by_cached_key(|(index, tower)| {
let mut rings = [0; TOWER_HEIGHT];
let mut count = 0;
for (idx, ring) in tower.rings.iter().copied().enumerate() {
let Some(ring) = ring else { break };
if rings[idx] == 0 {
count += 1;
for (j, ring2) in tower.rings[idx..].iter().copied().enumerate() {
if ring2 == Some(ring) {
rings[j] = count;
}
}
}
}
(rings, *index)
});
let mut color_assignment = [None::<Ring>; 100];
let mut num_seen = 0;
let relabelled_towers = filtered_towers.iter().map(|(_, x)| x).map(|tower| {
tower.map(|ring| {
ring.map(|ring| {
*color_assignment[ring.0.get()].get_or_insert_with(|| {
num_seen += 1;
let ring = Ring(NonZero::new(num_seen).unwrap());
ring
})
})
})
});
for x in relabelled_towers.into_iter().enumerate() {
x.hash(&mut hasher); x.hash(&mut hasher);
} }
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;
@ -408,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;
@ -449,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) {
@ -464,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));
} }
} }
@ -824,7 +775,7 @@ fn main() -> io::Result<()> {
let mut g = GameGenerator { let mut g = GameGenerator {
num_extra_towers: 2, num_extra_towers: 2,
num_ring_types: 16, num_ring_types: 13,
} }
.highest_burried_score::<4>(&seed); .highest_burried_score::<4>(&seed);