玩问题描述:在30个城市,给出枢纽个数,基于免疫优化算法对城市枢纽进行选择,并显示结果
算法描述:
1、识别抗原,将种群信息定义为一个结构体
2、产生初始抗体群,迭代寻优
3、抗体多样性评价
4、根据excellence,形成父代群,更新记忆库
5、选择,交叉,变异操作,再加入记忆库中抗体,产生新种群
6、画出免疫算法收敛曲线,配送中心选址图
7、找出配送最近点,计算派送点地址
部分代码
clc
clear
sizepop=50; % 种群规模
overbest=10; % 记忆库容量
MAXGEN=100; % 迭代次数
pcross=0.5; % 交叉概率
pmutation=0.4; % 变异概率
ps=0.95; % 多样性评价参数
length=3; % 枢纽中心数
M=sizepop+overbest;
individuals = struct('fitness',zeros(1,M), 'concentration',zeros(1,M),'excellence',zeros(1,M),'chrom',[]);
%% step2
individuals.chrom = popinit(M,length);
trace=[];
for iii=1:MAXGEN
for i=1:M
individuals.fitness(i) = fitness(individuals.chrom(i,:)); % 抗体与抗原亲和度(适应度值)计算
individuals.concentration(i) = concentration(i,M,individuals); % 抗体浓度计算
end
individuals.excellence = excellence(individuals,M,ps);
[best,index] = min(individuals.fitness); % 找出最优适应度
bestchrom = individuals.chrom(index,:); % 找出最优个体
average = mean(individuals.fitness); % 计算平均适应度
trace = [trace;best,average]; % 记录
bestindividuals = bestselect(individuals,M,overbest); % 更新记忆库
individuals = bestselect(individuals,M,sizepop); % 形成父代群
individuals = Select(individuals,sizepop); % 选择
individuals.chrom = Cross(pcross,individuals.chrom,sizepop,length); % 交叉
individuals.chrom = Mutation(pmutation,individuals.chrom,sizepop,length); % 变异
individuals = incorporate(individuals,sizepop,bestindividuals,overbest);
end
figure(1)
plot(trace(:,1));
hold on
% plot(trace(:,2),'--');
legend('最优适应度值','平均适应度值')
title('遗传算法收敛曲线','fontsize',12)
xlabel('迭代次数','fontsize',12)
ylabel('适应度值','fontsize',12)
%城市坐标
city_coordinate=[116.405285,39.90498901;
121.472644,31.23170602;
113.280637,23.1251703;
120.153576,30.2874504;
104.065735,30.6594605;
112.982279,28.194006;
114.298572,30.58435507;
114.085947,22.54708;
114.177314,22.2664109;
113.549134,22.19875010;
117.000923,36.67580011;
114.502461,38.045474012;
125.3245,43.88684013;
126.642464,45.756967014;
123.429096,41.796767015;
111.670801,40.818311016;
87.617733,43.792818017;
103.823557,36.058039018;
106.278179,38.46637019;
112.549248,37.857014020;
108.550522,34.1501021;
113.665412,34.75797022;
117.283042,31.86119023;
118.767413,32.041544024;
119.306239,26.075302025;
115.892151,28.676493026;
108.320004,22.82402027;
102.712251,25.040609028;
101.778916,36.623178029;
117.190182,39.125596030;
106.504962,29.533155031;
121.549792,29.868388032;
118.11022,24.490474033;
109.508268,18.247872034;
110.33119,20.031971035;
113.552724,22.255899036;
106.713478,26.578343037;
121.618622,38.91459038;
120.369557,36.094406039]
carge=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
for i=1:39
distance(i,:)=dist(city_coordinate(i,:),city_coordinate(bestchrom,:)');
end
[a,b]=min(distance');
index=cell(1,length);
for i=1:length
index{i}=find(b==i);
end
figure(1+1)
title('最优规划派送路线')
cargox=city_coordinate(bestchrom,1);
cargoy=city_coordinate(bestchrom,2);
plot(cargox,cargoy,'rs','LineWidth',2,...
'MarkerEdgeColor','r',...
'MarkerFaceColor','w',...
'MarkerSize',20)
hold on
plot(city_coordinate(:,1),city_coordinate(:,2),'o','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
for i=1:39
x=[city_coordinate(i,1),city_coordinate(bestchrom(b(i)),1)];
y=[city_coordinate(i,2),city_coordinate(bestchrom(b(i)),2)];
text(x,y,num2str(i))
plot(x,y,'b');
hold on
end
text()
需要完整代码可以加博主qq:1176688637
实现结果:
tips:标识出城市代号
text(x,y,num2str(i))