Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add population_size to metrics #488

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,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 +197,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 +246,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 +282,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 +342,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 Expand Up @@ -383,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);
}
}
7 changes: 6 additions & 1 deletion tests/selection_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);

Expand Down
Loading