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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
pub mod vec_float;
use crate::genetic::{Population, Selection};
pub struct KillFitnessNaN;
impl<T: Clone> Selection<T> for KillFitnessNaN {
fn kill(&mut self, population: &mut Population<T>) {
for individual in population.iter_mut() {
if !individual.get_fitness().is_finite() {
individual.kill();
}
}
}
}
impl KillFitnessNaN {
pub fn new() -> Self {
Self
}
}
pub struct LimitPopulation {
max_count: usize,
}
impl LimitPopulation {
pub fn new(max_count: usize) -> Self {
Self { max_count }
}
pub fn set_limit(&mut self, max_count: usize) {
self.max_count = max_count;
}
}
impl<T: Clone> Selection<T> for LimitPopulation {
fn kill(&mut self, population: &mut Population<T>) {
let alive_count = population.len_alive();
if alive_count > self.max_count {
kill_worst(population, alive_count - self.max_count);
}
}
}
pub fn kill_worst<T: Clone>(population: &mut Population<T>, count: usize) {
let mut kill_list: Vec<usize> = Vec::with_capacity(count);
kill_list.push(0);
let mut best_index = 0;
let mut best_fitness = population[kill_list[best_index]].get_fitness();
for n in 1..population.len() {
if !population[n].is_alive() {
continue;
}
if kill_list.len() < count {
kill_list.push(n);
if population[n].get_fitness() < best_fitness {
best_index = kill_list.len() - 1;
}
} else {
if population[n].get_fitness() > best_fitness {
kill_list[best_index] = n;
best_index = 0;
best_fitness = population[kill_list[best_index]].get_fitness();
for m in 1..kill_list.len() {
if population[kill_list[m]].get_fitness() < best_fitness {
best_index = m;
best_fitness = population[kill_list[best_index]].get_fitness();
}
}
}
}
}
for n in kill_list {
population[n].kill();
}
}