genetic_algorithm 0.27.1

A genetic algorithm implementation
Documentation
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use super::Evolve;
use crate::crossover::Crossover;
pub use crate::errors::TryFromStrategyBuilderError as TryFromBuilderError;
use crate::extension::{Extension, ExtensionNoop};
use crate::fitness::{Fitness, FitnessCache, FitnessOrdering, FitnessValue};
use crate::genotype::EvolveGenotype;
use crate::mutate::Mutate;
use crate::select::Select;
use crate::strategy::{Strategy, StrategyReporter, StrategyReporterNoop};
use rand::rngs::SmallRng;
use rand::SeedableRng;
use rayon::prelude::*;
use std::sync::mpsc::channel;

/// The builder for an Evolve struct.
#[derive(Clone, Debug)]
pub struct Builder<
    G: EvolveGenotype,
    M: Mutate<Genotype = G>,
    F: Fitness<Genotype = G>,
    S: Crossover<Genotype = G>,
    C: Select<Genotype = G>,
    E: Extension<Genotype = G>,
    SR: StrategyReporter<Genotype = G>,
> {
    pub genotype: Option<G>,
    pub target_population_size: usize,
    pub max_stale_generations: Option<usize>,
    pub max_generations: Option<usize>,
    pub max_chromosome_age: Option<usize>,
    pub target_fitness_score: Option<FitnessValue>,
    pub valid_fitness_score: Option<FitnessValue>,
    pub fitness_ordering: FitnessOrdering,
    pub fitness_cache: Option<FitnessCache>,
    pub par_fitness: bool,
    pub replace_on_equal_fitness: bool,
    pub mutate: Option<M>,
    pub fitness: Option<F>,
    pub crossover: Option<S>,
    pub select: Option<C>,
    pub extension: E,
    pub reporter: SR,
    pub rng_seed: Option<u64>,
}

impl<
        G: EvolveGenotype,
        M: Mutate<Genotype = G>,
        F: Fitness<Genotype = G>,
        S: Crossover<Genotype = G>,
        C: Select<Genotype = G>,
    > Default for Builder<G, M, F, S, C, ExtensionNoop<G>, StrategyReporterNoop<G>>
{
    fn default() -> Self {
        Self {
            genotype: None,
            target_population_size: 100,
            max_stale_generations: None,
            max_generations: None,
            max_chromosome_age: None,
            target_fitness_score: None,
            valid_fitness_score: None,
            fitness_ordering: FitnessOrdering::Maximize,
            fitness_cache: None,
            par_fitness: false,
            replace_on_equal_fitness: true,
            mutate: None,
            fitness: None,
            crossover: None,
            select: None,
            extension: ExtensionNoop::new(),
            reporter: StrategyReporterNoop::new(),
            rng_seed: None,
        }
    }
}
impl<
        G: EvolveGenotype,
        M: Mutate<Genotype = G>,
        F: Fitness<Genotype = G>,
        S: Crossover<Genotype = G>,
        C: Select<Genotype = G>,
    > Builder<G, M, F, S, C, ExtensionNoop<G>, StrategyReporterNoop<G>>
{
    pub fn new() -> Self {
        Self::default()
    }
}

