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
use crateGeneT;
/// Minimal evaluation contract for a chromosome.
///
/// `ChromosomeT` covers only the fitness/age evaluation surface — the smallest
/// set of methods that every chromosome type must provide regardless of its
/// internal representation (flat-slice, tree, variable-length, etc.).
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::traits::{ChromosomeT, GeneT, LinearChromosome};
/// use genetic_algorithms::chromosomes::Binary;
/// use genetic_algorithms::traits::ChromosomeT as _;
///
/// // Use a built-in type that implements ChromosomeT:
/// let mut chrom = Binary::new();
/// chrom.set_fitness(0.95);
/// assert_eq!(chrom.fitness(), 0.95);
/// chrom.set_age(2);
/// assert_eq!(chrom.age(), 2);
/// ```
///
/// # Flat-slice chromosomes
///
/// Types backed by a contiguous DNA slice (the traditional GA representation)
/// should implement [`LinearChromosome`](crate::traits::LinearChromosome) in
/// addition to this trait. `LinearChromosome: ChromosomeT` extends the contract
/// with `dna()`, `dna_mut()`, `set_dna()`, `set_fitness_fn()`, and provides
/// default implementations of `set_gene()` and `reset()`.
///
/// # Custom / non-linear chromosomes
///
/// Tree chromosomes, variable-length chromosomes, and other non-flat types
/// only need to implement `ChromosomeT`. They are compatible with any operator
/// or engine that is generic over `U: ChromosomeT`.
///
/// # Required bounds
///
/// `Clone + Default + Send + Sync + 'static` — chromosomes are cloned during
/// reproduction and moved across rayon threads during parallel evaluation.