Trait Codec

Source
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§

Source

fn encode(&self) -> Genotype<C>

Source

fn decode(&self, genotype: &Genotype<C>) -> T

Implementors§

Source§

impl Codec<BitChromosome, bool> for BitCodec<bool>

Source§

impl Codec<BitChromosome, Vec<bool>> for BitCodec<Vec<bool>>

Source§

impl Codec<BitChromosome, Vec<Vec<bool>>> for BitCodec<Vec<Vec<bool>>>

Source§

impl Codec<CharChromosome, Vec<char>> for CharCodec<Vec<char>>

Source§

impl Codec<CharChromosome, Vec<Vec<char>>> for CharCodec<Vec<Vec<char>>>

Source§

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);
Source§

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);
Source§

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);
Source§

impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<A>> for PermutationCodec<A>

Source§

impl<C: Chromosome, T> Codec<C, T> for FnCodec<C, T>

Source§

impl<T> Codec<BitChromosome, Vec<Arc<T>>> for SubSetCodec<T>

Source§

impl<T: Integer<T>> Codec<IntChromosome<T>, Vec<Vec<T>>> for IntCodec<T, Vec<Vec<T>>>

Implement the Codec trait for a Genotype of IntGenes. This will produce a Genotype with the given number of chromosomes and genes. The decode function will create a Vec<Vec<T>> or a matrix.

Source§

impl<T: Integer<T>> Codec<IntChromosome<T>, Vec<T>> for IntCodec<T, Vec<T>>

Implement the Codec trait for a Genotype of IntGenes. This will produce a Genotype with a single chromosome and num_genes genes. The decode function will create a Vec<T> or a vector.

Source§

impl<T: Integer<T>> Codec<IntChromosome<T>, T> for IntCodec<T, T>

Implement the Codec trait for a Genotype of IntGenes. This will produce a Genotype with a single chromosome and a single gene. The decode function will create a T or a single value.