10000 Supporting multiple mutations. · dadbob/GeneticAlgorithmPython@7a978a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a978a7

Browse files
authored
Supporting multiple mutations.
Supporting applying mutation for more than 1 gene using the num_mutations argument in the mutation() function.
1 parent e55a79e commit 7a978a7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

GA.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ def crossover(parents, offspring_size):
3232
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
3333
return offspring
3434

35-
def mutation(offspring_crossover):
36-
# Mutation changes a single gene in each offspring randomly.
35+
def mutation(offspring_crossover, num_mutations=1):
36+
mutations_counter = numpy.uint8(offspring_crossover.shape[1] / num_mutations)
37+
# Mutation changes a number of genes as defined by the num_mutations argument. The changes are random.
3738
for idx in range(offspring_crossover.shape[0]):
38-
# The random value to be added to the gene.
39-
random_value = numpy.random.uniform(-1.0, 1.0, 1)
40-
offspring_crossover[idx, 4] = offspring_crossover[idx, 4] + random_value
39+
gene_idx = mutations_counter - 1
40+
for mutation_num in range(num_mutations):
41+
# The random value to be added to the gene.
42+
random_value = numpy.random.uniform(-1.0, 1.0, 1)
43+
offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value
44+
gene_idx = gene_idx + mutations_counter
4145
return offspring_crossover

0 commit comments

Comments
 (0)
0