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
use crate::engines::score::Score;

use super::{genes::gene::Gene, genotype::Genotype};

pub struct Phenotype<G: Gene<G, A>, A> {
    pub genotype: Genotype<G, A>,
    pub score: Option<Score>,
    pub generation: i32,
}

impl<G: Gene<G, A>, A> Phenotype<G, A> {
    pub fn genotype(&self) -> &Genotype<G, A> {
        &self.genotype
    }

    pub fn from_genotype(genotype: Genotype<G, A>, generation: i32) -> Self {
        Phenotype {
            genotype,
            score: None,
            generation
        }
    }

    pub fn score(&self) -> &Option<Score> {
        &self.score
    }

    pub fn set_score(&mut self, score: Option<Score>) {
        self.score = score;
    }

    pub fn age(&self, generation: i32) -> i32 {
        generation - self.generation
    }
}

impl<G: Gene<G, A>, A> Clone for Phenotype<G, A> {
    fn clone(&self) -> Self {
        Phenotype {
            genotype: self.genotype.clone(),
            score: match &self.score {
                Some(score) => Some(score.clone()),
                None => None,
            },
            generation: self.generation
        }
    }
}

impl<G: Gene<G, A>, A> PartialEq for Phenotype<G, A> {
    fn eq(&self, other: &Self) -> bool {
        self.genotype == other.genotype && self.score == other.score && self.generation == other.generation
    }
}

impl<G: Gene<G, A>, A> PartialOrd for Phenotype<G, A> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.score.partial_cmp(&other.score)
    }
}

impl<G: Gene<G, A> + std::fmt::Debug, A> std::fmt::Debug for Phenotype<G, A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}, generation: {:?}, score: {:?}", self.genotype, self.generation, self.score)
    }
}