eevee 0.2.1

Generalized NeuroEvolution toolkit, based on NEAT
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
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! Functions related to performing measuring compatability for and performing crossover
//! reproduction.

use crate::genome::Connection;
use core::cmp::Ordering;
use rand::RngCore;

/// Count misaligned [Connection]s between 2 slices. Where `l` is more fit ( TODO really? ), we
/// consider disjoint genes to be misalignments of innovation ids < `r`s max, and excess are
/// misalignments of ids > `r`s max.
pub fn disjoint_excess_count<C: Connection>(l: &[C], r: &[C]) -> (f64, f64) {
    let mut l_iter = l.iter();
    let mut r_iter = r.iter();

    let mut l_conn = match l_iter.next() {
        Some(c) => c,
        None => return (0., r_iter.count() as f64),
    };

    let mut r_conn = match r_iter.next() {
        Some(c) => c,
        None => return (0., l_iter.count() as f64 + 1.),
    };

    let mut disjoint = 0.;
    let excess_passed = loop {
        match l_conn.inno().cmp(&r_conn.inno()) {
            Ordering::Equal => {
                l_conn = match l_iter.next() {
                    Some(c) => c,
                    None => break 0.,
                };

                r_conn = match r_iter.next() {
                    Some(c) => c,
                    None => break 1.,
                };
            }
            Ordering::Greater => {
                disjoint += 1.;
                r_conn = match r_iter.next() {
                    Some(c) => c,
                    None => break 1.,
                }
            }
            Ordering::Less => {
                disjoint += 1.;
                l_conn = match l_iter.next() {
                    Some(c) => c,
                    None => break 1.,
                }
            }
        }
    };

    (
        disjoint,
        l_iter.count() as f64 + r_iter.count() as f64 + excess_passed,
    )
}

/// Average param difference between aligned genes from `l` and `r`. Misaligned genes are not
/// considered
pub fn avg_param_diff<C: Connection>(l: &[C], r: &[C]) -> f64 {
    let mut diff = 0.;
    let mut count = 0.;
    let mut l_iter = l.iter();
    let mut r_iter = r.iter();

    let mut l_conn = match l_iter.next() {
        Some(c) => c,
        None => return 0.,
    };

    let mut r_conn = match r_iter.next() {
        Some(c) => c,
        None => return 0.,
    };

    loop {
        match l_conn.inno().cmp(&r_conn.inno()) {
            Ordering::Equal => {
                diff += l_conn.param_diff(r_conn);
                count += 1.;

                l_conn = match l_iter.next() {
                    Some(c) => c,
                    None => break,
                };

                r_conn = match r_iter.next() {
                    Some(c) => c,
                    None => break,
                };
            }
            Ordering::Greater => {
                r_conn = match r_iter.next() {
                    Some(c) => c,
                    None => break,
                }
            }
            Ordering::Less => {
                l_conn = match l_iter.next() {
                    Some(c) => c,
                    None => break,
                }
            }
        }
    }

    if count == 0. {
        0.
    } else {
        diff / count
    }
}

/// difference between [Connection]s in terms of crossover compatability. Higher deltas tend to
/// yield more destructive crossover.
pub fn delta<C: Connection>(l: &[C], r: &[C]) -> f64 {
    let l_size = l.len() as f64;
    let r_size = r.len() as f64;
    let fac = {
        let longest = f64::max(l_size, r_size);
        if longest < 20. {
            1.
        } else {
            longest
        }
    };

    if l_size == 0. || r_size == 0. {
        (C::EXCESS_COEFFICIENT * f64::max(l_size, r_size)) / fac
    } else {
        let (disjoint, excess) = disjoint_excess_count(l, r);
        (C::DISJOINT_COEFFICIENT * disjoint + C::EXCESS_COEFFICIENT * excess) / fac
            + C::PARAM_COEFFICIENT * avg_param_diff(l, r)
    }
}

