main.m
[x,y,d,t,h,iter,alpha,beta,e,m,n,el]=ants_information;
for i=1:iter
[app]=ants_primaryplacing(m,n); % 隨機安排200隻螞蟻的起始城市編號(14取1)
[at]=ants_cycle(app,m,n,h,t,alpha,beta); % 回傳200隻螞蟻拜訪14個城市的矩陣(at)
at=horzcat(at,at(:,1)); % 維度 = 200*15, 其中最後一行為出發的城市編號(即第一行)
[cost,f]=ants_cost(m,n,d,at,el); % cost每隻螞蟻的成本, f為扣除所有成本最小值*係數el(0.96)
[t]=ants_traceupdating(m,n,t,at,f,e); % 更新下一次的費洛蒙濃度tau
costoa(i)=mean(cost); % 第i次迭代全部螞蟻的平均成本
[mincost(i),number]=min(cost); % 記錄每一代的最低成本
besttour(i,:)=at(number,:); % 記錄每一代最佳螞蟻的拜訪城市順序記錄到besttour的第i列
iteration(i)=i;
end
plot(iteration,costoa);
title('average of cost (distance) versus number of cycles');
xlabel('iteration');
ylabel('distance');
[k,l]=min(mincost);
for i=1:n+1
X(i)=x(besttour(l,i));
Y(i)=y(besttour(l,i));
end
figure;
plot(X,Y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
xlabel('X');ylabel('y');axis('equal');
for i=1:n
text(X(i)+.5,Y(i),['\leftarrow node ',num2str(besttour(l,i))]);
end
title(['optimum course by the length of ',num2str(k)]);
留言列表
