pub struct FnCodec<C: Chromosome, T> { /* private fields */ }Expand description
A Codec that uses functions to encode and decode a Genotype to and from a type T.
Most of the other codecs in this module are more specialized and are used to create Genotypes of specific types of Chromosomes.
This one, however, is more general and can be used to create Genotypes of any type of Chromosome.
§Example
use radiate_core::*;
const N_QUEENS: usize = 8;
fn main() {
// this is a simple example of the NQueens problem.
// The resulting codec type will be FnCodec<IntChromosome<i8>, Vec<i8>>.
let codec = FnCodec::new()
.with_encoder(|| {
Genotype::new(vec![IntChromosome::new((0..N_QUEENS)
.map(|_| IntGene::from(0..N_QUEENS as i8))
.collect(),
)])
})
.with_decoder(|genotype| {
genotype[0]
.genes()
.iter()
.map(|g| *g.allele())
.collect::<Vec<i8>>()
});
// encode and decode
let genotype: Genotype<IntChromosome<i8>> = codec.encode();
let decoded: Vec<i8> = codec.decode(&genotype);
}§Type Parameters
C: The type of chromosome used in the genotype, which must implement theChromosometrait.T: The type that the genotype will be decoded to.
Implementations§
Source§impl<C: Chromosome, T> FnCodec<C, T>
impl<C: Chromosome, T> FnCodec<C, T>
pub fn new() -> Self
pub fn with_encoder<F>(self, encoder: F) -> Self
pub fn with_decoder<F>(self, decoder: F) -> Self
Trait Implementations§
Source§impl<C: Chromosome, T> Codec<C, T> for FnCodec<C, T>
impl<C: Chromosome, T> Codec<C, T> for FnCodec<C, T>
Auto Trait Implementations§
impl<C, T> Freeze for FnCodec<C, T>
impl<C, T> !RefUnwindSafe for FnCodec<C, T>
impl<C, T> !Send for FnCodec<C, T>
impl<C, T> !Sync for FnCodec<C, T>
impl<C, T> Unpin for FnCodec<C, T>
impl<C, T> !UnwindSafe for FnCodec<C, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more