#[allow(clippy::type_complexity)]
impl<
        G: EvolveGenotype,
        M: Mutate<Genotype = G>,
        F: Fitness<Genotype = G>,
        S: Crossover<Genotype = G>,
        C: Select<Genotype = G>,
        E: Extension<Genotype = G>,
        SR: StrategyReporter<Genotype = G>,
    > Builder<G, M, F, S, C, E, SR>
{
    pub fn build(self) -> Result<Evolve<G, M, F, S, C, E, SR>, TryFromBuilderError> {
        self.try_into()
    }

    pub fn with_genotype(mut self, genotype: G) -> Self {
        self.genotype = Some(genotype);
        self
    }
    pub fn with_target_population_size(mut self, target_population_size: usize) -> Self {
        self.target_population_size = target_population_size;
        self
    }
    pub fn with_max_stale_generations(mut self, max_stale_generations: usize) -> Self {
        self.max_stale_generations = Some(max_stale_generations);
        self
    }
    pub fn with_max_stale_generations_option(
        mut self,
        max_stale_generations_option: Option<usize>,
    ) -> Self {
        self.max_stale_generations = max_stale_generations_option;
        self
    }
    pub fn with_max_generations(mut self, max_generations: usize) -> Self {
        self.max_generations = Some(max_generations);
        self
    }
    pub fn with_max_generations_option(mut self, max_generations_option: Option<usize>) -> Self {
        self.max_generations = max_generations_option;
        self
    }
    pub fn with_max_chromosome_age(mut self, max_chromosome_age: usize) -> Self {
        self.max_chromosome_age = Some(max_chromosome_age);
        self
    }
    pub fn with_max_chromosome_age_option(
        mut self,
        max_chromosome_age_option: Option<usize>,
    ) -> Self {
        self.max_chromosome_age = max_chromosome_age_option;
        self
    }
    pub fn with_target_fitness_score(mut self, target_fitness_score: FitnessValue) -> Self {
        self.target_fitness_score = Some(target_fitness_score);
        self
    }
    pub fn with_target_fitness_score_option(
        mut self,
        target_fitness_score_option: Option<FitnessValue>,
    ) -> Self {
        self.target_fitness_score = target_fitness_score_option;
        self
    }
    pub fn with_valid_fitness_score(mut self, valid_fitness_score: FitnessValue) -> Self {
        self.valid_fitness_score = Some(valid_fitness_score);
        self
    }
    pub fn with_valid_fitness_score_option(
        mut self,
        valid_fitness_score_option: Option<FitnessValue>,
    ) -> Self {
        self.valid_fitness_score = valid_fitness_score_option;
        self
    }
    pub fn with_fitness_ordering(mut self, fitness_ordering: FitnessOrdering) -> Self {
        self.fitness_ordering = fitness_ordering;
        self
    }
    /// Only works when genes_hash is stored on chromosome, as this is the cache key.
    /// Only useful for long stale runs, but better to increase population diversity.
    /// Silently ignore cache_size of zero, to support superset builder which delays specialization
    pub fn with_fitness_cache(mut self, fitness_cache_size: usize) -> Self {
        match FitnessCache::try_new(fitness_cache_size) {
            Ok(cache) => self.fitness_cache = Some(cache),
            Err(_error) => (),
        }
        self
    }
    pub fn with_par_fitness(mut self, par_fitness: bool) -> Self {
        self.par_fitness = par_fitness;
        self
    }
    pub fn with_replace_on_equal_fitness(mut self, replace_on_equal_fitness: bool) -> Self {
        self.replace_on_equal_fitness = replace_on_equal_fitness;
        self
    }
    pub fn with_mutate(mut self, mutate: M) -> Self {
        self.mutate = Some(mutate);
        self
    }
    pub fn with_fitness(mut self, fitness: F) -> Self {
        self.fitness = Some(fitness);
        self
    }
    pub fn with_crossover(mut self, crossover: S) -> Self {
        self.crossover = Some(crossover);
        self
    }
    pub fn with_select(mut self, select: C) -> Self {
        self.select = Some(select);
        self
    }
    pub fn with_extension<E2: Extension<Genotype = G>>(
        self,
        extension: E2,
    ) -> Builder<G, M, F, S, C, E2, SR> {
        Builder {
            genotype: self.genotype,
            target_population_size: self.target_population_size,
            max_stale_generations: self.max_stale_generations,
            max_generations: self.max_generations,
            max_chromosome_age: self.max_chromosome_age,
            target_fitness_score: self.target_fitness_score,
            valid_fitness_score: self.valid_fitness_score,
            fitness_ordering: self.fitness_ordering,
            fitness_cache: self.fitness_cache,
            par_fitness: self.par_fitness,
            replace_on_equal_fitness: self.replace_on_equal_fitness,
            mutate: self.mutate,
            fitness: self.fitness,
            crossover: self.crossover,
            select: self.select,
            extension,
            reporter: self.reporter,
            rng_seed: self.rng_seed,
        }
    }
    pub fn with_reporter<SR2: StrategyReporter<Genotype = G>>(
        self,
        reporter: SR2,
    ) -> Builder<G, M, F, S, C, E, SR2> {
        Builder {
            genotype: self.genotype,
            target_population_size: self.target_population_size,
            max_stale_generations: self.max_stale_generations,
            max_generations: self.max_generations,
            max_chromosome_age: self.max_chromosome_age,
            target_fitness_score: self.target_fitness_score,
            valid_fitness_score: self.valid_fitness_score,
            fitness_ordering: self.fitness_ordering,
            fitness_cache: self.fitness_cache,
            par_fitness: self.par_fitness,
            replace_on_equal_fitness: self.replace_on_equal_fitness,
            mutate: self.mutate,
            fitness: self.fitness,
            crossover: self.crossover,
            select: self.select,
            extension: self.extension,
            reporter,
            rng_seed: self.rng_seed,
        }
    }
    pub fn with_rng_seed_from_u64(mut self, rng_seed: u64) -> Self {
        self.rng_seed = Some(rng_seed);
        self
    }
    pub fn with_rng_seed_from_u64_option(mut self, rng_seed_option: Option<u64>) -> Self {
        self.rng_seed = rng_seed_option;
        self
    }
}

