~ruther/ctu-fee-eoa

ref: 7de4f4b41b92ce93559ea9182849a54ccc87e270 ctu-fee-eoa/codes/eoa_lib/src/crossover.rs -rw-r--r-- 4.7 KiB
7de4f4b4 — Rutherther chore: do not clone for no reason in local search a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::marker::PhantomData;

use nalgebra::{allocator::Allocator, DefaultAllocator, Dim, OVector, Scalar, U1};
use rand::{Rng, RngCore};

use crate::{binary_string::BinaryString, pairing::ParentPairing, replacement::{EvaluatedPopulation, Population}};

pub trait Crossover<const D: usize> {
    type Chromosome;
    type Out;

    fn crossover(
        &self,
        parents: &EvaluatedPopulation<Self::Chromosome, Self::Out>,
        pairs: impl Iterator<Item = ParentPairing<D>>,
        rng: &mut dyn RngCore
    ) -> Population<Self::Chromosome>;
}

pub struct BinaryOnePointCrossover<D: Dim, TOutput> {
    _phantom1: PhantomData<D>,
    _phantom2: PhantomData<TOutput>
}

impl<D: Dim, TOutput> BinaryOnePointCrossover<D, TOutput>
where
    DefaultAllocator: Allocator<D>
{
    pub fn new() -> Self {
        Self {
            _phantom1: PhantomData,
            _phantom2: PhantomData,
        }
    }

    fn find_cross_point(&self, chromosome: &BinaryString<D>, rng: &mut dyn RngCore) -> usize {
        let (min, max) = (0, chromosome.vec.len());
        rng.random_range(min..max)
    }
}

// TODO: make common functions for ovector that will be used from both BinaryOnePointCrossover and OVectorOnePointCrossover
// for not repeating the code.

impl<D, TOutput> Crossover<2> for BinaryOnePointCrossover<D, TOutput>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    type Chromosome = BinaryString<D>;
    type Out = TOutput;

    fn crossover(
        &self,
        population: &EvaluatedPopulation<Self::Chromosome, Self::Out>,
        pairs: impl Iterator<Item = ParentPairing<2>>,
        rng: &mut dyn RngCore
    ) -> Population<Self::Chromosome> {

        let chromosome = &population.population[0].chromosome.vec;
        let len = population.population[0].chromosome.vec.len();
        let indices = OVector::<usize, D>::from_iterator_generic(chromosome.shape_generic().0, U1, 0..len);

        let mut offsprings = Vec::new();
        for pair in pairs {
            let (
                parent1,
                parent2
            ) = (&population.population[pair.x], &population.population[pair.y]);

            let (
                chromosome1,
                chromosome2
            ) = (&parent1.chromosome, &parent2.chromosome);

            let cross_point = self.find_cross_point(&population.population[0].chromosome, rng);

            offsprings.push(BinaryString::from_ovector(
                chromosome1.vec.zip_zip_map(
                    &chromosome2.vec,
                    &indices,
                    |first, second, i| if i <= cross_point { first } else { second }
                )));
        }

        Population::from_vec(offsprings)
    }
}


pub struct OVectorOnePointCrossover<D: Dim, T: Scalar, TOutput> {
    _phantom1: PhantomData<D>,
    _phantom2: PhantomData<TOutput>,
    _phantom3: PhantomData<T>
}

impl<D: Dim, T, TOutput> OVectorOnePointCrossover<D, T, TOutput>
where
    T: Scalar,
    DefaultAllocator: Allocator<D>
{
    pub fn new() -> Self {
        Self {
            _phantom1: PhantomData,
            _phantom2: PhantomData,
            _phantom3: PhantomData,
        }
    }

    fn find_cross_point(&self, chromosome: &OVector<T, D>, rng: &mut dyn RngCore) -> usize {
        let (min, max) = (0, chromosome.len());
        rng.random_range(min..max)
    }
}

impl<D, T, TOutput> Crossover<2> for OVectorOnePointCrossover<D, T, TOutput>
where
    T: Scalar,
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    type Chromosome = OVector<T, D>;
    type Out = TOutput;

    fn crossover(
        &self,
        population: &EvaluatedPopulation<Self::Chromosome, Self::Out>,
        pairs: impl Iterator<Item = ParentPairing<2>>,
        rng: &mut dyn RngCore
    ) -> Population<Self::Chromosome> {

        let chromosome = &population.population[0].chromosome;
        let len = population.population[0].chromosome.len();
        let indices = OVector::<usize, D>::from_iterator_generic(chromosome.shape_generic().0, U1, 0..len);

        let mut offsprings = Vec::new();
        for pair in pairs {
            let (
                parent1,
                parent2
            ) = (&population.population[pair.x], &population.population[pair.y]);

            let (
                chromosome1,
                chromosome2
            ) = (&parent1.chromosome, &parent2.chromosome);

            let cross_point = self.find_cross_point(&population.population[0].chromosome, rng);

            offsprings.push(
                chromosome1.zip_zip_map(
                    &chromosome2,
                    &indices,
                    |first, second, i| if i <= cross_point { first } else { second }
                ));
        }

        Population::from_vec(offsprings)
    }
}