population = [[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [1], [0]],
[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [3], [3]]]
def ProbabilityList2(population):
fitness = [chromosome[1] for chromosome in population]
total_weight=sum(fitness)
relative_fitness= [(chromosome[1]+1)/total_weight for chromosome in population]
return (relative_fitness)
我正在尝试根据以下逻辑:[[chromosome],[fitness],[counter]]
返回基于比例健身值的列表。我要做的就是在列表中的所有项目(个人)中生成基于此操作的概率列表,但我会收到错误:
TypeError: unsupported operand type(s) for +: 'int' and 'list'
我解决了在使用字典之前,但是在程序的循环中,我将获得重复的条目和选择功能崩溃,因为人口中的个人数量和概率(按位置索引)不均匀。关于我如何以这种格式计算的任何想法?
chromosome[1]
是列表。您可以使用chromosome[1][0]
访问它,也可以将其存储在列表之外。`
假设fitness
列表是人口的健身列表。因此,为了获得健身之和,您可以通过循环浏览该范围来获得其中的子名单之和。
def ProbabilityList2(population):
fitness = [ chromosome[1] for chromosome in population ]
total_weight=0
for i in range(len(fitness)):
total_weight+=sum(fitness[i])
这将为您提供以下健身列表和总和
[[1], [3], [4], [3]] # fitness list
11 # sum
尝试此功能:
def probabilityList2(population):
fitness = [chromosome[1][0] for chromosome in population]
total_weight=sum(fitness)
relative_fitness= [((chromosome[1][0])+1)/total_weight for chromosome in population]
return relative_fitness
probabilityList2(population)