Skip to content

Commit

Permalink
Fix empty fitnesses to StopCriterion, MutationRate and SelectionRate …
Browse files Browse the repository at this point in the history
…at the first generation and add several stop criteria
  • Loading branch information
Martin1887 committed Dec 27, 2018
1 parent 916b161 commit 434f17e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion oxigen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oxigen"
version = "1.2.1"
version = "1.3.0"
authors = ["Martin1887 <marcos.martin.pozo.delgado@gmail.com>"]
description = "Fast and parallel genetic algorithm library."
repository = "https://github.com/Martin1887/oxigen"
Expand Down
1 change: 1 addition & 0 deletions oxigen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ impl<T, Ind: Genotype<T>> GeneticExecution<T, Ind> {
.push((Box::new(Ind::generate(&self.genotype_size)), None));
}
self.fix();
current_fitnesses = self.compute_fitnesses(true);

if self.progress_log.0 > 0 {
self.print_progress_header();
Expand Down
31 changes: 29 additions & 2 deletions oxigen/src/stop_criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ pub enum StopCriteria {
Generation(u64),
/// Stop when the mean progress in the last generations is lower than a specific threshold.
Progress(f64),
/// Stop when the generation is bigger than the first value and the mean progress in the last
/// generations is lower than the specific threshold specified as the second value.
GenerationAndProgress(u64, f64),
/// Stop when the max fitness is bigger or equal than a specific threshold.
MaxFitness(f64),
/// Stop when the min fitness is bigger or equal than a specific threshold.
MinFitness(f64),
/// Stop when the average fitness is bigger or equal than a specific threshold.
AvgFitness(f64),
}

impl StopCriterion for StopCriteria {
Expand All @@ -34,13 +43,31 @@ impl StopCriterion for StopCriteria {
generation: u64,
progress: f64,
n_solutions: u16,
_population_fitness: &[f64],
population_fitness: &[f64],
) -> bool {
match self {
StopCriteria::SolutionFound => n_solutions > 0,
StopCriteria::SolutionsFound(i) => n_solutions >= *i,
StopCriteria::Generation(i) => generation >= *i,
StopCriteria::Generation(g) => generation >= *g,
StopCriteria::Progress(p) => progress <= *p,
StopCriteria::GenerationAndProgress(g, p) => generation >= *g && progress <= *p,
StopCriteria::MaxFitness(f) => {
*population_fitness
.iter()
.max_by(|x, y| x.partial_cmp(&y).unwrap())
.unwrap()
>= *f
}
StopCriteria::MinFitness(f) => {
*population_fitness
.iter()
.min_by(|x, y| x.partial_cmp(&y).unwrap())
.unwrap()
>= *f
}
StopCriteria::AvgFitness(f) => {
population_fitness.iter().sum::<f64>() / population_fitness.len() as f64 >= *f
}
}
}
}

0 comments on commit 434f17e

Please sign in to comment.