1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use super::{Crossover, KeepParent};
use crate::chromosome::Chromosome;
use crate::genotype::Genotype;
use crate::population::Population;
use rand::distributions::{Bernoulli, Distribution};
use rand::Rng;
#[derive(Clone, Debug)]
pub struct Uniform(pub KeepParent);
impl Crossover for Uniform {
fn call<T: Genotype, R: Rng>(&self, genotype: &T, population: &mut Population<T>, rng: &mut R) {
if population.size() < 2 {
return;
}
let crossover_indexes = genotype.crossover_indexes();
let bool_sampler = Bernoulli::new(0.5).unwrap();
if self.0 {
let mut child_chromosomes: Vec<Chromosome<T>> = Vec::with_capacity(population.size());
for chunk in population.chromosomes.chunks(2) {
if let [father, mother] = chunk {
let mut child_father_genes = father.genes.clone();
let mut child_mother_genes = mother.genes.clone();
for index in &crossover_indexes {
if bool_sampler.sample(rng) {
std::mem::swap(
&mut child_father_genes[*index],
&mut child_mother_genes[*index],
);
}
}
child_chromosomes.push(Chromosome::new(child_father_genes));
child_chromosomes.push(Chromosome::new(child_mother_genes));
}
}
population.chromosomes.append(&mut child_chromosomes);
} else {
for chunk in population.chromosomes.chunks_mut(2) {
if let [father, mother] = chunk {
for index in &crossover_indexes {
if bool_sampler.sample(rng) {
std::mem::swap(&mut father.genes[*index], &mut mother.genes[*index]);
}
}
mother.taint_fitness_score();
father.taint_fitness_score();
}
}
}
}
fn require_crossover_indexes(&self) -> bool {
true
}
fn require_crossover_points(&self) -> bool {
false
}
}