Bug fixes

This commit is contained in:
hal8174 2024-08-31 02:00:28 +02:00
parent a11b39cf9f
commit f20a1841c9
6 changed files with 75 additions and 25 deletions

View file

@ -29,7 +29,7 @@ impl<'a> GeneticAlgorithm<'a> {
}
}
population.sort_by_cached_key(|p| p.score());
population.sort_by_key(|p| p.score());
println!("Best score: {}", population[0].score());
population[0].print_visualization();
@ -56,14 +56,16 @@ impl<'a> GeneticAlgorithm<'a> {
for i in (self.population_keep + self.population_new)..self.population_size {
let j = i - (self.population_keep + self.population_new);
loop {
if let Some(p) = PathLayout::new(self.population[j].layout.mutate(rng)) {
if let Some(p) =
PathLayout::new(self.population[j % self.population_keep].layout.mutate(rng))
{
self.population[i] = p;
break;
}
}
}
self.population.sort_by_cached_key(|p| p.score());
self.population.sort_by_key(|p| p.score());
println!("Best score: {}", self.population[0].score());
self.population[0].print_visualization();
}
@ -122,20 +124,26 @@ pub struct PathLayout<'a> {
impl<'a> PathLayout<'a> {
pub fn new(layout: Layout<'a>) -> Option<PathLayout<'a>> {
layout.print_visualization();
let mut p = crate::belt_finding::Problem::from_layout(&layout);
// let start = std::time::Instant::now();
if !p.find_path() {
return None;
}
p.print();
// println!("Find Path: {:.2}", start.elapsed().as_secs_f32());
let mut c = ConflictAvoidance::new(p);
// let start = std::time::Instant::now();
if !c.remove_all_conflicts() {
return None;
}
// println!("Conflict avoidance: {:.2}", start.elapsed().as_secs_f32());
let paths = c.get_paths().to_vec();
let score = paths
@ -302,13 +310,13 @@ impl Layout<'_> {
let r: &[(&dyn Fn(&mut Layout, &mut R) -> bool, _)] = &[
(&Self::mutate_replace::<R>, 30),
(&Self::mutate_flip::<R>, 50),
(&Self::mutate_jiggle::<R>, 80),
(&Self::mutate_jiggle::<R>, 160),
];
loop {
let p = r.choose_weighted(rng, |i| i.1).unwrap();
if p.0(&mut s, rng) && rng.gen_bool(0.4) {
if p.0(&mut s, rng) && rng.gen_bool(0.2) {
break;
}
}