Fmt.
This commit is contained in:
2018-02-16 12:04:33 +01:00
parent 849c0aacb0
commit 95f67eedf7
4 changed files with 42 additions and 57 deletions

View File

@@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"] authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies] [dependencies]
rayon = "0.9" rayon = "1.0"
[[example]] [[example]]
name = "parabole" name = "parabole"

View File

@@ -1,8 +1,8 @@
#[macro_use] #[macro_use]
extern crate criterion; extern crate criterion;
extern crate genetisk;
extern crate rand; extern crate rand;
extern crate rayon; extern crate rayon;
extern crate genetisk;
use std::cmp::Ordering; use std::cmp::Ordering;
@@ -10,7 +10,7 @@ use criterion::Criterion;
use rand::distributions::{IndependentSample, Range}; use rand::distributions::{IndependentSample, Range};
use rayon::prelude::*; use rayon::prelude::*;
use genetisk::{Individual, Wrapper, Simulation}; use genetisk::{Individual, Simulation, Wrapper};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
struct Fitness(f64); struct Fitness(f64);
@@ -35,7 +35,6 @@ impl Ord for Fitness {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Parabole { struct Parabole {
x: f64, x: f64,
@@ -45,7 +44,9 @@ impl Individual for Parabole {
type Fitness = Fitness; type Fitness = Fitness;
fn mate(&self, other: &Parabole) -> Parabole { 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) { fn mutate(&mut self) {
@@ -102,13 +103,13 @@ fn criterion_benchmark(c: &mut Criterion) {
.take(5), .take(5),
) )
.map(|(a, b)| a.individual.mate(&b.individual)) .map(|(a, b)| a.individual.mate(&b.individual))
.map(|individual| { .map(|individual| Wrapper {
Wrapper {
individual: individual, individual: individual,
fitness: None, fitness: None,
}
}) })
.for_each(|wrapper| { population.push(wrapper); }); .for_each(|wrapper| {
population.push(wrapper);
});
// Mutate all to get new children. // Mutate all to get new children.
parents parents

View File

@@ -1,15 +1,13 @@
extern crate genetisk;
extern crate rand; extern crate rand;
extern crate rayon; extern crate rayon;
extern crate genetisk;
use std::cmp::Ordering; use std::cmp::Ordering;
use rand::distributions::{IndependentSample, Range}; use rand::distributions::{IndependentSample, Range};
use rayon::prelude::*; use rayon::prelude::*;
use genetisk::{Individual, Wrapper, MaximizeSelector, MinimizeSelector, Select, Simulation}; use genetisk::{Individual, MaximizeSelector, MinimizeSelector, Select, Simulation, Wrapper};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
struct Fitness(f64); struct Fitness(f64);
@@ -34,7 +32,6 @@ impl Ord for Fitness {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Parabole { struct Parabole {
x: f64, x: f64,
@@ -44,7 +41,9 @@ impl Individual for Parabole {
type Fitness = Fitness; type Fitness = Fitness;
fn mate(&self, other: &Parabole) -> Parabole { 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) { fn mutate(&mut self) {
@@ -61,7 +60,6 @@ impl Individual for Parabole {
} }
} }
fn main() { fn main() {
let initial = (0..300) let initial = (0..300)
.map(|i| Parabole { x: i as f64 }) .map(|i| Parabole { x: i as f64 })
@@ -91,13 +89,13 @@ fn main() {
.take(5), .take(5),
) )
.map(|(a, b)| a.individual.mate(&b.individual)) .map(|(a, b)| a.individual.mate(&b.individual))
.map(|individual| { .map(|individual| Wrapper {
Wrapper {
individual: individual, individual: individual,
fitness: None, fitness: None,
}
}) })
.for_each(|wrapper| { population.push(wrapper); }); .for_each(|wrapper| {
population.push(wrapper);
});
// Mutate all to get new children. // Mutate all to get new children.
parents parents

View File

@@ -1,9 +1,7 @@
extern crate rayon; extern crate rayon;
use rayon::prelude::*; use rayon::prelude::*;
pub trait Individual: Send { pub trait Individual: Send {
type Fitness: Send + Ord; type Fitness: Send + Ord;
@@ -13,7 +11,6 @@ pub trait Individual: Send {
fn mutate(&mut self); fn mutate(&mut self);
} }
pub trait Select<T>: Send pub trait Select<T>: Send
where where
T: Individual, T: Individual,
@@ -21,7 +18,6 @@ where
fn select(&self, population: Vec<Wrapper<T>>) -> Vec<Wrapper<T>>; fn select(&self, population: Vec<Wrapper<T>>) -> Vec<Wrapper<T>>;
} }
pub struct MaximizeSelector { pub struct MaximizeSelector {
count: usize, count: usize,
} }
@@ -43,7 +39,6 @@ where
} }
} }
pub struct MinimizeSelector { pub struct MinimizeSelector {
count: usize, count: usize,
} }
@@ -65,7 +60,6 @@ where
} }
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Wrapper<T> pub struct Wrapper<T>
where where
@@ -75,7 +69,6 @@ where
pub fitness: Option<T::Fitness>, pub fitness: Option<T::Fitness>,
} }
pub struct Simulation<T> pub struct Simulation<T>
where where
T: Individual, T: Individual,
@@ -92,17 +85,17 @@ where
initial initial
.into_iter() .into_iter()
.map(|individual| { .map(|individual| Wrapper {
Wrapper {
individual: individual, individual: individual,
fitness: None, fitness: None,
}
}) })
.for_each(|wrapper| { .for_each(|wrapper| {
population.push(wrapper); population.push(wrapper);
}); });
Simulation { population: population } Simulation {
population: population,
}
} }
#[inline] #[inline]
@@ -110,13 +103,10 @@ where
self.population self.population
.iter_mut() .iter_mut()
.filter(|wrapper| wrapper.fitness.is_none()) .filter(|wrapper| wrapper.fitness.is_none())
.for_each(|wrapper| { .for_each(|wrapper| wrapper.fitness = Some(wrapper.individual.fitness()));
wrapper.fitness = Some(wrapper.individual.fitness())
});
self.population.sort_unstable_by( self.population
|a, b| b.fitness.cmp(&a.fitness), .sort_unstable_by(|a, b| b.fitness.cmp(&a.fitness));
);
} }
pub fn evolve<F>(&mut self, func: F) pub fn evolve<F>(&mut self, func: F)
@@ -133,7 +123,6 @@ where
} }
} }
pub struct ParSimulation<T> pub struct ParSimulation<T>
where where
T: Individual, T: Individual,
@@ -150,15 +139,15 @@ where
initial initial
.into_par_iter() .into_par_iter()
.map(|individual| { .map(|individual| Wrapper {
Wrapper {
individual: individual, individual: individual,
fitness: None, fitness: None,
}
}) })
.collect_into(&mut population); .collect_into_vec(&mut population);
ParSimulation { population: population } ParSimulation {
population: population,
}
} }
#[inline] #[inline]
@@ -166,13 +155,10 @@ where
self.population self.population
.par_iter_mut() .par_iter_mut()
.filter(|wrapper| wrapper.fitness.is_none()) .filter(|wrapper| wrapper.fitness.is_none())
.for_each(|wrapper| { .for_each(|wrapper| wrapper.fitness = Some(wrapper.individual.fitness()));
wrapper.fitness = Some(wrapper.individual.fitness())
});
self.population.par_sort_unstable_by( self.population
|a, b| b.fitness.cmp(&a.fitness), .par_sort_unstable_by(|a, b| b.fitness.cmp(&a.fitness));
);
} }
pub fn evolve<F>(&mut self, func: F) pub fn evolve<F>(&mut self, func: F)