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 """ 11 Computes fitness value for the whole population. 12 All simple classes assume only one sub-population. 13 """ 14 15 def __init__(self, executor_method="map"): 16 super().__init__() 17 if executor_method not in ["map", "submit"]: 18 raise ValueError( 19 f'executor_method must be either "map" or "submit", got {executor_method}' 20 ) 21 self.executor_method = executor_method 22 23 @overrides 24 def _evaluate(self, population): 25 """ 26 Updates the fitness score of the given individuals, then returns the best individual 27 28 Parameters 29 ---------- 30 population: 31 the population of the evolutionary experiment 32 33 Returns 34 ------- 35 individual 36 the individual with the best fitness of the given individuals 37 """ 38 super()._evaluate(population) 39 40 if len(population.sub_populations) != 1: 41 raise ValueError( 42 f"SimpleBreeder can only handle one subpopulation. \ 43 Got: {len(population.sub_populations)}" 44 ) 45 sub_population = population.sub_populations[0] 46 individuals = sub_population.individuals 47 sp_eval: IndividualEvaluator = sub_population.evaluator 48 49 if self.executor_method == "submit": 50 eval_futures = [ 51 self.executor.submit( 52 sp_eval.evaluate, ind, sub_population.individuals 53 ) 54 for ind in sub_population.individuals 55 ] 56 eval_results = [future.result() for future in eval_futures] 57 elif self.executor_method == "map": 58 eval_results = self.executor.map( 59 sp_eval.evaluate_individual, sub_population.individuals 60 ) 61 for ind, fitness_score in zip( 62 sub_population.individuals, eval_results 63 ): 64 ind.fitness.set_fitness(fitness_score) 65 best_ind: Individual = individuals[0] 66 best_fitness: Fitness = best_ind.fitness 67 68 for ind in individuals[1:]: 69 if ind.fitness.better_than(ind, best_fitness, best_ind): 70 best_ind = ind 71 best_fitness = ind.fitness 72 73 return best_ind
10class SimplePopulationEvaluator(PopulationEvaluator): 11 """ 12 Computes fitness value for the whole population. 13 All simple classes assume only one sub-population. 14 """ 15 16 def __init__(self, executor_method="map"): 17 super().__init__() 18 if executor_method not in ["map", "submit"]: 19 raise ValueError( 20 f'executor_method must be either "map" or "submit", got {executor_method}' 21 ) 22 self.executor_method = executor_method 23 24 @overrides 25 def _evaluate(self, population): 26 """ 27 Updates the fitness score of the given individuals, then returns the best individual 28 29 Parameters 30 ---------- 31 population: 32 the population of the evolutionary experiment 33 34 Returns 35 ------- 36 individual 37 the individual with the best fitness of the given individuals 38 """ 39 super()._evaluate(population) 40 41 if len(population.sub_populations) != 1: 42 raise ValueError( 43 f"SimpleBreeder can only handle one subpopulation. \ 44 Got: {len(population.sub_populations)}" 45 ) 46 sub_population = population.sub_populations[0] 47 individuals = sub_population.individuals 48 sp_eval: IndividualEvaluator = sub_population.evaluator 49 50 if self.executor_method == "submit": 51 eval_futures = [ 52 self.executor.submit( 53 sp_eval.evaluate, ind, sub_population.individuals 54 ) 55 for ind in sub_population.individuals 56 ] 57 eval_results = [future.result() for future in eval_futures] 58 elif self.executor_method == "map": 59 eval_results = self.executor.map( 60 sp_eval.evaluate_individual, sub_population.individuals 61 ) 62 for ind, fitness_score in zip( 63 sub_population.individuals, eval_results 64 ): 65 ind.fitness.set_fitness(fitness_score) 66 best_ind: Individual = individuals[0] 67 best_fitness: Fitness = best_ind.fitness 68 69 for ind in individuals[1:]: 70 if ind.fitness.better_than(ind, best_fitness, best_ind): 71 best_ind = ind 72 best_fitness = ind.fitness 73 74 return best_ind
Computes fitness value for the whole population. All simple classes assume only one sub-population.