Struct GeneticSim

Source
pub struct GeneticSim<F, NG, G>
where F: FitnessFn<G>, NG: NextgenFn<G>,
{ pub genomes: Vec<G>, /* private fields */ }
Expand description

The simulation controller.

use genetic_rs_common::prelude::*;

#[derive(Debug, Clone)]
struct MyGenome {
    a: f32,
    b: f32,
}

impl RandomlyMutable for MyGenome {
    fn mutate(&mut self, rate: f32, rng: &mut impl rand::Rng) {
        self.a += rng.gen::<f32>() * rate;
        self.b += rng.gen::<f32>() * rate;
    }
}

impl DivisionReproduction for MyGenome {
    fn divide(&self, rng: &mut impl rand::Rng) -> Self {
        let mut child = self.clone();
        child.mutate(0.25, rng); // you'll generally want to use a constant mutation rate for mutating children.
        child
    }
}

impl Prunable for MyGenome {} // if we wanted to, we could implement the `despawn` function to run any cleanup code as needed. in this example, though, we do not need it.

impl GenerateRandom for MyGenome {
    fn gen_random(rng: &mut impl rand::Rng) -> Self {
        Self {
            a: rng.gen(),
            b: rng.gen(),
        }
    }
}

fn main() {
    let my_fitness_fn = |e: &MyGenome| {
        e.a * e.b // should result in genomes increasing their value
    };

    let mut rng = rand::thread_rng();

    let mut sim = GeneticSim::new(
        Vec::gen_random(&mut rng, 1000),
        my_fitness_fn,
        division_pruning_nextgen,
    );

    for _ in 0..100 {
        // if this were a more complex simulation, you might test genomes in `sim.genomes` between `next_generation` calls to provide a more accurate reward.
        sim.next_generation();
    }

    dbg!(sim.genomes);
}

Fields§

§genomes: Vec<G>

The current population of genomes

Implementations§

Source§

impl<F, NG, G> GeneticSim<F, NG, G>
where F: FitnessFn<G>, NG: NextgenFn<G>,

Source

pub fn new( starting_genomes: Vec<G>, fitness: F, next_gen: NG, ) -> GeneticSim<F, NG, G>

Creates a GeneticSim with a given population of starting_genomes (the size of which will be retained), a given fitness function, and a given nextgen function.

Source

pub fn next_generation(&mut self)

Uses the next_gen provided in GeneticSim::new to create the next generation of genomes.

Auto Trait Implementations§

§

impl<F, NG, G> Freeze for GeneticSim<F, NG, G>
where F: Freeze, NG: Freeze,

§

impl<F, NG, G> RefUnwindSafe for GeneticSim<F, NG, G>

§

impl<F, NG, G> Send for GeneticSim<F, NG, G>
where F: Send, NG: Send, G: Send,

§

impl<F, NG, G> Sync for GeneticSim<F, NG, G>
where F: Sync, NG: Sync, G: Sync,

§

impl<F, NG, G> Unpin for GeneticSim<F, NG, G>
where F: Unpin, NG: Unpin, G: Unpin,

§

impl<F, NG, G> UnwindSafe for GeneticSim<F, NG, G>
where F: UnwindSafe, NG: UnwindSafe, G: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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