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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
//! The search goal to optimize towards (maximize or minimize).
//!
//! Each problem will usually have its own specific [Fitness] function, therefore you need to
//! implement it yourself. Because the [Fitness] function is specific, it is also bound to the
//! [Genotype] through a trait attribute (no reason to make it generic, as the client implements for
//! a single [Genotype] type).
//!
//! See [Fitness] Trait for examples and further documentation
pub mod placeholders;
pub mod prelude;
use crate::chromosome::Chromosome;
use crate::genotype::Genotype;
use crate::population::Population;
use crate::strategy::{StrategyAction, StrategyState};
use rayon::prelude::*;
use std::cell::RefCell;
use std::time::Instant;
use thread_local::ThreadLocal;
/// Use isize for easy handling of scores (ordering, comparing) as floats are tricky in that regard.
pub type FitnessValue = isize;
#[derive(Copy, Clone, Debug)]
pub enum FitnessOrdering {
Maximize,
Minimize,
}
/// This is just a shortcut for `Self::Genotype`
pub type FitnessGenotype<F> = <F as Fitness>::Genotype;
/// This is just a shortcut for `<Self::Genotype as Genotype>::Chromosome`
pub type FitnessChromosome<F> = <<F as Fitness>::Genotype as Genotype>::Chromosome;
/// This is just a shortcut for `Population<<Self::Genotype as Genotype::Chromosome>`
pub type FitnessPopulation<F> = Population<<<F as Fitness>::Genotype as Genotype>::Chromosome>;
/// The fitness function, is implemented as a fitness method object.
///
/// Normally the fitness returns [`Some(FitnessValue)`](FitnessValue) for the chromosome, which can be minimized or
/// maximized in the search strategy (e.g. [Evolve](crate::strategy::evolve::Evolve)) by providing the [FitnessOrdering].
///
/// If the fitness returns `None`, the chromosome is assumed invalid and taken last in the [selection](crate::select) phase (also when minimizing).
///
/// # User implementation
///
/// There are two possible levels to implement. At least one level needs to be implemented:
/// * [`calculate_for_chromosome(...) -> Option<FitnessValue>`](Fitness::calculate_for_chromosome)
/// * The standard situation, suits all strategies. Implementable with all Genotypes.
/// * Standard [Genotype]s have [GenesOwner](crate::chromosome::GenesOwner) chromosomes. These
/// chromosomes have a `genes` field, which can be read for the calculations.
/// * non-standard [Genotype]s with [GenesPointer](crate::chromosome::GenesPointer) chromosomes.
/// These chromosomes have don't have a `genes` field, so you need to retrieve the genes using
/// [genotype.genes_slice(&chromosome)](crate::genotype::Genotype::genes_slice), which can then
/// be read for the calculations. But for these types you usually don't want to reach this call
/// level, see other level below
/// * [`calculate_for_population(...) -> Vec<Option<FitnessValue>>`](Fitness::calculate_for_population)
/// * *Only overwrite for matrix Genotypes (designed for possible GPU acceleration)*
/// * If not overwritten, results in calling
/// [calculate_for_chromosome](Fitness::calculate_for_chromosome) for each chromosome in the
/// population. So it doesn't have to be implemented by default, but it is a possible point to
/// intercept with a custom implementation where the whole population data is available.
/// * Only for [Genotype] with [GenesPointer](crate::chromosome::GenesPointer) chromosomes. These
/// chromosomes don't have a `genes` field to read, but a `row_id`. The matrix [Genotype] has a contiguous
/// memory `data` field with all the data, which can be calculated in one go.
/// * [DynamicMatrixGenotype](crate::genotype::DynamicMatrixGenotype)
/// * [StaticMatrixGenotype](crate::genotype::StaticMatrixGenotype)
/// * The order and length of the rows in the genotype data matrix needs to be preserved in the
/// returned vector as it matches the row_id on the chromosome
/// * The order and length of the population does not matter at all and will most likely not align.
/// The population is provided, because the full genotype matrix data also contains untainted
/// chromosomes which already have a fitness_score (and will not be updated). The results for
/// these chromosomes will be ignored, thus these do not have to be recalculated, so knowing
/// which ones might be relevant (or not). The order of the results still need to align, so if the
/// calculation is skipped, a `None` value should be inserted in the results to keep the order
/// and length aligned.
///
/// The strategies use different levels of calls in [Fitness]. So you cannot always just intercept at
/// [calculate_for_population](Fitness::calculate_for_population) and be sure
/// [calculate_for_chromosome](Fitness::calculate_for_chromosome) will not be called:
///
/// * Population level calculations (calling
/// [calculate_for_chromosome](Fitness::calculate_for_chromosome) indirectly through
/// [calculate_for_population](Fitness::calculate_for_population), if not overwritten)
/// * [Evolve](crate::strategy::evolve::Evolve)
/// * [HillClimb](crate::strategy::hill_climb::HillClimb) with [SteepestAscent](crate::strategy::hill_climb::HillClimbVariant::SteepestAscent)
/// * Chromosome level calculations (calling
/// [calculate_for_chromosome](Fitness::calculate_for_chromosome) directly, bypassing
/// [calculate_for_population](Fitness::calculate_for_population) entirely)
/// * [Permutate](crate::strategy::permutate::Permutate)
/// * [HillClimb](crate::strategy::hill_climb::HillClimb) with [Stochastic](crate::strategy::hill_climb::HillClimbVariant::Stochastic)
///
/// Therefore, additionally, you might need to implement
/// [calculate_for_chromosome](Fitness::calculate_for_chromosome) for
/// [GenesPointer](crate::chromosome::GenesPointer) chromosomes. This is sometimes needed when
/// testing out different strategies with different call levels. Problably no longer needed once
/// settled on a strategy.
///
/// # Panics
///
/// [calculate_for_chromosome](Fitness::calculate_for_chromosome) has a default implementation which panics, because it doesn't need to
/// be implemented for genotypes which implement [calculate_for_population](Fitness::calculate_for_population). Will panic if reached and not implemented.
///
/// # Example (calculate_for_chromosome, standard GenesOwner chromosome):
/// ```rust
/// use genetic_algorithm::fitness::prelude::*;
///
/// #[derive(Clone, Debug)]
/// pub struct CountTrue;
/// impl Fitness for CountTrue {
/// type Genotype = BinaryGenotype;
/// fn calculate_for_chromosome(
/// &mut self,
/// chromosome: &FitnessChromosome<Self>,
/// _genotype: &FitnessGenotype<Self>
/// ) -> Option<FitnessValue> {
/// Some(chromosome.genes.iter().filter(|&value| *value).count() as FitnessValue)
/// }
/// }
/// ```
///
/// # Example (calculate_for_population, static matrix calculation, GenesPointer chromosome):
/// ```rust
/// use genetic_algorithm::fitness::prelude::*;
/// use genetic_algorithm::strategy::evolve::prelude::*;
///
/// #[derive(Clone, Debug)]
/// pub struct SumStaticMatrixGenes;
/// impl Fitness for SumStaticMatrixGenes {
/// type Genotype = StaticMatrixGenotype<u16, 10, 100>;
/// fn calculate_for_population(
/// &mut self,
/// population: &Population<StaticMatrixChromosome>,
/// genotype: &FitnessGenotype<Self>,
/// ) -> Vec<Option<FitnessValue>> {
/// // pure matrix data calculation on [[T; N] M]
/// // the order of the rows needs to be preserved as it matches the row_id on the chromosome
/// // the order of the population does not matter at all and will most likely not align
/// genotype
/// .data
/// .iter()
/// .map(|genes| {
/// genes
/// .iter()
/// .sum::<u16>() as FitnessValue
/// })
/// .map(Some)
/// .collect()
/// }
/// }
/// ```
///
/// # Example (calculate_for_population, dynamic matrix calculation, GenesPointer chromosome):
/// ```rust
/// use genetic_algorithm::fitness::prelude::*;
/// use genetic_algorithm::strategy::evolve::prelude::*;
///
/// #[derive(Clone, Debug)]
/// pub struct SumDynamicMatrixGenes;
/// impl Fitness for SumDynamicMatrixGenes {
/// type Genotype = DynamicMatrixGenotype<u16>;
/// fn calculate_for_population(
/// &mut self,
/// population: &Population<DynamicMatrixChromosome>,
/// genotype: &FitnessGenotype<Self>,
/// ) -> Vec<Option<FitnessValue>> {
/// // pure matrix data calculation on Vec<T> with genes_size step
/// // the order of the rows needs to be preserved as it matches the row_id on the chromosome
/// // the order of the population does not matter at all and will most likely not align
/// genotype
/// .data
/// .chunks(genotype.genes_size())
/// .map(|genes| {
/// genes
/// .iter()
/// .sum::<u16>() as FitnessValue
/// })
/// .map(Some)
/// .collect()
/// }
/// }
/// ```
///
/// # Example (calculate_for_chromosome, matrix fall back, GenesPointer chromosome):
/// *Note: For exploration purposes when switching stratgies a lot, not really used in final implementation*
/// ```rust
/// use genetic_algorithm::fitness::prelude::*;
/// use genetic_algorithm::strategy::hill_climb::prelude::*;
///
/// #[derive(Clone, Debug)]
/// pub struct SumStaticMatrixGenes;
/// impl Fitness for SumStaticMatrixGenes {
/// type Genotype = StaticMatrixGenotype<u16, 10, 100>;
/// fn calculate_for_chromosome(
/// &mut self,
/// chromosome: &FitnessChromosome<Self>,
/// genotype: &Self::Genotype,
/// ) -> Option<FitnessValue> {
/// let score = genotype.genes_slice(chromosome).iter().sum::<u16>();
/// Some(score as FitnessValue)
/// }
/// }
///
pub trait Fitness: Clone + Send + Sync + std::fmt::Debug {
type Genotype: Genotype;
fn call_for_state_population<S: StrategyState<Self::Genotype>>(
&mut self,
state: &mut S,
genotype: &Self::Genotype,
thread_local: Option<&ThreadLocal<RefCell<Self>>>,
) {
let now = Instant::now();
self.call_for_population(state.population_as_mut(), genotype, thread_local);
state.add_duration(StrategyAction::Fitness, now.elapsed());
}
fn call_for_state_chromosome<S: StrategyState<Self::Genotype>>(
&mut self,
state: &mut S,
genotype: &Self::Genotype,
) {
if let Some(chromosome) = state.chromosome_as_mut() {
let now = Instant::now();
self.call_for_chromosome(chromosome, genotype);
state.add_duration(StrategyAction::Fitness, now.elapsed());
}
}
/// Pass thread_local for external control of fitness caching in multithreading
fn call_for_population(
&mut self,
population: &mut FitnessPopulation<Self>,
genotype: &Self::Genotype,
thread_local: Option<&ThreadLocal<RefCell<Self>>>,
) {
let fitness_scores = self.calculate_for_population(population, genotype);
if fitness_scores.is_empty() {
if let Some(thread_local) = thread_local {
population
.chromosomes
.par_iter_mut()
.filter(|c| c.fitness_score().is_none())
.for_each_init(
|| {
thread_local
.get_or(|| std::cell::RefCell::new(self.clone()))
.borrow_mut()
},
|fitness, chromosome| {
fitness.call_for_chromosome(chromosome, genotype);
},
);
} else {
population
.chromosomes
.iter_mut()
.filter(|c| c.fitness_score().is_none())
.for_each(|c| self.call_for_chromosome(c, genotype));
}
} else {
genotype.update_population_fitness_scores(population, fitness_scores);
}
}
fn call_for_chromosome(
&mut self,
chromosome: &mut FitnessChromosome<Self>,
genotype: &Self::Genotype,
) {
chromosome.set_fitness_score(self.calculate_for_chromosome(chromosome, genotype));
}
/// Optional interception point for client implementation.
///
/// The order and length of the results need to align with the order and length of the genotype data matrix.
/// The order and length of the population does not matter at all and will most likely not align.
fn calculate_for_population(
&mut self,
_population: &FitnessPopulation<Self>,
_genotype: &Self::Genotype,
) -> Vec<Option<FitnessValue>> {
Vec::new()
}
/// Optional interception point for client implementation
fn calculate_for_chromosome(
&mut self,
_chromosome: &FitnessChromosome<Self>,
_genotype: &Self::Genotype,
) -> Option<FitnessValue> {
panic!("Implement calculate_for_chromosome for your Fitness (or higher in the call stack when using StaticMatrixGenotype)");
}
}