Skip to content

Commit

Permalink
Add population size to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kkafar committed May 5, 2024
1 parent 2efb6e2 commit 9daa62e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndividualT, MutOpT, CrossOpT, SelOpT, ReplOpT, PopGenT, FitnessT, ProbeT>
where
IndividualT: IndividualTrait,
Expand All @@ -176,6 +184,7 @@ where
#[derive(Default)]
pub struct Metrics {
pub generation: usize,
pub population_size: usize,
pub start_time: Option<std::time::Instant>,

/// This field can not be relied upon. It is updated only in the begining
Expand All @@ -196,9 +205,11 @@ impl Metrics {
start_time: Option<std::time::Instant>,
duration: Option<std::time::Duration>,
generation: usize,
population_size: usize,
) -> Self {
Metrics {
generation,
population_size,
start_time,
total_dur: duration,
pop_gen_dur: None,
Expand Down Expand Up @@ -243,9 +254,10 @@ where
pub fn new(
config: GAConfig<IndividualT, MutOpT, CrossOpT, SelOpT, ReplOpT, PopGenT, FitnessT, ProbeT>,
) -> 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(),
}
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9daa62e

Please sign in to comment.