pub struct Generation<C, T>where
C: Chromosome,{ /* private fields */ }Expand description
A Generation represents a single generation in the evolutionary process. It contains the ecosystem, best solution, index, metrics, score, objective, and optionally the Pareto front for multi-objective problems.
The Generation struct is designed to be efficient in terms of memory usage by utilizing reference counting for the ecosystem data when possible. This allows for multiple generations to share the same ecosystem data without unnecessary duplication. However, because of this, the generation’s ecosystem is treated as ‘copy on read’ if it is shared. So, the first time you access the ecosystem, it will be cloned if it is shared.
This is the main structure returned by the engine after each epoch, and it provides access to all relevant information about that generation.
§Example
use radiate_core::*;
use radiate_engines::*;
use std::time::Duration;
let engine = GeneticEngine::builder()
.codec(FloatChromosome::from((10, 0.0..1.0)))
.fitness_fn(|vec: Vec<f32>| -vec.iter().map(|x| x * x).sum::<f32>())
.build();
let mut generation = engine.iter().take(10).last().unwrap();
{
// triggers a clone of the ecosystem if it is shared. it is in this case.
let ecosystem: &Ecosystem<FloatChromosome> = generation.ecosystem();
}
{
// Would trigger a clone of the ecosystem if it is shared. It is NOT in this case
// because it was just converted to an owned ecosystem above.
let population: &Population<FloatChromosome> = generation.population();
assert!(population.len() == 100);
}
let solution: &Vec<f32> = generation.value();
let index: usize = generation.index();
let score: &Score = generation.score();
let time: Duration = generation.time();
assert!(solution.len() == 10);
assert!(index == 10);Implementations§
Source§impl<C, T> Generation<C, T>where
C: Chromosome,
impl<C, T> Generation<C, T>where
C: Chromosome,
pub fn score(&self) -> &Score
pub fn front(&self) -> Option<&Front<Phenotype<C>>>
pub fn value(&self) -> &T
pub fn index(&self) -> usize
pub fn metrics(&self) -> &MetricSet
pub fn objective(&self) -> &Objective
Sourcepub fn ecosystem(&mut self) -> &Ecosystem<C>where
C: Clone,
pub fn ecosystem(&mut self) -> &Ecosystem<C>where
C: Clone,
Access the ecosystem, cloning it if it is shared. When this is called, if the ecosystem is in the [EcosystemSnapshot::Shared] variant, it will be cloned into the [EcosystemSnapshot::Owned] variant for future accesses. When the generation is created from a [Context], the ecosystem is always in the shared variant to avoid unnecessary cloning of the ecosystem.
Sourcepub fn population(&mut self) -> &Population<C>where
C: Clone,
pub fn population(&mut self) -> &Population<C>where
C: Clone,
Access the population from the ecosystem. Just like Generation::ecosystem, if the ecosystem is shared, it will be cloned on first access.
Sourcepub fn species(&mut self) -> Option<&[Species<C>]>where
C: Clone,
pub fn species(&mut self) -> Option<&[Species<C>]>where
C: Clone,
Access the species from the ecosystem. Just like Generation::ecosystem, if the ecosystem is shared, it will be cloned on first access.
pub fn time(&self) -> Duration
pub fn seconds(&self) -> f64
Trait Implementations§
Source§impl<C, T> Clone for Generation<C, T>
impl<C, T> Clone for Generation<C, T>
Source§fn clone(&self) -> Generation<C, T>
fn clone(&self) -> Generation<C, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more