目录
matlab实现2-opt
最近学习了TSP问题,局部搜索算法是求解TSP问题的一种有效方法。网上关于2-opt代码很少,自己用matlab实现2-opt。分享给大家,欢迎一起学习交流。
一、2-opt简介
2-opt属于局部搜索算法(local search algorithm),是解决组合优化问题的一种有效工具。
二、连接重组方式
三、代码实现
function route = fun_2opt(route,dislist,maxcount)
% 输入route, dislist,maxcount—路径,距离矩阵,最大迭代次数
M = size(route,2);
f_route = CalDist(dislist,route);
count = 0;
while count < maxcount
cha = randperm(M,2);
while abs(cha(1)-cha(2)) <= 1
cha = randperm(M,2);
end
a = min(cha);
b = max(cha);
new_route = route;
new_route(a:b) = fliplr(new_route(a:b));
f_route_new = CalDist(dislist, new_route);
if f_route_new >= f_route
count = count + 1;
else
count = 0;
route = new_route;
f_route = f_route_new;
end
end
版权声明:本文为CSDN博主「qq_30977037」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_30977037/article/details/117356753## matlab实现2-opt