Your question is more towards the software engineering problem, and not towards the EA side. But, I would like to give some suggestions towards your design decision of choosing 5 genes from the genome. You are restricting EA to explore the search space for your solutions, and your EA could definitely get stuck in local optima and might not be able to escape as mutated solutions will be changed around just 5 genes (If the limitation is from a problem itself, can't do anything about it). As an example, EA might find a near-optimal solution by just changing 3 genes or by changing 7 genes. If it is a design choice, you might like to consider revisit your design decision for EA.
In general, the EA community uses mutation probability for genes. When one would like to mutate offsprings, it is based on hyperparameter mutation probability (Also, as this is hyperparameter, you also can tune this for your problem to achieve good results later).
Let's say if your design decision of mutating 5 genes is a constraint, and also you would like to use tunable hyper-parameter mutation probability. You can have your solution like below:
import random
def mutate(genotype, m_probe):
mutated_offspring = []
mutated_genes_indexes = set()
for index, gene in enumerate(genotype):
if random.uniform(0, 1) >= m_probe and len(mutated_genes_indexes) < 5:
mutated_offspring.append(int(not gene))
mutated_genes_indexes.add(index)
else:
mutated_offspring.append(int(gene))
print("Mutated genes indexes: ", mutated_genes_indexes)
return mutated_offspring
# Each genes have 20% probability to get mutated! NOTE: with higher probability you might not find 5 genes mutated, 20 is chosen based on the constraint and can be tuned later with this constraint.
genotype = [1,0,0,1,0,0,1,1,1,0]
print(mutate(genotype, 0.20))
My preferred design decision would be an equal chance for all genes to get mutated without constraint. In that case, a solution might look like below:
import random
def mutate(genotype, m_probe):
mutated_offspring = []
mutated_genes_indexes = set()
for index, gene in enumerate(genotype):
if random.uniform(0, 1) >= m_probe:
mutated_offspring.append(int(not gene))
mutated_genes_indexes.add(index)
else:
mutated_offspring.append(int(gene))
print("Mutated genes indexes: ", mutated_genes_indexes)
return mutated_offspring
genotype = [1,0,0,1,0,0,1,1,1,0]
print(mutate(genotype, 0.50))