【发布时间】:2012-10-12 16:45:17
【问题描述】:
我正在尝试实施本地搜索(使用 2-opt)来解决旅行商问题。但是,我无法正确地重新创建完整的电路(节点之旅)。我认为我的算法可能有缺陷。这是我的 2-opt 实现:
a->b->...d->c->...a
切换到
a->d->...b->c->...a
我的一般流程看起来像标准交换: 将 b 存储为 temp 将 c 存储为 temp2 a->d b->c
我的代码如下所示:
node* twoOpt(double** distance_matrix, node* tour, int NUM_CITIES)
{
int rand1, rand2;
int temp, temp2;
rand1 = rand() % NUM_CITIES; //a
rand2 = rand() % NUM_CITIES; //d
do
{
//Ensure the two numbers are different
while (rand1 == rand2 || tour[rand1].destination == rand2 || rand1 == tour[rand2].destination)
rand2 = rand() % NUM_CITIES;
//Make swap
temp = tour[rand1].destination; //b
temp2 = tour[rand2].destination; //c
tour[rand1].destination = rand2;
tour[rand1].distance = distance_matrix[rand1][rand2]; //a->d
tour[temp].destination = temp2;
tour[temp].distance = distance_matrix[temp][temp2]; //b->c
} while(!travelTour(tour, NUM_CITIES));
return tour;
}
现在我明白这段代码并不完美。例如,如果重新洗牌的两个节点没有创建完整的电路,则代码只会在再次尝试之前更改第二个节点。但我的问题是为什么我一开始就无法完成完整的巡演。
感谢您的帮助!
【问题讨论】:
标签: c search local traveling-salesman