clc
clear
close all
E=0.000001;
maxnum=30;%最大迭代次数
narvs=3;%目标函数的自变量个数
particlesize=20;%粒子群规模
c1=1.2;%每个粒子的个体学习因子,加速度常数
c2=1.6;%每个粒子的社会学习因子,加速度常数
w=0.8;%惯性因子
vmax1=0.5;%粒子的最大飞翔速度
vamx2=-0.5;
v=rand(particlesize,narvs);%粒子飞翔速度
x=zeros(particlesize,3);
x(:,1)=1+rand(1);
x(:,2)=7+rand(1);%粒子所在位置
x(:,3)=0.2+0.1*rand(1);
%定义适应度函数
% ROUT=zeros(particlesize,100);
for i=1:particlesize
[TT(i),ROUT{i},f(i)]=ACO(x(i,1),x(i,2),x(i,3));
end
personalbest_x=x;
personalbest_faval=f;
% personalbest_faval=TT;
[globalbest_faval(1),i]=min(personalbest_faval);
globalbest_x=personalbest_x(i,:);
globalbest_route=ROUT{i};
ff(1)=globalbest_faval(1);
k=2;
w
route=globalbest_route;
[minkl,p]=min(globalbest_faval);
xbest=globalbest_x(p,:);
LENROUT=length(route);
disp(['Alpha 表征信息素重要程度的参数=',num2str(xbest(1))]);
disp(['Beta 表征启发式因子重要程度的参数=',num2str(xbest(2))]);
disp(['Rho 信息素蒸发系数=',num2str(xbest(3))]);
disp(sprintf('Shortroad is: %s',num2str(route)));
disp(sprintf('Mininum is: %d',minkl));
figure(2)
% set(gcf,'color','white');
plot(1:length(ff),ff)
title('粒子群蚁群算法收敛曲线变化趋势')
xlabel('迭代次数');
ylabel('适应度值');
first_address = [
100,10
150,10
170,30
180,20
200,10
200,100
200,150
190,180
180,200
160,200
170,180
140,180
135,200
130,180
100,200
125,100
200,300
10,300
10,200
10,180
10,100
10,10
50,30
100,10
];%first_address表示测试数据中的节点坐标
SumOfCity = size(first_address,1);%节点个数,实际返回行数,即为节点个数
for n=1:size(first_address)
for m=1:size(first_address)
if length_address(n,m)~=0
length_address(m,n)=length_address(n,m); %对称矩阵
end
end
end
MM=size(length_address,1);
% 画出节点分布图形
figure;
grid on;
hold on;
title (strcat('井下机车最短运输路径',date))
%title( sprintf('井下机车最短运输路径 Event occured at %s',datestr (date ,15)));
for i=1:MM-1
plot(first_address(i,1),first_address(i,2),'bo','MarkerSize',10);
str=num2str(i);
text(first_address(i,1)-5,first_address(i,2)+5,str,'Color','red','FontSize',12);
end
% m=length(route);
for i=1:LENROUT
plot(first_address(route(i),1),first_address(route(i),2),'MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor',[0.5,0.5,0.5]) ;
hold on;
end
for i=1:MM
for j=1:MM
if(length_address(i,j)~=0)
line([first_address(i,1),first_address(j,1)],[first_address(i,2),first_address(j,2)],'Color','g','LineWidth',3);%划线
text((first_address(i,1)+first_address(j,1))/2,(first_address(i,2)+first_address(j,2))/2,num2str(length_address(i,j)));%标注线段距离
end
end
end
%% 最短路径
for p=1:LENROUT-1
if(route(p+1)~=24)
line([first_address(route(p),1),first_address(route(p+1),1)],[first_address(route(p),2),first_address(route(p+1),2)],'Color','r','LineWidth',3);%划线
text((first_address(route(p),1)+first_address(route(p+1),1))/2,(first_address(route(p),2)+first_address(route(p+1),2))/2,num2str(length_address(route(p),route(p+1))));%标注线段距离
else
line([first_address(route(p),1),first_address(1,1)],[first_address(route(p),2),first_address(1,2)],'Color','r','LineWidth',3);%划线
text((first_address(route(p),1)+first_address(1,1))/2,(first_address(route(p),2)+first_address(1,2))/2,num2str(length_address(route(p),route(p+1))));%标注线段距离
end
end
axis([0 250 0 400])
% 图形显示最优及平均函数值变化趋势
% figure(2);
% plot( minPL);
% title('迭代优化过程');
% xlabel('迭代次数');
% ylabel('Cost');
% hold on;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.