radiate_core/genome/chromosomes/
bit.rs

1use crate::{Chromosome, Gene, Valid, random_provider};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4use std::fmt::{Debug, Display};
5
6/// A gene that represents a single bit. The `allele` is a `bool` that is randomly assigned.
7/// The `allele` is either `true` or `false`. This is the simplest form of a gene and
8/// in traditional genetic algorithms is the gene that is used to represent the individuals.
9///
10/// # Example
11/// ``` rust
12/// use radiate_core::*;
13///
14/// // Craete a new BitGene from the allele `true`.
15/// let gene: BitGene = true.into();
16///
17/// // Create a new BitGene with a random allele.
18/// let gene = BitGene::new();
19///
20/// // Get the allele (bool) of the BitGene.
21/// let allele = gene.allele();
22///
23/// // Create a new BitGene from the allele.
24/// let gene = gene.with_allele(allele);
25/// ```
26///
27#[derive(Clone, PartialEq, Debug)]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29pub struct BitGene {
30    allele: bool,
31}
32
33impl BitGene {
34    /// Create a new [`BitGene`] with a random allele.
35    pub fn new() -> Self {
36        BitGene {
37            allele: random_provider::bool(0.5),
38        }
39    }
40}
41
42/// Implement the [`Gene`] trait for the [`BitGene`].
43/// This allows the [`BitGene`] to be used in a [`Chromosome`] - specifically the
44/// [`BitChromosome`], thus allowing the [`BitGene`] to be used in the `GeneticEngine`.
45impl Gene for BitGene {
46    type Allele = bool;
47
48    fn allele(&self) -> &bool {
49        &self.allele
50    }
51
52    fn new_instance(&self) -> BitGene {
53        BitGene::new()
54    }
55
56    fn with_allele(&self, allele: &bool) -> BitGene {
57        BitGene { allele: *allele }
58    }
59}
60
61/// Because a [`BitGene`] is either `true` or `false` it is always valid.
62impl Valid for BitGene {}
63
64impl Default for BitGene {
65    fn default() -> Self {
66        Self::new()
67    }
68}
69
70impl Display for BitGene {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        write!(f, "{}", if self.allele { 1 } else { 0 })
73    }
74}
75
76impl From<BitGene> for bool {
77    fn from(gene: BitGene) -> bool {
78        gene.allele
79    }
80}
81
82impl From<bool> for BitGene {
83    fn from(allele: bool) -> BitGene {
84        BitGene { allele }
85    }
86}
87
88/// A [`Chromosome`] that contains [`BitGene`].
89/// A [`BitChromosome`] is a collection of [`BitGene`] that represent the genetic
90/// material of an individual in the population.
91#[derive(Clone, PartialEq, Default, Debug)]
92#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
93pub struct BitChromosome {
94    genes: Vec<BitGene>,
95}
96
97impl BitChromosome {
98    /// Create a new [`BitChromosome`] with the given length.
99    /// The length is the number of genes in the chromosome.
100    pub fn new(length: usize) -> Self {
101        let genes = (0..length).map(|_| BitGene::new()).collect();
102        BitChromosome { genes }
103    }
104}
105
106impl Chromosome for BitChromosome {
107    type Gene = BitGene;
108
109    fn genes(&self) -> &[Self::Gene] {
110        &self.genes
111    }
112
113    fn genes_mut(&mut self) -> &mut [Self::Gene] {
114        &mut self.genes
115    }
116}
117
118impl Valid for BitChromosome {
119    fn is_valid(&self) -> bool {
120        self.genes.iter().all(|gene| gene.is_valid())
121    }
122}
123
124impl From<Vec<BitGene>> for BitChromosome {
125    fn from(genes: Vec<BitGene>) -> Self {
126        BitChromosome { genes }
127    }
128}
129
130impl From<Vec<bool>> for BitChromosome {
131    fn from(alleles: Vec<bool>) -> Self {
132        BitChromosome {
133            genes: alleles.into_iter().map(BitGene::from).collect(),
134        }
135    }
136}
137
138#[cfg(test)]
139mod test {
140    use super::*;
141
142    #[test]
143    fn test_into() {
144        let gene = BitGene::new();
145        let copy = gene.clone();
146        let allele: bool = gene.into();
147        assert_eq!(allele, copy.allele);
148    }
149
150    #[test]
151    fn test_from() {
152        let gene = BitGene::new();
153        let copy = gene.clone();
154        let allele: BitGene = copy;
155        assert_eq!(allele, gene);
156    }
157
158    #[test]
159    fn test_from_allele() {
160        let gene = BitGene::new();
161        let copy = gene.clone();
162        let allele = gene.allele();
163        let new_gene = gene.with_allele(allele);
164        assert_eq!(new_gene, copy);
165    }
166
167    #[test]
168    #[cfg(feature = "serde")]
169    fn test_bit_gene_serialization() {
170        let gene = BitGene::new();
171        let serialized = serde_json::to_string(&gene).expect("Failed to serialize BitGene");
172        let deserialized: BitGene =
173            serde_json::from_str(&serialized).expect("Failed to deserialize BitGene");
174
175        let chromosome = BitChromosome::new(10);
176        let serialized_chromosome =
177            serde_json::to_string(&chromosome).expect("Failed to serialize BitChromosome");
178        let deserialized_chromosome: BitChromosome = serde_json::from_str(&serialized_chromosome)
179            .expect("Failed to deserialize BitChromosome");
180
181        assert_eq!(gene, deserialized);
182        assert_eq!(chromosome, deserialized_chromosome);
183    }
184}