radiate_core/codecs/
permutation.rs

1use crate::{
2    Chromosome, Codec, Gene, Genotype, PermutationChromosome, PermutationGene, random_provider,
3};
4use std::sync::Arc;
5
6#[derive(Clone)]
7pub struct PermutationCodec<A: PartialEq + Clone> {
8    alleles: Arc<[A]>,
9}
10
11impl<A: PartialEq + Clone> PermutationCodec<A> {
12    pub fn new(alleles: Vec<A>) -> Self {
13        PermutationCodec {
14            alleles: alleles.into_boxed_slice().into(),
15        }
16    }
17}
18
19impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<A>> for PermutationCodec<A> {
20    fn encode(&self) -> Genotype<PermutationChromosome<A>> {
21        Genotype::from(PermutationChromosome::new(
22            random_provider::indexes(0..self.alleles.len())
23                .iter()
24                .map(|i| PermutationGene::new(*i, Arc::clone(&self.alleles)))
25                .collect(),
26            Arc::clone(&self.alleles),
27        ))
28    }
29
30    fn decode(&self, genotype: &Genotype<PermutationChromosome<A>>) -> Vec<A> {
31        genotype
32            .iter()
33            .flat_map(|chromosome| chromosome.genes().iter().map(|gene| gene.allele().clone()))
34            .collect()
35    }
36}
37
38impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<Vec<A>>>
39    for Vec<PermutationChromosome<A>>
40{
41    fn encode(&self) -> Genotype<PermutationChromosome<A>> {
42        Genotype::from(
43            self.iter()
44                .map(|chromosome| {
45                    PermutationChromosome::new(
46                        chromosome
47                            .genes()
48                            .iter()
49                            .map(|gene| gene.new_instance())
50                            .collect(),
51                        Arc::clone(chromosome.alleles()),
52                    )
53                })
54                .collect::<Vec<PermutationChromosome<A>>>(),
55        )
56    }
57
58    fn decode(&self, genotype: &Genotype<PermutationChromosome<A>>) -> Vec<Vec<A>> {
59        genotype
60            .iter()
61            .map(|chromosome| {
62                chromosome
63                    .genes()
64                    .iter()
65                    .map(|gene| gene.allele().clone())
66                    .collect::<Vec<A>>()
67            })
68            .collect::<Vec<Vec<A>>>()
69    }
70}
71
72impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<A>> for PermutationChromosome<A> {
73    fn encode(&self) -> Genotype<PermutationChromosome<A>> {
74        Genotype::from(PermutationChromosome::new(
75            self.genes()
76                .iter()
77                .map(|gene| gene.new_instance())
78                .collect(),
79            Arc::clone(self.alleles()),
80        ))
81    }
82
83    fn decode(&self, genotype: &Genotype<PermutationChromosome<A>>) -> Vec<A> {
84        genotype
85            .iter()
86            .flat_map(|chromosome| chromosome.genes().iter().map(|gene| gene.allele().clone()))
87            .collect()
88    }
89}