|
7-BFO细菌觅食算法的障碍路径寻优Path Planning-三角形障碍
链接:https://pan.baidu.com/s/1JlaGt5Ovh_m3FNoVsbOJCg 提取码:ybe9
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
主程序如下:
- clc,clear,close all
- warning off
- format longG
- model = CreateModel();
- % BFO优化算法 参数
- sizepop = 50; % 种群数量
- Nc = 20; % 趋化次数
- Ns = 4; % 游动次数
- Nre = 4; % 复制次数
- Ned = 2; % 驱散(迁移)次数
- Sr = ceil( sizepop/2 ); % 复制(分裂)次数
- Ped = 0.25; % 细菌驱散(迁移)概率
- nvar = model.n; % n个未知量
- popmin_x = model.xmin; % x
- popmax_x = model.xmax; % x
- popmin_y = model.ymin; % y
- popmax_y = model.ymax; % y
- %% 初始化种群
- fitnesszbest = inf;
- for i=1:sizepop
- pop(i).position = CreateRandomSolution(model); % 初始化个体
- [pop(i).fitness , pop(i).sol]= fun( pop(i).position, model ); % 适应度值
- % 更新个体最优
- pop(i).Best.position = pop(i).position;
- pop(i).Best.fitness = pop(i).fitness;
- pop(i).Best.sol = pop(i).sol;
- if(pop(i).Best.fitness<fitnesszbest)
- fitnesszbest = pop(i).Best.fitness;
- zbest = pop(i).Best;
- end
- end
- %% 记录一组最优值
- NcSizepop = 0; % 记录最优适应度值(函数值)
- %% 迭代寻优
- for i = 1:Ned % 驱散(迁移)次数
- for k = 1:Nre % 复制次数
-
- for m = 1:Nc % 趋化次数
- for j=1:sizepop % 种群
-
- % 翻转x
- delta = 2*rand(nvar, 1)-0.5;
- PHI = delta./(sqrt( delta'*delta ));
- C = 0.5*rand(nvar, 1);
- popnew.position.x = pop(j).position.x + C'.*PHI';
- % 取值范围约束
- popnew.position.x = max( popnew.position.x, popmin_x );
- popnew.position.x = min( popnew.position.x, popmax_x );
-
- % 翻转y
- delta = 2*rand(nvar, 1)-0.5;
- PHI = delta./(sqrt( delta'*delta ));
- C = 0.5*rand(nvar, 1);
- popnew.position.y = pop(j).position.y + C'.*PHI';
- % 取值范围约束
- popnew.position.y = max( popnew.position.y, popmin_y );
- popnew.position.y = min( popnew.position.y, popmax_y );
-
- % 更新当前适应度值?
- [popnew.fitness , popnew.sol]= fun( popnew.position, model ); % 适应度值
-
- if popnew.fitness<pop(j).fitness
- pop(j).fitness = popnew.fitness;
- pop(j).position = popnew.position;
- pop(j).sol = popnew.sol;
- end
- if pop(j).fitness<pop(j).Best.fitness
- pop(j).Best.fitness = pop(j).fitness;
- pop(j).Best.position = pop(j).position;
- pop(j).Best.sol = pop(j).sol;
- end
- if(pop(j).Best.fitness<fitnesszbest)
- fitnesszbest = pop(j).Best.fitness;
- zbest = pop(j).Best;
- end
-
- end % sizepop 种群数量
-
- % 记录最优适应度值
- NcSizepop = NcSizepop+1;
- fitness_iter(NcSizepop) = fitnesszbest;
-
- figure(1)
- PlotSolution(zbest.sol, model)
- pause(0.1)
- end % Nc 趋化次数
-
- % 复制操作
- [maxF,index] = sort([pop.fitness],'descend'); % 降序排列
- for Nre2 = 1:Sr % 将最大适应度值的Sr个种群,进行更新
- pop(index(Nre2)).position = CreateRandomSolution(model); % 初始化个体
- [pop(index(Nre2)).fitness , pop(index(Nre2)).sol]= fun( pop(index(Nre2)).position, model ); % 适应度值
- % 比较 个体间比较
- if pop(index(Nre2)).fitness<fitnesszbest
- fitnesszbest = pop(index(Nre2)).fitness;
- zbest = pop(index(Nre2)).Best;
- end
- end
- end % Nre 复制操作
-
- for j=1:sizepop % 种群
- if Ped>rand
- pop(j).position = CreateRandomSolution(model); % 初始化个体
- [pop(j).fitness , pop(j).sol]= fun( pop(j).position, model ); % 适应度值
- % 比较 个体间比较
- if pop(j).fitness<fitnesszbest
- fitnesszbest = pop(j).fitness;
- zbest = pop(j).Best;
- end
- end
- end
-
- end % Ned 驱散(迁移)次数
- disp('最优解')
- disp(zbest)
- fprintf('\n')
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- xlabel('迭代次数'); ylabel('适应度曲线');
- axis tight
- grid on
- figure(1)
- PlotSolution(zbest.sol, model)
- pause(0.1)
复制代码 参考:
【1】结果动态图
【2】子函数参考链接
【3】细菌觅食算法BFO函数优化
|
|