资源简介
ABC_TSP,人工蜂群算法优化旅行商问题matlab代码,完整无误。
代码片段和文件信息
%%%%%%%%%%%%%
clear all
close all
clc
%%%%%%%%%%%%初始化种群参数 %%%%%%%%%%
NP = 20;
FoodNumber = NP/2;
Limit = 100;
maxCycle = 2500;
%%%%%%%%%%%% 定义全局变量 %%%%%%%%%
global NC;
global d;
NC = 14;
%城市位置矩阵
x(1) = 16.47;x(2) = 16.47;x(3) = 20.09;x(4) = 22.39;x(5) = 25.23;x(6) = 22.00;x(7) = 20.47;
x(8) = 17.20;x(9) = 16.30;x(10) = 14.05;x(11) = 16.53;x(12) = 21.52;x(13) = 19.41;x(14) = 20.09;
y(1) = 96.10;y(2) = 94.44;y(3) = 92.54;y(4) = 93.37;y(5) = 97.24;y(6) = 96.05;y(7) = 97.02;
y(8) = 96.29;y(9) = 97.38;y(10) = 98.12;y(11) = 97.38;y(12) = 95.59;y(13) = 97.13;y(14) = 94.55;
%计算各城市之间的距离
for m = 1:14
for n = 1:14
d(mn) =sqrt((x(m)-x(n))^2+(y(m)-y(n))^2);
end
end
%画初始路径图
figure(1);
for m = 1:NC
scatter(x(m)y(m)50‘r‘)
hold on
end
plot([x(1)x(2)x(3)x(4)x(5)x(6)x(7)...
x(8)x(9)x(10)x(11)x(12)x(13)x(14)x(1)][y(1)y(2)y(3)y(4)y(5)y(6)y(7)...
y(8)y(9)y(10)y(11)y(12)y(13)y(14)y(1)]);
title(‘初始路径‘);
xlabel(‘城市X坐标‘);
ylabel(‘城市Y坐标‘);
runtime = 10;
GlobalMins = zeros(1runtime);
for r = 1:runtime
%初始化蜂群
a=initial(NP/2NC);
for i = 1:FoodNumber
Foods(i:) = a(i:);
end
%计算适应度函数值
for i = 1:FoodNumber
route = Foods(i:);
Fitness(i) = calculateFitness(route);
end
% 初始化搜索次数,用于和Limit比较
trial=zeros(1FoodNumber);
%找出适应度函数值的最小值
BestInd = find(Fitness == min(Fitness));
BestInd = BestInd(end);
GlobalMin = Fitness(BestInd);
GlobalParams = Foods(BestInd:);
%迭代开始
iter = 1;
j = 1;
while ((iter <= maxCycle))
%%%%%%%%采蜜蜂模式 %%%%%%%%
for i = 1:FoodNumber
%随机交换3个城市的顺序,改变此时的路线
route_next = Foods(i:);
l = round(2+12*rand());
m = round(2+12*rand());
n = round(2+12*rand());
temp = route_next(l);
route_next(l) = route_next(m);
route_next(m) = route_next(n);
route_next(n) = temp;
%计算新蜜源的适应度函数值
FitnessSol = calculateFitness(route_next);
%使用贪婪准则,寻找最优蜜源
if (FitnessSol < Fitness(i))
Foods(i:) = route_next;
Fitness(i) = FitnessSol;
trial(i) = 0;
else
trial(i) = trial(i)+1;
end
end
%%%%%%%%%%%根据适应度值计算采蜜蜂被跟随蜂跟随的概率
prob = (0.9*Fitness./max(Fitness))+0.1;
%%%%%%%%%%%%% 观察蜂 %%%%%%%%
i = 1;
t = 0;
while(t < FoodNumber)
if(rand < prob(i))
t = t+1;
%随机交换3个城市的顺序,改变此时的路线
route_next = Foods(i:);
l = round(2+12*rand());
m = round(2+12*rand());
n = round(2+12*rand());
temp = route_next(l);
route_next(l) = route_next(m);
route_next(m) = route_next(n);
route_next(n) = temp;
%计算新蜜源的适应度函数值
FitnessSol = calculateFitness(route_next);
%使用贪婪准则,寻找最优蜜源
if (FitnessSol < Fitness(i))
Foods(i:) = route_next;
Fitness(i) = FitnessSol;
trial(i) = 0;
else
tri
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-04-14 13:21 ABC_TSP\
文件 5198 2017-07-25 17:37 ABC_TSP\ABC_TSP.m
文件 226 2017-07-24 21:44 ABC_TSP\calculateFitness.m
文件 261 2017-07-24 21:26 ABC_TSP\initial.m
- 上一篇:多目标优化ABC
- 下一篇:滑模变结构控制MATLAB仿真.zip
评论
共有 条评论