📄 tsp.m
字号:
function TSP
N=10; %城市数目
cityx=[0.4,0.2439,0.1707,0.2293,0.5171,0.8732,0.6878,0.8488,0.6683,0.6195];
cityy=[0.4439,0.1463,0.2293,0.761,0.9414,0.6536,0.5219,0.3609,0.2536,0.2634];
for i=1:N
for j=1:N
d(i,j)=sqrt((cityx(i)-cityx(j))^2+(cityy(i)-cityy(j))^2);
end
end
%TSP网络参数设置
global A
global B
global C
global D
A=500;
B=500;
C=1000;
D=500;
x0=0.02;
deltat=10e-6;
flag=0; %结束标志
while flag==0
V=rand(N,N);
X=log(2*V-1)*x0/2;
X=X+0.01;
for num=1:1000
V=(1+tanh(X/x0))/2;
for x=1:N
for i=1:N
if V(x,i)<0.5
V(x,i)=0;
else
V(x,i)=1;
end
end
end
daoX=daoshu(X,V,d,N);
X=X+deltat*daoX;
end
flag=test(V,N);
end
V
for x=1:N
for i=1:N
if V(x,i)==1
routex(i)=cityx(x);
routey(i)=cityy(x);
end
end
end
routex(N+1)=routex(1);
routey(N+1)=routey(1);
routex;
routey;
plot(routex,routey,'o-');
%总路程
distance=0;
for i=1:N
distance=distance+sqrt((routex(i)-routex(i+1))^2+(routey(i)-routey(i+1))^2);
end
disp(distance);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -