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
// imports

// [[file:~/Workspace/Programming/structure-predication/spdkit/spdkit.note::*imports][imports:1]]
use std::marker::PhantomData;

use crate::common::*;
use crate::fitness::*;
use crate::individual::*;
use crate::operators::*;
use crate::population::*;

use super::*;
// imports:1 ends here

// base

// [[file:~/Workspace/Programming/structure-predication/spdkit/spdkit.note::*base][base:1]]
pub struct Valuer<G, F, C>
where
    G: Genome,
    F: EvaluateFitness<G>,
    C: EvaluateObjectiveValue<G>,
{
    fitness: Option<F>,
    creator: Option<C>,
    _g: PhantomData<G>,
}

impl<G, F, C> Valuer<G, F, C>
where
    G: Genome,
    F: EvaluateFitness<G>,
    C: EvaluateObjectiveValue<G>,
{
    pub fn new() -> Self {
        Self {
            fitness: None,
            creator: None,
            _g: PhantomData,
        }
    }

    /// Set fitness evaluator for building population.
    pub fn with_fitness(mut self, f: F) -> Self {
        self.fitness = Some(f);
        self
    }

    /// Set evaluator of objective value for creating individuals.
    pub fn with_creator(mut self, c: C) -> Self {
        self.creator = Some(c);
        self
    }

    /// Create individuals from genomes.
    pub fn create_individuals(&self, genomes: Vec<G>) -> Vec<Individual<G>> {
        if let Some(creator) = &self.creator {
            creator.create(genomes)
        } else {
            panic!("creator not set!");
        }
    }

    /// Build a population from individuals.
    pub fn build_population(&mut self, indvs: Vec<Individual<G>>) -> Population<G> {
        if let Some(fitness) = &mut self.fitness {
            Population::build(indvs, fitness)
        } else {
            panic!("fitness not set!");
        }
    }
}
// base:1 ends here