Skip to content

Commit

Permalink
Rename all occurences of metadata
Browse files Browse the repository at this point in the history
Closes #480
  • Loading branch information
kkafar committed May 1, 2024
1 parent a2abda5 commit e0bc93e
Show file tree
Hide file tree
Showing 27 changed files with 281 additions and 281 deletions.
12 changes: 6 additions & 6 deletions examples/jssp/problem/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl JsspCrossover {

fn apply_single(
&mut self,
_metadata: &Metrics,
_metrics: &Metrics,
parent_1: &JsspIndividual,
parent_2: &JsspIndividual,
) -> (JsspIndividual, JsspIndividual) {
Expand Down Expand Up @@ -53,13 +53,13 @@ impl JsspCrossover {
}

impl CrossoverOperator<JsspIndividual> for JsspCrossover {
fn apply(&mut self, metadata: &Metrics, selected: &[&JsspIndividual]) -> Vec<JsspIndividual> {
fn apply(&mut self, metrics: &Metrics, selected: &[&JsspIndividual]) -> Vec<JsspIndividual> {
assert!(selected.len() & 1 == 0);

let mut output = Vec::with_capacity(selected.len());

for parents in selected.chunks(2) {
let (child_1, child_2) = self.apply_single(metadata, parents[0], parents[1]);
let (child_1, child_2) = self.apply_single(metrics, parents[0], parents[1]);
output.push(child_1);
output.push(child_2);
}
Expand All @@ -77,7 +77,7 @@ impl NoopCrossover {

fn apply_single(
&mut self,
_metadata: &Metrics,
_metrics: &Metrics,
parent_1: &JsspIndividual,
parent_2: &JsspIndividual,
) -> (JsspIndividual, JsspIndividual) {
Expand All @@ -86,13 +86,13 @@ impl NoopCrossover {
}

impl CrossoverOperator<JsspIndividual> for NoopCrossover {
fn apply(&mut self, metadata: &Metrics, selected: &[&JsspIndividual]) -> Vec<JsspIndividual> {
fn apply(&mut self, metrics: &Metrics, selected: &[&JsspIndividual]) -> Vec<JsspIndividual> {
assert!(selected.len() & 1 == 0);

let mut output = Vec::with_capacity(selected.len());

for parents in selected.chunks(2) {
let (child_1, child_2) = self.apply_single(metadata, parents[0], parents[1]);
let (child_1, child_2) = self.apply_single(metrics, parents[0], parents[1]);
output.push(child_1);
output.push(child_2);
}
Expand Down
48 changes: 24 additions & 24 deletions examples/jssp/problem/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Probe<JsspIndividual> for JsspProbe {
// iterinfo,<generation>,<eval_time>,<sel_time>,<cross_time>,<mut_time>,<repl_time>,<iter_time>

#[inline]
fn on_start(&mut self, _metadata: &ecrs::ga::Metrics) {
fn on_start(&mut self, _metrics: &ecrs::ga::Metrics) {
// Writing csv header to each file
info!(target: "popmetrics", "event_name,generation,total_duration,population_size,diversity,distance_avg");
info!(target: "popgentime", "event_name,time");
Expand All @@ -93,7 +93,7 @@ impl Probe<JsspIndividual> for JsspProbe {
info!(target: "iterinfo", "event_name,generation,eval_time,sel_time,cross_time,mut_time,repl_time,iter_time");
}

fn on_initial_population_created(&mut self, metadata: &ecrs::ga::Metrics, population: &[JsspIndividual]) {
fn on_initial_population_created(&mut self, metrics: &ecrs::ga::Metrics, population: &[JsspIndividual]) {
debug_assert_eq!(self.repeated.len(), 0);
self.repeated.resize(population.len(), false);

Expand All @@ -102,64 +102,64 @@ impl Probe<JsspIndividual> for JsspProbe {
let diversity = self.estimate_pop_diversity(population);
let distance_avg = self.estimate_avg_distance(population);
info!(target: "popmetrics", "diversity,0,0,{},{diversity},{distance_avg}", population.len());
info!(target: "popgentime", "popgentime,{}", metadata.pop_gen_dur.unwrap().as_millis());
info!(target: "popgentime", "popgentime,{}", metrics.pop_gen_dur.unwrap().as_millis());
}

fn on_new_best(&mut self, metadata: &ecrs::ga::Metrics, individual: &JsspIndividual) {
fn on_new_best(&mut self, metrics: &ecrs::ga::Metrics, individual: &JsspIndividual) {
info!(
target: "newbest",
"newbest,{},{},{}",
metadata.generation,
metadata.total_dur.unwrap().as_millis(),
metrics.generation,
metrics.total_dur.unwrap().as_millis(),
individual.fitness
);
}

fn on_new_generation(&mut self, metadata: &ecrs::ga::Metrics, generation: &[JsspIndividual]) {
fn on_new_generation(&mut self, metrics: &ecrs::ga::Metrics, generation: &[JsspIndividual]) {
// TODO: As this metric is useless right now I'm disabling it temporarily
// let diversity = self.estimate_pop_diversity(generation);
let diversity = self.estimate_pop_diversity(generation);
let distance_avg = self.estimate_avg_distance(generation);
info!(
target: "popmetrics",
"diversity,{},{},{},{diversity},{distance_avg}",
metadata.generation,
metadata.total_dur.unwrap().as_millis(),
metrics.generation,
metrics.total_dur.unwrap().as_millis(),
generation.len()
);
}

fn on_best_fit_in_generation(&mut self, metadata: &ecrs::ga::Metrics, individual: &JsspIndividual) {
fn on_best_fit_in_generation(&mut self, metrics: &ecrs::ga::Metrics, individual: &JsspIndividual) {
info!(
target: "bestingen",
"bestingen,{},{},{}",
metadata.generation,
metadata.total_dur.unwrap().as_millis(),
metrics.generation,
metrics.total_dur.unwrap().as_millis(),
individual.fitness
);
}

#[inline]
fn on_iteration_start(&mut self, _metadata: &ecrs::ga::Metrics) { /* defaults to noop */
fn on_iteration_start(&mut self, _metrics: &ecrs::ga::Metrics) { /* defaults to noop */
}

#[inline]
fn on_iteration_end(&mut self, metadata: &ecrs::ga::Metrics) {
fn on_iteration_end(&mut self, metrics: &ecrs::ga::Metrics) {
info!(target: "iterinfo", "iterinfo,{},{},{},{},{},{},{}",
metadata.generation,
metadata.pop_eval_dur.unwrap().as_millis(),
metadata.selection_dur.unwrap().as_millis(),
metadata.crossover_dur.unwrap().as_millis(),
metadata.mutation_dur.unwrap().as_millis(),
metadata.replacement_dur.unwrap().as_millis(),
metadata.iteration_dur.unwrap().as_millis()
metrics.generation,
metrics.pop_eval_dur.unwrap().as_millis(),
metrics.selection_dur.unwrap().as_millis(),
metrics.crossover_dur.unwrap().as_millis(),
metrics.mutation_dur.unwrap().as_millis(),
metrics.replacement_dur.unwrap().as_millis(),
metrics.iteration_dur.unwrap().as_millis()
);
}

#[inline]
fn on_end(
&mut self,
metadata: &ecrs::ga::Metrics,
metrics: &ecrs::ga::Metrics,
_population: &[JsspIndividual],
best_individual: &JsspIndividual,
) {
Expand Down Expand Up @@ -201,8 +201,8 @@ impl Probe<JsspIndividual> for JsspProbe {
solution_string,
hash: format!("{:x}", hash),
fitness: best_individual.fitness,
generation_count: metadata.generation,
total_time: metadata.total_dur.unwrap().as_millis(),
generation_count: metrics.generation,
total_time: metrics.total_dur.unwrap().as_millis(),
chromosome: best_individual.chromosome(),
};
let serialized_object = serde_json::to_string_pretty(&outdata).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/jssp/problem/replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl JsspReplacement {
impl ReplacementOperator<JsspIndividual> for JsspReplacement {
fn apply(
&mut self,
_metadata: &Metrics,
_metrics: &Metrics,
mut population: Vec<JsspIndividual>,
mut children: Vec<JsspIndividual>,
) -> Vec<JsspIndividual> {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl ReplaceWithRandomPopulation {
impl ReplacementOperator<JsspIndividual> for ReplaceWithRandomPopulation {
fn apply(
&mut self,
_metadata: &Metrics,
_metrics: &Metrics,
population: Vec<JsspIndividual>,
_children: Vec<JsspIndividual>,
) -> Vec<JsspIndividual> {
Expand Down
2 changes: 1 addition & 1 deletion examples/jssp/problem/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl EmptySelection {
impl SelectionOperator<JsspIndividual> for EmptySelection {
fn apply<'a>(
&mut self,
_metadata: &ecrs::ga::Metrics,
_metrics: &ecrs::ga::Metrics,
_population: &'a [JsspIndividual],
_count: usize,
) -> Vec<&'a JsspIndividual> {
Expand Down
62 changes: 31 additions & 31 deletions src/ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ where
ProbeT: Probe<IndividualT>,
{
config: GAConfig<IndividualT, MutOpT, CrossOpT, SelOpT, ReplOpT, PopGenT, FitnessT, ProbeT>,
metadata: Metrics,
metrics: Metrics,
timer: Timer,
}

Expand All @@ -244,7 +244,7 @@ where
) -> Self {
GeneticSolver {
config,
metadata: Metrics::new(None, None, 0),
metrics: Metrics::new(None, None, 0),
timer: Timer::new(),
}
}
Expand All @@ -270,60 +270,60 @@ where
}

pub fn run(&mut self) -> Option<IndividualT> {
self.metadata.start_time = Some(std::time::Instant::now());
self.config.probe.on_start(&self.metadata);
self.metrics.start_time = Some(std::time::Instant::now());
self.config.probe.on_start(&self.metrics);

self.timer.start();
let mut population = self.gen_pop();
self.metadata.pop_gen_dur = Some(self.timer.elapsed());
self.metrics.pop_gen_dur = Some(self.timer.elapsed());

self.timer.start();
self.eval_pop(&mut population);
self.metadata.pop_eval_dur = Some(self.timer.elapsed());
self.metrics.pop_eval_dur = Some(self.timer.elapsed());

self.config
.probe
.on_initial_population_created(&self.metadata, &population);
.on_initial_population_created(&self.metrics, &population);

let mut best_individual_all_time = Self::find_best_individual(&population).clone();

self.metadata.total_dur = Some(self.metadata.start_time.unwrap().elapsed());
self.metrics.total_dur = Some(self.metrics.start_time.unwrap().elapsed());
self.config
.probe
.on_new_best(&self.metadata, &best_individual_all_time);
.on_new_best(&self.metrics, &best_individual_all_time);

let mut iteration_timer = Timer::new();
for generation_no in 1..=self.config.params.generation_limit {
self.metadata.generation = generation_no;
self.metadata.total_dur = Some(self.metadata.start_time.unwrap().elapsed());
self.metrics.generation = generation_no;
self.metrics.total_dur = Some(self.metrics.start_time.unwrap().elapsed());
iteration_timer.start();

self.config.probe.on_iteration_start(&self.metadata);
self.config.probe.on_iteration_start(&self.metrics);

// 2. Evaluate fitness for each individual.
self.timer.start();
self.eval_pop(&mut population);
self.metadata.pop_eval_dur = Some(self.timer.elapsed());
self.metrics.pop_eval_dur = Some(self.timer.elapsed());

// 4. Create mating pool by applying selection operator.
self.timer.start();
let mating_pool: Vec<&IndividualT> =
self.config
.selection_operator
.apply(&self.metadata, &population, population.len());
self.metadata.selection_dur = Some(self.timer.elapsed());
.apply(&self.metrics, &population, population.len());
self.metrics.selection_dur = Some(self.timer.elapsed());

// 5. From mating pool create new generation (apply crossover & mutation).

self.timer.start();
let mut children = self.config.crossover_operator.apply(&self.metadata, &mating_pool);
self.metadata.crossover_dur = Some(self.timer.elapsed());
let mut children = self.config.crossover_operator.apply(&self.metrics, &mating_pool);
self.metrics.crossover_dur = Some(self.timer.elapsed());

self.timer.start();
children
.iter_mut()
.for_each(|child| self.config.mutation_operator.apply(&self.metadata, child));
self.metadata.mutation_dur = Some(self.timer.elapsed());
.for_each(|child| self.config.mutation_operator.apply(&self.metrics, child));
self.metrics.mutation_dur = Some(self.timer.elapsed());

if self.config.replacement_operator.requires_children_fitness() {
self.eval_pop(&mut children);
Expand All @@ -334,8 +334,8 @@ where
population = self
.config
.replacement_operator
.apply(&self.metadata, population, children);
self.metadata.replacement_dur = Some(self.timer.elapsed());
.apply(&self.metrics, population, children);
self.metrics.replacement_dur = Some(self.timer.elapsed());

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.",
Expand All @@ -346,34 +346,34 @@ where
// 7. Check for stop condition (Is good enough individual found)? If not goto 2.
self.timer.start();
self.eval_pop(&mut population);
self.metadata.pop_eval_dur = Some(self.timer.elapsed());
self.metrics.pop_eval_dur = Some(self.timer.elapsed());

self.config.probe.on_new_generation(&self.metadata, &population);
self.config.probe.on_new_generation(&self.metrics, &population);

let best_individual = Self::find_best_individual(&population);
self.config
.probe
.on_best_fit_in_generation(&self.metadata, best_individual);
.on_best_fit_in_generation(&self.metrics, best_individual);

if *best_individual < best_individual_all_time {
best_individual_all_time = best_individual.clone();
self.config
.probe
.on_new_best(&self.metadata, &best_individual_all_time);
.on_new_best(&self.metrics, &best_individual_all_time);
}

self.metadata.iteration_dur = Some(iteration_timer.elapsed());
self.config.probe.on_iteration_end(&self.metadata);
self.metrics.iteration_dur = Some(iteration_timer.elapsed());
self.config.probe.on_iteration_end(&self.metrics);

if self.metadata.start_time.unwrap().elapsed() >= self.config.params.max_duration {
if self.metrics.start_time.unwrap().elapsed() >= self.config.params.max_duration {
break;
}
}

self.metadata.total_dur = Some(self.metadata.start_time.unwrap().elapsed());
self.metrics.total_dur = Some(self.metrics.start_time.unwrap().elapsed());
self.config
.probe
.on_end(&self.metadata, &population, &best_individual_all_time);
.on_end(&self.metrics, &population, &best_individual_all_time);
Some(best_individual_all_time)
}
}
Expand All @@ -383,7 +383,7 @@ mod tests {
use super::Metrics;

#[test]
fn gametadata_can_be_constructed_with_new_fn() {
fn metrics_can_be_constructed_with_new_fn() {
Metrics::new(None, None, 0);
}
}
6 changes: 3 additions & 3 deletions src/ga/operators/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait CrossoverOperator<IndividualT: IndividualTrait> {
/// FIXME: Understand lifetimes here!
// fn apply_iter<'i, InputIter, OutputIter>(
// &mut self,
// metadata: &GAMetadata,
// metrics: &Metrics,
// selected: InputIter,
// ) -> OutputIter
// where
Expand All @@ -26,11 +26,11 @@ pub trait CrossoverOperator<IndividualT: IndividualTrait> {
///
/// ## Arguments
///
/// * `metadata` - metadata provided by the GA runtime,
/// * `metrics` - metrics provided by the GA runtime,
/// * `selected` - result of running selection operator,
///
/// ## Returns
///
/// Vector of individuals created during the crossover stage.
fn apply(&mut self, metadata: &Metrics, selected: &[&IndividualT]) -> Vec<IndividualT>;
fn apply(&mut self, metrics: &Metrics, selected: &[&IndividualT]) -> Vec<IndividualT>;
}
Loading

0 comments on commit e0bc93e

Please sign in to comment.