以特定概率替换整数列表的列表

问题描述 投票:2回答:1

我有以下整数列表:

[[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]]

[将整个列表作为总体,将每个子列表作为一个个体,如下例所示:Population scheme

我需要创建一个函数,该函数将读取个体并以一定概率随机变异其中一个染色体,并考虑到列表中的数字只能在0-3范围内。

有人会知道任何方法或任何方法来开始开发此方法吗?我完全迷路了,不知道从哪里开始,我尝试过的所有方法都失败了,所以我正在寻找建议。

python genetic-algorithm mutation
1个回答
1
投票
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]);
练习:您可能希望修改程序以使染色体突变为不同于已经存在的染色体。
© www.soinside.com 2019 - 2024. All rights reserved.