You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `best_solution()` method accepts a new optional parameter called `pop_fitness`. It accepts a list of the fitness values of the solutions in the population. If `None`, then the `cal_pop_fitness()` method is called to calculate the fitness values of the population.
# Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1.
288
+
ifmutation_num_genes==0:
289
+
ifself.mutation_probabilityisNone:
290
+
ifnotself.suppress_warnings: warnings.warn("The percentage of genes to mutate (mutation_percent_genes={mutation_percent}) resutled in selecting ({mutation_num}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.".format(mutation_percent=mutation_percent_genes, mutation_num=mutation_num_genes))
if (mutation_percent_genes<=0ormutation_percent_genes>100):
285
295
self.valid_parameters=False
286
296
raiseValueError("The percentage of selected genes for mutation (mutation_percent_genes) must be > 0 and <= 100 but ({mutation_percent_genes}) found.\n".format(mutation_percent_genes=mutation_percent_genes))
287
297
else:
298
+
# If mutation_percent_genes equals the string "default", then it is replaced by the numeric value 10.
288
299
ifmutation_percent_genes=='default'.lower():
289
300
mutation_percent_genes=10
301
+
290
302
# Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated.
# Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1.
293
305
ifmutation_num_genes==0:
294
-
ifnotself.suppress_warnings: warnings.warn("The percentage of genes to mutate (mutation_percent_genes={mutation_percent}) resutled in selecting ({mutation_num}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.".format(mutation_percent=mutation_percent_genes, mutation_num=mutation_num_genes))
306
+
ifself.mutation_probabilityisNone:
307
+
ifnotself.suppress_warnings: warnings.warn("The percentage of genes to mutate (mutation_percent_genes={mutation_percent}) resutled in selecting ({mutation_num}) genes. The number of genes to mutate is set to 1 (mutation_num_genes=1).\nIf you do not want to mutate any gene, please set mutation_type=None.".format(mutation_percent=mutation_percent_genes, mutation_num=mutation_num_genes))
295
308
mutation_num_genes=1
296
309
else:
297
310
self.valid_parameters=False
298
-
raiseValueError("Unexpected type for the 'mutation_percent_genes' parameter. A numeric value is expected but ({mutation_percent_genes_value}) of type {mutation_percent_genes_type} found.".format(mutation_percent_genes_value=mutation_percent_genes, mutation_percent_genes_type=type(mutation_percent_genes)))
311
+
raiseValueError("Unexpected value or type of the 'mutation_percent_genes' parameter. It only accepts the string 'default' or a numeric value but ({mutation_percent_genes_value}) of type {mutation_percent_genes_type} found.".format(mutation_percent_genes_value=mutation_percent_genes, mutation_percent_genes_type=type(mutation_percent_genes)))
299
312
else:
300
313
# The percent of genes to mutate is adaptive not fixed.
301
314
iftype(mutation_percent_genes) in [list, tuple, numpy.ndarray]:
@@ -323,7 +336,7 @@ def __init__(self,
323
336
self.valid_parameters=False
324
337
raiseValueError("When mutation_type='adaptive', then the 'mutation_percent_genes' parameter must have only 2 elements but ({mutation_percent_genes_length}) element(s) found.".format(mutation_percent_genes_length=len(mutation_percent_genes)))
325
338
else:
326
-
ifmutation_percent_genes!='default'.lower():
339
+
ifself.mutation_probabilityisNone:
327
340
self.valid_parameters=False
328
341
raiseValueError("Unexpected type for the 'mutation_percent_genes' parameter. When mutation_type='adaptive', then list/tuple/numpy.ndarray is expected but ({mutation_percent_genes_value}) of type {mutation_percent_genes_type} found.".format(mutation_percent_genes_value=mutation_percent_genes, mutation_percent_genes_type=type(mutation_percent_genes)))
329
342
# The mutation_num_genes parameter exists. Checking whether adaptive mutation is used.
Returns information about the best solution found by the genetic algorithm.
1550
-
The following is returned:
1564
+
Accepts the following parameters:
1565
+
pop_fitness: An optional parameter holding the fitness values of the solutions in the current population. If None, then the cal_pop_fitness() method is called to calculate the fitness of the population.
1566
+
The following are returned:
1551
1567
-best_solution: Best solution in the current population.
1552
1568
-best_solution_fitness: Fitness value of the best solution.
1553
1569
-best_match_idx: Index of the best solution in the current population.
@@ -1561,12 +1577,13 @@ def best_solution(self):
1561
1577
1562
1578
# Getting the best solution after finishing all generations.
1563
1579
# At first, the fitness is calculated for each solution in the final generation.
1564
-
fitness=self.cal_pop_fitness()
1580
+
ifpop_fitnessisNone:
1581
+
pop_fitness=self.cal_pop_fitness()
1565
1582
# Then return the index of that solution corresponding to the best fitness.
0 commit comments