@@ -59,7 +59,7 @@ def solve_duplicate_genes_randomly(self,
59
59
else :
60
60
temp_val = new_solution [duplicate_index ] + temp_val
61
61
else :
62
- if gene_type [duplicate_index ] in pygad .GA .supported_int_types :
62
+ if gene_type [duplicate_index ][ 0 ] in pygad .GA .supported_int_types :
63
63
temp_val = self .unique_int_gene_from_range (solution = new_solution ,
64
64
gene_index = duplicate_index ,
65
65
min_val = min_val ,
@@ -186,43 +186,43 @@ def unique_int_gene_from_range(self,
186
186
Returns:
187
187
selected_value: The new value of the gene. It may be identical to the original gene value in case there are no possible unique values for the gene.
188
188
"""
189
-
189
+
190
190
if self .gene_type_single == True :
191
191
if step is None :
192
- all_gene_values = numpy .arange (min_val , max_val , dtype = gene_type [0 ])
192
+ # all_gene_values = numpy.arange(min_val,
193
+ # max_val,
194
+ # dtype=gene_type[0])
195
+ all_gene_values = numpy .asarray (numpy .arange (min_val , max_val ),
196
+ dtype = gene_type [0 ])
193
197
else :
194
- # For non-integer steps, the numpy.arange() function returns zeros id the dtype parameter is set to an integer data type. So, this returns zeros if step is non-integer and dtype is set to an int data type: numpy.arange(min_val, max_val, step, dtype=gene_type[0])
198
+ # For non-integer steps, the numpy.arange() function returns zeros if the dtype parameter is set to an integer data type. So, this returns zeros if step is non-integer and dtype is set to an int data type: numpy.arange(min_val, max_val, step, dtype=gene_type[0])
195
199
# To solve this issue, the data type casting will not be handled inside numpy.arange(). The range is generated by numpy.arange() and then the data type is converted using the numpy.asarray() function.
196
200
all_gene_values = numpy .asarray (numpy .arange (min_val , max_val , step ),
197
201
dtype = gene_type [0 ])
198
202
else :
199
203
if step is None :
200
- all_gene_values = numpy .arange (min_val , max_val , dtype = gene_type [gene_index ][0 ])
204
+ # all_gene_values = numpy.arange(min_val,
205
+ # max_val,
206
+ # dtype=gene_type[gene_index][0])
207
+ all_gene_values = numpy .asarray (numpy .arange (min_val , max_val ),
208
+ dtype = gene_type [gene_index ][0 ])
201
209
else :
202
- all_gene_values = numpy .asarray (numpy .arange (min_val , max_val , step ), dtype = gene_type [gene_index ][0 ])
210
+ all_gene_values = numpy .asarray (numpy .arange (min_val , max_val , step ),
211
+ dtype = gene_type [gene_index ][0 ])
203
212
204
213
if mutation_by_replacement :
205
214
pass
206
215
else :
207
216
all_gene_values = all_gene_values + solution [gene_index ]
208
217
209
- # Similar to the round_genes() method in the pygad module,
210
- # Create a round_gene() method to round a single gene.
211
218
if self .gene_type_single == True :
212
- if not gene_type [1 ] is None :
213
- all_gene_values = numpy .round (gene_type [0 ](all_gene_values ),
214
- gene_type [1 ])
215
- else :
216
- if type (all_gene_values ) is numpy .ndarray :
217
- all_gene_values = numpy .asarray (all_gene_values , dtype = gene_type [0 ])
218
- else :
219
- all_gene_values = gene_type [0 ](all_gene_values )
219
+ # Note that we already know that the data type is integer.
220
+ all_gene_values = numpy .asarray (all_gene_values ,
221
+ dtype = gene_type [0 ])
220
222
else :
221
- if not gene_type [gene_index ][1 ] is None :
222
- all_gene_values = numpy .round (gene_type [gene_index ][0 ](all_gene_values ),
223
- gene_type [gene_index ][1 ])
224
- else :
225
- all_gene_values = gene_type [gene_index ][0 ](all_gene_values )
223
+ # Note that we already know that the data type is integer.
224
+ all_gene_values = numpy .asarray (all_gene_values ,
225
+ gene_type [gene_index ][0 ])
226
226
227
227
values_to_select_from = list (set (all_gene_values ) - set (solution ))
228
228
@@ -232,11 +232,6 @@ def unique_int_gene_from_range(self,
232
232
else :
233
233
selected_value = random .choice (values_to_select_from )
234
234
235
- #if self.gene_type_single == True:
236
- # selected_value = gene_type[0](selected_value)
237
- #else:
238
- # selected_value = gene_type[gene_index][0](selected_value)
239
-
240
235
return selected_value
241
236
242
237
def unique_genes_by_space (self ,
@@ -310,7 +305,8 @@ def unique_gene_by_space(self,
310
305
curr_gene_space = self .gene_space [gene_idx ].copy ()
311
306
else :
312
307
# Return the entire gene space from the 'gene_space' attribute.
313
- curr_gene_space = list (self .gene_space [gene_idx ]).copy ()
308
+ # curr_gene_space = list(self.gene_space[gene_idx]).copy()
309
+ curr_gene_space = self .gene_space [gene_idx ]
314
310
315
311
# If the gene space has only a single value, use it as the new gene value.
316
312
if type (curr_gene_space ) in pygad .GA .supported_int_float_types :
@@ -585,15 +581,12 @@ def find_two_duplicates(self,
585
581
gene_indices = numpy .where (numpy .array (solution ) == gene )[0 ]
586
582
if len (gene_indices ) == 1 :
587
583
continue
588
- # print("Gene value", gene, "Gene indices", gene_indices)
589
584
for gene_idx in gene_indices :
590
- # print(" Current Gene Index", gene_idx)
591
585
number_alternate_values = len (set (gene_space_unpacked [gene_idx ]))
592
586
if number_alternate_values > 1 :
593
587
return gene_idx , gene
594
588
# This means there is no way to solve the duplicates between the genes.
595
589
# Because the space of the duplicates genes only has a single value and there is no alternatives.
596
- # print("Cannot solve duplicates between the genes with value {gene} at indices {gene_indices}.".format(gene_indices=gene_indices, gene=gene))
597
590
return None , gene
598
591
599
592
def unpack_gene_space (self , num_values_from_range = 100 ):
@@ -609,13 +602,13 @@ def unpack_gene_space(self, num_values_from_range=100):
609
602
for space_idx , space in enumerate (gene_space_unpacked ):
610
603
if type (space ) in pygad .GA .supported_int_float_types :
611
604
gene_space_unpacked [space_idx ] = [space ]
612
- elif type ( space ) is None :
605
+ elif space is None :
613
606
# Randomly generate the value using the mutation range.
614
607
gene_space_unpacked [space_idx ] = numpy .arange (start = self .random_mutation_min_val ,
615
608
stop = self .random_mutation_max_val )
616
609
elif type (space ) is range :
617
610
# Convert the range to a list.
618
- gene_space_unpacked [space_idx ] = list (range )
611
+ gene_space_unpacked [space_idx ] = list (space )
619
612
elif type (space ) is dict :
620
613
# Create a list of values using the dict range.
621
614
# Use numpy.linspace()
@@ -703,68 +696,58 @@ def solve_duplicates_deeply(self,
703
696
"""
704
697
705
698
gene_space_unpacked = self .unpack_gene_space ()
699
+ # Create a copy into the attribute because it will be changed later.
706
700
self .gene_space_unpacked = gene_space_unpacked .copy ()
707
701
708
702
duplicate_index , duplicate_value = self .find_two_duplicates (solution ,
709
703
gene_space_unpacked )
710
- # print()
711
- # print("Duplicate_index, Duplicate_value", duplicate_index, duplicate_value)
712
704
713
705
if duplicate_index is None :
714
706
# Impossible to solve the duplicates for the genes with value duplicate_value.
715
707
return None
716
708
717
- # gene_duplicate_value = solution[duplicate_index]
718
-
719
709
720
710
# Without copy(), the gene will be removed from the gene_space.
721
711
# Convert the space to list because tuples do not have copy()
722
712
gene_other_values = list (gene_space_unpacked [duplicate_index ]).copy ()
723
- # This removes all the occurrences of this value using the __ne__ magic function.
724
- # gene_other_values = list(filter((duplicate_value).__ne__, gene_other_values))
713
+
714
+ # This removes all the occurrences of this value.
725
715
gene_other_values = [v for v in gene_other_values if v != duplicate_value ]
716
+
726
717
# The remove() function only removes the first occurrence of the value.
718
+ # Do not use it.
727
719
# gene_other_values.remove(duplicate_value)
728
- # if len(gene_other_values) == 0: return None
729
720
730
- # print("Gene_other_values", gene_other_values)
731
721
# Two conditions to solve the duplicates of the value D:
732
722
# 1. From gene_other_values, select a value V such that it is available in the gene space of another gene X.
733
723
# 2. Find an alternate value for the gene X that will not cause any duplicates.
734
724
# 2.1 If the gene X does not have alternatives, then go back to step 1 to find another gene.
735
725
# 2.2 Set the gene X to the value D.
736
726
# 2.3 Set the target gene to the value V.
737
- # search_gene_space = gene_space_unpacked.copy()
738
727
# Set the space of the duplicate gene to empty list []. Do not remove it to not alter the indices of the gene spaces.
739
- # search_gene_space[duplicate_index] = []
740
728
gene_space_unpacked [duplicate_index ] = []
741
- # print("search_gene_space", search_gene_space)
742
729
743
730
for other_value in gene_other_values :
744
731
for space_idx , space in enumerate (gene_space_unpacked ):
745
- # print("other_value in space", other_value, space)
746
732
if other_value in space :
747
733
if other_value in solution and list (solution ).index (other_value ) != space_idx :
748
734
continue
749
735
else :
750
- # print(" Current Space", space, space_idx)
751
736
# Find an alternate value for the third gene.
752
737
# Copy the space so that the original space is not changed after removing the value.
753
738
space_other_values = space .copy ()
754
739
# This removes all the occurrences of this value. It is not enough to use the remove() function because it only removes the first occurrence.
755
740
space_other_values = [v for v in space_other_values if v != other_value ]
756
- # print("Space_other_values", space_other_values, other_value)
741
+
757
742
for val in space_other_values :
758
- # print("val", val)
759
743
if val in solution :
760
744
# If the value exists in another gene of the solution, then we cannot use this value as it will cause another duplicate.
761
745
# End the current iteration and go check another value.
762
746
continue
763
747
else :
764
748
solution [space_idx ] = val
765
749
solution [duplicate_index ] = other_value
766
- # print("solution", solution)
767
750
return solution
768
- # print("Cannot solve the duplicate genes with value {duplicate_value}.".format(duplicate_value=duplicate_value))
769
- return None
770
751
752
+ # Reaching here means we cannot solve the duplicate genes.
753
+ return None
0 commit comments