radiate_core/codecs/
mod.rs

1use super::genome::genotype::Genotype;
2
3pub mod bit;
4pub mod char;
5pub mod float;
6pub mod function;
7pub mod int;
8pub mod permutation;
9pub mod subset;
10
11use crate::Chromosome;
12pub use bit::BitCodec;
13pub use char::CharCodec;
14pub use float::FloatCodec;
15pub use function::FnCodec;
16pub use int::IntCodec;
17pub use permutation::PermutationCodec;
18pub use subset::SubSetCodec;
19
20/// The `Codec` is a core concept in Radiate, as it allows for the encoding and decoding from
21/// a `Genotype` to the type `T` (commonly called Phenotype in biology) that is being optimized.
22///
23/// In order to have a valid `GeneticEngine`, a `Codec` must be supplied. In a sense, the encoding is the
24/// 'domain language' of the `GeneticEngine`. It is the way that the `GeneticEngine` interacts with the
25/// problem space. The `Codec` is responsible for converting to and from this 'domain language'.
26///
27/// # Example
28/// ``` rust
29/// use radiate_core::*;
30///
31/// // A simple struct to represent the NQueens problem.
32/// #[derive(Clone, Debug, PartialEq)]
33/// struct NQueens(Vec<i32>);
34///
35/// // A Codec for the NQueens problem.
36/// struct NQueensCodec {
37///    size: i32,
38/// }
39///
40/// // Implement the Codec trait for the NQueensCodec. The `encode` function creates a `Genotype`
41/// // with a single chromosome of `size` genes. The `decode` function creates a `NQueens` from the
42/// // `Genotype`.
43/// impl Codec<IntChromosome<i32>, NQueens> for NQueensCodec {
44///     fn encode(&self) -> Genotype<IntChromosome<i32>> {
45///         let genes = (0..self.size).map(|_| IntGene::from(0..self.size)).collect();
46///         let chromosomes = vec![IntChromosome::new(genes)];
47///         Genotype::new(chromosomes)
48///     }
49///
50///     fn decode(&self, genotype: &Genotype<IntChromosome<i32>>) -> NQueens {
51///         NQueens(genotype[0].iter().map(|g| *g.allele()).collect())
52///     }
53/// }
54///
55/// // Create a new NQueensCodec with a size of 5.
56/// let codec = NQueensCodec { size: 5 };
57///
58/// // encode a new Genotype of IntGenes with a size of 5. The result will be a genotype with a single chromosome with 5 genes.
59/// // The genes will have a min value of 0, a max value of 5, an upper_bound of 5, and a lower_bound of 0.
60/// // The alleles will be random values between 0 and 5. It will look something like:
61/// // Genotype {
62/// //     chromosomes: [
63/// //         IntChromosome<i32> {
64/// //             genes: [
65/// //                 IntGene { allele: 3, min: 0, max: 5, ... },
66/// //                 IntGene { allele: 7, min: 0, max: 5, ... },
67/// //                 IntGene { allele: 1, min: 0, max: 5, ... },
68/// //                 IntGene { allele: 5, min: 0, max: 5, ... },
69/// //                 IntGene { allele: 2, min: 0, max: 5, ... },
70/// //             ]
71/// //         }
72/// //     ]
73/// // }
74/// let genotype = codec.encode();
75///
76/// // decode the genotype to a NQueens. The result will be a NQueens struct with a Vec<i32> of 8 random values between 0 and 8.
77/// // It will look something like:
78/// // NQueens([3, 7, 1, 5, 2])
79/// let nqueens = codec.decode(&genotype);
80/// ```
81///
82/// # Type Parameters
83/// - `C`: The type of the Chromosome that is being optimized - the 'problem space'.
84/// - `T`: The type of the Phenotype that is being optimized the expression of the 'problem space'.
85///
86pub trait Codec<C: Chromosome, T> {
87    fn encode(&self) -> Genotype<C>;
88
89    fn decode(&self, genotype: &Genotype<C>) -> T;
90}
91
92pub trait RefCodec<C: Chromosome, O>: Codec<C, O> {
93    fn decode<'a, 'b>(&self, genotype: &'a Genotype<C>) -> &'b Genotype<C>
94    where
95        'a: 'b;
96}
97
98// impl<T, F> RefCodec<GraphChromosome<T>, Graph<T>> for F
99// where
100//     T: Clone + PartialEq + Default,
101//     F: Codec<GraphChromosome<T>, Graph<T>>,
102// {
103//     fn decode<'a, 'b>(
104//         &self,
105//         genotype: &'a Genotype<GraphChromosome<T>>,
106//     ) -> &'b Genotype<GraphChromosome<T>>
107//     where
108//         'a: 'b,
109//     {
110//         genotype
111//     }
112// }