1from overrides import overrides
2
3from eckity.evaluators.individual_evaluator import IndividualEvaluator
4from eckity.evaluators.population_evaluator import PopulationEvaluator
5from eckity.fitness.fitness import Fitness
6from eckity.individual import Individual
7
8
9class SimplePopulationEvaluator(PopulationEvaluator):
10 @overrides
11 def _evaluate(self, population):
12 """
13 Updates the fitness score of the given individuals, then returns the best individual
14
15 Parameters
16 ----------
17 population:
18 the population of the evolutionary experiment
19
20 Returns
21 -------
22 individual
23 the individual with the best fitness out of the given individuals
24 """
25 super()._evaluate(population)
26 for sub_population in population.sub_populations:
27 sub_population = population.sub_populations[0]
28 sp_eval: IndividualEvaluator = sub_population.evaluator
29 eval_results = self.executor.map(sp_eval.evaluate_individual, sub_population.individuals)
30 for ind, fitness_score in zip(sub_population.individuals, eval_results):
31 ind.fitness.set_fitness(fitness_score)
32
33
34 # only one subpopulation in simple case
35 individuals = population.sub_populations[0].individuals
36
37 best_ind: Individual = population.sub_populations[0].individuals[0]
38 best_fitness: Fitness = best_ind.fitness
39
40 for ind in individuals[1:]:
41 if ind.fitness.better_than(ind, best_fitness, best_ind):
42 best_ind = ind
43 best_fitness = ind.fitness
44
45 return best_ind