【问题标题】:Handling duplicates when using Partially Matched Crossover for Genetic Algorithm使用遗传算法的部分匹配交叉时处理重复项
【发布时间】:2020-06-04 18:59:18
【问题描述】:

我是遗传算法的新手,正在研究 python 实现。我已经完成了交叉步骤,并且正在尝试部分匹配的交叉。对于我的最终输出,我希望得到一个不包含重复数字的列表。但是,在某些情况下,我会引入重复项。 例如,获取列表

Mate 1 [1,2,3,5,4,6]

Mate 2 [6,5,4,3,2,1]

如果交叉部分是[3,5,4] -> [4,3,2]

那么映射前的后代就变成了[1,2,4,3,2,6]。我对算法的理解是交叉外的映射是4 -> 3, 5 -> 3 and 2 -> 4。但是,这会导致 [1,4,4,3,2,6] 的输出有重复并且缺少 5。

如何解决这个问题?前4个就变成5个了吗?这将如何扩展到可能引入多个重复的更大列表?

【问题讨论】:

  • 在您的问题中,您似乎将 3 映射到 4 和 5?而且您还将 4 映射到 2 和 3?这个video 展示了 PMX 中的映射是如何工作的。

标签: python optimization genetic-algorithm evolutionary-algorithm


【解决方案1】:

我不确定你是否正确实施:

对于部分匹配的交叉(see explanation),如果您的交叉点是示例中建议的 2 和 5,那么您只能获得

offspring1 = [6, 2, 3, 5, 4, 1]
offspring2 = [1, 5, 4, 3, 2, 6]

如果你从 mate1 中选择 3,5,4 并按照 mate2 的顺序填写其余部分,你将得到后代 1,但如果你从 mate2 中选择 4,3,2 并按照 mate 1 的顺序填写其余部分,你将得到后代 2

参见下面的实现:

mate1 = [1,2,3,5,4,6]
mate2 = [6,5,4,3,2,1]


crossoverpoint1 = 2
crossoverpoint2=5
child = []

#fill in the initial genes in order of mate1
count = 0
for i in mate1:
    if(count == crossoverpoint1):
        break
    if(i not in mate2[crossoverpoint1:crossoverpoint2]):
        child.append(i)
        count= count+1

#select the genes within the crossover points from mate2          
child.extend(mate2[crossoverpoint1:crossoverpoint2])

#fill in the remaining genes in order of mate1
child.extend([x for x in mate1 if x not in child])

print(child)

输出:

[1, 5, 4, 3, 2, 6]

为了获得后代 1,将 mate1 换成 mate2。 您也可以尝试不同的交叉点,如果有帮助请告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2016-08-31
    • 2015-07-03
    • 2016-09-16
    相关资源
    最近更新 更多