File tree 1 file changed +9
-5
lines changed 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -32,10 +32,14 @@ def crossover(parents, offspring_size):
32
32
offspring [k , crossover_point :] = parents [parent2_idx , crossover_point :]
33
33
return offspring
34
34
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.
37
38
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
41
45
return offspring_crossover
You can’t perform that action at this time.
0 commit comments