1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//! This module contains the definition of the Age trait and the provided age functions. use age::AgeFunctions::*; /// This trait defines the manner in which old individuals are fitness decreased. pub trait Age: Send + Sync { /// Returns the minimum number of generations required to start the age unfitness. fn age_threshold(&self) -> u64; /// Returns the fitness after decrease it by age using the /// number of `age_generations` that exceeds the age threshold. /// The generation when the individual is created is the age 0. /// This function is called when `age_generations` is bigger or equal than 0. /// E.g.: /// ```ignore /// individual.age=2, age_threshold=2 -> age_generations=0 /// ``` /// ```ignore /// individual.age=1, age_threshold=2 -> age_generations=-1 (not called) /// ``` /// ```ignore /// individual.age=4, age_threshold=2 -> age_generations=2 /// ``` fn age_unfitness(&self, age_generations: u64, fitness: f64) -> f64; } /// Defines the age threshold parameter. pub struct AgeThreshold(pub u64); /// Defines the age slope parameter. pub struct AgeSlope(pub f64); /// Provided age functions. pub enum AgeFunctions { /// No age unfitness. None, /// Linear function of iterations. Linear(AgeThreshold, AgeSlope), /// Quadratic function of iterations. Quadratic(AgeThreshold, AgeSlope), } impl Age for AgeFunctions { fn age_threshold(&self) -> u64 { match self { None => 0, Linear(threshold, _) => threshold.0, Quadratic(threshold, _) => threshold.0, } } fn age_unfitness(&self, age_generations: u64, fitness: f64) -> f64 { match self { None => fitness, Linear(_, sp) => fitness - sp.0 * age_generations as f64, Quadratic(_, sp) => fitness - sp.0 * age_generations as f64 * age_generations as f64, } } }