|
4-ACO算法(蚁群算法)的栅格路径寻优计算
链接:https://pan.baidu.com/s/1nUCFCm1601EU94oKq_hWbw 密码:8rmq
具体链接在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; % 终止节点
- %% ACO 参数
- maxiter = 30; % 迭代次数
- sizepop = 10; % 种群数量
- popmin1 = 1; popmax1 = 51; % x1
- popmin2 = 1; popmax2 = 51; % x2
- Rou = 1.0; % 信息素增量强度
- P0 = 0.1; % 转移概率
- % 初始化种群
- for i=1:sizepop
- x1 = round( popmin1 + (popmax1-popmin1)*rand );
- x2 = round( popmin2 + (popmax2-popmin2)*rand );
- pop(i,1) = x1;
- pop(i,2) = x2;
- [path{i}, fitness(i)] = fun_dijstra( [startpoint, pop(i,:), endpoint], A, dij );
- end
- % 记录一组最优值
- [bestfitness,bestindex]=min(fitness);
- zbest.pop=pop(bestindex,:); % 全局最佳
- zbest.path=path{bestindex}; % 全局最佳对应的最优路径
- gbest=pop; % 个体最佳
- fitnessgbest=fitness; % 个体最佳适应度值
- fitnesszbest=bestfitness; % 全局最佳适应度值
- %% 迭代寻优
- for i=1:maxiter
- lamda = 1/i; % 信息素挥发因子
- [bestfit,flag] =min(fitness);
- for j=1:sizepop
- Pt(j) = (fitness(j)-bestfit)./bestfit;
- end
- for j=1:sizepop
- % 转移概率值
- if Pt(j)<P0
- pop(j,:) = round( pop(j,:) + (2*rand-1)*lamda/2 );
- else
- pop(j,1) = round( pop(j,1) + (popmax1-popmin1)*(rand-0.5) );
- pop(j,2) = round( pop(j,2) + (popmax2-popmin2)*(rand-0.5) );
- end
-
- % 越界处理
- if pop(j,1)>popmax1
- pop(j,1)=popmax1;
- end
- if pop(j,1)<popmin1
- pop(j,1)=popmin1;
- end
- if pop(j,2)>popmax2
- pop(j,2)=popmax2;
- end
- if pop(j,2)<popmin2
- pop(j,2)=popmin2;
- end
-
- % 适应度值
- [path1, fitness1] = fun_dijstra( [startpoint, pop(j,:), endpoint], A, dij );
- [path2, fitness2] = fun_dijstra( [startpoint, gbest(j,:), endpoint], A, dij );
-
- if fitness1<fitness2
- gbest(j,:) = pop(j,:);
- else
- pop(j,:) = gbest(j,:);
- end
-
- [path{j}, fitness3] = fun_dijstra( [startpoint, pop(j,:), endpoint], A, dij );
- fitness(j) = (1-Rou)*fitness(j) + fitness3;
-
- if fitness(j) < fitnesszbest
- fitnesszbest = fitness(j);
- fitnessgbest(j) = fitness3;
-
- zbest.pop = pop(j,:);
- zbest.path = path{j};
- end
- end
- fitness_iter(i) = fitnesszbest;
- end
- %% 结果显示
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- grid on
- xlabel('迭代次数');
- ylabel('适应度函数值')
- fprintf('最优个体')
- zbest
- %% 绘图
- 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
复制代码 适应度函数:- function [path, fitness] = fun_dijstra( pop, A, dij )
- path =[];
- for i=1:length(pop)-1
- % path1 = find_path2(pop(i), pop(i+1), A); % 找路径
- path1 = dijkstra(pop(i), pop(i+1), dij); % 找路径
- path = [path,path1];
- end
- % 删除重复的节点
- index=[];
- for i=1:length(path)-1
- if(path(i)==path(i+1))
- index=[index,i];
- end
- end
- path(index)=[];
- fitness = ca_tsp(path,dij);
复制代码
参考:
【1】基于穷举法的机器人避障路径寻优(免费)
【2】智能车辆局部避障路径规划及横向运动控制研究_陈东
【3】3-PSO算法(粒子群算法)的栅格路径寻优计算
|
|