[][src]Trait ecosystem::Organism

pub trait Organism {
    fn fitness(&self) -> f64;
fn breed(&self, other: &Self) -> Self;
fn mutate(&mut self, rate: f64); }

An interface for breeding, mutation, and fitness evaluation functionality.

The example code in this trait's method documentation is drawn from the 'π approximator' example of this crate's repository (https://github.com/thfm/ecosystem/).

Required methods

fn fitness(&self) -> f64

Evaluates the organism's fitness.

Examples

impl Organism for PiApproximator {
    fn fitness(&self) -> f64 {
        let diff = (std::f64::consts::PI - self.value).abs();
        1.0 / diff
    }
}

fn breed(&self, other: &Self) -> Self

Creates a new child by breeding the organism with another.

Examples

impl Organism for PiApproximator {
    fn breed(&self, other: &Self) -> Self {
        Self {
            value: (self.value + other.value) / 2.0,
        }
    }
}

fn mutate(&mut self, rate: f64)

Modifies (or mutates) the organism, based on the given rate.

Examples

use rand::Rng;

impl Organism for PiApproximator {
    fn mutate(&mut self, rate: f64) {
        let change = rand::thread_rng().gen_range(-rate, rate);
        self.value += change;
    }
}
Loading content...

Implementors

Loading content...