~ruther/ctu-fee-eoa

c47387ebf95401195e25fd03014e113ec8655e23 — Rutherther a month ago 372dc75
refactor: abstract pairing to n-ary
3 files changed, 22 insertions(+), 20 deletions(-)

M codes/eoa_lib/src/crossover.rs
M codes/eoa_lib/src/evolution.rs
M codes/eoa_lib/src/pairing.rs
M codes/eoa_lib/src/crossover.rs => codes/eoa_lib/src/crossover.rs +9 -9
@@ 5,14 5,14 @@ use rand::{Rng, RngCore};

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

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

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


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

impl<D, TOutput> Crossover for BinaryOnePointCrossover<D, TOutput>
impl<D, TOutput> Crossover<2> for BinaryOnePointCrossover<D, TOutput>
where
    D: Dim,
    DefaultAllocator: Allocator<D>


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



@@ 66,7 66,7 @@ where
            let (
                parent1,
                parent2
            ) = (&population.population[pair.0], &population.population[pair.1]);
            ) = (&population.population[pair.x], &population.population[pair.y]);

            let (
                chromosome1,


@@ 113,7 113,7 @@ where
    }
}

impl<D, T, TOutput> Crossover for OVectorOnePointCrossover<D, T, TOutput>
impl<D, T, TOutput> Crossover<2> for OVectorOnePointCrossover<D, T, TOutput>
where
    T: Scalar,
    D: Dim,


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



@@ 138,7 138,7 @@ where
            let (
                parent1,
                parent2
            ) = (&population.population[pair.0], &population.population[pair.1]);
            ) = (&population.population[pair.x], &population.population[pair.y]);

            let (
                chromosome1,


@@ 157,4 157,4 @@ where

        Population::from_vec(offsprings)
    }
}
\ No newline at end of file
}

M codes/eoa_lib/src/evolution.rs => codes/eoa_lib/src/evolution.rs +3 -3
@@ 28,13 28,13 @@ pub struct EvolutionResult<TInput, TResult> {
    pub iterations: usize
}

pub fn evolution_algorithm<TChromosome: Clone, TResult: Clone>(
pub fn evolution_algorithm<TChromosome: Clone, TResult: Clone, const DParents: usize>(
    initial_population: Population<TChromosome>,
    parents_count: usize,
    fitness: &impl FitnessFunction<In = TChromosome, Out = TResult>,
    selection: &impl Selection<TChromosome, TResult>,
    pairing: &mut impl Pairing<Chromosome = TChromosome, Out = TResult>,
    crossover: &impl Crossover<Chromosome = TChromosome, Out = TResult>,
    pairing: &mut impl Pairing<DParents, Chromosome = TChromosome, Out = TResult>,
    crossover: &impl Crossover<DParents, Chromosome = TChromosome, Out = TResult>,
    perturbation: &impl PerturbationOperator<Chromosome = TChromosome>,
    replacement: &impl Replacement<TChromosome, TResult>,
    better_than: &impl BetterThanOperator<TResult>,

M codes/eoa_lib/src/pairing.rs => codes/eoa_lib/src/pairing.rs +10 -8
@@ 1,10 1,12 @@
use std::marker::PhantomData;

use nalgebra::{Const, OVector, SVector};

use crate::replacement::EvaluatedPopulation;

pub type ParentPairing = (usize, usize);
pub type ParentPairing<const D: usize> = OVector<usize, Const<D>>;

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



@@ 12,7 14,7 @@ pub trait Pairing {
        &mut self,
        population: &EvaluatedPopulation<Self::Chromosome, Self::Out>,
        parents: T
    ) -> impl Iterator<Item = ParentPairing>;
    ) -> impl Iterator<Item = ParentPairing<D>>;
}

pub struct AdjacentPairing<TChromosome, TOutput> {


@@ 29,7 31,7 @@ impl<TChromosome, TOutput> AdjacentPairing<TChromosome, TOutput> {
    }
}

impl<TChromosome, TOutput> Pairing for AdjacentPairing<TChromosome, TOutput> {
impl<TChromosome, TOutput> Pairing<2> for AdjacentPairing<TChromosome, TOutput> {
    type Chromosome = TChromosome;
    type Out = TOutput;



@@ 37,7 39,7 @@ impl<TChromosome, TOutput> Pairing for AdjacentPairing<TChromosome, TOutput> {
        &mut self,
        _: &EvaluatedPopulation<Self::Chromosome, Self::Out>,
        parents: T
    ) -> impl Iterator<Item = ParentPairing> {
    ) -> impl Iterator<Item = ParentPairing<2>> {
        AdjacentIterator(parents, 0)
    }
}


@@ 45,7 47,7 @@ impl<TChromosome, TOutput> Pairing for AdjacentPairing<TChromosome, TOutput> {
pub struct AdjacentIterator<T: Iterator<Item = usize>>(T, usize);

impl<T: Iterator<Item = usize>> Iterator for AdjacentIterator<T> {
    type Item = ParentPairing;
    type Item = ParentPairing<2>;

    fn next(&mut self) -> Option<Self::Item> {
        let first = self.0.next();


@@ 55,10 57,10 @@ impl<T: Iterator<Item = usize>> Iterator for AdjacentIterator<T> {
            // Still two elements left, return them
            (Some(first), Some(second)) => {
                self.1 = second; // Save previous
                Some((first, second))
                Some(ParentPairing::<2>::new(first, second))
            },
            // Only one element remaining, return it and the previous one
            (Some(first), _) => Some((first, self.1)),
            (Some(first), _) => Some(ParentPairing::<2>::new(first, self.1)),
            (None, Some(_)) => panic!("Iterators cannot return something after they return none."),
            // No more elements
            _ => None