From 29450a1c65ebe2b1232ce8c879833f1852118325 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Sun, 8 Sep 2024 22:56:32 +0200 Subject: [PATCH] Add timing output. --- examples/layout.rs | 2 +- src/layout/mod.rs | 29 +++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/layout.rs b/examples/layout.rs index 051417f..aa6bf71 100644 --- a/examples/layout.rs +++ b/examples/layout.rs @@ -19,7 +19,7 @@ fn main() { dbg!(&p); - let mut g = GeneticAlgorithm::new(&p, 40, 5, 5, &mut rng); + let mut g = GeneticAlgorithm::new(&p, 20, 2, 0, &mut rng); for i in 0..100 { println!("Generatrion {i}"); diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 5d9ab55..309aaaf 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -28,15 +28,26 @@ impl<'a> GeneticAlgorithm<'a> { ) -> GeneticAlgorithm<'a> { let mut population = Vec::new(); + let start = Instant::now(); + + let mut count: usize = 0; + while population.len() < population_size { + count += 1; if let Some(p) = PathLayout::new(Layout::new(problem, rng)) { population.push(p); } } + println!("Layouts accepted: {}/{}", population_size, count); + population.sort_by_key(|p| p.score()); - println!("Best score: {}", population[0].score()); + println!( + "Best score: {}. Time: {:.2}s", + population[0].score(), + start.elapsed().as_secs_f32() + ); population[0].print_visualization(); GeneticAlgorithm { @@ -49,6 +60,7 @@ impl<'a> GeneticAlgorithm<'a> { } pub fn generation(&mut self, rng: &mut R) { + let start_new = Instant::now(); for i in self.population_keep..(self.population_keep + self.population_new) { loop { if let Some(p) = PathLayout::new(Layout::new(self.problem, rng)) { @@ -58,6 +70,9 @@ impl<'a> GeneticAlgorithm<'a> { } } + let duration_new = start_new.elapsed(); + let start_mutate = Instant::now(); + for i in (self.population_keep + self.population_new)..self.population_size { let j = i - (self.population_keep + self.population_new); loop { @@ -69,9 +84,15 @@ impl<'a> GeneticAlgorithm<'a> { } } } + let duration_mutate = start_mutate.elapsed(); self.population.sort_by_key(|p| p.score()); - println!("Best score: {}", self.population[0].score()); + println!( + "Best score: {}. Time new: {:.2}s. Time mutate: {:.2}s", + self.population[0].score(), + duration_new.as_secs_f32(), + duration_mutate.as_secs_f32() + ); self.population[0].print_visualization(); } @@ -131,14 +152,10 @@ impl<'a> PathLayout<'a> { pub fn new(layout: Layout<'a>) -> Option> { let mut p = crate::belt_finding::Problem::from_layout(&layout); - // let start = std::time::Instant::now(); - if !p.find_path() { return None; } - // println!("Find Path: {:.2}", start.elapsed().as_secs_f32()); - let mut c = ConflictAvoidance::new(&p); let start = std::time::Instant::now();