2016-11-03 80 views
1

我遵循遗传算法的方法来解决背包问题here。我知道他们使用直接值编码方案而不是二进制表示法。交叉功能如下:遗传算法:交叉0-1背包

def cxSet(ind1, ind2): 
"""Apply a crossover operation on input sets. The first child is the 
intersection of the two sets, the second child is the difference of the 
two sets. 
""" 
temp = set(ind1)    # Used in order to keep type 
ind1 &= ind2     # Intersection (inplace) 
ind2 ^= temp     # Symmetric Difference (inplace) 
return ind1, ind2 

如果我要编码背包问题的二进制表示的染色体,inersection将是一个与运算。设定差异的分期手术会是什么?此外,我只是想知道这种类型的交叉可能是什么原因,如果这种类型的交叉与其他常见的交叉技术如单点交叉或双点交叉有优势。

回答

1

解决此类事情最简单的方法是让一个小例子:

s1 = {1, 2, 3, 4} => 11110000 
s2 = {3, 4, 5, 6} => 00111100 
s1 intersect s2 = {3, 4} => 00110000 
s1 difference s2 = {1, 2, 5, 6} => 11001100 

望着这一点,我们提出了以下位运算符:

  1. 相交:S1和S2(通常&

  2. 差:S1 XOR S2(通常^