【发布时间】:2018-05-04 21:37:51
【问题描述】:
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'
我在使用字典之前解决了这个问题,但是在程序的循环过程中,我得到了重复的条目并且选择函数崩溃了,因为人口中的个体数量和概率(按位置索引)是不均匀的。关于如何以这种格式计算它的任何想法?
【问题讨论】:
标签: python python-3.x list probability genetic-algorithm