#[allow(clippy::type_complexity)]
impl<
        G: EvolveGenotype,
        M: Mutate<Genotype = G>,
        F: Fitness<Genotype = G>,
        S: Crossover<Genotype = G>,
        C: Select<Genotype = G>,
        E: Extension<Genotype = G>,
        SR: StrategyReporter<Genotype = G>,
    > Builder<G, M, F, S, C, E, SR>
{
    pub fn rng(&self) -> SmallRng {
        if let Some(seed) = self.rng_seed {
            SmallRng::seed_from_u64(seed)
        } else {
            // SmallRng::from_entropy()
            SmallRng::from_rng(rand::thread_rng()).unwrap()
        }
    }
    pub fn call(self) -> Result<Evolve<G, M, F, S, C, E, SR>, TryFromBuilderError> {
        let mut evolve: Evolve<G, M, F, S, C, E, SR> = self.try_into()?;
        evolve.call();
        Ok(evolve)
    }
    pub fn call_repeatedly(
        self,
        max_repeats: usize,
    ) -> Result<
        (
            Evolve<G, M, F, S, C, E, SR>,
            Vec<Evolve<G, M, F, S, C, E, SR>>,
        ),
        TryFromBuilderError,
    > {
        let mut runs: Vec<Evolve<G, M, F, S, C, E, SR>> = vec![];
        (0..max_repeats)
            .filter_map(|iteration| {
                let mut contending_run: Evolve<G, M, F, S, C, E, SR> =
                    self.clone().try_into().ok()?;
                contending_run.state.current_iteration = iteration;
                Some(contending_run)
            })
            .map(|mut contending_run| {
                contending_run.call();
                let stop = contending_run.is_finished_by_target_fitness_score();
                runs.push(contending_run);
                stop
            })
            .any(|x| x);

        let best_run = self.extract_best_run(&mut runs);
        Ok((best_run, runs))
    }

    pub fn call_par_repeatedly(
        self,
        max_repeats: usize,
    ) -> Result<
        (
            Evolve<G, M, F, S, C, E, SR>,
            Vec<Evolve<G, M, F, S, C, E, SR>>,
        ),
        TryFromBuilderError,
    > {
        let _valid_builder: Evolve<G, M, F, S, C, E, SR> = self.clone().try_into()?;
        let mut runs: Vec<Evolve<G, M, F, S, C, E, SR>> = vec![];
        rayon::scope(|s| {
            let builder = &self;
            let (sender, receiver) = channel();

            s.spawn(move |_| {
                (0..max_repeats)
                    .filter_map(|iteration| {
                        let mut contending_run: Evolve<G, M, F, S, C, E, SR> =
                            builder.clone().try_into().ok()?;
                        contending_run.state.current_iteration = iteration;
                        Some(contending_run)
                    })
                    .par_bridge()
                    .map_with(sender, |sender, mut contending_run| {
                        contending_run.call();
                        let stop = contending_run.is_finished_by_target_fitness_score();
                        sender.send(contending_run).unwrap();
                        stop
                    })
                    .any(|x| x);
            });
            receiver.iter().for_each(|contending_run| {
                runs.push(contending_run);
            });
        });
        let best_run = self.extract_best_run(&mut runs);
        Ok((best_run, runs))
    }

    pub fn call_speciated(
        self,
        number_of_species: usize,
    ) -> Result<
        (
            Evolve<G, M, F, S, C, E, SR>,
            Vec<Evolve<G, M, F, S, C, E, SR>>,
        ),
        TryFromBuilderError,
    > {
        let _valid_builder: Evolve<G, M, F, S, C, E, SR> = self.clone().try_into()?;
        let mut species_runs: Vec<Evolve<G, M, F, S, C, E, SR>> = vec![];
        (0..number_of_species)
            .filter_map(|iteration| {
                let mut species_run: Evolve<G, M, F, S, C, E, SR> = self.clone().try_into().ok()?;
                species_run.state.current_iteration = iteration;
                Some(species_run)
            })
            .map(|mut species_run| {
                species_run.call();
                let stop = species_run.is_finished_by_target_fitness_score();
                species_runs.push(species_run);
                stop
            })
            .any(|x| x);

        let final_run = if let Some(index_finished_by_target_fitness_score) = species_runs
            .iter()
            .position(|species_run| species_run.is_finished_by_target_fitness_score())
        {
            species_runs.remove(index_finished_by_target_fitness_score)
        } else {
            let seed_genes_list = species_runs
                .iter()
                .filter_map(|species_run| species_run.best_genes())
                .collect();
            let mut final_genotype = self.genotype.clone().unwrap();
            final_genotype.reset(); // not needed, clone is unused
            final_genotype.set_seed_genes_list(seed_genes_list);
            let mut final_run: Evolve<G, M, F, S, C, E, SR> =
                self.clone().with_genotype(final_genotype).try_into()?;

            final_run.call();
            final_run
        };
        Ok((final_run, species_runs))
    }

    pub fn call_par_speciated(
        self,
        number_of_species: usize,
    ) -> Result<
        (
            Evolve<G, M, F, S, C, E, SR>,
            Vec<Evolve<G, M, F, S, C, E, SR>>,
        ),
        TryFromBuilderError,
    > {
        let _valid_builder: Evolve<G, M, F, S, C, E, SR> = self.clone().try_into()?;
        let mut species_runs: Vec<Evolve<G, M, F, S, C, E, SR>> = vec![];
        rayon::scope(|s| {
            let builder = &self;
            let (sender, receiver) = channel();

            s.spawn(move |_| {
                (0..number_of_species)
                    .filter_map(|iteration| {
                        let mut species_run: Evolve<G, M, F, S, C, E, SR> =
                            builder.clone().try_into().ok()?;
                        species_run.state.current_iteration = iteration;
                        Some(species_run)
                    })
                    .par_bridge()
                    .map_with(sender, |sender, mut species_run| {
                        species_run.call();
                        let stop = species_run.is_finished_by_target_fitness_score();
                        sender.send(species_run).unwrap();
                        stop
                    })
                    .any(|x| x);
            });

            receiver.iter().for_each(|species_run| {
                species_runs.push(species_run);
            });
        });

        let final_run = if let Some(index_finished_by_target_fitness_score) = species_runs
            .iter()
            .position(|species_run| species_run.is_finished_by_target_fitness_score())
        {
            species_runs.remove(index_finished_by_target_fitness_score)
        } else {
            let seed_genes_list = species_runs
                .iter()
                .filter_map(|species_run| species_run.best_genes())
                .collect();
            let mut final_genotype = self.genotype.clone().unwrap();
            final_genotype.reset(); // not needed, clone is unused
            final_genotype.set_seed_genes_list(seed_genes_list);
            let mut final_run: Evolve<G, M, F, S, C, E, SR> =
                self.clone().with_genotype(final_genotype).try_into()?;

            final_run.call();
            final_run
        };
        Ok((final_run, species_runs))
    }

    pub fn extract_best_run(
        &self,
        runs: &mut Vec<Evolve<G, M, F, S, C, E, SR>>,
    ) -> Evolve<G, M, F, S, C, E, SR> {
        let mut best_index = 0;
        let mut best_fitness_score: Option<FitnessValue> = None;
        runs.iter().enumerate().for_each(|(index, contending_run)| {
            let contending_fitness_score = contending_run.best_fitness_score();
            match (best_fitness_score, contending_fitness_score) {
                (None, None) => {}
                (Some(_), None) => {}
                (None, Some(_)) => {
                    best_index = index;
                    best_fitness_score = contending_fitness_score;
                }
                (Some(current_fitness_value), Some(contending_fitness_value)) => {
                    match self.fitness_ordering {
                        FitnessOrdering::Maximize => {
                            if contending_fitness_value >= current_fitness_value {
                                best_index = index;
                                best_fitness_score = contending_fitness_score;
                            }
                        }
                        FitnessOrdering::Minimize => {
                            if contending_fitness_value <= current_fitness_value {
                                best_index = index;
                                best_fitness_score = contending_fitness_score;
                            }
                        }
                    }
                }
            }
        });
        runs.remove(best_index)
    }
}