Struct Ecosystem

Source
pub struct Ecosystem<O: Organism> {
    pub organisms: Vec<O>,
    pub generation: u32,
}
Expand description

A collection of organisms.

Fields§

§organisms: Vec<O>

A vector containing the organisms.

§generation: u32

The current generation number.

Implementations§

Source§

impl<O: Organism + Send + Sync> Ecosystem<O>

Source

pub fn new(organisms: Vec<O>) -> Self

Creates a new ecosystem with the given organisms.

Examples found in repository?
examples/monkeys.rs (line 70)
68fn main() {
69    let monkeys: Vec<Monkey> = (0..POPULATION_COUNT).map(|_| Monkey::new()).collect();
70    let mut ecosystem = Ecosystem::new(monkeys);
71
72    while ecosystem.fittest().phrase != String::from(Monkey::TARGET_PHRASE) {
73        ecosystem.breed_next_generation(MUTATION_RATE);
74        println!("{}", ecosystem.fittest().phrase);
75    }
76}
More examples
Hide additional examples
examples/pi_approx.rs (line 39)
32fn main() {
33    let mut rng = rand::thread_rng();
34    let approximators: Vec<PiApproximator> = (0..POPULATION_COUNT)
35        .map(|_| PiApproximator {
36            value: rng.gen_range(-MAX_INITIAL_VALUE, MAX_INITIAL_VALUE),
37        })
38        .collect();
39    let mut ecosystem = Ecosystem::new(approximators);
40    for _ in 0..GENERATIONS {
41        ecosystem.breed_next_generation(MUTATION_RATE);
42        println!("{}", ecosystem.fittest().value);
43    }
44}
Source

pub fn fittest(&self) -> &O

Returns the organism in the ecosystem with the highest fitness.

Examples found in repository?
examples/monkeys.rs (line 72)
68fn main() {
69    let monkeys: Vec<Monkey> = (0..POPULATION_COUNT).map(|_| Monkey::new()).collect();
70    let mut ecosystem = Ecosystem::new(monkeys);
71
72    while ecosystem.fittest().phrase != String::from(Monkey::TARGET_PHRASE) {
73        ecosystem.breed_next_generation(MUTATION_RATE);
74        println!("{}", ecosystem.fittest().phrase);
75    }
76}
More examples
Hide additional examples
examples/pi_approx.rs (line 42)
32fn main() {
33    let mut rng = rand::thread_rng();
34    let approximators: Vec<PiApproximator> = (0..POPULATION_COUNT)
35        .map(|_| PiApproximator {
36            value: rng.gen_range(-MAX_INITIAL_VALUE, MAX_INITIAL_VALUE),
37        })
38        .collect();
39    let mut ecosystem = Ecosystem::new(approximators);
40    for _ in 0..GENERATIONS {
41        ecosystem.breed_next_generation(MUTATION_RATE);
42        println!("{}", ecosystem.fittest().value);
43    }
44}
Source

pub fn breed_next_generation(&mut self, mutation_rate: f64)

Creates the next generation of organisms through the breeding of suitable organisms.

Examples found in repository?
examples/monkeys.rs (line 73)
68fn main() {
69    let monkeys: Vec<Monkey> = (0..POPULATION_COUNT).map(|_| Monkey::new()).collect();
70    let mut ecosystem = Ecosystem::new(monkeys);
71
72    while ecosystem.fittest().phrase != String::from(Monkey::TARGET_PHRASE) {
73        ecosystem.breed_next_generation(MUTATION_RATE);
74        println!("{}", ecosystem.fittest().phrase);
75    }
76}
More examples
Hide additional examples
examples/pi_approx.rs (line 41)
32fn main() {
33    let mut rng = rand::thread_rng();
34    let approximators: Vec<PiApproximator> = (0..POPULATION_COUNT)
35        .map(|_| PiApproximator {
36            value: rng.gen_range(-MAX_INITIAL_VALUE, MAX_INITIAL_VALUE),
37        })
38        .collect();
39    let mut ecosystem = Ecosystem::new(approximators);
40    for _ in 0..GENERATIONS {
41        ecosystem.breed_next_generation(MUTATION_RATE);
42        println!("{}", ecosystem.fittest().value);
43    }
44}

Auto Trait Implementations§

§

impl<O> Freeze for Ecosystem<O>

§

impl<O> RefUnwindSafe for Ecosystem<O>
where O: RefUnwindSafe,

§

impl<O> Send for Ecosystem<O>
where O: Send,

§

impl<O> Sync for Ecosystem<O>
where O: Sync,

§

impl<O> Unpin for Ecosystem<O>
where O: Unpin,

§

impl<O> UnwindSafe for Ecosystem<O>
where O: 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