Crate roulette_wheel [−] [src]
A Little implementation of the roulette-wheel principle, RouletteWheel<T>
.
https://wikipedia.org/wiki/Fitness_proportionate_selection
Examples usages
use roulette_wheel::RouletteWheel; fn evaluate(individual: &i32) -> f32 { *individual as f32 } // mmm...! let population: Vec<_> = (1..10).into_iter().collect(); let fitnesses: Vec<_> = population.iter().map(|ind| evaluate(ind)).collect(); let rw: RouletteWheel<_> = fitnesses.into_iter().zip(population).collect(); // let's collect the individuals in the order in which the roulette wheel gives them let individuals: Vec<_> = rw.into_iter().map(|(_, ind)| ind).collect(); // rw.select_iter() will not consume the roulette wheel // while rw.into_iter() will ! fn crossover(mother: &i32, _father: &i32) -> i32 { mother.clone() } // unimplemented!() // now merge each individual by couples let new_population: Vec<_> = individuals.chunks(2) .filter(|couple| couple.len() == 2) .map(|couple| { let (mother, father) = (couple[0], couple[1]); crossover(&mother, &father) // note: for this example we return only one individual, // the population will shrink // .flat_map() can resolve this issue }).collect();
Structs
IntoSelectIter |
An iterator that moves out of a RouletteWheel. |
RouletteWheel |
A roulette-wheel container |
SelectIter |
Immutable RouletteWheel iterator |