Generation

Struct Generation 

Source
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,

Source

pub fn score(&self) -> &Score

Source

pub fn front(&self) -> Option<&Front<Phenotype<C>>>

Source

pub fn value(&self) -> &T

Source

pub fn index(&self) -> usize

Source

pub fn metrics(&self) -> &MetricSet

Source

pub fn objective(&self) -> &Objective

Source

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.

Source

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.

Source

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.

Source

pub fn time(&self) -> Duration

Source

pub fn seconds(&self) -> f64

Trait Implementations§

Source§

impl<C, T> Clone for Generation<C, T>
where C: Chromosome + Clone, T: Clone,

Source§

fn clone(&self) -> Generation<C, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C, T> Debug for Generation<C, T>
where C: Chromosome, T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<C, T> From<&Context<C, T>> for Generation<C, T>
where C: Chromosome + Clone, T: Clone,

Source§

fn from(context: &Context<C, T>) -> Generation<C, T>

Converts to this type from the input type.
Source§

impl<C, T> FromIterator<Generation<C, T>> for Front<Phenotype<C>>
where C: Chromosome + Clone,

Source§

fn from_iter<I>(iter: I) -> Front<Phenotype<C>>
where I: IntoIterator<Item = Generation<C, T>>,

Creates a value from an iterator. Read more
Source§

impl<C, T> Scored for Generation<C, T>
where C: Chromosome,

Source§

fn score(&self) -> Option<&Score>

Auto Trait Implementations§

§

impl<C, T> Freeze for Generation<C, T>
where T: Freeze,

§

impl<C, T> !RefUnwindSafe for Generation<C, T>

§

impl<C, T> Send for Generation<C, T>
where T: Send,

§

impl<C, T> Sync for Generation<C, T>
where T: Sync,

§

impl<C, T> Unpin for Generation<C, T>
where T: Unpin, C: Unpin,

§

impl<C, T> !UnwindSafe for Generation<C, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more