Skip to main content

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::shuffled_indices(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| {
34                chromosome
35                    .as_slice()
36                    .iter()
37                    .map(|gene| gene.allele().clone())
38            })
39            .collect()
40    }
41}
42
43impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<Vec<A>>>
44    for Vec<PermutationChromosome<A>>
45{
46    fn encode(&self) -> Genotype<PermutationChromosome<A>> {
47        Genotype::from(
48            self.iter()
49                .map(|chromosome| {
50                    PermutationChromosome::new(
51                        chromosome
52                            .as_slice()
53                            .iter()
54                            .map(|gene| gene.new_instance())
55                            .collect(),
56                        Arc::clone(chromosome.alleles()),
57                    )
58                })
59                .collect::<Vec<PermutationChromosome<A>>>(),
60        )
61    }
62
63    fn decode(&self, genotype: &Genotype<PermutationChromosome<A>>) -> Vec<Vec<A>> {
64        genotype
65            .iter()
66            .map(|chromosome| {
67                chromosome
68                    .as_slice()
69                    .iter()
70                    .map(|gene| gene.allele().clone())
71                    .collect::<Vec<A>>()
72            })
73            .collect::<Vec<Vec<A>>>()
74    }
75}
76
77impl<A: PartialEq + Clone> Codec<PermutationChromosome<A>, Vec<A>> for PermutationChromosome<A> {
78    fn encode(&self) -> Genotype<PermutationChromosome<A>> {
79        Genotype::from(PermutationChromosome::new(
80            self.as_slice()
81                .iter()
82                .map(|gene| gene.new_instance())
83                .collect(),
84            Arc::clone(self.alleles()),
85        ))
86    }
87
88    fn decode(&self, genotype: &Genotype<PermutationChromosome<A>>) -> Vec<A> {
89        genotype
90            .iter()
91            .flat_map(|chromosome| {
92                chromosome
93                    .as_slice()
94                    .iter()
95                    .map(|gene| gene.allele().clone())
96            })
97            .collect()
98    }
99}