From 288b5e8cdd7ba9fdf0f5a7076253bc09c9fef82d Mon Sep 17 00:00:00 2001 From: Matthew Bradbury Date: Wed, 18 Oct 2023 12:04:53 +0000 Subject: [PATCH] Fix check for undefined pop fitness when using parallel processing --- pygad/pygad.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pygad/pygad.py b/pygad/pygad.py index db17751..2ad3c2a 100644 --- a/pygad/pygad.py +++ b/pygad/pygad.py @@ -1646,7 +1646,9 @@ def cal_pop_fitness(self): last_generation_elitism_as_list = [ list(gen_elitism) for gen_elitism in self.last_generation_elitism] - pop_fitness = ["undefined"] * len(self.population) + undefined_pop_fitness = object() + + pop_fitness = [undefined_pop_fitness] * len(self.population) if self.parallel_processing is None: # Calculating the fitness value of each solution in the current population. for sol_idx, sol in enumerate(self.population): @@ -1708,8 +1710,12 @@ def cal_pop_fitness(self): # Reaching this block means that batch fitness calculation is used. # Indices of the solutions to calculate their fitness. - solutions_indices = numpy.where( - numpy.array(pop_fitness) == "undefined")[0] + solutions_indices = numpy.array([ + sol_idx + for (sol_idx, fitness) + in enumerate(pop_fitness) + if fitness is undefined_pop_fitness + ]) # Number of batches. num_batches = int(numpy.ceil(len(solutions_indices) / self.fitness_batch_size)) # For each batch, get its indices and call the fitness function. @@ -1787,7 +1793,7 @@ def cal_pop_fitness(self): solutions_to_submit = [] for sol_idx, sol in enumerate(self.population): # The "undefined" value means that the fitness of this solution must be calculated. - if pop_fitness[sol_idx] == "undefined": + if pop_fitness[sol_idx] is undefined_pop_fitness: solutions_to_submit.append(sol.copy()) solutions_to_submit_indices.append(sol_idx)