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
use crateNumericAllele;
use ;
/// A [`Valid`] type is a type that can be checked for validity. This is used for checking if a gene
/// or a chromosome is valid. For example, a gene that represents a number between 0 and 1 can be checked
/// for validity by ensuring that the allele is between 0 and 1.
///
/// The `GeneticEngine` will check the validity of the [Chromosome](super::chromosome::Chromosome) and `Phenotype` and remove any
/// invalid individuals from the population, replacing them with new individuals at the given generation.
/// A [`Gene`] is a single unit of information in a [Chromosome](super::chromosome::Chromosome).
/// This is the most basic building block of this entire library.
///
/// Any type that implements this trait can be used as a gene in a chromosome, as such
/// it can be used in any genetic algorithm that uses this library.
///
/// # Example
/// ```
/// use radiate_core::*;
///
/// // A simple gene that represents a point.
/// #[derive(Clone, Debug, PartialEq)]
/// struct PointGene {
/// allele: (f32, f32),
/// }
///
/// // Implement the Gene trait for the PointGene.
/// impl Gene for PointGene {
/// type Allele = (f32, f32);
///
/// fn allele(&self) -> &Self::Allele {
/// &self.allele
/// }
///
/// fn allele_mut(&mut self) -> &mut Self::Allele {
/// &mut self.allele
/// }
///
/// fn new_instance(&self) -> Self {
/// PointGene { allele: (0.0, 0.0) }
/// }
///
/// fn with_allele(&self, allele: &Self::Allele) -> Self {
/// PointGene { allele: *allele }
/// }
/// }
///
/// // You must also implement the [`Valid`] trait for the gene.
/// // The default implementation of the [`Valid`] trait is to return true.
/// impl Valid for PointGene {
/// fn is_valid(&self) -> bool {
/// let (x, y) = self.allele;
/// // Check if the x and y values are between 0 and 1.
/// x >= 0.0 && x <= 1.0 && y >= 0.0 && y <= 1.0
/// }
/// }
/// ```
/// A [Gene] that represents a number. This gene can be used to represent any type of number,
/// including integers, floats, etc. Essentially, any gene that can `Add`, `Sub`, `Mul`, and `Div`
/// can be used as a [ArithmeticGene].