"unique" 遗传算法交叉 - TSP
"unique" crossover for genetic algorithm - TSP
我正在创建一个 Genetic Algorithm to solve the Traveling Salesman Problem。
目前两个二维表分别表示需要交叉的两个parents:
path_1 = np.shuffle(np.arange(12).reshape(6, 2))
path_2 = np.arange(12).reshape(6,2)
假设列表中的每个元素表示笛卡尔平面上的一个 (x, y)
坐标,而二维列表表示 "traveling salesman" 必须采用的路径(从索引 0 到索引 -1)。
由于 TSP 要求所有点都包含在路径中,因此此交叉的结果 child 必须 没有重复的 点。
我不知道如何进行这种交叉并得到 child 两者的代表 parents。
你可以这样做,
使用任何方法从一个 parent 中选择一半(或 0 to (length - 1)
之间的任何随机数)坐标,让我们说 i % 2 == 0
的位置。
可以使用多种方法将它们定位到 child 中:随机定位,或全部定位在开始(或结束)或备用位置。
现在剩下的坐标需要来自第二个parent,你可以在第二个parent中遍历,如果没有选择坐标,将它添加到空白处。
例如,
我选择 parent 1 中的偶数位置坐标并将其放入 child 中的偶数位置索引中,然后遍历 parent 2 以将剩余的坐标放在奇数中child.
中的位置索引
def crossover(p1, p2, number_of_cities):
chk = {}
for i in range(number_of_cities):
chk[i] = 0
child = [-1] * number_of_cities
for x in range(len(p1)):
if x % 2 == 0:
child[x] = p1[x]
chk[p1[x]] = 1
y = 1
for x in range(len(p2)):
if chk[p2[x]] == 0:
child[y] = p2[x]
y += 2
return child
这种方法保留了从两个 parent 访问的城市的顺序。
也因为它不是对称的 p1
和 p2
可以切换给 2 children 并且可以选择更好的(或两者)。
您需要使用有序交叉运算符,例如OX1。
OX1 is a fairly simple permutation crossover.
Basically, a swath of consecutive alleles from parent 1 drops down,
and remaining values are placed in the child in the order which they
appear in parent 2.
我曾经 运行 与这些操作员一起使用 TSP:
我正在创建一个 Genetic Algorithm to solve the Traveling Salesman Problem。
目前两个二维表分别表示需要交叉的两个parents:
path_1 = np.shuffle(np.arange(12).reshape(6, 2))
path_2 = np.arange(12).reshape(6,2)
假设列表中的每个元素表示笛卡尔平面上的一个 (x, y)
坐标,而二维列表表示 "traveling salesman" 必须采用的路径(从索引 0 到索引 -1)。
由于 TSP 要求所有点都包含在路径中,因此此交叉的结果 child 必须 没有重复的 点。
我不知道如何进行这种交叉并得到 child 两者的代表 parents。
你可以这样做,
使用任何方法从一个 parent 中选择一半(或 0 to (length - 1)
之间的任何随机数)坐标,让我们说 i % 2 == 0
的位置。
可以使用多种方法将它们定位到 child 中:随机定位,或全部定位在开始(或结束)或备用位置。
现在剩下的坐标需要来自第二个parent,你可以在第二个parent中遍历,如果没有选择坐标,将它添加到空白处。
例如,
我选择 parent 1 中的偶数位置坐标并将其放入 child 中的偶数位置索引中,然后遍历 parent 2 以将剩余的坐标放在奇数中child.
中的位置索引def crossover(p1, p2, number_of_cities):
chk = {}
for i in range(number_of_cities):
chk[i] = 0
child = [-1] * number_of_cities
for x in range(len(p1)):
if x % 2 == 0:
child[x] = p1[x]
chk[p1[x]] = 1
y = 1
for x in range(len(p2)):
if chk[p2[x]] == 0:
child[y] = p2[x]
y += 2
return child
这种方法保留了从两个 parent 访问的城市的顺序。
也因为它不是对称的 p1
和 p2
可以切换给 2 children 并且可以选择更好的(或两者)。
您需要使用有序交叉运算符,例如OX1。
OX1 is a fairly simple permutation crossover. Basically, a swath of consecutive alleles from parent 1 drops down, and remaining values are placed in the child in the order which they appear in parent 2.
我曾经 运行 与这些操作员一起使用 TSP: