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>"]
[dependencies]
rayon = "0.9"
rayon = "1.0"
[[example]]
name = "parabole"

View File

@@ -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

View File

@@ -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

View File

@@ -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<T>: Send
where
T: Individual,
@@ -21,7 +18,6 @@ where
fn select(&self, population: Vec<Wrapper<T>>) -> Vec<Wrapper<T>>;
}
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<T>
where
@@ -75,7 +69,6 @@ where
pub fitness: Option<T::Fitness>,
}
pub struct Simulation<T>
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<F>(&mut self, func: F)
@@ -133,7 +123,6 @@ where
}
}
pub struct ParSimulation<T>
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<F>(&mut self, func: F)