pub trait Codec<C: Chromosome, T> {
// Required methods
fn encode(&self) -> Genotype<C>;
fn decode(&self, genotype: &Genotype<C>) -> T;
}Expand description
The Codec is a core concept in Radiate, as it allows for the encoding and decoding from
a Genotype to the type T (commonly called Phenotype in biology) that is being optimized.
In order to have a valid GeneticEngine, a Codec must be supplied. In a sense, the encoding is the
‘domain language’ of the GeneticEngine. It is the way that the GeneticEngine interacts with the
problem space. The Codec is responsible for converting to and from this ‘domain language’.
§Example
use radiate_core::*;
// A simple struct to represent the NQueens problem.
#[derive(Clone, Debug, PartialEq)]
struct NQueens(Vec<i32>);
// A Codec for the NQueens problem.
struct NQueensCodec {
size: i32,
}
// Implement the Codec trait for the NQueensCodec. The `encode` function creates a `Genotype`
// with a single chromosome of `size` genes. The `decode` function creates a `NQueens` from the
// `Genotype`.
impl Codec<IntChromosome<i32>, NQueens> for NQueensCodec {
fn encode(&self) -> Genotype<IntChromosome<i32>> {
let genes = (0..self.size).map(|_| IntGene::from(0..self.size)).collect();
let chromosomes = vec![IntChromosome::new(genes)];
Genotype::new(chromosomes)
}
fn decode(&self, genotype: &Genotype<IntChromosome<i32>>) -> NQueens {
NQueens(genotype[0].iter().map(|g| *g.allele()).collect())
}
}
// Create a new NQueensCodec with a size of 5.
let codec = NQueensCodec { size: 5 };
// encode a new Genotype of IntGenes with a size of 5. The result will be a genotype with a single chromosome with 5 genes.
// 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.
// The alleles will be random values between 0 and 5. It will look something like:
// Genotype {
// chromosomes: [
// IntChromosome<i32> {
// genes: [
// IntGene { allele: 3, min: 0, max: 5, ... },
// IntGene { allele: 7, min: 0, max: 5, ... },
// IntGene { allele: 1, min: 0, max: 5, ... },
// IntGene { allele: 5, min: 0, max: 5, ... },
// IntGene { allele: 2, min: 0, max: 5, ... },
// ]
// }
// ]
// }
let genotype = codec.encode();
// 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.
// It will look something like:
// NQueens([3, 7, 1, 5, 2])
let nqueens = codec.decode(&genotype);§Type Parameters
C: The type of the Chromosome that is being optimized - the ‘problem space’.T: The type of the Phenotype that is being optimized the expression of the ‘problem space’.
Required Methods§
Implementations on Foreign Types§
Source§impl Codec<BitChromosome, Vec<Vec<bool>>> for Vec<BitChromosome>
impl Codec<BitChromosome, Vec<Vec<bool>>> for Vec<BitChromosome>
Source§impl Codec<CharChromosome, Vec<Vec<char>>> for Vec<CharChromosome>
impl Codec<CharChromosome, Vec<Vec<char>>> for Vec<CharChromosome>
Source§impl Codec<FloatChromosome, Vec<Vec<f32>>> for Vec<FloatChromosome>
Implement the Codec trait for a Vec for FloatChromosome.
This is effectively the same as creating a FloatCodec matrix
impl Codec<FloatChromosome, Vec<Vec<f32>>> for Vec<FloatChromosome>
Implement the Codec trait for a Vec for FloatChromosome. This is effectively the same as creating a FloatCodec matrix
§Example
use radiate_core::*;
let codec = vec![
FloatChromosome::from((3, 0.0..1.0)),
FloatChromosome::from((4, 0.0..1.0)),
];
let genotype: Genotype<FloatChromosome> = codec.encode();
let decoded: Vec<Vec<f32>> = codec.decode(&genotype);
assert_eq!(decoded.len(), 2);
assert_eq!(decoded[0].len(), 3);
assert_eq!(decoded[1].len(), 4);Source§impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<Vec<A>>> for Vec<PermutationChromosome<A>>
impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<Vec<A>>> for Vec<PermutationChromosome<A>>
Source§impl<T: Integer<T>> Codec<IntChromosome<T>, Vec<Vec<T>>> for Vec<IntChromosome<T>>
impl<T: Integer<T>> Codec<IntChromosome<T>, Vec<Vec<T>>> for Vec<IntChromosome<T>>
Implementors§
impl Codec<BitChromosome, bool> for BitCodec<bool>
impl Codec<BitChromosome, Vec<bool>> for BitChromosome
impl Codec<BitChromosome, Vec<bool>> for BitCodec<Vec<bool>>
impl Codec<BitChromosome, Vec<Vec<bool>>> for BitCodec<Vec<Vec<bool>>>
impl Codec<CharChromosome, Vec<char>> for CharChromosome
impl Codec<CharChromosome, Vec<char>> for CharCodec<Vec<char>>
impl Codec<CharChromosome, Vec<Vec<char>>> for CharCodec<Vec<Vec<char>>>
impl Codec<FloatChromosome, f32> for FloatCodec<f32>
Implement the Codec trait for a FloatCodec with a f32 type.
This will decode to a single f32 value.
The encode function creates a Genotype with a single chromosomes
and a single gene per chromosome.
§Example
use radiate_core::*;
// Create a new FloatCodec with a single gene
// per chromosome - a single f32 value.
let codec = FloatCodec::scalar(0.0..1.0);
let genotype: Genotype<FloatChromosome> = codec.encode();
let decoded: f32 = codec.decode(&genotype);impl Codec<FloatChromosome, Vec<f32>> for FloatChromosome
Implement the Codec trait for a single FloatChromosome. This is effectively the same as creating a FloatCodec vector
use radiate_core::*;
let codec = FloatChromosome::from((3, 0.0..1.0));
let genotype: Genotype<FloatChromosome> = codec.encode();
let decoded: Vec<f32> = codec.decode(&genotype);
assert_eq!(decoded.len(), 3);impl Codec<FloatChromosome, Vec<f32>> for FloatCodec<Vec<f32>>
Implement the Codec trait for a FloatCodec with a Vec<f32> type.
This will decode to a vector of f32 values.
The encode function creates a Genotype with a single chromosomes
and num_genes genes per chromosome.
§Example
use radiate_core::*;
// Create a new FloatCodec with 3 genes
// per chromosome - a vector with 3 f32 values.
let codec = FloatCodec::vector(3, 0.0..1.0);
let genotype: Genotype<FloatChromosome> = codec.encode();
let decoded: Vec<f32> = codec.decode(&genotype);
assert_eq!(decoded.len(), 3);impl Codec<FloatChromosome, Vec<Vec<f32>>> for FloatCodec<Vec<Vec<f32>>>
Implement the Codec trait for a FloatCodec with a Vec<Vec<f32>> type.
This will decode to a matrix of f32 values.
The encode function creates a Genotype with num_chromosomes chromosomes
and num_genes genes per chromosome.
- Example:
use radiate_core::*;
// Create a new FloatCodec with 3 chromosomes and 4 genes
// per chromosome - a 3x4 matrix of f32 values.
let codec = FloatCodec::matrix(3, 4, 0.0..1.0);
let genotype: Genotype<FloatChromosome> = codec.encode();
let decoded: Vec<Vec<f32>> = codec.decode(&genotype);
assert_eq!(decoded.len(), 3);
assert_eq!(decoded[0].len(), 4);impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<A>> for PermutationChromosome<A>
impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<A>> for PermutationCodec<A>
impl<C: Chromosome, T> Codec<C, T> for FnCodec<C, T>
impl<T> Codec<BitChromosome, Vec<Arc<T>>> for SubSetCodec<T>
impl<T: Integer<T>> Codec<IntChromosome<T>, Vec<Vec<T>>> for IntCodec<T, Vec<Vec<T>>>
§Example
use radiate_core::*;
// Create a new IntCodec with 10 chromosomes with 10 genes
// per chromosome - a matrix of i32 values.
let codec = IntCodec::matrix(10, 10, 0..100);
let genotype: Genotype<IntChromosome<i32>> = codec.encode();
let decoded: Vec<Vec<i32>> = codec.decode(&genotype);impl<T: Integer<T>> Codec<IntChromosome<T>, Vec<T>> for IntChromosome<T>
impl<T: Integer<T>> Codec<IntChromosome<T>, Vec<T>> for IntCodec<T, Vec<T>>
§Example
use radiate_core::*;
// Create a new IntCodec with 10 genes
// per chromosome - a vector of i32 values.
let codec = IntCodec::vector(10, 0..100);
let genotype: Genotype<IntChromosome<i32>> = codec.encode();
let decoded: Vec<i32> = codec.decode(&genotype);impl<T: Integer<T>> Codec<IntChromosome<T>, T> for IntCodec<T, T>
§Example
use radiate_core::*;
// Create a new IntCodec with a single gene
// per chromosome - a single i32 value.
let codec = IntCodec::scalar(0..100);
let genotype: Genotype<IntChromosome<i32>> = codec.encode();
let decoded: i32 = codec.decode(&genotype);