File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Expand file tree Collapse file tree 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):
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
You can’t perform that action at this time.
0 commit comments