【发布时间】:2018-04-23 21:27:34
【问题描述】:
我需要对 GA 进行适应度比例选择方法,但是我的人口不能松散结构(顺序),在这种情况下,在生成概率时,我相信个人得到了错误的权重,程序是:
population=[[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [6], [0]],
[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [4], [1]],
[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [6], [2]],
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [4], [3]]]
popultion_d={'0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1': 6,
'0,0,1,1,1,0,0,1,1,0,1,1,0,0,0,1': 4,
'0,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0': 6,
'1,0,0,1,1,1,0,0,1,1,0,1,1,0,0,0': 4}
def ProbabilityList(population_d):
fitness = population_d.values()
total_fit = (sum(fitness))
relative_fitness = [f/total_fit for f in fitness]
probabilities = [sum(relative_fitness[:i+1]) for i in range(len(relative_fitness))]
return (probabilities)
def FitnessProportionateSelection(population, probabilities, number):
chosen = []
for n in range(number):
r = random.random()
for (i, individual) in enumerate(population):
if r <= probabilities[i]:
chosen.append(list(individual))
break
return chosen
number=2
人口元素为:[[个人],[健身],[计数器]]
概率函数输出为:[0.42857142857142855, 0.5714285714285714, 0.8571428571428571, 1.0]
我在这里注意到的是,前一个权重与下一个权重相加,不一定是新月顺序,因此认为适合度最低的染色体具有较高的权重。
我不想订购它,因为我需要稍后按位置索引列表,所以我想我会有错误的匹配。
有人知道在这种情况下执行加权选择的可能解决方案、包或不同方法吗?
p.s:我知道字典在这里可能是多余的,但我在使用列表本身时遇到了其他几个问题。
编辑:我尝试使用random.choices(),如下所示(使用相对适应度):
def FitnessChoices(population, probabilities, number):
return random.choices(population, probabilities, number)
但我收到此错误:TypeError: choices() takes from 2 to 3 positional arguments but 4 were given
谢谢!
【问题讨论】:
标签: python python-3.x selection genetic-algorithm