Skip to content

Commit

Permalink
feat: add population_size to metrics (#488)
Browse files Browse the repository at this point in the history
- Add population size to metrics
- Cleanup and fix tests
- fmt 1

<!-- If applicable - remember to add the PR to the EA Rust project (ONLY
IF THERE IS NO LINKED ISSUE) -->

## Description

## Linked issues <!-- Please use "Resolves #<issue_no> syntax in case
this PR should be linked to an issue -->

Closes #487

## Important implementation details <!-- if any, optional section -->
  • Loading branch information
kkafar authored May 5, 2024
1 parent 2efb6e2 commit 7c8148b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 7c8148b

Please sign in to comment.