Skip to content

Commit 19be8b0

Browse files
committed
Refactoring the code
1 parent 160a836 commit 19be8b0

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

pygad/helper/unique.py

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def solve_duplicate_genes_randomly(self,
5959
else:
6060
temp_val = new_solution[duplicate_index] + temp_val
6161
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:
6363
temp_val = self.unique_int_gene_from_range(solution=new_solution,
6464
gene_index=duplicate_index,
6565
min_val=min_val,
@@ -186,43 +186,43 @@ def unique_int_gene_from_range(self,
186186
Returns:
187187
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.
188188
"""
189-
189+
190190
if self.gene_type_single == True:
191191
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])
193197
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])
195199
# 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.
196200
all_gene_values = numpy.asarray(numpy.arange(min_val, max_val, step),
197201
dtype=gene_type[0])
198202
else:
199203
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])
201209
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])
203212

204213
if mutation_by_replacement:
205214
pass
206215
else:
207216
all_gene_values = all_gene_values + solution[gene_index]
208217

209-
# Similar to the round_genes() method in the pygad module,
210-
# Create a round_gene() method to round a single gene.
211218
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])
220222
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])
226226

227227
values_to_select_from = list(set(all_gene_values) - set(solution))
228228

@@ -232,11 +232,6 @@ def unique_int_gene_from_range(self,
232232
else:
233233
selected_value = random.choice(values_to_select_from)
234234

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-
240235
return selected_value
241236

242237
def unique_genes_by_space(self,
@@ -310,7 +305,8 @@ def unique_gene_by_space(self,
310305
curr_gene_space = self.gene_space[gene_idx].copy()
311306
else:
312307
# 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]
314310

315311
# If the gene space has only a single value, use it as the new gene value.
316312
if type(curr_gene_space) in pygad.GA.supported_int_float_types:
@@ -585,15 +581,12 @@ def find_two_duplicates(self,
585581
gene_indices = numpy.where(numpy.array(solution) == gene)[0]
586582
if len(gene_indices) == 1:
587583
continue
588-
# print("Gene value", gene, "Gene indices", gene_indices)
589584
for gene_idx in gene_indices:
590-
# print(" Current Gene Index", gene_idx)
591585
number_alternate_values = len(set(gene_space_unpacked[gene_idx]))
592586
if number_alternate_values > 1:
593587
return gene_idx, gene
594588
# This means there is no way to solve the duplicates between the genes.
595589
# 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))
597590
return None, gene
598591

599592
def unpack_gene_space(self, num_values_from_range=100):
@@ -609,13 +602,13 @@ def unpack_gene_space(self, num_values_from_range=100):
609602
for space_idx, space in enumerate(gene_space_unpacked):
610603
if type(space) in pygad.GA.supported_int_float_types:
611604
gene_space_unpacked[space_idx] = [space]
612-
elif type(space) is None:
605+
elif space is None:
613606
# Randomly generate the value using the mutation range.
614607
gene_space_unpacked[space_idx] = numpy.arange(start=self.random_mutation_min_val,
615608
stop=self.random_mutation_max_val)
616609
elif type(space) is range:
617610
# Convert the range to a list.
618-
gene_space_unpacked[space_idx] = list(range)
611+
gene_space_unpacked[space_idx] = list(space)
619612
elif type(space) is dict:
620613
# Create a list of values using the dict range.
621614
# Use numpy.linspace()
@@ -703,68 +696,58 @@ def solve_duplicates_deeply(self,
703696
"""
704697

705698
gene_space_unpacked = self.unpack_gene_space()
699+
# Create a copy into the attribute because it will be changed later.
706700
self.gene_space_unpacked = gene_space_unpacked.copy()
707701

708702
duplicate_index, duplicate_value = self.find_two_duplicates(solution,
709703
gene_space_unpacked)
710-
# print()
711-
# print("Duplicate_index, Duplicate_value", duplicate_index, duplicate_value)
712704

713705
if duplicate_index is None:
714706
# Impossible to solve the duplicates for the genes with value duplicate_value.
715707
return None
716708

717-
# gene_duplicate_value = solution[duplicate_index]
718-
719709

720710
# Without copy(), the gene will be removed from the gene_space.
721711
# Convert the space to list because tuples do not have copy()
722712
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.
725715
gene_other_values = [v for v in gene_other_values if v != duplicate_value]
716+
726717
# The remove() function only removes the first occurrence of the value.
718+
# Do not use it.
727719
# gene_other_values.remove(duplicate_value)
728-
# if len(gene_other_values) == 0: return None
729720

730-
# print("Gene_other_values", gene_other_values)
731721
# Two conditions to solve the duplicates of the value D:
732722
# 1. From gene_other_values, select a value V such that it is available in the gene space of another gene X.
733723
# 2. Find an alternate value for the gene X that will not cause any duplicates.
734724
# 2.1 If the gene X does not have alternatives, then go back to step 1 to find another gene.
735725
# 2.2 Set the gene X to the value D.
736726
# 2.3 Set the target gene to the value V.
737-
# search_gene_space = gene_space_unpacked.copy()
738727
# 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] = []
740728
gene_space_unpacked[duplicate_index] = []
741-
# print("search_gene_space", search_gene_space)
742729

743730
for other_value in gene_other_values:
744731
for space_idx, space in enumerate(gene_space_unpacked):
745-
# print("other_value in space", other_value, space)
746732
if other_value in space:
747733
if other_value in solution and list(solution).index(other_value) != space_idx:
748734
continue
749735
else:
750-
# print(" Current Space", space, space_idx)
751736
# Find an alternate value for the third gene.
752737
# Copy the space so that the original space is not changed after removing the value.
753738
space_other_values = space.copy()
754739
# This removes all the occurrences of this value. It is not enough to use the remove() function because it only removes the first occurrence.
755740
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+
757742
for val in space_other_values:
758-
# print("val", val)
759743
if val in solution:
760744
# If the value exists in another gene of the solution, then we cannot use this value as it will cause another duplicate.
761745
# End the current iteration and go check another value.
762746
continue
763747
else:
764748
solution[space_idx] = val
765749
solution[duplicate_index] = other_value
766-
# print("solution", solution)
767750
return solution
768-
# print("Cannot solve the duplicate genes with value {duplicate_value}.".format(duplicate_value=duplicate_value))
769-
return None
770751

752+
# Reaching here means we cannot solve the duplicate genes.
753+
return None

0 commit comments

Comments
 (0)