Skip to content

Commit 160a836

Browse files
committed
Refactor unique.py
1 parent 66ec895 commit 160a836

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

pygad/helper/unique.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import pygad
99

1010
class Unique:
11-
# DEEP-DUPLICATE-REMOVAL-NEEDED
12-
NUM_DUPLICATE1 = 0
13-
NUM_DUPLICATE2 = 0
11+
1412
def solve_duplicate_genes_randomly(self,
1513
solution,
1614
min_val,
@@ -155,15 +153,13 @@ def solve_duplicate_genes_by_space(self,
155153
build_initial_pop=build_initial_pop)
156154
else:
157155
# DEEP-DUPLICATE-REMOVAL-NEEDED
156+
# Search by this phrase to find where deep duplicates removal should be applied.
157+
158158
# If there exist duplicate genes, then changing either of the 2 duplicating genes (with indices 2 and 3) will not solve the problem.
159159
# This problem can be solved by randomly changing one of the non-duplicating genes that may make a room for a unique value in one the 2 duplicating genes.
160160
# For example, if gene_space=[[3, 0, 1], [4, 1, 2], [0, 2], [3, 2, 0]] and the solution is [3 2 0 0], then the values of the last 2 genes duplicate.
161161
# There are no possible changes in the last 2 genes to solve the problem. But it could be solved by changing the second gene from 2 to 4.
162162
# As a result, any of the last 2 genes can take the value 2 and solve the duplicates.
163-
# print("DEEP-DUPLICATE-REMOVAL-NEEDED1")
164-
# print("DEEP-DUPLICATE-REMOVAL-NEEDED1\n", new_solution, not_unique_indices, len(not_unique_indices))
165-
# DEEP-DUPLICATE-REMOVAL-NEEDED
166-
Unique.NUM_DUPLICATE1 += 1
167163
return new_solution, not_unique_indices, len(not_unique_indices)
168164

169165
return new_solution, not_unique_indices, num_unsolved_duplicates
@@ -288,7 +284,7 @@ def unique_genes_by_space(self,
288284
_, unique_gene_indices = numpy.unique(new_solution, return_index=True)
289285
not_unique_indices = set(range(len(new_solution))) - set(unique_gene_indices)
290286
# self.logger.info("not_unique_indices INSIDE", not_unique_indices)
291-
287+
292288
return new_solution, not_unique_indices, num_unsolved_duplicates
293289

294290
def unique_gene_by_space(self,
@@ -455,15 +451,18 @@ def unique_gene_by_space(self,
455451

456452
if len(values_to_select_from) == 0:
457453
# DEEP-DUPLICATE-REMOVAL-NEEDED
454+
# Search by this phrase to find where deep duplicates removal should be applied.
455+
458456
# Reaching this block means there is no value in the gene space of this gene to solve the duplicates.
459457
# To solve the duplicate between the 2 genes, the solution is to change the value of a third gene that makes a room to solve the duplicate.
460458

461459
if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but the gene space does not have enough values to prevent duplicates.")
460+
462461
solution2 = self.solve_duplicates_deeply(solution)
463462
if solution2 is None:
464-
# print("DEEP-DUPLICATE-REMOVAL-NEEDED2")
465-
# print("DEEP-DUPLICATE-REMOVAL-NEEDED2", solution, gene_idx, solution[gene_idx])
466-
Unique.NUM_DUPLICATE2 += 1
463+
# Cannot solve duplicates. At the moment, we are changing the value of a third gene to solve the duplicates between 2 genes.
464+
# Maybe a 4th, 5th, 6th, or even more genes need to be changed to solve the duplicates.
465+
pass
467466
else:
468467
solution = solution2
469468
value_from_space = solution[gene_idx]

0 commit comments

Comments
 (0)