mod mass_deduplication;
mod mass_degeneration;
mod mass_extinction;
mod mass_genesis;
use crate::chromosome::Chromosome;
mod noop;
mod wrapper;
pub use self::mass_deduplication::MassDeduplication as ExtensionMassDeduplication;
pub use self::mass_degeneration::MassDegeneration as ExtensionMassDegeneration;
pub use self::mass_extinction::MassExtinction as ExtensionMassExtinction;
pub use self::mass_genesis::MassGenesis as ExtensionMassGenesis;
pub use self::noop::Noop as ExtensionNoop;
pub use self::wrapper::Wrapper as ExtensionWrapper;
use crate::genotype::{EvolveGenotype, Genotype};
use crate::strategy::evolve::{EvolveConfig, EvolveState};
use crate::strategy::StrategyReporter;
use rand::Rng;
pub type ExtensionGenotype<E> = <E as Extension>::Genotype;
pub type ExtensionEvolveState<E> = EvolveState<<E as Extension>::Genotype>;
pub type ExtensionAllele<E> = <<E as Extension>::Genotype as Genotype>::Allele;
pub trait Extension: Clone + Send + Sync + std::fmt::Debug {
type Genotype: EvolveGenotype;
#[deprecated(since = "0.26.0", note = "use `after_selection_complete` instead")]
fn call<R: Rng, SR: StrategyReporter<Genotype = Self::Genotype>>(
&mut self,
genotype: &mut Self::Genotype,
state: &mut EvolveState<Self::Genotype>,
config: &EvolveConfig,
reporter: &mut SR,
rng: &mut R,
) {
self.after_selection_complete(genotype, state, config, reporter, rng);
}
fn after_selection_complete<R: Rng, SR: StrategyReporter<Genotype = Self::Genotype>>(
&mut self,
_genotype: &mut Self::Genotype,
_state: &mut EvolveState<Self::Genotype>,
_config: &EvolveConfig,
_reporter: &mut SR,
_rng: &mut R,
) {
}
fn after_crossover_complete<R: Rng, SR: StrategyReporter<Genotype = Self::Genotype>>(
&mut self,
_genotype: &mut Self::Genotype,
_state: &mut EvolveState<Self::Genotype>,
_config: &EvolveConfig,
_reporter: &mut SR,
_rng: &mut R,
) {
}
fn after_mutation_complete<R: Rng, SR: StrategyReporter<Genotype = Self::Genotype>>(
&mut self,
_genotype: &mut Self::Genotype,
_state: &mut EvolveState<Self::Genotype>,
_config: &EvolveConfig,
_reporter: &mut SR,
_rng: &mut R,
) {
}
fn after_generation_complete<R: Rng, SR: StrategyReporter<Genotype = Self::Genotype>>(
&mut self,
_genotype: &mut Self::Genotype,
_state: &mut EvolveState<Self::Genotype>,
_config: &EvolveConfig,
_reporter: &mut SR,
_rng: &mut R,
) {
}
fn extract_elite_chromosomes(
&self,
_genotype: &Self::Genotype,
state: &mut EvolveState<Self::Genotype>,
config: &EvolveConfig,
elitism_size: usize,
) -> Vec<Chromosome<ExtensionAllele<Self>>> {
let mut elite_chromosomes: Vec<Chromosome<ExtensionAllele<Self>>> =
Vec::with_capacity(elitism_size);
for index in state
.population
.best_chromosome_indices(elitism_size, config.fitness_ordering)
.into_iter()
.rev()
{
let chromosome = state.population.chromosomes.swap_remove(index);
elite_chromosomes.push(chromosome);
}
elite_chromosomes
}
fn extract_unique_elite_chromosomes(
&self,
_genotype: &Self::Genotype,
state: &mut EvolveState<Self::Genotype>,
config: &EvolveConfig,
elitism_size: usize,
) -> Vec<Chromosome<ExtensionAllele<Self>>> {
let mut elite_chromosomes: Vec<Chromosome<ExtensionAllele<Self>>> =
Vec::with_capacity(elitism_size);
for index in state
.population
.best_unique_chromosome_indices(elitism_size, config.fitness_ordering)
.into_iter()
.rev()
{
let chromosome = state.population.chromosomes.swap_remove(index);
elite_chromosomes.push(chromosome);
}
elite_chromosomes
}
fn extract_unique_chromosomes(
&self,
_genotype: &Self::Genotype,
state: &mut EvolveState<Self::Genotype>,
_config: &EvolveConfig,
) -> Vec<Chromosome<ExtensionAllele<Self>>> {
let mut unique_chromosomes: Vec<Chromosome<ExtensionAllele<Self>>> = Vec::new();
for index in state
.population
.unique_chromosome_indices()
.into_iter()
.rev()
{
let chromosome = state.population.chromosomes.swap_remove(index);
unique_chromosomes.push(chromosome);
}
unique_chromosomes
}
}
#[derive(Clone, Debug)]
pub struct ExtensionEvent(pub String);