Skip to content

Commit 102e1fd

Browse files
author
Ahmed Gad
committed
Fix selecting a unique value from gene space
1 parent ce702a9 commit 102e1fd

File tree

7 files changed

+7
-3
lines changed

7 files changed

+7
-3
lines changed
40 Bytes
Binary file not shown.
53 Bytes
Binary file not shown.
30 Bytes
Binary file not shown.

pygad/helper/misc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,12 @@ def generate_gene_value_from_space(self,
349349
# We must check if the selected value will respect the allow_duplicate_genes parameter.
350350
# Instead of selecting a value randomly, we have to select a value that will be unique if allow_duplicate_genes=False.
351351
# Only select a value from the current gene space that is, hopefully, unique.
352+
print("KKKKKKKK", value_from_space)
352353
value_from_space = self.select_unique_value(gene_values=value_from_space,
353354
solution=solution,
354355
gene_index=gene_idx)
356+
print("<<<<<<<<<<", value_from_space)
357+
# TODO WHY THIS VALUE IS NONE
355358

356359
# The gene space might be [None, 1, 7].
357360
# It might happen that the value None is selected.

pygad/helper/unique.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ def select_unique_value(self, gene_values, solution, gene_index):
262262
"""
263263

264264
values_to_select_from = list(set(list(gene_values)) - set(solution))
265+
print("PPPPPPPPP", values_to_select_from)
265266

266267
if len(values_to_select_from) == 0:
267268
# If there are no values, then keep the current gene value.
268269
if not self.suppress_warnings: warnings.warn(f"'allow_duplicate_genes=False' but cannot find a unique value for the gene at index {gene_index} with value {solution[gene_index]}.")
269270
selected_value = solution[gene_index]
270271
else:
271272
selected_value = random.choice(values_to_select_from)
272-
273273
return selected_value
274274

275275
def unique_genes_by_space(self,

pygad/pygad.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ def initialize_population(self,
13761376
# 4) Solve duplicates if not allowed.
13771377

13781378
# Create an empty population.
1379-
self.population = numpy.zeros(shape=self.pop_size, dtype=object)
1379+
self.population = numpy.empty(shape=self.pop_size, dtype=object)
13801380

13811381
# 1) Create the initial population either randomly or using the gene space.
13821382
if self.gene_space is None:
@@ -1404,6 +1404,7 @@ def initialize_population(self,
14041404
gene_value=None,
14051405
solution=self.population[sol_idx],
14061406
sample_size=1)
1407+
print('AAAAAAAAA', self.population[sol_idx, gene_idx])
14071408

14081409
# 2) Change the data type and round all genes within the initial population.
14091410
self.population = self.change_population_dtype_and_round(self.population)

test_gene_space_allow_duplicate_genes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_gene_space_dict_with_step():
168168
assert num_outside == 0
169169

170170
def test_gene_space_list_single_value():
171-
num_outside, ga_instance = number_respect_gene_space(gene_space=[5], allow_duplicate_genes=True)
171+
num_outside, ga_instance = number_respect_gene_space(gene_space=[5])
172172

173173
assert num_outside == 0
174174

0 commit comments

Comments
 (0)