Added ParSimulation that is using rayon.

This commit is contained in:
2018-01-03 08:02:02 +01:00
parent 22ace4415e
commit d1c17d01a6
4 changed files with 191 additions and 132 deletions

View File

@@ -90,6 +90,64 @@ where
pub fn with_population(initial: Vec<T>) -> Self {
let mut population = Vec::with_capacity(initial.len() * 2);
initial
.into_iter()
.map(|individual| {
Wrapper {
individual: individual,
fitness: None,
}
})
.for_each(|wrapper| {
population.push(wrapper);
});
Simulation { population: population }
}
#[inline]
pub fn calculate(&mut self) {
self.population
.iter_mut()
.filter(|wrapper| wrapper.fitness.is_none())
.for_each(|wrapper| {
wrapper.fitness = Some(wrapper.individual.fitness())
});
self.population.sort_unstable_by(
|a, b| b.fitness.cmp(&a.fitness),
);
}
pub fn evolve<F>(&mut self, func: F)
where
F: Fn(&[Wrapper<T>], &mut Vec<Wrapper<T>>),
{
let mut population = Vec::with_capacity(self.population.len());
func(&self.population, &mut population);
self.population = population;
self.calculate();
}
}
pub struct ParSimulation<T>
where
T: Individual,
{
pub population: Vec<Wrapper<T>>,
}
impl<T> ParSimulation<T>
where
T: Individual,
{
pub fn with_population(initial: Vec<T>) -> Self {
let mut population = Vec::with_capacity(initial.len() * 2);
initial
.into_par_iter()
.map(|individual| {
@@ -100,7 +158,7 @@ where
})
.collect_into(&mut population);
Simulation { population: population }
ParSimulation { population: population }
}
#[inline]

View File

@@ -1,131 +0,0 @@
extern crate rayon;
use rayon::prelude::*;
pub trait Individual: Send {
type Fitness: Send + Ord;
fn fitness(&self) -> Self::Fitness;
fn mate(&self, other: &Self) -> Self;
fn mutate(&mut self);
}
pub trait Select<T>: Send
where
T: Individual
{
fn select(&self, population: Vec<Wrapper<T>>) -> Vec<Wrapper<T>>;
}
pub struct MaximizeSelector {
count: usize,
}
impl MaximizeSelector {
pub fn new(count: usize) -> Self {
MaximizeSelector {
count: count,
}
}
}
impl<T> Select<T> for MaximizeSelector
where
T: Individual
{
fn select(&self, mut population: Vec<Wrapper<T>>) -> Vec<Wrapper<T>> {
population.sort_unstable_by(|a, b| b.fitness.cmp(&a.fitness));
population.into_iter().take(self.count).collect()
}
}
pub struct MinimizeSelector {
count: usize,
}
impl MinimizeSelector {
pub fn new(count: usize) -> Self {
MinimizeSelector {
count: count,
}
}
}
impl<T> Select<T> for MinimizeSelector
where
T: Individual
{
fn select(&self, mut population: Vec<Wrapper<T>>) -> Vec<Wrapper<T>> {
population.sort_unstable_by(|a, b| a.fitness.cmp(&b.fitness));
population.into_iter().take(self.count).collect()
}
}
#[derive(Clone)]
pub struct Wrapper<T>
where
T: Individual,
{
pub individual: T,
pub fitness: Option<T::Fitness>,
}
pub struct Simulation<T>
where
T: Individual,
{
pub population: Vec<Wrapper<T>>,
}
impl<T> Simulation<T>
where
T: Individual,
{
pub fn with_population(initial: Vec<T>) -> Self {
let mut population = Vec::with_capacity(initial.len() * 2);
initial
.into_par_iter()
.map(|individual| Wrapper {
individual: individual,
fitness: None,
})
.collect_into(&mut population);
Simulation {
population: population,
}
}
#[inline]
pub fn calculate(&mut self) {
self.population.par_iter_mut()
.filter(|wrapper| wrapper.fitness.is_none())
.for_each(|wrapper| wrapper.fitness = Some(wrapper.individual.fitness()));
self.population.par_sort_unstable_by(|a, b| b.fitness.cmp(&a.fitness));
}
pub fn evolve<F>(&mut self, func: F)
where
F: Fn(&[Wrapper<T>], &mut Vec<Wrapper<T>>)
{
let mut population = Vec::with_capacity(self.population.len());
func(&self.population, &mut population);
self.population = population;
self.calculate();
}
}