use super::*;
use pheno::{Fitness, Phenotype};
use rayon::prelude::*;
#[derive(Clone, Copy, Debug)]
pub struct UnstableMaximizeSelector {
count: usize,
}
impl UnstableMaximizeSelector {
pub fn new(count: usize) -> UnstableMaximizeSelector {
UnstableMaximizeSelector { count }
}
}
impl<T, F> Selector<T, F> for UnstableMaximizeSelector
where
T: Phenotype<F>,
F: Fitness,
T: Send,
T: Sync,
{
fn select<'a>(&self, population: &'a [T]) -> Result<Parents<&'a T>, String> {
if self.count == 0 || self.count % 2 != 0 || self.count * 2 >= population.len() {
return Err(format!(
"Invalid parameter `count`: {}. Should be larger than zero, a \
multiple of two and less than half the population size.",
self.count
));
}
let mut borrowed: Vec<&T> = population.iter().collect();
borrowed.par_sort_unstable_by(|x, y| y.fitness().cmp(&x.fitness()));
let mut index = 0;
let mut result: Parents<&T> = Vec::new();
while index < self.count {
result.push((borrowed[index], borrowed[index + 1]));
index += 2;
}
Ok(result)
}
}
#[cfg(test)]
mod tests {
use pheno::*;
use sim::select::*;
use test::Test;
#[test]
fn test_count_zero() {
let selector = UnstableMaximizeSelector::new(0);
let population: Vec<Test> = (0..100).map(|i| Test { f: i }).collect();
assert!(selector.select(&population).is_err());
}
#[test]
fn test_count_odd() {
let selector = UnstableMaximizeSelector::new(5);
let population: Vec<Test> = (0..100).map(|i| Test { f: i }).collect();
assert!(selector.select(&population).is_err());
}
#[test]
fn test_count_too_large() {
let selector = UnstableMaximizeSelector::new(100);
let population: Vec<Test> = (0..100).map(|i| Test { f: i }).collect();
assert!(selector.select(&population).is_err());
}
#[test]
fn test_result_size() {
let selector = UnstableMaximizeSelector::new(20);
let population: Vec<Test> = (0..100).map(|i| Test { f: i }).collect();
assert_eq!(20, selector.select(&population).unwrap().len() * 2);
}
#[test]
fn test_result_ok() {
let selector = UnstableMaximizeSelector::new(20);
let population: Vec<Test> = (0..100).map(|i| Test { f: i }).collect();
assert_eq!(selector.select(&population).unwrap()[0].0.fitness().f, 99);
}
#[test]
fn test_contains_best() {
let selector = UnstableMaximizeSelector::new(2);
let population: Vec<Test> = (0..100).map(|i| Test { f: i }).collect();
let parents = selector.select(&population).unwrap()[0];
assert_eq!(
parents.0.fitness(),
population
.iter()
.max_by_key(|x| x.fitness())
.unwrap()
.fitness()
);
}
}