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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! This module contains the definition of the SurvivalPressure trait and the provided
//! survival_pressure functions.

use genotype::Genotype;
use rand::distributions::Uniform;
use rand::prelude::*;
use rayon::prelude::*;
use std::cmp::PartialEq;
use std::sync::mpsc::channel;
use IndWithFitness;
use SurvivalPressureFunctions::*;

/// This struct represents a `m` value, that shall be lower than the
/// population size and greater than the number of generated individuals
pub struct M(usize);
impl M {
    /// Creates a M object.
    ///
    /// # Panics
    /// If m is not bigger than the number of children and lower than the
    /// population size.
    pub fn new(m: usize, n_children: usize, population_size: usize) -> M {
        if m > n_children && m < population_size {
            M { 0: m }
        } else {
            panic!(
                "m shall be greater than the number of children and lower than the population size"
            )
        }
    }
}

/// This struct represents a reproduction step allocating the indexes of the
/// parents and children in the population.
pub struct Reproduction {
    pub parents: (usize, usize),
    pub children: (usize, usize),
}

/// This trait defines the kill function used to remove individuals at the end of a generation.
pub trait SurvivalPressure<T: PartialEq + Send + Sync, G: Genotype<T>>: Send + Sync {
    /// Removes individuals according to the population size; the population and
    /// the fitness of the population; and the parents and children relationships.
    /// Population is not sorted in any particular order, but children are after `population_size`.
    /// Note that in oxigen 1.x versions this function returned the indexes to be
    /// deleted without modifying the population, not it directly removes the individuals.
    fn kill(
        &self,
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
    );
}

/// Provided survival pressure functions.
pub enum SurvivalPressureFunctions {
    /// Kill worst individuals until reach the population size.
    Worst,
    /// Kill the most similar individual to each child.
    ChildrenReplaceMostSimilar,
    /// Kill individuals that have been crossed. Note that since the same parents can produce
    /// many different children (they are selected to cross several times) the populationn size
    /// can be increased using this function.
    ChildrenReplaceParents,
    /// Kill individuals that have been crossed and after that the worst individuals until reach
    /// the initial population size.
    ChildrenReplaceParentsAndTheRestWorst,
    /// Kill individuals that have been crossed and after that random individuals until reach
    /// the initial population size.
    ChildrenReplaceParentsAndTheRestRandomly,
    /// Kill individuals that have been crossed and after that randomly select
    /// an individual to fight to the the most similar to it
    /// until reach the initial population size. Note that this function is slow using the
    /// default Genotype similarity function because genes are compared one by one.
    ChildrenReplaceParentsAndTheRestRandomMostSimilar,
    /// Kill individuals that have been crossed and after that the most similar individuals
    /// until reach the initial population size. Note that this function is slow using the
    /// default Genotype similarity function because genes are compared one by one.
    ChildrenReplaceParentsAndTheRestMostSimilar,
    /// Kill individuals that have been crossed and after that the oldest individuals until reach
    /// the initial population size.
    /// /// Note that this function is slow because each individual similarity is computed across the
    /// rest of individuals of the population.
    ChildrenReplaceParentsAndTheRestOldest,
    /// Each child fight with the most similar individual in the population.
    ChildrenFightMostSimilar,
    /// Each child fight with a random parent in a parricide battle for survival.
    /// Note that because the same parents can produce many different children (they are selected
    /// to cross several times) the populationn size can be increased using this function.
    ChildrenFightParents,
    /// Each child fight with a random parent in a parricide battle for survival and after that
    /// the worst individuals until reach the initial population size.
    ChildrenFightParentsAndTheRestWorst,
    /// Each child fight with a random parent in a parricide battle for survival and after that
    /// random individuals until reach the initial population size.
    ChildrenFightParentsAndTheRestRandomly,
    /// Each child fight with a random parent in a parricide battle for survival and after that
    /// randomly select an individual to fight to the the most similar to it
    /// until reach the initial population size.
    /// Note that this function is slow because each individual similarity is computed across the
    /// rest of individuals of the population.
    ChildrenFightParentsAndTheRestRandomMostSimilar,
    /// Each child fight with a random parent in a parricide battle for survival and after that
    /// the most similar individuals until reach the initial population size.
    /// Note that this function is slow because each individual similarity is computed across the
    /// rest of individuals of the population.
    ChildrenFightParentsAndTheRestMostSimilar,
    /// Each child fight with a random parent in a parricide battle for survival and after that
    /// the oldest individuals until reach the initial population size.
    ChildrenFightParentsAndTheRestOldest,
    /// `m` individuals are randomly selected from the population (note that n < m < population_size
    /// where n is the number of generated individuals shall be true) and the most similars to the
    /// children are killed one by one.
    Overpopulation(M),
    /// Like `OverPopulation` but the existing individuals in the population that are most
    /// similar to each children fight with the corresponding children and the best survive.
    CompetitiveOverpopulation(M),
    /// Like `ChildrenFightParents` but instead of random fight between one child with one parent,
    /// the tuple of most similar parent-child is selected to fight.
    DeterministicOverpopulation,
}

