Skip to content

Commit d44c9bd

Browse files
authored
Update pygad.py
1 parent eca2f56 commit d44c9bd

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

pygad/pygad.py

Lines changed: 11 additions & 5 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

@@ -2479,4 +2485,4 @@ def load(filename):
24792485
except:
24802486
# raise BaseException("Error loading the file. If the file already exists, please reload all the functions previously used (e.g. fitness function).")
24812487
raise BaseException("Error loading the file.")
2482-
return ga_in
2488+
return ga_in

0 commit comments

Comments
 (0)