3

I'm coding my first genetic algorithm in Python. I particularly care about the optimization and population scalability.

import numpy as np
population = np.random.randint(-1, 2, size=(10,10))

Here I make a [10,10] array, with random number between -1 and 1.
And now I want to perform a specific mutation ( mutation rate depends on the specimens fitness ) for each specimen of my array.

For example, I have:

print population
[[ 0  0  1  1 -1  1  1  0  1  0]
[ 0  1 -1 -1  0  1 -1 -1  0 -1]
[ 0  0  0  0  0  0  0  0  0  0]
[ 0  0  0  0  0  0  0  0  0  0]
[ 0  0  0  0  0  0  0  0  0  0]
[ 0  1  1  0  0  0  1  1  0  1]
[ 1 -1  0  0  1  0 -1  1  1  0]
[ 1 -1  1 -1  0 -1  0  0  1  1]
[ 0  0  0  0  0  0  0  0  0  0]
[ 0  1  1  0  0  0  1 -1  1  0]]

I want to perform the mutation of this array with a specific mutation rate for each sub-array in population. I try this but the optimization is not perfect and I need to perform a different mutation for each sub-array (each sub-array is a specimen) in the population (the main array, "population").

population[0][numpy.random.randint(0, len(population[0]), size=10/2)] = np.random.randint(-1, 2, size=10/2)

I'm looking for a way to apply something like a mutation mask on all the main-array. Something like that:

 population[array element select with the mutation rate] = random_array[with the same size]

I think it's the best way (because we only to an array selection and after we replace this selection with the random array), but I don't know how to perform this. And if you have other solution I am on it ^^.

2
  • What are your design / operations targets -- ~ static scales of the population[]'s representation in [SPACE]-dimension [GB] + What are your target performance / latency targets in [TIME]-dimension for say ~ 1E+9 mutation epochs in [s]? Commented Aug 16, 2017 at 15:31
  • Sorry but I don't really understand your question ^^. I don't really have target performance, I just want to make my program faster. And to save processor power even if I have to use more memory. Here I have 10 specimens in a population and each specimen has 10 variable. So the number of specimens in my population is directly linked with my computing time (much bigger, much longer). And the number of variable in each specimen is determined by the problem/function who I try to fit. So sure, I have planned to increase that number of variables by the specimen. Commented Aug 16, 2017 at 16:17

1 Answer 1

1

Let's say you have an array fitness with the fitness of each specimen, with size len(population). Let's also say you have a function fitness_mutation_prob that, for a given fitness, gives you the mutation probability for each of the elements in the specimen. For example, if the values of fitness range from 0 to 1, fitness_mutation_prob(fitness) could be something like (1 - fitness), or np.square(1 - fitness), or whatever. You can then do:

r = np.random.random(size=population.shape)
mut_probs = fitness_mutation_prob(fitness)
m = r < mut_probs[:, np.newaxis]
population[m] = np.random.randint(-1, 2, size=np.count_nonzero(m))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, that work perfectly :). Do you think that the best and most rapid way to mutate my next generation? Because when my population size increase I will get slower and slower because I need to generate the "r" array to build the mutation boolean mask "m". And "r" array is composed of randoms numbers who need many resources.
@JulienBlanchon Another possibility would be to compute the number of mutations per row, e.g. num_muts = np.floor(mut_probs * (population.shape[1] + 1)), and then for i, n in enumerate(num_muts): population[i, np.random.randint(poulation.shape[1], size=n)] = np.random.randint(-1, 2, size=n). That should require less memory but I'm not sure it will be faster having a loop. Also np.random.randint(poulation.shape[1], size=n) might produce repeated indices, although maybe that's not too important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.