impl<T: PartialEq + Send + Sync, G: Genotype<T>> SurvivalPressure<T, G>
    for SurvivalPressureFunctions
{
    fn kill(
        &self,
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
    ) {
        match self {
            Worst => {
                Self::remove_worst(population_size, population);
            }
            ChildrenReplaceMostSimilar => {
                Self::children_replace_most_similar(population, parents_children, population_size);
            }
            ChildrenReplaceParents => {
                Self::children_replace_parents(population, parents_children);
            }
            ChildrenReplaceParentsAndTheRestWorst => {
                Self::children_replace_parents(population, parents_children);
                Self::remove_worst(population_size, population);
            }
            ChildrenReplaceParentsAndTheRestRandomly => {
                Self::children_replace_parents(population, parents_children);
                Self::remove_randomly(population_size, population);
            }
            ChildrenReplaceParentsAndTheRestRandomMostSimilar => {
                Self::children_replace_parents(population, parents_children);
                Self::random_most_similar(population_size, population);
            }
            ChildrenReplaceParentsAndTheRestMostSimilar => {
                Self::children_replace_parents(population, parents_children);
                Self::most_similar(population_size, population);
            }
            ChildrenReplaceParentsAndTheRestOldest => {
                Self::children_replace_parents(population, parents_children);
                Self::remove_oldest(population_size, population);
            }
            ChildrenFightMostSimilar => {
                Self::children_fight_most_similar(population, parents_children, population_size);
            }
            ChildrenFightParents => {
                Self::children_fight_parents(population, parents_children);
            }
            ChildrenFightParentsAndTheRestWorst => {
                Self::children_fight_parents(population, parents_children);
                Self::remove_worst(population_size, population);
            }
            ChildrenFightParentsAndTheRestRandomly => {
                Self::children_fight_parents(population, parents_children);
                Self::remove_randomly(population_size, population);
            }
            ChildrenFightParentsAndTheRestRandomMostSimilar => {
                Self::children_fight_parents(population, parents_children);
                Self::random_most_similar(population_size, population);
            }
            ChildrenFightParentsAndTheRestMostSimilar => {
                Self::children_fight_parents(population, parents_children);
                Self::most_similar(population_size, population);
            }
            ChildrenFightParentsAndTheRestOldest => {
                Self::children_fight_parents(population, parents_children);
                Self::remove_oldest(population_size, population);
            }
            Overpopulation(m) => {
                Self::overpopulation(m.0, population_size, population, parents_children);
            }
            CompetitiveOverpopulation(m) => {
                Self::competitive_overpopulation(
                    m.0,
                    population_size,
                    population,
                    parents_children,
                );
            }
            DeterministicOverpopulation => {
                Self::deterministic_overpopulation(population, parents_children);
            }
        }
    }
}