#[inline]
fn pick_gene<C: Connection>(base_conn: &C, opt_conn: Option<&C>, rng: &mut impl RngCore) -> C {
    let mut conn = if let Some(r_conn) = opt_conn {
        // TODO be able to differentiate PickLEQ and PickLNE
        if rng.next_u64() < C::PROBABILITY_PICK_RL {
            r_conn
        } else {
            base_conn
        }
        .to_owned()
    } else {
        base_conn.to_owned()
    };

    // TODO It seems like it will always check RAND_DISABLED, and sometimes
    // check KEEP_DISABLED. I wonder if checking RAND_DISABLED first would bypass
    // RAND_DISABLED% of checks that would then check KEEP_DISABLED?
    if (!base_conn.enabled() || opt_conn.is_some_and(|r_conn| !r_conn.enabled()))
        && rng.next_u64() < C::PROBABILITY_KEEP_DISABLED
    {
        conn.disable();
    }

    conn
}

/// crossover connections where l and r are equally fit
fn crossover_eq<C: Connection>(l: &[C], r: &[C], rng: &mut impl RngCore) -> Vec<C> {
    // TODO I wonder what the actual average case overlap between genomes is?
    // probably pretty close, could we measure this?
    let mut cross = Vec::with_capacity(l.len() + r.len());
    let mut l_idx = 0;
    let mut r_idx = 0;
    loop {
        match (l.get(l_idx), r.get(r_idx)) {
            (None, None) => break,
            (None, Some(_)) => {
                // TODO is it faster to extend, or to loop-push?
                cross.extend(r[r_idx..].iter().map(|conn| pick_gene(conn, None, rng)));
                break;
            }
            (Some(_), None) => {
                cross.extend(l[l_idx..].iter().map(|conn| pick_gene(conn, None, rng)));
                break;
            }
            (Some(l_conn), Some(r_conn)) => match l_conn.inno().cmp(&r_conn.inno()) {
                Ordering::Equal => {
                    cross.push(pick_gene(l_conn, Some(r_conn), rng));
                    l_idx += 1;
                    r_idx += 1;
                }
                Ordering::Less => {
                    cross.push(pick_gene(l_conn, None, rng));
                    l_idx += 1;
                }
                Ordering::Greater => {
                    cross.push(pick_gene(r_conn, None, rng));
                    r_idx += 1;
                }
            },
        }
    }

    cross.shrink_to_fit(); // TODO what happens if I remove this
    cross
}

/// crossover connections where l is more fit than r
fn crossover_ne<C: Connection>(l: &[C], r: &[C], rng: &mut impl RngCore) -> Vec<C> {
    // copy l, pick_gene where l.inno() == r.inno()
    let mut cross = Vec::with_capacity(l.len());
    let mut r_idx = 0;
    for l_conn in l {
        // TODO is r_idx < r.len() && r[r_idx] or maybe even get_unchecked
        while r
            .get(r_idx)
            .is_some_and(|r_conn| r_conn.inno() < l_conn.inno())
        {
            r_idx += 1;
        }

        // TODO above applies here
        cross.push(pick_gene(
            l_conn,
            r.get(r_idx)
                .is_some_and(|r_conn| r_conn.inno() == l_conn.inno())
                .then(|| &r[r_idx]),
            rng,
        ))
    }

    cross
}

