%% 该文件演示基于TSP-PSO算法
clc;clear
%% 下载数据
cityDist=
cityCoor=[1:13;1:13]';%城市坐标矩阵
figure
plot(cityCoor(:,1),cityCoor(:,2),'ms','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')
legend('城市位置')
ylim([4 78])
title('城市分布图','fontsize',12)
xlabel('km','fontsize',12)
ylabel('km','fontsize',12)
%ylim([min(cityCoor(:,2))-1 max(cityCoor(:,2))+1])
grid on
%% 计算城市间距离
n=size(cityCoor,1); %城市数目
%cityDist=zeros(n,n); %城市距离矩阵
% cityDist=[0,12.9000000000000,20.8000000000000,28,35.7000000000000,22.1000000000000,30.2000000000000,6,11.9000000000000,16.3000000000000,23.7000000000000,27.8000000000000,36;12.9000000000000,0,7.90000000000000,15.1000000000000,22.8000000000000,9.20000000000000,17.3000000000000,18.9000000000000,21,8.80000000000000,16.2000000000000,20.3000000000000,28.5000000000000;20.8000000000000,7.90000000000000,0,7.20000000000000,14.9000000000000,17.1000000000000,25.2000000000000,26.8000000000000,28.9000000000000,16.7000000000000,24.1000000000000,28.2000000000000,36.4000000000000;28,15.1000000000000,7.20000000000000,0,7.70000000000000,24.3000000000000,18,34,36.1000000000000,23.9000000000000,31.3000000000000,35.4000000000000,32.9000000000000;35.7000000000000,22.8000000000000,14.9000000000000,7.70000000000000,0,18.4000000000000,10.3000000000000,41.7000000000000,43.8000000000000,31.6000000000000,25.7000000000000,33.4000000000000,25.2000000000000;22.1000000000000,9.20000000000000,17.1000000000000,24.3000000000000,18.4000000000000,0,8.10000000000000,25,26.9000000000000,14.7000000000000,7.30000000000000,26.2000000000000,23;30.2000000000000,17.3000000000000,25.2000000000000,18,10.3000000000000,8.10000000000000,0,33.1000000000000,35,22.8000000000000,15.4000000000000,23.1000000000000,14.9000000000000;6,18.9000000000000,26.8000000000000,34,41.7000000000000,25.0000000000000,33.1000000000000,0,5.90000000000000,10.3000000000000,17.7000000000000,21.8000000000000,30;11.9000000000000,21,28.9000000000000,36.1000000000000,43.8000000000000,26.9000000000000,35,5.90000000000000,0,12.2000000000000,19.6000000000000,23.7000000000000,31.9000000000000;16.3000000000000,8.80000000000000,16.7000000000000,23.9000000000000,31.6000000000000,14.7000000000000,22.8000000000000,10.3000000000000,12.2000000000000,0,7.40000000000000,11.5000000000000,19.7000000000000;23.7000000000000,16.2000000000000,24.1000000000000,31.3000000000000,25.7000000000000,7.30000000000000,15.4000000000000,17.7000000000000,19.6000000000000,7.40000000000000,0,18.9000000000000,20.3000000000000;27.8000000000000,20.3000000000000,28.2000000000000,35.4000000000000,33.4000000000000,26.2000000000000,23.1000000000000,21.8000000000000,23.7000000000000,11.5000000000000,18.9000000000000,0,8.20000000000000;36,28.5000000000000,36.4000000000000,32.9000000000000,25.2000000000000,23,14.9000000000000,30,31.9000000000000,19.7000000000000,20.3000000000000,8.20000000000000,0]
nMax=200; %进化次数
indiNumber=1000; %个体数目
individual=zeros(indiNumber,n);
%^初始化粒子位置
for i=1:indiNumber
individual(i,:)=randperm(n);
end
%% 计算种群适应度
indiFit=fitness(individual,cityCoor,cityDist);
[value,index]=min(indiFit);
tourPbest=individual; %当前个体最优
tourGbest=individual(index,:) ; %当前全局最优
recordPbest=inf*ones(1,indiNumber); %个体最优记录
recordGbest=indiFit(index); %群体最优记录
xnew1=individual;
%% 循环寻找最优路径
L_best=zeros(1,nMax);
for N=1:nMax
N
%计算适应度值
indiFit=fitness(individual,cityCoor,cityDist);
%更新当前最优和历史最优
for i=1:indiNumber
if indiFit(i)<recordPbest(i)
recordPbest(i)=indiFit(i);
tourPbest(i,:)=individual(i,:);
end
if indiFit(i)<recordGbest
recordGbest=indiFit(i);
tourGbest=individual(i,:);
end
end
[value,index]=min(recordPbest);
recordGbest(N)=recordPbest(index);
%% 交叉操作
for i=1:indiNumber
% 与个体最优进行交叉
c1=unidrnd(n-1); %产生交叉位
c2=unidrnd(n-1); %产生交叉位
while c1==c2
c1=round(rand*(n-2))+1;
c2=round(rand*(n-2))+1;
end
chb1=min(c1,c2);
chb2=max(c1,c2);
cros=tourPbest(i,chb1:chb2);
ncros=size(cros,2);
%删除与交叉区域相同元素
for j=1:ncros
for k=1:n
if xnew1(i,k)==cros(j)
xnew1(i,k)=0;
for t=1:n-k
temp=xnew1(i,k+t-1);
xnew1(i,k+t-1)=xnew1(i,k+t);
xnew1(i,k+t)=temp;
end
end
end
end
%插入交叉区域
xnew1(i,n-ncros+1:n)=cros;
%新路径长度变短则接受
dist=0;
for j=1:n-1
dist=dist+cityDist(xnew1(i,j),xnew1(i,j+1));
end
dist=dist+cityDist(xnew1(i,1),xnew1(i,n));
if indiFit(i)>dist
individual(i,:)=xnew1(i,:);
end
% 与全体最优进行交叉
c1=round(rand*(n-2))+1; %产生交叉位
c2=round(rand*(n-2))+1; %产生交叉位
while c1==c2
c1=round(rand*(n-2))+1;
c2=round(rand*(n-2))+1;
end
chb1=min(c1,c2);
chb2=max(c1,c2);
cros=tourGbest(chb1:chb2);
ncros=size(cros,2);
%删除与交叉区域相同元素
for j=1:ncros
for k=1:n
if xnew1(i,k)==cros(j)
xnew1(i,k)=0;
for t=1:n-k
temp=xnew1(i,k+t-1);
xnew1(i,k+t-1)=xnew1(i,k+t);
xnew1(i,k+t)=temp;
end
end
end
end
%插入交叉区域
xnew1(i,n-ncros+1:n)=cros;
%新路径长度变短则接受
dist=0;
for j=1:n-1
dist=dist+cityDist(xnew1(i,j),xnew1(i,j+1));
end
dist=dist+cityDist(xnew1(i,1),xnew1(i,n));
if indiFit(i)>dist
individual(i,:)=xnew1(i,:);
end
%% 变异操作
c1=round(rand*(n-1))+1; %产生变异位
c2=round(rand*(n-1))+1; %产生变异位
while c1==c2
c1=round(rand*(n-2))+1;
c2=round(rand*(n-2))+1;
end
temp=xnew1(i,c1);
xnew1(i,c1)=xnew1(i,c2);
xnew1(i,c2)=temp;
%新路径长度变短则接受
dist=0;
for j=1:n-1
dist=dist+cityDist(xnew1(i,j),xnew1(i,j+1));
end
dist=dist+cityDist(xnew1(i,1),xnew1(i,n));
if indiFit(i)>dist
individual(i,:)=xnew1(i,:);
end
end
[value,index]=min(indiFit);
L_best(N)=indiFit(index);
tourGbest=individual(index,:);
end
%% 结果作图
figure
plot(L_best)
title('算法训练过程')
xlabel('迭代次数')
ylabel('适应度值')
grid on
figure
hold on
plot([cityCoor(tourGbest(1),1),cityCoor(tourGbest(n),1)],[cityCoor(tourGbest(1),2),...
cityCoor(tourGbest(n),2)],'ms-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
for i=2:n
plot([cityCoor(tourGbest(i-1),1),cityCoor(tourGbest(i),1)],[cityCoor(tourGbest(i-1),2),...
cityCoor(tourGbest(i),2)],'ms-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
end
legend('规划路径')
scatter(cityCoor(:,1),cityCoor(:,2));
title('规划路径','fontsize',10)
xlabel('km','fontsize',10)
ylabel('km','fontsize',10)
grid on
ylim([4 80])
disp(['最短距离:' num2str(value)]);
disp(['最短路径:' num2str([tourGbest tourGbest(1)])]);
评论2