impl SurvivalPressureFunctions {
    fn remove_worst<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
    ) {
        sort_population(population);
        let mut i = population.len();
        while i > population_size {
            population.pop();
            i -= 1;
        }
    }

    fn remove_randomly<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
    ) {
        let mut rgen = SmallRng::from_entropy();
        let mut i = population.len();
        while i > population_size {
            let chosen = rgen.sample(Uniform::from(0..i));
            population.remove(chosen);
            i -= 1;
        }
    }
    fn remove_oldest<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
    ) {
        sort_population_by_age(population);
        let mut i = population.len();
        while i > population_size {
            population.pop();
            i -= 1;
        }
    }

    fn children_replace_most_similar<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
        population_size: usize,
    ) {
        let mut killed = Vec::with_capacity(parents_children.len() * 2);
        for repr in parents_children.iter() {
            let most_similar_0 = population
                .par_iter()
                .enumerate()
                // Children start at population_size (added at the end)
                .filter(|(i, _el)| *i < population_size && !killed.contains(i))
                .map(|(i, el)| (i, population[repr.children.0].ind.distance(&el.ind)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                .unwrap()
                .0;
            killed.push(most_similar_0);

            let most_similar_1 = population
                .par_iter()
                .enumerate()
                // Children start at population_size (added at the end)
                .filter(|(i, _el)| *i < population_size && !killed.contains(i))
                .map(|(i, el)| (i, population[repr.children.1].ind.distance(&el.ind)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                .unwrap()
                .0;
            killed.push(most_similar_1);
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn children_replace_parents<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
    ) {
        let mut killed = Vec::with_capacity(parents_children.len() * 2);
        let (sender, receiver) = channel();
        parents_children
            .par_iter()
            .for_each_with(sender, |s, repr| {
                s.send(repr.parents).unwrap();
            });
        for parents in receiver {
            killed.push(parents.0);
            killed.push(parents.1);
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        killed.dedup();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn children_fight_most_similar<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
        population_size: usize,
    ) {
        let mut killed = Vec::with_capacity(parents_children.len() * 2);
        for repr in parents_children.iter() {
            let most_similar_0 = population
                .par_iter()
                .enumerate()
                // Children start at population_size (added at the end)
                .filter(|(i, _el)| *i < population_size && !killed.contains(i))
                .map(|(i, el)| (i, population[repr.children.0].ind.distance(&el.ind)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                .unwrap()
                .0;
            if population[most_similar_0].fitness.unwrap().fitness
                > population[repr.children.0].fitness.unwrap().fitness
            {
                killed.push(repr.children.0);
            } else {
                killed.push(most_similar_0);
            }

            let most_similar_1 = population
                .par_iter()
                .enumerate()
                // Children start at population_size (added at the end)
                .filter(|(i, _el)| *i < population_size && !killed.contains(i))
                .map(|(i, el)| (i, population[repr.children.1].ind.distance(&el.ind)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                .unwrap()
                .0;
            if population[most_similar_1].fitness.unwrap().fitness
                > population[repr.children.1].fitness.unwrap().fitness
            {
                killed.push(repr.children.1);
            } else {
                killed.push(most_similar_1);
            }
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn children_fight_parents<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
    ) {
        let mut killed = Vec::with_capacity(parents_children.len() * 2);
        let mut rgen = SmallRng::from_entropy();
        for repr in parents_children {
            let par0 = &population[repr.parents.0];
            let par1 = &population[repr.parents.1];
            let child0 = &population[repr.children.0];
            let child1 = &population[repr.children.1];
            if rgen.gen_bool(0.5) {
                // First parent fights with first child
                if par0.fitness.unwrap().fitness > child0.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.0);
                } else {
                    // The child survives
                    killed.push(repr.parents.0);
                }
                // Second parent fights with the second child
                if par1.fitness.unwrap().fitness > child1.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.1);
                } else {
                    // The child survives
                    killed.push(repr.parents.1);
                }
            } else {
                // First parent fights with second child
                if par0.fitness.unwrap().fitness > child1.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.1);
                } else {
                    // The child survives
                    killed.push(repr.parents.0);
                }
                // Second parent fights with the first child
                if par1.fitness.unwrap().fitness > child0.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.0);
                } else {
                    // The child survives
                    killed.push(repr.parents.1);
                }
            }
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        killed.dedup();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn deterministic_overpopulation<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
    ) {
        let mut killed = Vec::with_capacity(parents_children.len() * 2);
        for repr in parents_children {
            let par0 = &population[repr.parents.0];
            let par1 = &population[repr.parents.1];
            let child0 = &population[repr.children.0];
            let child1 = &population[repr.children.1];
            let dist0_0 = par0.ind.distance(&child0.ind);
            let dist0_1 = par0.ind.distance(&child1.ind);
            let dist1_0 = par1.ind.distance(&child0.ind);
            let dist1_1 = par1.ind.distance(&child1.ind);
            // The sum of distances comparing parent 0 to child 0 and
            // parent 1 to child 1 is lower than the another case
            if dist0_0 + dist1_1 <= dist0_1 + dist1_0 {
                // First parent fights with first child
                if par0.fitness.unwrap().fitness > child0.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.0);
                } else {
                    // The child survives
                    killed.push(repr.parents.0);
                }
                // Second parent fights with the second child
                if par1.fitness.unwrap().fitness > child1.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.1);
                } else {
                    // The child survives
                    killed.push(repr.parents.1);
                }
            } else {
                // First parent fights with second child
                if par0.fitness.unwrap().fitness > child1.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.1);
                } else {
                    // The child survives
                    killed.push(repr.parents.0);
                }
                // Second parent fights with the first child
                if par1.fitness.unwrap().fitness > child0.fitness.unwrap().fitness {
                    // The parent survives
                    killed.push(repr.children.0);
                } else {
                    // The child survives
                    killed.push(repr.parents.1);
                }
            }
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        killed.dedup();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn overpopulation<T: PartialEq + Send + Sync, G: Genotype<T>>(
        m: usize,
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
    ) {
        if m < parents_children.len() * 2 {
            panic!(
                "m < children length. m: {}, children_len: {}",
                m,
                parents_children.len() * 2
            )
        }
        let mut killed = Vec::with_capacity(parents_children.len() * 2);
        let mut choosable: Vec<usize> = Vec::with_capacity(m);
        let mut rgen = SmallRng::from_entropy();
        let mut children: Vec<usize> = Vec::with_capacity(parents_children.len() * 2);
        for (child0, child1) in parents_children
            .iter()
            .map(|pc| (pc.children.0, pc.children.1))
        {
            children.push(child0);
            children.push(child1);
        }
        for _i in 0..m {
            let mut chosen = rgen.sample(Uniform::from(0..population_size));
            // children have not to be checked because they are after population_size
            while choosable.contains(&chosen) {
                chosen = rgen.sample(Uniform::from(0..population_size));
            }
            choosable.push(chosen);
        }
        for child in children {
            let most_similar = choosable
                .par_iter()
                .enumerate()
                .map(|(i, el)| (i, population[child].ind.distance(&population[*el].ind)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                .unwrap()
                .0;
            killed.push(choosable[most_similar]);
            choosable.remove(most_similar);
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn competitive_overpopulation<T: PartialEq + Send + Sync, G: Genotype<T>>(
        m: usize,
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
        parents_children: &[Reproduction],
    ) {
        if m < parents_children.len() * 2 {
            panic!(
                "m < children length. m: {}, children_len: {}",
                m,
                parents_children.len() * 2
            )
        }
        let mut killed = Vec::with_capacity(parents_children.len() * 2);
        let mut choosable: Vec<usize> = Vec::with_capacity(m);
        let mut rgen = SmallRng::from_entropy();
        let mut children: Vec<usize> = Vec::with_capacity(parents_children.len() * 2);
        for (child0, child1) in parents_children
            .iter()
            .map(|pc| (pc.children.0, pc.children.1))
        {
            children.push(child0);
            children.push(child1);
        }
        for _i in 0..m {
            let mut chosen = rgen.sample(Uniform::from(0..population_size));
            // children have not to be checked because they are after population_size
            while choosable.contains(&chosen) {
                chosen = rgen.sample(Uniform::from(0..population_size));
            }
            choosable.push(chosen);
        }
        for child in children {
            let most_similar = choosable
                .par_iter()
                .enumerate()
                .map(|(i, el)| (i, population[child].ind.distance(&population[*el].ind)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                .unwrap()
                .0;
            if population[choosable[most_similar]].fitness.unwrap().fitness
                > population[child].fitness.unwrap().fitness
            {
                killed.push(child);
            } else {
                killed.push(choosable[most_similar]);
            }
            choosable.remove(most_similar);
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn random_most_similar<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
    ) {
        let tobekilled = population.len() - population_size;
        let mut killed = Vec::with_capacity(tobekilled);
        let mut choosable: Vec<usize> = population.iter().enumerate().map(|(i, _p)| i).collect();
        let mut rgen = SmallRng::from_entropy();
        let mut n_killed = 0;
        while n_killed < tobekilled {
            let chosen = rgen.sample(Uniform::from(0..choosable.len()));

            let most_similar = choosable
                .par_iter()
                .enumerate()
                .filter(|(i, _el)| *i != chosen)
                .map(|(i, el)| (i, population[chosen].ind.distance(&population[*el].ind)))
                .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                .unwrap()
                .0;
            if population[choosable[chosen]].fitness.unwrap().fitness
                > population[choosable[most_similar]].fitness.unwrap().fitness
            {
                killed.push(choosable[most_similar]);
            } else {
                killed.push(choosable[chosen]);
            }
            if chosen > most_similar {
                choosable.remove(chosen);
                choosable.remove(most_similar);
            } else {
                choosable.remove(most_similar);
                choosable.remove(chosen);
            }
            n_killed += 1;
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        for el in killed.iter().rev() {
            population.remove(*el);
        }
    }

    fn most_similar<T: PartialEq + Send + Sync, G: Genotype<T>>(
        population_size: usize,
        population: &mut Vec<IndWithFitness<T, G>>,
    ) {
        let tobekilled = population.len() - population_size;
        // Vec with elements to kill in population
        let mut killed = Vec::with_capacity(tobekilled);
        // Vec with the similarity of each individual in the population with
        // any other individual in the population
        let mut dists: Vec<Vec<(usize, f64)>> = population
            .iter()
            .enumerate()
            .map(|_el| Vec::with_capacity(population.len()))
            .collect();
        dists.par_iter_mut().enumerate().for_each(|(i, dists)| {
            for (j, indwf) in population.iter().enumerate() {
                dists.push((j, population[i].ind.distance(&indwf.ind)));
            }
        });
        let mut n_killed = 0;
        while n_killed < tobekilled {
            // Get the element in the population that has the biggest similarity
            // with any other and that individual
            let (chosen, (most_similar, _distance)) = dists
                .par_iter()
                .enumerate()
                .filter(|(i, _el)| !killed.contains(i))
                .map(|(i, distances)| {
                    (
                        i,
                        distances
                            .iter()
                            .filter(|(j, _dist)| i != *j && !killed.contains(j))
                            .min_by(|x, y| x.1.partial_cmp(&y.1).unwrap())
                            .unwrap(),
                    )
                })
                .min_by(|x, y| (x.1).1.partial_cmp(&(y.1).1).unwrap())
                .unwrap();

            if population[chosen].fitness.unwrap().fitness
                > population[*most_similar].fitness.unwrap().fitness
            {
                killed.push(*most_similar);
            } else {
                killed.push(chosen);
            }
            n_killed += 1;
        }
        // killed must be sorted because the indexes of elements at the right
        // change when removing elements from vector
        killed.par_sort_unstable();
        for i in killed.iter().rev() {
            population.remove(*i);
        }
    }
}

fn sort_population<T: PartialEq + Send + Sync, G: Genotype<T>>(
    population: &mut [IndWithFitness<T, G>],
) {
    population.par_sort_unstable_by(|el1, el2| {
        el2.fitness
            .unwrap()
            .fitness
            .partial_cmp(&el1.fitness.unwrap().fitness)
            .unwrap()
    });
}

fn sort_population_by_age<T: PartialEq + Send + Sync, G: Genotype<T>>(
    population: &mut [IndWithFitness<T, G>],
) {
    population.par_sort_unstable_by(|el1, el2| {
        el2.fitness
            .unwrap()
            .age
            .partial_cmp(&el1.fitness.unwrap().age)
            .unwrap()
    });
}