Skip to content

Commit 288b5e8

Browse files
committed
Fix check for undefined pop fitness when using parallel processing
1 parent eca2f56 commit 288b5e8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pygad/pygad.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,9 @@ def cal_pop_fitness(self):
16461646
last_generation_elitism_as_list = [
16471647
list(gen_elitism) for gen_elitism in self.last_generation_elitism]
16481648

1649-
pop_fitness = ["undefined"] * len(self.population)
1649+
undefined_pop_fitness = object()
1650+
1651+
pop_fitness = [undefined_pop_fitness] * len(self.population)
16501652
if self.parallel_processing is None:
16511653
# Calculating the fitness value of each solution in the current population.
16521654
for sol_idx, sol in enumerate(self.population):
@@ -1708,8 +1710,12 @@ def cal_pop_fitness(self):
17081710
# Reaching this block means that batch fitness calculation is used.
17091711

17101712
# Indices of the solutions to calculate their fitness.
1711-
solutions_indices = numpy.where(
1712-
numpy.array(pop_fitness) == "undefined")[0]
1713+
solutions_indices = numpy.array([
1714+
sol_idx
1715+
for (sol_idx, fitness)
1716+
in enumerate(pop_fitness)
1717+
if fitness is undefined_pop_fitness
1718+
])
17131719
# Number of batches.
17141720
num_batches = int(numpy.ceil(len(solutions_indices) / self.fitness_batch_size))
17151721
# For each batch, get its indices and call the fitness function.
@@ -1787,7 +1793,7 @@ def cal_pop_fitness(self):
17871793
solutions_to_submit = []
17881794
for sol_idx, sol in enumerate(self.population):
17891795
# The "undefined" value means that the fitness of this solution must be calculated.
1790-
if pop_fitness[sol_idx] == "undefined":
1796+
if pop_fitness[sol_idx] is undefined_pop_fitness:
17911797
solutions_to_submit.append(sol.copy())
17921798
solutions_to_submit_indices.append(sol_idx)
17931799

0 commit comments

Comments
 (0)