|
8 | 8 | import pygad |
9 | 9 |
|
10 | 10 | class Unique: |
11 | | - # DEEP-DUPLICATE-REMOVAL-NEEDED |
12 | | - NUM_DUPLICATE1 = 0 |
13 | | - NUM_DUPLICATE2 = 0 |
| 11 | + |
14 | 12 | def solve_duplicate_genes_randomly(self, |
15 | 13 | solution, |
16 | 14 | min_val, |
@@ -155,15 +153,13 @@ def solve_duplicate_genes_by_space(self, |
155 | 153 | build_initial_pop=build_initial_pop) |
156 | 154 | else: |
157 | 155 | # DEEP-DUPLICATE-REMOVAL-NEEDED |
| 156 | + # Search by this phrase to find where deep duplicates removal should be applied. |
| 157 | + |
158 | 158 | # If there exist duplicate genes, then changing either of the 2 duplicating genes (with indices 2 and 3) will not solve the problem. |
159 | 159 | # 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. |
160 | 160 | # 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. |
161 | 161 | # 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. |
162 | 162 | # 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 |
167 | 163 | return new_solution, not_unique_indices, len(not_unique_indices) |
168 | 164 |
|
169 | 165 | return new_solution, not_unique_indices, num_unsolved_duplicates |
@@ -288,7 +284,7 @@ def unique_genes_by_space(self, |
288 | 284 | _, unique_gene_indices = numpy.unique(new_solution, return_index=True) |
289 | 285 | not_unique_indices = set(range(len(new_solution))) - set(unique_gene_indices) |
290 | 286 | # self.logger.info("not_unique_indices INSIDE", not_unique_indices) |
291 | | - |
| 287 | + |
292 | 288 | return new_solution, not_unique_indices, num_unsolved_duplicates |
293 | 289 |
|
294 | 290 | def unique_gene_by_space(self, |
@@ -455,15 +451,18 @@ def unique_gene_by_space(self, |
455 | 451 |
|
456 | 452 | if len(values_to_select_from) == 0: |
457 | 453 | # DEEP-DUPLICATE-REMOVAL-NEEDED |
| 454 | + # Search by this phrase to find where deep duplicates removal should be applied. |
| 455 | + |
458 | 456 | # Reaching this block means there is no value in the gene space of this gene to solve the duplicates. |
459 | 457 | # 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. |
460 | 458 |
|
461 | 459 | 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 | + |
462 | 461 | solution2 = self.solve_duplicates_deeply(solution) |
463 | 462 | 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 |
467 | 466 | else: |
468 | 467 | solution = solution2 |
469 | 468 | value_from_space = solution[gene_idx] |
|
0 commit comments