Reputation: 469
I have the following list of integers:
[[0, 2, 3, 1, 3, 2, 0, 1],
[0, 3, 2, 1, 2, 3, 0, 1],
[1, 2, 3, 0, 3, 2, 1, 0],
[2, 1, 3, 0, 3, 1, 2, 0]]
Taking this whole list as a population and each sublist inside as an individual, like this example: Population scheme
I need to create a function that will read the individuals and randomly mutate one of the chromosomes with a certain probability, taking into consideration that the numbers in the list can be only in this 0-3 range.
Would anyone know any method, or whatever approach to start developing this? I am completely lost and don't know where to begin, everything I tried has failed, so I am looking for suggestion how would one do it.
Upvotes: 2
Views: 699
Reputation: 315
from random import randint, uniform;
def mutateIndividual(ind):
if uniform(0,1) < prob: # random probability of mutation
mutationIndex = randint(0, len(ind)) # select one chromosome
ind[mutationIndex] = randint(0,3) # mutate
return ind;
for i in range(0, len(population)): # outer loop on each individual
population[i] = mutateIndividual(population[i]);
Exercise: You may want to modify the program to mutate a chromosome to something different than what it already is.
Upvotes: 1