Skip to content

Commit dede266

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

31 files changed

+13
-8965
lines changed

.DS_Store

-2 KB
Binary file not shown.

example.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

example2.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

pygad/.DS_Store

2 KB
Binary file not shown.
-205 Bytes
Binary file not shown.
-78 KB
Binary file not shown.
-260 Bytes
Binary file not shown.
-15.7 KB
Binary file not shown.
-14.4 KB
Binary file not shown.

pygad/helper/misc.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,9 @@ 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)
353352
value_from_space = self.select_unique_value(gene_values=value_from_space,
354353
solution=solution,
355354
gene_index=gene_idx)
356-
print("<<<<<<<<<<", value_from_space)
357-
# TODO WHY THIS VALUE IS NONE
358355

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

pygad/helper/unique.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,17 @@ 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)
266265

267266
if len(values_to_select_from) == 0:
268-
# If there are no values, then keep the current gene value.
269-
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]}.")
270-
selected_value = solution[gene_index]
267+
if solution[gene_index] is None:
268+
# The initial population is created as an empty array (numpy.empty()).
269+
# If we are assigning values to the initial population, then the gene value is already None.
270+
# If the gene value is None, then we do not have an option other than selecting a value even if it causes duplicates.
271+
# If there is no value that is unique to the solution, then select any of the current values randomly from the current set of gene values.
272+
selected_value = random.choice(gene_values)
273+
else:
274+
# If the gene is not None, then just keep its current value as long as there are no values that make it unique.
275+
selected_value = solution[gene_index]
271276
else:
272277
selected_value = random.choice(values_to_select_from)
273278
return selected_value
@@ -307,7 +312,6 @@ def unique_genes_by_space(self,
307312
build_initial_pop=build_initial_pop)
308313

309314
if temp_val in solution:
310-
# self.logger.info("temp_val, duplicate_index", temp_val, duplicate_index, solution)
311315
num_unsolved_duplicates = num_unsolved_duplicates + 1
312316
if not self.suppress_warnings: warnings.warn(f"Failed to find a unique value for gene with index {duplicate_index} whose value is {solution[duplicate_index]}. Consider adding more values in the gene space or use a wider range for initial population or random mutation.")
313317
else:

pygad/pygad.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,6 @@ 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])
14081407

14091408
# 2) Change the data type and round all genes within the initial population.
14101409
self.population = self.change_population_dtype_and_round(self.population)
-332 Bytes
Binary file not shown.
-5.97 KB
Binary file not shown.
-17.4 KB
Binary file not shown.
-7.11 KB
Binary file not shown.
Binary file not shown.
-237 Bytes
Binary file not shown.
-13.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)