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>
impl<O: Organism + Send + Sync> Ecosystem<O>
Sourcepub fn new(organisms: Vec<O>) -> Self
pub fn new(organisms: Vec<O>) -> Self
Creates a new ecosystem with the given organisms.
Examples found in repository?
More 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}
Sourcepub fn fittest(&self) -> &O
pub fn fittest(&self) -> &O
Returns the organism in the ecosystem with the highest fitness.
Examples found in repository?
More 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}
Sourcepub fn breed_next_generation(&mut self, mutation_rate: f64)
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?
More 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more