~ruther/ctu-fee-eoa

ref: e72ea86dfb9bdd3992b14612afcc948f73e1a423 ctu-fee-eoa/codes/eoa_lib/src/population.rs -rw-r--r-- 4.1 KiB
e72ea86d — Rutherther fix: properly evaluate nsga population 5 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
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
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 empty() -> Self {
        Self {
            population: vec![]
        }
    }

    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 join(&mut self, mut offsprings: Population<TChromosome>) {
        self.population.append(&mut offsprings.population);
    }

    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 empty() -> Self {
        Self {
            population: vec![]
        }
    }

    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 split_at(self, len: usize) -> (Self, Self) {
        let mut left = self.population;
        let right = left.split_off(len);

        (
            Self::from_vec(left),
            Self::from_vec(right),
        )
    }

    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 nonevaluated(self) -> Population<TChromosome> {
        Population::from_vec(self.deconstruct()
                             .into_iter()
                             .map(|individual| individual.chromosome)
                             .collect::<Vec<_>>())
    }

    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()
    }
}