~ruther/ctu-fee-eoa

ref: 79a2ec1f864b259b69e909656d2751aad38b45b9 ctu-fee-eoa/codes/eoa_lib/src/population.rs -rw-r--r-- 3.3 KiB
79a2ec1f — Rutherther feat: add convenient function for stochastic ranking evaluation 11 days 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
use crate::{comparison::BetterThanOperator, fitness::FitnessFunction};

#[derive(Clone, Debug)]
pub struct Population<TChromosome> {
    population: Vec<TChromosome>
}

#[derive(Clone, Debug)]
pub struct EvaluatedChromosome<TChromosome, TResult> {
    pub chromosome: TChromosome,
    pub evaluation: TResult,
}

#[derive(Clone, Debug)]
pub struct EvaluatedPopulation<TChromosome, TResult> {
    pub population: Vec<EvaluatedChromosome<TChromosome, TResult>>
}

impl<TChromosome> Population<TChromosome> {
    pub fn from_vec(vec: Vec<TChromosome>) -> Self {
        Self {
            population: vec
        }
    }

    pub fn from_iterator(iter: impl Iterator<Item = TChromosome>) -> Self {
        Self::from_vec(iter.collect())
    }

    pub fn evaluate<T: FitnessFunction<In = TChromosome>>(self, func: &T) -> Result<EvaluatedPopulation<TChromosome, T::Out>, T::Err> {
        EvaluatedPopulation::evaluate(
            self.population,
            func
        )
    }

    pub fn into_iter(self) -> impl Iterator<Item = TChromosome> {
        self.population.into_iter()
    }

    pub fn iter(&self) -> impl Iterator<Item = &TChromosome> {
        self.population.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut TChromosome> {
        self.population.iter_mut()
    }
}

impl<TInput, TResult> EvaluatedChromosome<TInput, TResult> {
    pub fn deconstruct(self) -> (TInput, TResult) {
        (self.chromosome, self.evaluation)
    }
}

impl<TChromosome, TResult> EvaluatedPopulation<TChromosome, TResult> {
    pub fn new() -> Self {
        Self {
            population: vec![]
        }
    }

    pub fn evaluate<T: FitnessFunction<In = TChromosome, Out = TResult>>(chromosomes: Vec<TChromosome>, func: &T) -> Result<Self, T::Err> {
        func.fit_population(chromosomes)
            .map(|population| Self::from_vec(population))
    }

    pub fn from_vec(vec: Vec<EvaluatedChromosome<TChromosome, TResult>>) -> Self {
        Self {
            population: vec
        }
    }

    pub fn best_candidate(&self, better_than: &impl BetterThanOperator<TResult>) -> &EvaluatedChromosome<TChromosome, TResult> {
        let mut best_so_far = &self.population[0];
        for individual in self.population.iter().skip(1) {
            if better_than.better_than(&individual.evaluation, &best_so_far.evaluation) {
                best_so_far = individual;
            }
        }

        best_so_far
    }

    pub fn add(&mut self, c: EvaluatedChromosome<TChromosome, TResult>) {
        self.population.push(c)
    }

    pub fn deconstruct(self) -> Vec<EvaluatedChromosome<TChromosome, TResult>> {
        self.population
    }

    pub fn join(&mut self, mut offsprings: EvaluatedPopulation<TChromosome, TResult>) {
        self.population.append(&mut offsprings.population);
    }

    pub fn iter(&self) -> impl Iterator<Item = &EvaluatedChromosome<TChromosome, TResult>> {
        self.population.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut EvaluatedChromosome<TChromosome, TResult>> {
        self.population.iter_mut()
    }

}

impl<TChromosome, TResult: Copy> EvaluatedPopulation<TChromosome, TResult> {
    pub fn evaluations_vec(&self) -> Vec<TResult> {
        self.population
            .iter()
            .map(|individual| individual.evaluation)
            .collect()
    }
}