【发布时间】:2021-11-23 19:29:09
【问题描述】:
我正在研究 Python 中的遗传算法。在我的问题中,我将个人存储在这样的列表中:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
在每次迭代中,每个人都有一定的死亡概率,因此它将从列表中删除。
更重要的是,在每次迭代中,个体随机配对,例如:
[1, 5], [7, 10], [3, 4], [6, 8], [2, 9]
并且这些对有一定的概率会有一个孩子,它将作为下一个数字(11、12 等)添加到列表中
每一对可能只出现一次,所以我必须存储每一对,并在随机选择两个人后,检查他们是否还没有成为一对。
我设法做到了所有这些:
reproducing_prob = 0.1 #probability of reproduction
death_prob = 0.1 #probability of death
pop_size = 10 #starting population size
pop_start = list(range(1, pop_size+1))
used_pairs = set() #storing pairs that already appeared
new_creature = 10
for day in range(10):
print("\nDay ", day)
print("Population: ", pop_start)
dead_creatures = []
for creature in pop_start: #iterating through whole list to check if creatures die
death = np.random.uniform(0,1)
if death < death_prob:
print("Dead: ", creature)
dead_creatures.append(creature)
pop_start = [creature for creature in pop_start if creature not in dead_creatures]
pop_temp = pop_start.copy()
while len(pop_temp) > 1: #looping through list until there aren't enough elements to pair them up
idx1, idx2 = random.sample(range(0, len(pop_temp)), 2)
rand1, rand2 = pop_temp[idx1], pop_temp[idx2]
print("Found pair: ", rand1, rand2)
if ((rand1, rand2) not in used_pairs) and ((rand2, rand1) not in used_pairs): #check if random pair hasn't been already used
for i in sorted([idx1, idx2], reverse=True):
pop_temp.pop(i)
pair = rand1, rand2
used_pairs.add(pair)
reproducing = np.random.uniform(0,1)
if reproducing < reproducing_prob:
pop_size += 1
new_creature += 1
print("New creature! ", new_creature)
pop_start.append(new_creature)
但在某些情况下,经过几次迭代后,我至少还剩下 2 个元素,它们已经配对,我最终会陷入无限循环:
Day 0
Population: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Found pair: 10 3
Found pair: 7 5
New creature! 11
Found pair: 8 2
Found pair: 9 1
Found pair: 6 4
Day 1
Population: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Dead: 8
Found pair: 6 10
Found pair: 1 11
Found pair: 4 5
Found pair: 2 9
Found pair: 3 7
Day 2
Population: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11]
Dead: 6
Found pair: 5 11
Found pair: 10 1
Found pair: 3 2
Found pair: 9 7
Day 3
Population: [1, 2, 3, 4, 5, 7, 9, 10, 11]
Found pair: 11 9
Found pair: 4 7
Found pair: 5 10
Found pair: 2 1
Day 4
Population: [1, 2, 3, 4, 5, 7, 9, 10, 11]
Dead: 10
Found pair: 5 3
New creature! 12
Found pair: 2 7
Found pair: 9 1
Found pair: 4 9
Found pair: 1 11
Found pair: 11 1
Found pair: 1 11
Found pair: 11 1
等等。
是否有任何有效的方法来检查每次迭代是否可以创建任何新对,如果不能,则中断 while 循环?当然,我可以在每个复制过程之后对留在pop_temp 列表中的元素进行组合,并检查这些组合中是否有任何组合不在used_pairs 集合中,但是由于多次迭代和高复制概率,它的效率将非常低,因为我的列表中会有数千个元素。
【问题讨论】:
-
个人可以多对出现吗?说
(1, 2), (2, 3)? -
不,他们不能。在每次迭代中,每个个体只能出现一对。
-
您能否发布一个您的解决方案不起作用的示例
-
当然,我在一个问题中加了一个例子
标签: python list genetic-algorithm