Skip to content

Commit 5acccee

Browse files
author
JimChen
committed
remove a bunch of dup code
1 parent ed66c37 commit 5acccee

File tree

1 file changed

+56
-97
lines changed

1 file changed

+56
-97
lines changed

pygad.py

Lines changed: 56 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99

1010
class GA:
11-
1211
supported_int_types = [int, numpy.int, numpy.int8, numpy.int16, numpy.int32,
1312
numpy.int64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64]
1413
supported_float_types = [float, numpy.float,
@@ -2981,50 +2980,33 @@ def solve_duplicate_genes_randomly(self, solution, min_val, max_val, mutation_by
29812980
for duplicate_index in not_unique_indices:
29822981
for trial_index in range(num_trials):
29832982
if self.gene_type_single:
2984-
if gene_type[0] in GA.supported_int_types:
2985-
temp_val = self.unique_int_gene_from_range(solution=new_solution,
2986-
gene_index=duplicate_index,
2987-
min_val=min_val,
2988-
max_val=max_val,
2989-
mutation_by_replacement=mutation_by_replacement,
2990-
gene_type=gene_type)
2991-
else:
2992-
temp_val = numpy.random.uniform(low=min_val,
2993-
high=max_val,
2994-
size=1)
2995-
if mutation_by_replacement:
2996-
pass
2997-
else:
2998-
temp_val = new_solution[duplicate_index] + temp_val
2983+
gene_type_index = 0
2984+
target_gene_type = gene_type
29992985
else:
3000-
if gene_type[duplicate_index] in GA.supported_int_types:
3001-
temp_val = self.unique_int_gene_from_range(solution=new_solution,
3002-
gene_index=duplicate_index,
3003-
min_val=min_val,
3004-
max_val=max_val,
3005-
mutation_by_replacement=mutation_by_replacement,
3006-
gene_type=gene_type)
2986+
gene_type_index = duplicate_index
2987+
target_gene_type = gene_type[duplicate_index]
2988+
if gene_type[gene_type_index] in GA.supported_int_types:
2989+
temp_val = self.unique_int_gene_from_range(
2990+
solution=new_solution,
2991+
gene_index=duplicate_index,
2992+
min_val=min_val,
2993+
max_val=max_val,
2994+
mutation_by_replacement=mutation_by_replacement,
2995+
gene_type=gene_type
2996+
)
2997+
else:
2998+
temp_val = numpy.random.uniform(low=min_val,
2999+
high=max_val,
3000+
size=1)
3001+
if mutation_by_replacement:
3002+
pass
30073003
else:
3008-
temp_val = numpy.random.uniform(low=min_val,
3009-
high=max_val,
3010-
size=1)
3011-
if mutation_by_replacement:
3012-
pass
3013-
else:
3014-
temp_val = new_solution[duplicate_index] + temp_val
3004+
temp_val = new_solution[duplicate_index] + temp_val
30153005

3016-
if self.gene_type_single:
3017-
if gene_type[1] is not None:
3018-
temp_val = numpy.round(gene_type[0](temp_val),
3019-
gene_type[1])
3020-
else:
3021-
temp_val = gene_type[0](temp_val)
3006+
if target_gene_type[1] is not None:
3007+
temp_val = numpy.round(target_gene_type[0](temp_val), target_gene_type[1])
30223008
else:
3023-
if gene_type[duplicate_index][1] is not None:
3024-
temp_val = numpy.round(gene_type[duplicate_index][0](temp_val),
3025-
gene_type[duplicate_index][1])
3026-
else:
3027-
temp_val = gene_type[duplicate_index][0](temp_val)
3009+
temp_val = target_gene_type[0](temp_val)
30283010

30293011
if temp_val in new_solution and trial_index == (num_trials - 1):
30303012
num_unsolved_duplicates = num_unsolved_duplicates + 1
@@ -3067,33 +3049,29 @@ def solve_duplicate_genes_by_space(self, solution, gene_type, num_trials=10, bui
30673049
# First try to solve the duplicates.
30683050
# For a solution like [3 2 0 0], the indices of the 2 duplicating genes are 2 and 3.
30693051
# The next call to the find_unique_value() method tries to change the value of the gene with index 3 to solve the duplicate.
3070-
if len(not_unique_indices) > 0:
3071-
new_solution, not_unique_indices, num_unsolved_duplicates = self.unique_genes_by_space(new_solution=new_solution,
3072-
gene_type=gene_type,
3073-
not_unique_indices=not_unique_indices,
3074-
num_trials=10,
3075-
build_initial_pop=build_initial_pop)
3076-
else:
3077-
return new_solution, not_unique_indices, len(not_unique_indices)
30783052

3079-
# Do another try if there exist duplicate genes.
3080-
# If there are no possible values for the gene 3 with index 3 to solve the duplicate, try to change the value of the other gene with index 2.
3081-
if len(not_unique_indices) > 0:
3053+
# If there exist duplicate genes, then changing either of the 2 duplicating genes (with indices 2 and 3) will not solve the problem.
3054+
# 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.
3055+
# 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.
3056+
# 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.
3057+
# As a result, any of the last 2 genes can take the value 2 and solve the duplicates.
3058+
retry = 0
3059+
while len(not_unique_indices) > 0 and retry < 2:
3060+
new_solution, not_unique_indices, num_unsolved_duplicates = self.unique_genes_by_space(
3061+
new_solution=new_solution,
3062+
gene_type=gene_type,
3063+
not_unique_indices=not_unique_indices,
3064+
num_trials=10,
3065+
build_initial_pop=build_initial_pop
3066+
)
30823067
not_unique_indices = set(numpy.where(new_solution == new_solution[list(not_unique_indices)[0]])[0]) - set([list(not_unique_indices)[0]])
3083-
new_solution, not_unique_indices, num_unsolved_duplicates = self.unique_genes_by_space(new_solution=new_solution,
3084-
gene_type=gene_type,
3085-
not_unique_indices=not_unique_indices,
3086-
num_trials=10,
3087-
build_initial_pop=build_initial_pop)
3088-
else:
3089-
# If there exist duplicate genes, then changing either of the 2 duplicating genes (with indices 2 and 3) will not solve the problem.
3090-
# 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.
3091-
# 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.
3092-
# 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.
3093-
# As a result, any of the last 2 genes can take the value 2 and solve the duplicates.
3068+
# Do another try if there exist duplicate genes.
3069+
# If there are no possible values for the gene 3 with index 3 to solve the duplicate, try to change the value of the other gene with index 2.
3070+
retry += 1
3071+
if len(not_unique_indices) > 0:
30943072
return new_solution, not_unique_indices, len(not_unique_indices)
3095-
3096-
return new_solution, not_unique_indices, num_unsolved_duplicates
3073+
else:
3074+
return new_solution, not_unique_indices, num_unsolved_duplicates
30973075

30983076
def solve_duplicate_genes_by_space_OLD(self, solution, gene_type, num_trials=10):
30993077
# /////////////////////////
@@ -3115,7 +3093,6 @@ def solve_duplicate_genes_by_space_OLD(self, solution, gene_type, num_trials=10)
31153093
gene_idx=duplicate_index,
31163094
gene_type=gene_type
31173095
)
3118-
31193096
if temp_val in new_solution and trial_index == (num_trials - 1):
31203097
# print("temp_val, duplicate_index", temp_val, duplicate_index, new_solution)
31213098
num_unsolved_duplicates = num_unsolved_duplicates + 1
@@ -3127,7 +3104,6 @@ def solve_duplicate_genes_by_space_OLD(self, solution, gene_type, num_trials=10)
31273104
new_solution[duplicate_index] = temp_val
31283105
# print("SOLVED", duplicate_index)
31293106
break
3130-
31313107
# Update the list of duplicate indices after each iteration.
31323108
_, unique_gene_indices = numpy.unique(new_solution, return_index=True)
31333109
not_unique_indices = set(range(len(solution))) - set(unique_gene_indices)
@@ -3151,39 +3127,28 @@ def unique_int_gene_from_range(self, solution, gene_index, min_val, max_val, mut
31513127
"""
31523128

31533129
if self.gene_type_single:
3154-
if step is None:
3155-
all_gene_values = numpy.arange(min_val, max_val, dtype=gene_type[0])
3156-
else:
3157-
# 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])
3158-
# 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.
3159-
all_gene_values = numpy.asarray(numpy.arange(min_val, max_val, step), dtype=gene_type[0])
3130+
target_gene_type = gene_type
31603131
else:
3161-
if step is None:
3162-
all_gene_values = numpy.arange(min_val, max_val, dtype=gene_type[gene_index][0])
3163-
else:
3164-
all_gene_values = numpy.asarray(numpy.arange(min_val, max_val, step), dtype=gene_type[gene_index][0])
3132+
target_gene_type = gene_type[gene_index]
3133+
if step is None:
3134+
all_gene_values = numpy.arange(min_val, max_val, dtype=target_gene_type[0])
3135+
else:
3136+
# 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])
3137+
# 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.
3138+
all_gene_values = numpy.asarray(numpy.arange(min_val, max_val, step), dtype=target_gene_type[0])
31653139

31663140
if mutation_by_replacement:
31673141
pass
31683142
else:
31693143
all_gene_values = all_gene_values + solution[gene_index]
31703144

3171-
if self.gene_type_single:
3172-
if gene_type[1] is not None:
3173-
all_gene_values = numpy.round(gene_type[0](all_gene_values),
3174-
gene_type[1])
3175-
else:
3176-
if type(all_gene_values) is numpy.ndarray:
3177-
all_gene_values = numpy.asarray(all_gene_values, dtype=gene_type[0])
3178-
else:
3179-
all_gene_values = gene_type[0](all_gene_values)
3145+
if target_gene_type[1] is not None:
3146+
all_gene_values = numpy.round(target_gene_type[0](all_gene_values), target_gene_type[1])
31803147
else:
3181-
if gene_type[gene_index][1] is not None:
3182-
all_gene_values = numpy.round(gene_type[gene_index][0](all_gene_values),
3183-
gene_type[gene_index][1])
3148+
if type(all_gene_values) is numpy.ndarray:
3149+
all_gene_values = numpy.asarray(all_gene_values, dtype=target_gene_type[0])
31843150
else:
3185-
all_gene_values = gene_type[gene_index][0](all_gene_values)
3186-
3151+
all_gene_values = target_gene_type[0](all_gene_values)
31873152
values_to_select_from = list(set(all_gene_values) - set(solution))
31883153

31893154
if len(values_to_select_from) == 0:
@@ -3192,12 +3157,6 @@ def unique_int_gene_from_range(self, solution, gene_index, min_val, max_val, mut
31923157
selected_value = solution[gene_index]
31933158
else:
31943159
selected_value = random.choice(values_to_select_from)
3195-
3196-
# if self.gene_type_single:
3197-
# selected_value = gene_type[0](selected_value)
3198-
# else:
3199-
# selected_value = gene_type[gene_index][0](selected_value)
3200-
32013160
return selected_value
32023161

32033162
def unique_genes_by_space(self, new_solution, gene_type, not_unique_indices, num_trials=10, build_initial_pop=False):

0 commit comments

Comments
 (0)