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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::sync::Arc;
use cge::Network;
use cmaes_utils::*;
use threads::update_generation;
use generation::initialize_generation;
use mutation::mutate;
use options::EANT2Options;
use select::select;
use fitness::NNFitnessFunction;
pub fn eant_loop<T>(object: &T, options: EANT2Options) -> (Network, f64)
where T: 'static + NNFitnessFunction + Clone + Send + Sync
{
let cmaes_options = get_cmaes_options(options.cmaes_end_conditions).end_conditions;
let threads = options.threads as usize;
let fitness_threshold = options.fitness_threshold;
let threshold = options.similar_fitness;
let max_generations = options.max_generations;
let inputs = options.inputs;
let outputs = options.outputs;
let cmaes_runs = options.cmaes_runs;
let population_size = options.population_size;
let offspring_count = options.offspring_count;
let print = options.print_option;
let weights = options.weights;
let transfer_function = options.transfer_function;
let object = Arc::new(object.clone());
let mut g = 0;
let mut generation = initialize_generation(population_size,
offspring_count,
inputs,
outputs,
transfer_function,
object);
loop {
if print {
println!("Beginning EANT2 generation {}", g + 1);
}
update_generation(&mut generation,
&cmaes_options,
cmaes_runs,
threads);
generation = select(population_size, generation, threshold);
generation.sort_by(|a, b| a.fitness.partial_cmp(&b.fitness).expect("12"));
let best = generation[0].fitness;
if best <= fitness_threshold || g + 1 >= max_generations {
let solution = generation[0].clone();
if print {
println!("EANT2 terminated in {} generations", g + 1);
println!("Solution found with size {} and {} fitness", solution.network.size + 1, solution.fitness);
}
return (solution.network, best);
}
let mut new_individuals = Vec::new();
for individual in &generation {
for _ in 0..offspring_count {
let mut new = individual.clone();
mutate(&mut new, &weights);
new_individuals.push(new);
}
}
generation.extend_from_slice(&new_individuals);
for i in &mut generation {
for age in &mut i.ages {
*age += 1;
}
}
g += 1;
if print {
println!("Current best fitness: {}", generation[0].fitness);
}
}
}