|
1-BFO算法(细菌觅食算法)的栅格路径寻优计算
链接:https://pan.baidu.com/s/1LHSw6LmdT9moI6xY83EzOQ 密码:q954
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
主程序如下:
- clc,clear,close all
- warning off
- %% MAP
- load('data2.mat');
- K = 4; % 与节点自身相连的节点数
- [x,y,A,dij, citynum,a,b] = init_map(data2,K);
- %% 目标
- startpoint=39; % 起始节点
- endpoint=18; % 终止节点
- %% BFO优化算法 参数
- % maxiter = 100; % 最大迭代次数
- sizepop = 20; % 种群数量
- Nc = 20; % 趋化次数
- Ns = 4; % 游动次数
- C(:,1) = 1*ones(sizepop,1); % 翻转选定方向后,单个细菌前进的步长
- Nre = 2; % 复制次数
- Ned = 2; % 驱散(迁移)次数
- Sr = ceil( sizepop/2 ); % 复制(分裂)次数
- Ped = 0.25; % 细菌驱散(迁移)概率
- d_attract = 0.05; % 吸引剂的数量
- w_attract = 0.05; % 吸引剂的释放速度
- h_repellant = 0.05; % 排斥剂的数量
- w_repellant = 0.05; % 排斥剂的释放速度
- ww = 0.0; % 适应度增量因子
- nvar = 3; % 3个未知量 --- 起始节点到目标节点之间找3个点出来
- popmin1 = 1; popmax1 = 51; % x1
- popmin2 = 1; popmax2 = 51; % x2
- popmin3 = 1; popmax3 = 51; % x3
- Cmin = -5; % 最小步长
- Cmax = 5; % 最大步长
- %% 初始化种群
- for i=1:sizepop
- x1 = round(popmin1 + (popmax1-popmin1)*rand);
- x2 = round(popmin2 + (popmax2-popmin2)*rand);
- x3 = round(popmin3 + (popmax3-popmin3)*rand);
-
- pop(i,1) = x1; % 初始化个体
- pop(i,2) = x2; % 初始化个体
- pop(i,3) = x3; % 初始化个体
-
- [path{i}, fitness(i)] = fun_dijstra([startpoint, x1,x2, x3, endpoint], A, dij ); % 适应度值
- C(i,1) = Cmin + (Cmax-Cmin)*rand; % 步长
- end
- clear x1 x2 x3
- %% 记录一组最优值
- [bestfitness,bestindex]=min(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- fitnesszbest=bestfitness; % 全局最佳适应度值
- NcSizepop = 0; % 记录最优适应度值(函数值)
- zbestpath = path{bestindex}; % 最优路径
- %% 迭代寻优
- for i = 1:Ned % 驱散(迁移)次数
- for k = 1:Nre % 复制次数
-
- for m = 1:Nc % 趋化次数
- for j=1:sizepop % 种群
- % Jcc计算
- Jcc = sum( -d_attract*exp( w_attract*sum(pop(j,1)-pop(:,1).^2) ) + ...
- h_repellant*exp( w_repellant*sum(pop(j,2)-pop(:,2).^2) ));
-
- poplast = pop(j,:); % 当前的种群个体
- fitness(j) = fitness(j) + ww*Jcc;
- Jlast = fitness(j); % 当前适应度值
-
- % 翻转
- delta = 15*rand(1,nvar)-0.5;
- pop(j,:) = round( pop(j,:) + C(j,:).*delta./(sqrt( delta*delta' )) );
-
- % x1
- if pop(j,1)>popmax1
- pop(j,1)=popmax1;
- end
- if pop(j,1)<popmin1
- pop(j,1)=popmin1;
- end
- % x2
- if pop(j,2)>popmax2
- pop(j,2)=popmax2;
- end
- if pop(j,2)<popmin2
- pop(j,2)=popmin2;
- end
- % x3
- if pop(j,3)>popmax3
- pop(j,3)=popmax3;
- end
- if pop(j,3)<popmin3
- pop(j,3)=popmin3;
- end
-
- % 更新当前适应度值?
- [path{j}, fitness(j)] = fun_dijstra([startpoint, pop(j,:), endpoint], A, dij ); % 适应度值
-
- % 游动
- n=0;
- while(n<Ns) % 游动次数
- if fitness(j)<Jlast
- Jlast = fitness(j);
- poplast = pop(j,:);
- else % 不更新适应度值
- n=Ns;
- end
- end
- % 适应度更新
- % 比较 个体间比较
- if Jlast<bestfitness
- bestfitness = Jlast;
- % zbest = pop(j,:);
- zbest = poplast;
- zbestpath = path{j}; % 最优解对应的最优路径
- end
- end % sizepop 种群数量
-
- % 记录最优适应度值
- NcSizepop = NcSizepop+1;
- fitness_iter(NcSizepop) = bestfitness;
- end % Nc 趋化次数
-
- % 复制操作
- [maxF, index] = sort(fitness,'descend'); % 降序排列
- for Nre2 = 1:Sr % 将最大适应度值的Sr个种群,进行更新
- pop(index(Nre2),1) = round(popmin1 + (popmax1-popmin1)*rand);
- pop(index(Nre2),2) = round(popmin2 + (popmax2-popmin2)*rand);
- pop(index(Nre2),3) = round(popmin3 + (popmax2-popmin3)*rand);
- [path{index(Nre2)}, fitness(index(Nre2))] = fun_dijstra([startpoint, pop(index(Nre2),:), endpoint], A, dij );
- C(index(Nre2),1) = Cmin + (Cmax-Cmin)*rand; % 步长
- % 比较 个体间比较
- if fitness(index(Nre2))<bestfitness
- bestfitness = fitness(index(Nre2));
- zbest = pop(index(Nre2),:);
- zbestpath = path{index(Nre2)}; % 最优解对应的最优路径
- end
- end
- end % Nre 复制操作
-
- for j=1:sizepop % 种群
- if Ped>rand
- pop(j,1) = round(popmin1 + (popmax1-popmin1)*rand);
- pop(j,2) = round(popmin2 + (popmax2-popmin2)*rand);
- pop(j,3) = round(popmin3 + (popmax2-popmin3)*rand);
- [path{j}, fitness(j)] = fun_dijstra([startpoint, pop(j,:), endpoint], A, dij );
- % 比较 个体间比较
- if fitness(j)<bestfitness
- bestfitness = fitness(j);
- zbest = pop(j,:);
- zbestpath = path{j}; % 最优解对应的最优路径
- end
- end
- end
-
- end % Ned 驱散(迁移)次数
- disp('最优解')
- disp(zbest)
- fprintf('\n')
- disp('最优解对应的最优路径')
- disp(zbestpath)
- fprintf('\n')
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- % loglog(fitness_iter,'ro-','linewidth',2)
- axis tight
- grid on
- %% 绘图
- zbest1 = zbest;
- zbest.pop = zbest1;
- zbest.path = zbestpath;
- figure(3)
- colormap([0 0 0;1 1 1]),pcolor(0.5:size(a,2)+0.5,0.5:size(a,1)+0.5,b)
- hold on
- % 节点网络结构初始化
- for i=1:citynum
- plot(x(i)+0.5,y(i)+0.5,'ro','MarkerEdgeColor','r','MarkerFaceColor','g','markersize',8);
- hold on;
- text(x(i)+0.5,y(i)+0.5+0.2,num2str(i),'Color',[1 0 0]);
- end
- % 连线
- for i=1:length(zbest.path)-1
- plot([x(zbest.path(i))+0.5,x(zbest.path(i+1))+0.5],[y(zbest.path(i))+0.5,y(zbest.path(i+1))+0.5],'b-','MarkerEdgeColor','r','MarkerFaceColor','g','markersize',8,'linewidth',2);
- end
- axis tight;
- axis off;
- hold off
复制代码
参考:
【1】基于穷举法的机器人避障路径寻优(免费)
【2】智能车辆局部避障路径规划及横向运动控制研究_陈东
【3】3-PSO算法(粒子群算法)的栅格路径寻优计算
|
|