I am having problems with my for loops. moreover with multiple for loops, I don't seem to understand how they work.
herunder is my double for loop i want to implement in a genetic algorithm. The input of the code: population
is an arraylist such as the mating_pool
:
mating_pool = [[1,4,7,2,8,2],[3,5,6,8,5,4]]
r_mutation
is the rate of mutation of a single element, anywhere in the population. i.e. the chance that a single element mutates.
def mutation(population,r_mutation):
mut_pop = population
for _i in range(len(population)):
for _j in range(len(population[_i])):
_ = random.randint(0,100)
if _ < 100*r_mutation:
mut_pop[_i][_j] = random.randint(0,20)
return mut_pop
r_mutation = 0.80
mutated_pop = mutation(mating_pool, r_mutation)
In my case, even with high r_mutation
, mutated_pop
equals mating_pool
at every point. I want the integers in the population
to mutate with a chance of 80%. How can I solve this?