diff --git a/Cargo.toml b/Cargo.toml index aa0c7a8..a856c4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["logaritmisk "] [dependencies] -rayon = "0.9" +rayon = "1.0" [[example]] name = "parabole" diff --git a/benches/parabole.rs b/benches/parabole.rs index 4e7ba68..7abb201 100644 --- a/benches/parabole.rs +++ b/benches/parabole.rs @@ -1,8 +1,8 @@ #[macro_use] extern crate criterion; +extern crate genetisk; extern crate rand; extern crate rayon; -extern crate genetisk; use std::cmp::Ordering; @@ -10,7 +10,7 @@ use criterion::Criterion; use rand::distributions::{IndependentSample, Range}; use rayon::prelude::*; -use genetisk::{Individual, Wrapper, Simulation}; +use genetisk::{Individual, Simulation, Wrapper}; #[derive(Clone, Copy, Debug)] struct Fitness(f64); @@ -35,7 +35,6 @@ impl Ord for Fitness { } } - #[derive(Clone, Debug)] struct Parabole { x: f64, @@ -45,7 +44,9 @@ impl Individual for Parabole { type Fitness = Fitness; fn mate(&self, other: &Parabole) -> Parabole { - Parabole { x: (self.x + other.x) / 2.0 } + Parabole { + x: (self.x + other.x) / 2.0, + } } fn mutate(&mut self) { @@ -102,13 +103,13 @@ fn criterion_benchmark(c: &mut Criterion) { .take(5), ) .map(|(a, b)| a.individual.mate(&b.individual)) - .map(|individual| { - Wrapper { - individual: individual, - fitness: None, - } + .map(|individual| Wrapper { + individual: individual, + fitness: None, }) - .for_each(|wrapper| { population.push(wrapper); }); + .for_each(|wrapper| { + population.push(wrapper); + }); // Mutate all to get new children. parents diff --git a/examples/parabole.rs b/examples/parabole.rs index 63d3da5..4e94769 100644 --- a/examples/parabole.rs +++ b/examples/parabole.rs @@ -1,15 +1,13 @@ +extern crate genetisk; extern crate rand; extern crate rayon; -extern crate genetisk; use std::cmp::Ordering; - use rand::distributions::{IndependentSample, Range}; use rayon::prelude::*; -use genetisk::{Individual, Wrapper, MaximizeSelector, MinimizeSelector, Select, Simulation}; - +use genetisk::{Individual, MaximizeSelector, MinimizeSelector, Select, Simulation, Wrapper}; #[derive(Clone, Copy, Debug)] struct Fitness(f64); @@ -34,7 +32,6 @@ impl Ord for Fitness { } } - #[derive(Clone, Debug)] struct Parabole { x: f64, @@ -44,7 +41,9 @@ impl Individual for Parabole { type Fitness = Fitness; fn mate(&self, other: &Parabole) -> Parabole { - Parabole { x: (self.x + other.x) / 2.0 } + Parabole { + x: (self.x + other.x) / 2.0, + } } fn mutate(&mut self) { @@ -61,7 +60,6 @@ impl Individual for Parabole { } } - fn main() { let initial = (0..300) .map(|i| Parabole { x: i as f64 }) @@ -91,13 +89,13 @@ fn main() { .take(5), ) .map(|(a, b)| a.individual.mate(&b.individual)) - .map(|individual| { - Wrapper { - individual: individual, - fitness: None, - } + .map(|individual| Wrapper { + individual: individual, + fitness: None, }) - .for_each(|wrapper| { population.push(wrapper); }); + .for_each(|wrapper| { + population.push(wrapper); + }); // Mutate all to get new children. parents diff --git a/src/lib.rs b/src/lib.rs index 87d3060..66ecc89 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,7 @@ extern crate rayon; - use rayon::prelude::*; - pub trait Individual: Send { type Fitness: Send + Ord; @@ -13,7 +11,6 @@ pub trait Individual: Send { fn mutate(&mut self); } - pub trait Select: Send where T: Individual, @@ -21,7 +18,6 @@ where fn select(&self, population: Vec>) -> Vec>; } - pub struct MaximizeSelector { count: usize, } @@ -43,7 +39,6 @@ where } } - pub struct MinimizeSelector { count: usize, } @@ -65,7 +60,6 @@ where } } - #[derive(Clone)] pub struct Wrapper where @@ -75,7 +69,6 @@ where pub fitness: Option, } - pub struct Simulation where T: Individual, @@ -92,17 +85,17 @@ where initial .into_iter() - .map(|individual| { - Wrapper { - individual: individual, - fitness: None, - } + .map(|individual| Wrapper { + individual: individual, + fitness: None, }) .for_each(|wrapper| { population.push(wrapper); }); - Simulation { population: population } + Simulation { + population: population, + } } #[inline] @@ -110,13 +103,10 @@ where self.population .iter_mut() .filter(|wrapper| wrapper.fitness.is_none()) - .for_each(|wrapper| { - wrapper.fitness = Some(wrapper.individual.fitness()) - }); + .for_each(|wrapper| wrapper.fitness = Some(wrapper.individual.fitness())); - self.population.sort_unstable_by( - |a, b| b.fitness.cmp(&a.fitness), - ); + self.population + .sort_unstable_by(|a, b| b.fitness.cmp(&a.fitness)); } pub fn evolve(&mut self, func: F) @@ -133,7 +123,6 @@ where } } - pub struct ParSimulation where T: Individual, @@ -150,15 +139,15 @@ where initial .into_par_iter() - .map(|individual| { - Wrapper { - individual: individual, - fitness: None, - } + .map(|individual| Wrapper { + individual: individual, + fitness: None, }) - .collect_into(&mut population); + .collect_into_vec(&mut population); - ParSimulation { population: population } + ParSimulation { + population: population, + } } #[inline] @@ -166,13 +155,10 @@ where self.population .par_iter_mut() .filter(|wrapper| wrapper.fitness.is_none()) - .for_each(|wrapper| { - wrapper.fitness = Some(wrapper.individual.fitness()) - }); + .for_each(|wrapper| wrapper.fitness = Some(wrapper.individual.fitness())); - self.population.par_sort_unstable_by( - |a, b| b.fitness.cmp(&a.fitness), - ); + self.population + .par_sort_unstable_by(|a, b| b.fitness.cmp(&a.fitness)); } pub fn evolve(&mut self, func: F)