Add timing output.

This commit is contained in:
hal8174 2024-09-08 22:56:32 +02:00
parent 79c8b0c710
commit 29450a1c65
2 changed files with 24 additions and 7 deletions

View file

@ -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}");

View file

@ -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<R: Rng + ?Sized>(&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<PathLayout<'a>> {
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();