From 9daa62ee85ba4f9faa628d35b50e78fb91de25a0 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Sun, 5 May 2024 15:18:50 +0200 Subject: [PATCH 1/3] Add population size to metrics --- src/ga.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ga.rs b/src/ga.rs index 71af3e0..3e8a51c 100644 --- a/src/ga.rs +++ b/src/ga.rs @@ -151,6 +151,14 @@ pub struct GAParams { pub max_duration: std::time::Duration, } +pub struct Telemetry { + +} + +pub struct Instrumentation { + pub telemetry: Telemetry, +} + pub struct GAConfig where IndividualT: IndividualTrait, @@ -176,6 +184,7 @@ where #[derive(Default)] pub struct Metrics { pub generation: usize, + pub population_size: usize, pub start_time: Option, /// This field can not be relied upon. It is updated only in the begining @@ -196,9 +205,11 @@ impl Metrics { start_time: Option, duration: Option, generation: usize, + population_size: usize, ) -> Self { Metrics { generation, + population_size, start_time, total_dur: duration, pop_gen_dur: None, @@ -243,9 +254,10 @@ where pub fn new( config: GAConfig, ) -> Self { + let population_size = config.params.population_size; GeneticSolver { config, - metrics: Metrics::new(None, None, 0), + metrics: Metrics::new(None, None, 0, population_size), timer: Timer::new(), } } @@ -278,6 +290,8 @@ where let mut population = self.gen_pop(); self.metrics.pop_gen_dur = Some(self.timer.elapsed()); + self.metrics.population_size = population.len(); + self.timer.start(); self.eval_pop(&mut population); self.metrics.pop_eval_dur = Some(self.timer.elapsed()); @@ -336,6 +350,8 @@ where .apply(&self.metrics, population, children); self.metrics.replacement_dur = Some(self.timer.elapsed()); + self.metrics.population_size = population.len(); + assert_eq!(population.len(), self.config.params.population_size, "There was change in population size from {} to {} in generation {}. Dynamic population size is currently not supported.", self.config.params.population_size, From 6592bb4054600f4fd836032cdb5cee880d3222a1 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Sun, 5 May 2024 15:20:45 +0200 Subject: [PATCH 2/3] Cleanup and fix tests --- src/ga.rs | 10 +--------- tests/selection_tests.rs | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/ga.rs b/src/ga.rs index 3e8a51c..58a2159 100644 --- a/src/ga.rs +++ b/src/ga.rs @@ -151,14 +151,6 @@ pub struct GAParams { pub max_duration: std::time::Duration, } -pub struct Telemetry { - -} - -pub struct Instrumentation { - pub telemetry: Telemetry, -} - pub struct GAConfig where IndividualT: IndividualTrait, @@ -399,6 +391,6 @@ mod tests { #[test] fn metrics_can_be_constructed_with_new_fn() { - Metrics::new(None, None, 0); + Metrics::new(None, None, 0, 0); } } diff --git a/tests/selection_tests.rs b/tests/selection_tests.rs index d2a216f..935dd43 100644 --- a/tests/selection_tests.rs +++ b/tests/selection_tests.rs @@ -186,7 +186,7 @@ fn boltzmann_returns_demanded_size() { ); // FIXME: We must add mocking! - let metrics = Metrics::new(Some(std::time::Instant::now()), None, 40); + let metrics = Metrics::new(Some(std::time::Instant::now()), None, 40, expected_population_size); let selected = Boltzmann::new(expected_selection_size, 0.2, 6.0, 300, true).apply(&metrics, &population); From 562e372b96cda941003059c2722e896a29347e56 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Sun, 5 May 2024 15:20:55 +0200 Subject: [PATCH 3/3] fmt 1 --- tests/selection_tests.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/selection_tests.rs b/tests/selection_tests.rs index 935dd43..85619ff 100644 --- a/tests/selection_tests.rs +++ b/tests/selection_tests.rs @@ -186,7 +186,12 @@ fn boltzmann_returns_demanded_size() { ); // FIXME: We must add mocking! - let metrics = Metrics::new(Some(std::time::Instant::now()), None, 40, expected_population_size); + let metrics = Metrics::new( + Some(std::time::Instant::now()), + None, + 40, + expected_population_size, + ); let selected = Boltzmann::new(expected_selection_size, 0.2, 6.0, 300, true).apply(&metrics, &population);