4 releases
Uses old Rust 2015
0.2.2 | Feb 25, 2017 |
---|---|
0.2.1 | Feb 25, 2017 |
0.2.0 | Feb 24, 2017 |
0.1.0 | May 20, 2016 |
#14 in #fitness
15KB
212 lines
roulette-wheel-rs
A little implementation of roulette wheels (used in genetic algorithm for selection)
lib.rs
:
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();
Dependencies
~330–560KB