eckity.evaluators.simple_population_evaluator

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