8000 None values for crossover_type and mutation_type · lkampoli/GeneticAlgorithmPython@bfc2c4b · GitHub
[go: up one dir, main page]

Skip to content

Commit bfc2c4b

Browse files
authored
None values for crossover_type and mutation_type
Solves issue ahmedfgad#40 when either crossover_type and mutation_type is set to None: ahmedfgad#40
1 parent e54bf91 commit bfc2c4b

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

pygad.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,24 @@ def __init__(self,
240240

241241
# crossover: Refers to the method that applies the crossover operator based on the selected type of crossover in the crossover_type property.
242242
# Validating the crossover type: crossover_type
243-
if not (type(crossover_type) is str):
244-
self.valid_parameters = False
245-
raise TypeError("The expected type of the 'crossover_type' parameter is str but ({crossover_type}) found.".format(crossover_type=type(crossover_type)))
246-
247-
crossover_type = crossover_type.lower()
248-
if (crossover_type == "single_point"):
249-
self.crossover = self.single_point_crossover
250-
elif (crossover_type == "two_points"):
251-
self.crossover = self.two_points_crossover
252-
elif (crossover_type == "uniform"):
253-
self.crossover = self.uniform_crossover
254-
elif (crossover_type == "scattered"):
255-
self.crossover = self.scattered_crossover
256-
elif (crossover_type is None):
243+
if (crossover_type is None):
257244
self.crossover = None
258-
else:
245+
elif not (type(crossover_type) is str):
259246
self.valid_parameters = False
260-
raise ValueError("Undefined crossover type. \nThe assigned value to the crossover_type ({crossover_type}) argument does not refer to one of the supported crossover types which are: \n-single_point (for single point crossover)\n-two_points (for two points crossover)\n-uniform (for uniform crossover)\n-scattered (for scattered crossover).\n".format(crossover_type=crossover_type))
247+
raise TypeError("The expected type of the 'crossover_type' parameter is str but ({crossover_type}) found.".format(crossover_type=type(crossover_type)))
248+
else: # type crossover_type is str
249+
crossover_type = crossover_type.lower()
250+
if (crossover_type == "single_point"):
251+
self.crossover = self.single_point_crossover
252+
elif (crossover_type == "two_points"):
253+
self.crossover = self.two_points_crossover
254+
elif (crossover_type == "uniform"):
255+
self.crossover = self.uniform_crossover
256+
elif (crossover_type == "scattered"):
257+
self.crossover = self.scattered_crossover
258+
else:
259+
self.valid_parameters = False
260+
raise ValueError("Undefined crossover type. \nThe assigned value to the crossover_type ({crossover_type}) argument does not refer to one of the supported crossover types which are: \n-single_point (for single point crossover)\n-two_points (for two points crossover)\n-uniform (for uniform crossover)\n-scattered (for scattered crossover).\n".format(crossover_type=crossover_type))
261261

262262
self.crossover_type = crossover_type
263263

@@ -277,26 +277,26 @@ def __init__(self,
277277
# mutation: Refers to the method that applies the mutation operator based on the selected type of mutation in the mutation_type property.
278278
# Validating the mutation type: mutation_type
279279
# "adaptive" mutation is supported starting from PyGAD 2.10.0
280-
if not (type(mutation_type) is str):
281-
self.valid_parameters = False
282-
raise TypeError("The expected type of the 'mutation_type' parameter is str but ({mutation_type}) found.".format(mutation_type=type(mutation_type)))
283-
284-
mutation_type = mutation_type.lower()
285-
if (mutation_type == "random"):
286-
self.mutation = self.random_mutation
287-
elif (mutation_type == "swap"):
288-
self.mutation = self.swap_mutation
289-
elif (mutation_type == "scramble"):
290-
self.mutation = self.scramble_mutation
291-
elif (mutation_type == "inversion"):
292-
self.mutation = self.inversion_mutation
293-
elif (mutation_type == "adaptive"):
294-
self.mutation = self.adaptive_mutation
295-
elif (mutation_type is None):
280+
if mutation_type is None:
296281
self.mutation = None
297-
else:
282+
elif not (type(mutation_type) is str):
298283
self.valid_parameters = False
299-
raise ValueError("Undefined mutation type. \nThe assigned value to the mutation_type argument ({mutation_type}) does not refer to one of the supported mutation types which are: \n-random (for random mutation)\n-swap (for swap mutation)\n-inversion (for inversion mutation)\n-scramble (for scramble mutation)\n-adaptive (for adaptive mutation).\n".format(mutation_type=mutation_type))
284+
raise TypeError("The expected type of the 'mutation_type' parameter is str but ({mutation_type}) found.".format(mutation_type=type(mutation_type)))
285+
else: # type mutation_type is str
286+
mutation_type = mutation_type.lower()
287+
if (mutation_type == "random"):
288+
self.mutation = self.random_mutation
289+
elif (mutation_type == "swap"):
290+
self.mutation = self.swap_mutation
291+
elif (mutation_type == "scramble"):
292+
self.mutation = self.scramble_mutation
293+
elif (mutation_type == "inversion"):
294+
self.mutation = self.inversion_mutation
295+
elif (mutation_type == "adaptive"):
296+
self.mutation = self.adaptive_mutation
297+
else:
298+
self.valid_parameters = False
299+
raise ValueError("Undefined mutation type. \nThe assigned value to the mutation_type argument ({mutation_type}) does not refer to one of the supported mutation types which are: \n-random (for random mutation)\n-swap (for swap mutation)\n-inversion (for inversion mutation)\n-scramble (for scramble mutation)\n-adaptive (for adaptive mutation).\n".format(mutation_type=mutation_type))
300300

301301
self.mutation_type = mutation_type
302302

0 commit comments

Comments
 (0)
0