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

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