/// Perform crossover reproduction across 2 [Connection] slices `l` and `r`. `l_fit` describes
/// how fit `l` is compared to `r`, which determines who's genes to prioritize when misaligned.
pub fn crossover<C: Connection>(
    l: &[C],
    r: &[C],
    l_fit: Ordering,
    rng: &mut impl RngCore,
) -> Vec<C> {
    let mut usort = match l_fit {
        Ordering::Equal => crossover_eq(l, r, rng),
        Ordering::Less => crossover_ne(r, l, rng),
        Ordering::Greater => crossover_ne(l, r, rng),
    };

    usort.sort_by_key(|c| c.inno());
    usort
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{
        assert_f64_approx, assert_some_normalized,
        genome::{connection::BWConnection, WConnection},
        new_t,
        random::default_rng,
    };
    use eevee_macros::fn_matrix;
    use std::collections::{HashMap, HashSet};

    fn_matrix! {
        T: WConnection,
        /// avg_param_diff: weight difference reflected
        #[test]
        fn test_avg_param_diff() {
            let diff = avg_param_diff(
                &[
                    new_t!(T, inno = 1, weight = 0.5,),
                    new_t!(T, inno = 2, weight = -0.5,),
                    new_t!(T, inno = 3, weight = 1.0,),
                ],
                &[
                    new_t!(T, inno = 1, weight = 0.0,),
                    new_t!(T, inno = 2, weight = -1.0,),
                    new_t!(T, inno = 4, weight = 2.0,),
                ],
            );
            assert_f64_approx!(diff, 0.5, "diff ne: {diff}, 0.5");
        }
    }

    fn_matrix! {
        T: BWConnection,
        /// avg_param_diff: combines weight+bias diffs
        #[test]
        fn test_avg_param_diff() {
            let diff = avg_param_diff(
                &[
                    new_t!(T, inno = 1, weight = 0.5, bias = 1.),
                    new_t!(T, inno = 2, weight = -0.5,),
                    new_t!(T, inno = 3, weight = 1.0,),
                ],
                &[
                    new_t!(T, inno = 1, weight = 0.0, bias = 0.),
                    new_t!(T, inno = 2, weight = -1.0,),
                    new_t!(T, inno = 4, weight = 2.0,),
                ],
            );
            let diff_w = 0.5;
            let diff_b = 1. / 2.;
            assert_f64_approx!(diff, diff_w + diff_b, "diff ne: {diff}, 0.5");
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// avg_param_diff: empty inputs return 0.0
        #[test]
        fn test_avg_param_diff_empty() {
            let full = vec![
                new_t!(T, inno = 1, weight = 0.0,),
                new_t!(T, inno = 2, weight = -1.0,),
                new_t!(T, inno = 4, weight = 2.0,),
            ];

            let diff = avg_param_diff(&full, &[]);
            assert_f64_approx!(diff, 0.0, "diff ne: {diff}, 0.");

            let diff = avg_param_diff(&[], &full);
            assert_f64_approx!(diff, 0.0, "diff ne: {diff}, 0.");

            let diff = avg_param_diff::<T>(&[], &[]);
            assert_f64_approx!(diff, 0.0, "diff ne: {diff}, 0.");
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// avg_param_diff: no overlapping genes return 0.0
        #[test]
        fn test_avg_param_diff_no_overlap() {
            let diff = avg_param_diff(
                &[
                    new_t!(T, inno = 1, weight = 0.5,),
                    new_t!(T, inno = 2, weight = -0.5,),
                    new_t!(T, inno = 3, weight = 1.0,),
                ],
                &[
                    new_t!(T, inno = 5, weight = 0.5,),
                    new_t!(T, inno = 6, weight = -0.5,),
                ],
            );
            assert_f64_approx!(diff, 0., "diff ne: {diff}, 0.")
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// avg_param_diff: identical connections return 0.0
        #[test]
        fn test_avg_param_diff_no_diff() {
            let diff = avg_param_diff(
                &[
                    new_t!(T, inno = 1, weight = 0.5,),
                    new_t!(T, inno = 2, weight = -0.5,),
                    new_t!(T, inno = 3, weight = 1.0,),
                ],
                &[
                    new_t!(T, inno = 1, weight = 0.5,),
                    new_t!(T, inno = 2, weight = -0.5,),
                    new_t!(T, inno = 3, weight = 1.0,),
                ],
            );
            assert_f64_approx!(diff, 0.0, "diff ne: {diff}, 0.");
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// disjoint_excess_count: counts misaligned and excess genes
        #[test]
        fn test_disjoint_excess_count() {
            assert_eq!(
                (4.0, 2.0),
                disjoint_excess_count(
                    &[
                        new_t!(T, inno = 1),
                        new_t!(T, inno = 2),
                        new_t!(T, inno = 6),
                    ],
                    &[
                        new_t!(T, inno = 1),
                        new_t!(T, inno = 3),
                        new_t!(T, inno = 4),
                        new_t!(T, inno = 8),
                        new_t!(T, inno = 10),
                    ]
                )
            );
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// disjoint_excess_count: is symmetric (same result regardless of order)
        #[test]
        fn test_disjoint_excess_count_symmetrical() {
            let l = vec![
                new_t!(T, inno = 1),
                new_t!(T, inno = 2),
                new_t!(T, inno = 6),
            ];
            let r = vec![
                new_t!(T, inno = 1),
                new_t!(T, inno = 3),
                new_t!(T, inno = 4),
                new_t!(T, inno = 8),
                new_t!(T, inno = 10),
            ];
            assert_eq!(disjoint_excess_count(&l, &r), disjoint_excess_count(&r, &l));
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// disjoint_excess_count: handles empty inputs correctly
        #[test]
        fn test_disjoint_excess_count_empty() {
            let full = vec![new_t!(T, inno = 1), new_t!(T, inno = 2)];
            assert_eq!((0.0, 2.0), disjoint_excess_count(&full, &[]));
            assert_eq!((0.0, 2.0), disjoint_excess_count(&[], &full));
            assert_eq!((0.0, 0.0), disjoint_excess_count::<T>(&[], &[]));
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// disjoint_excess_count: handles left excess genes
        #[test]
        fn test_disjoint_excess_count_hanging_l() {
            assert_eq!(
                (0.0, 1.0),
                disjoint_excess_count(
                    &[
                        new_t!(T, inno = 0),
                        new_t!(T, inno = 1),
                        new_t!(T, inno = 2),
                    ],
                    &[new_t!(T, inno = 0), new_t!(T, inno = 1),]
                )
            )
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// disjoint_excess_count: all genes disjoint and excess
        #[test]
        fn test_disjoint_excess_count_no_overlap() {
            assert_eq!(
                (2.0, 2.0),
                disjoint_excess_count(
                    &[new_t!(T, inno = 1), new_t!(T, inno = 2),],
                    &[new_t!(T, inno = 3), new_t!(T, inno = 4),]
                )
            );
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// disjoint_excess_count: handles inno wraparound correctly
        #[test]
        fn test_disjoint_excess_count_short_larger_inno() {
            assert_eq!(
                (3.0, 1.0),
                disjoint_excess_count(
                    &[new_t!(T, inno = 10)],
                    &[
                        new_t!(T, inno = 1),
                        new_t!(T, inno = 2),
                        new_t!(T, inno = 3),
                    ]
                )
            );
        }
    }

    fn assert_crossover_eq<C: Connection>(l: &[C], r: &[C]) {
        for (l, r) in [(l, r), (r, l)] {
            let l_map = l.iter().map(|c| (c.inno(), c)).collect::<HashMap<_, &_>>();
            let r_map = r.iter().map(|c| (c.inno(), c)).collect::<HashMap<_, &_>>();
            let inno = l_map
                .keys()
                .collect::<HashSet<_>>()
                .union(&r_map.keys().collect::<HashSet<_>>())
                .cloned()
                .cloned()
                .collect::<HashSet<_>>();

            let mut rng = default_rng();
            for _ in 0..1000 {
                let lr = crossover_eq(l, r, &mut rng);
                assert_eq!(inno.len(), lr.len());

                let lr_inno = lr.iter().map(|c| c.inno()).collect::<HashSet<_>>();
                assert!(inno.is_subset(&lr_inno));
                assert!(inno.is_superset(&lr_inno));
                assert!(lr.is_sorted_by_key(|c| c.inno()));
                for ref lr_conn in lr {
                    match (l_map.get(&lr_conn.inno()), r_map.get(&lr_conn.inno())) {
                        (None, None) => panic!("{} is in neither l nor r", lr_conn.inno()),
                        (None, Some(conn)) | (Some(conn), None) => {
                            assert_some_normalized!(lr_conn, [*conn]; {.enable()})
                        }
                        (Some(l_conn), Some(r_conn)) => {
                            assert_some_normalized!(lr_conn, [*l_conn, *r_conn]; {.enable()});
                        }
                    }
                }
            }
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_eq: equal fitness preserves all genes from both parents
        #[test]
        fn test_crossover_eq() {
            let l = [
                new_t!(T, inno = 0, from = 1_1),
                new_t!(T, inno = 1, from = 1_2),
                new_t!(T, inno = 2, from = 1_3),
            ];
            let r = [
                new_t!(T, inno = 0, from = 2_1),
                new_t!(T, inno = 2, from = 2_2),
                new_t!(T, inno = 3, from = 2_3),
            ];

            assert_crossover_eq(&l, &r);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_eq: handles empty inputs
        #[test]
        fn test_crossover_eq_empty() {
            let l = [new_t!(T, inno = 2, from = 1)];

            assert_crossover_eq(&l, &[]);
            assert_crossover_eq::<T>(&[], &[]);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_eq: handles inno ordering differences between parents
        #[test]
        fn test_crossover_eq_overflow() {
            let l = [new_t!(T, inno = 0, from = 1_1)];
            let r = [new_t!(T, inno = 1, from = 2_1)];

            assert_crossover_eq(&l, &r);

            let l = [new_t!(T, inno = 1, from = 1_1)];
            let r = [new_t!(T, inno = 0, from = 2_1)];

            assert_crossover_eq(&l, &r);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_eq: panics when left parent overshoots right parent's max inno
        #[test]
        #[should_panic(expected = "not from r_0")]
        fn test_crossover_eq_catchup_l() {
            let l = [
                new_t!(T, inno = 0, from = 1_1),
                new_t!(T, inno = 1, from = 1_2),
            ];
            let r = [new_t!(T, inno = 1, from = 2_1)];
            let mut rng = default_rng();
            for _ in 0..1000 {
                let lr = crossover_eq(&l, &r, &mut rng);
                assert_eq!(lr.len(), 2);
                assert_some_normalized!(&lr[0], [&l[0]]; {.enable()});
                assert_some_normalized!(&lr[1], [&r[0]]; {.enable()}, "not from r_0");
            }
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_eq: panics when right parent overshoots left parent's max inno
        #[test]
        #[should_panic(expected = "not from l_0")]
        fn test_crossover_eq_catchup_r() {
            let l = [new_t!(T, inno = 1, from = 2_1)];
            let r = [
                new_t!(T, inno = 0, from = 1_1),
                new_t!(T, inno = 1, from = 1_2),
            ];
            let mut rng = default_rng();
            for _ in 0..1000 {
                let lr = crossover_eq(&l, &r, &mut rng);
                assert_eq!(lr.len(), 2);
                assert_some_normalized!(&lr[0], [&r[0]]; {.enable()});
                assert_some_normalized!(&lr[1], [&l[0]]; {.enable()}, "not from l_0");
            }
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_eq: panics when both parents share all genes but offspring selects only from left
        #[test]
        #[should_panic(expected = "not from l_1")]
        fn test_crossover_eq_both_step_l() {
            let l = [
                new_t!(T, inno = 0, from = 1_1),
                new_t!(T, inno = 1, from = 1_2),
            ];
            let r = [
                new_t!(T, inno = 0, from = 2_1),
                new_t!(T, inno = 1, from = 2_2),
            ];
            let mut rng = default_rng();
            for _ in 0..1000 {
                let lr = crossover_eq(&l, &r, &mut rng);
                assert_eq!(lr.len(), 2);
                assert_some_normalized!(&lr[0], [&l[0], &r[0]]; {.enable()});
                assert_some_normalized!(&lr[1], [&l[1]]; {.enable()}, "not from l_1");
            }
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_eq: panics when both parents share all genes but offspring selects only from right
        #[test]
        #[should_panic(expected = "not from r_1")]
        fn test_crossover_eq_both_step_r() {
            let l = [
                new_t!(T, inno = 0, from = 1_1),
                new_t!(T, inno = 1, from = 1_2),
            ];
            let r = [
                new_t!(T, inno = 0, from = 2_1),
                new_t!(T, inno = 1, from = 2_2),
            ];
            let mut rng = default_rng();
            for _ in 0..1000 {
                let lr = crossover_eq(&l, &r, &mut rng);
                assert_eq!(lr.len(), 2);
                assert_some_normalized!(&lr[0], [&l[0], &r[0]]; {.enable()});
                assert_some_normalized!(&lr[1], [&r[1]]; {.enable()}, "not from r_1");
            }
        }
    }

    fn assert_crossover_ne<C: Connection>(l: &[C], r: &[C]) {
        for (l, r) in [(l, r), (r, l)] {
            let l_map = l.iter().map(|c| (c.inno(), c)).collect::<HashMap<_, &_>>();
            let r_map = r.iter().map(|c| (c.inno(), c)).collect::<HashMap<_, &_>>();
            let l_keys = l_map.keys().cloned().collect::<HashSet<_>>();
            let inno = l_keys
                .union(&r_map.keys().cloned().collect::<HashSet<_>>())
                .cloned()
                .collect::<HashSet<_>>();

            let mut rng = default_rng();
            for _ in 0..1000 {
                let lr = crossover_ne(l, r, &mut rng);
                assert_eq!(lr.len(), l.len());

                let lr_inno = lr.iter().map(|c| c.inno()).collect::<HashSet<_>>();
                assert!(l_keys.is_subset(&lr_inno));
                assert!(l_keys.is_superset(&lr_inno));
                assert!(inno.is_superset(&lr_inno));
                assert!(lr.is_sorted_by_key(|c| c.inno()));
                for ref lr_conn in lr {
                    match (l_map.get(&lr_conn.inno()), r_map.get(&lr_conn.inno())) {
                        (None, None) => panic!("{} is in neither l nor r", lr_conn.inno()),
                        (None, Some(conn)) => panic!("{} is in only r", conn.inno()),
                        (Some(conn), None) => {
                            assert_some_normalized!(lr_conn, [*conn]; {.enable()})
                        }
                        (Some(l_conn), Some(r_conn)) => {
                            assert_some_normalized!(lr_conn, [*l_conn, *r_conn]; {.enable()})
                        }
                    }
                }
            }
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_ne: unequal fitness preserves all genes from fitter (left) parent
        #[test]
        fn test_crossover_ne() {
            let l = [
                new_t!(T, inno = 0, from = 1_1),
                new_t!(T, inno = 1, from = 1_2),
                new_t!(T, inno = 2, from = 1_3),
                new_t!(T, inno = 9, from = 1_4),
            ];
            let r = [
                new_t!(T, inno = 0, from = 2_1),
                new_t!(T, inno = 2, from = 2_2),
                new_t!(T, inno = 3, from = 2_3),
                new_t!(T, inno = 4, from = 2_4),
                new_t!(T, inno = 7, from = 2_5),
            ];

            assert_crossover_ne(&l, &r);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_ne: handles empty inputs
        #[test]
        fn test_crossover_ne_empty() {
            let l = [new_t!(T, inno = 0, from = 1_1)];

            assert_crossover_ne(&l, &[]);
            assert_crossover_ne::<T>(&[], &[]);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_ne: parents share no genes; offspring contains all from fitter (left)
        #[test]
        fn test_crossover_ne_no_overlap() {
            let l = [
                new_t!(T, inno = 1, from = 1_1),
                new_t!(T, inno = 3, from = 1_2),
                new_t!(T, inno = 5, from = 1_3),
            ];
            let r = [
                new_t!(T, inno = 0, from = 2_1),
                new_t!(T, inno = 2, from = 2_2),
                new_t!(T, inno = 4, from = 2_3),
            ];

            assert_crossover_ne(&l, &r);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_ne: parents share all genes; offspring contains all genes
        #[test]
        fn test_crossover_ne_full_overlap() {
            let l = [
                new_t!(T, inno = 1, from = 1_1),
                new_t!(T, inno = 2, from = 1_2),
                new_t!(T, inno = 3, from = 1_3),
            ];
            let r = [
                new_t!(T, inno = 1, from = 2_1),
                new_t!(T, inno = 2, from = 2_2),
                new_t!(T, inno = 3, from = 2_3),
            ];

            assert_crossover_ne(&l, &r);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_ne: handles inno wraparound correctly
        #[test]
        fn test_crossover_ne_overflow() {
            let l = [new_t!(T, inno = 10, from = 1_1)];
            let r = [
                new_t!(T, inno = 1, from = 2_1),
                new_t!(T, inno = 2, from = 2_2),
            ];

            assert_crossover_ne(&l, &r);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_ne: left parent has no genes less than right parent's genes
        #[test]
        fn test_crossover_ne_no_lt() {
            let l = [new_t!(T, inno = 0, from = 1_1)];
            let r = [new_t!(T, inno = 10, from = 2_1)];

            assert_crossover_ne(&l, &r);
        }
    }

    fn_matrix! {
        T: WConnection | BWConnection,
        /// crossover_lt: less-than ordering produces same innos as crossover_ne with swapped args
        #[test]
        fn test_crossover_lt() {
            let l = [
                new_t!(T, inno = 0, from = 1_1),
                new_t!(T, inno = 1, from = 1_2),
                new_t!(T, inno = 2, from = 1_3),
            ];
            let r = [
                new_t!(T, inno = 0, from = 2_1),
                new_t!(T, inno = 2, from = 2_2),
                new_t!(T, inno = 3, from = 2_3),
                new_t!(T, inno = 4, from = 2_4),
            ];

            let mut rng = default_rng();
            assert_crossover_ne(&l, &r);
            for (le, ge) in crossover(&l, &r, Ordering::Less, &mut rng)
                .iter()
                .zip(crossover_ne(&r, &l, &mut rng))
            {
                assert_eq!(le.inno(), ge.inno());
            }
        }
    }
}