当前位置:   article > 正文

【蚁群路径规划】基于MATLAB的蚁群算法的二维路径规划_基于蚁群算法的二维路径规划算法matlab代码

基于蚁群算法的二维路径规划算法matlab代码
  1. %% 清空环境
  2. clc;clear
  3. %% 障碍物数据
  4. position = load('barrier.txt');
  5. plot([0,200],[0,200],'.');
  6. hold on
  7. B = load('barrier.txt');
  8. xlabel('km','fontsize',12)
  9. ylabel('km','fontsize',12)
  10. title('二维规划空间','fontsize',12)
  11. %% 描述起点和终点
  12. S = [20,180];
  13. T = [160,90];
  14. plot([S(1),T(1)],[S(2),T(2)],'.');
  15. % 图形标注
  16. text(S(1)+2,S(2),'S');
  17. text(T(1)+2,T(2),'T');
  18. %% 描绘障碍物图形
  19. fill(position(1:4,1),position(1:4,2),[0,0,0]);
  20. fill(position(5:8,1),position(5:8,2),[0,0,0]);
  21. fill(position(9:12,1),position(9:12,2),[0,0,0]);
  22. fill(position(13:15,1),position(13:15,2),[0,0,0]);
  23. % 下载链路端点数据
  24. L = load('lines.txt');
  25. %% 描绘线及中点
  26. v = zeros(size(L));
  27. for i=1:20
  28. plot([position(L(i,1),1),position(L(i,2),1)],[position(L(i,1),2)...
  29. ,position(L(i,2),2)],'color','black','LineStyle','--');
  30. v(i,:) = (position(L(i,1),:)+position(L(i,2),:))/2;
  31. plot(v(i,1),v(i,2),'*');
  32. text(v(i,1)+2,v(i,2),strcat('v',num2str(i)));
  33. end
  34. %% 描绘可行路径
  35. sign = load('matrix.txt');
  36. [n,m]=size(sign);
  37. for i=1:n
  38. if i == 1
  39. for k=1:m-1
  40. if sign(i,k) == 1
  41. plot([S(1),v(k-1,1)],[S(2),v(k-1,2)],'color',...
  42. 'black','Linewidth',2,'LineStyle','-');
  43. end
  44. end
  45. continue;
  46. end
  47. for j=2:i
  48. if i == m
  49. if sign(i,j) == 1
  50. plot([T(1),v(j-1,1)],[T(2),v(j-1,2)],'color',...
  51. 'black','Linewidth',2,'LineStyle','-');
  52. end
  53. else
  54. if sign(i,j) == 1
  55. plot([v(i-1,1),v(j-1,1)],[v(i-1,2),v(j-1,2)],...
  56. 'color','black','Linewidth',2,'LineStyle','-');
  57. end
  58. end
  59. end
  60. end
  61. path = DijkstraPlan(position,sign);
  62. j = path(22);
  63. plot([T(1),v(j-1,1)],[T(2),v(j-1,2)],'color','yellow','LineWidth',3,'LineStyle','-.');
  64. i = path(22);
  65. j = path(i);
  66. count = 0;
  67. while true
  68. plot([v(i-1,1),v(j-1,1)],[v(i-1,2),v(j-1,2)],'color','yellow','LineWidth',3,'LineStyle','-.');
  69. count = count + 1;
  70. i = j;
  71. j = path(i);
  72. if i == 1 || j==1
  73. break;
  74. end
  75. end
  76. plot([S(1),v(i-1,1)],[S(2),v(i-1,2)],'color','yellow','LineWidth',3,'LineStyle','-.');
  77. count = count+3;
  78. pathtemp(count) = 22;
  79. j = 22;
  80. for i=2:count
  81. pathtemp(count-i+1) = path(j);
  82. j = path(j);
  83. end
  84. path = pathtemp;
  85. % path = [1 9 8 7 13 14 12 22];
  86. %% 蚁群算法参数初始化
  87. pathCount = length(path)-2; %经过线段数量
  88. pheCacuPara=2; %信息素计算参数
  89. pheThres = 0.8; %信息素选择阈值
  90. pheUpPara=[0.1 0.0003]; %信息素更新参数
  91. qfz= zeros(pathCount,10); %启发值
  92. phePara = ones(pathCount,10)*pheUpPara(2); %信息素
  93. qfzPara1 = ones(10,1)*0.5; %启发信息参数
  94. qfzPara2 = 1.1; %启发信息参数
  95. m=10; %种群数量
  96. NC=500; %循环次数
  97. pathk = zeros(pathCount,m); %搜索结果记录
  98. shortestpath = zeros(1,NC); %进化过程记录
  99. %% 初始最短路径
  100. dijpathlen = 0;
  101. vv = zeros(22,2);
  102. vv(1,:) = S;
  103. vv(22,:) = T;
  104. vv(2:21,:) = v;
  105. for i=1:pathCount-1
  106. dijpathlen = dijpathlen + sqrt((vv(path(i),1)-vv(path(i+1),1))^2+(vv(path(i),2)-vv(path(i+1),2))^2);
  107. end
  108. LL = dijpathlen;
  109. %% 经过的链接线
  110. lines = zeros(pathCount,4);
  111. for i = 1:pathCount
  112. lines(i,1:2) = B(L(path(i+1)-1,1),:);
  113. lines(i,3:4) = B(L(path(i+1)-1,2),:);
  114. end
  115. bestPath=zeros(pathCount,1);
  116. %% 循环搜索
  117. for num = 1:NC
  118. %% 蚂蚁迭代寻优一次
  119. for i=1:pathCount
  120. for k=1:m
  121. q = rand();
  122. qfz(i,:) = (qfzPara2-abs((1:10)'/10-qfzPara1))/qfzPara2; %启发信息
  123. if q<=pheThres%选择信息素最大值
  124. arg = phePara(i,:).*(qfz(i,:).^pheCacuPara);
  125. j = find(arg == max(arg));
  126. pathk(i,k) = j(1);
  127. else % 轮盘赌选择
  128. arg = phePara(i,:).*(qfz(i,:).^pheCacuPara);
  129. sumarg = sum(arg);
  130. qq = (q-pheThres)/(1-pheThres);
  131. qtemp = 0;
  132. j = 1;
  133. while qtemp < qq
  134. qtemp = qtemp + (phePara(i,j)*(qfz(i,j)^pheCacuPara))/sumarg;
  135. j=j+1;
  136. end
  137. j=j-1;
  138. pathk(i,k) = j(1);
  139. end
  140. % 信息素更新
  141. phePara(i,j) = (1-pheUpPara(1))*phePara(i,j)+pheUpPara(1)*pheUpPara(2);
  142. end
  143. end
  144. %% 计算路径长度
  145. len = zeros(1,k);
  146. for k=1:m
  147. Pstart = S;
  148. Pend = lines(1,1:2) + (lines(1,3:4)-lines(1,1:2))*pathk(1,k)/10;
  149. for l=1:pathCount
  150. len(1,k) = len(1,k)+sqrt(sum((Pend-Pstart).^2));
  151. Pstart = Pend;
  152. if l<pathCount
  153. Pend = lines(l+1,1:2) + (lines(l+1,3:4)-lines(l+1,1:2))*pathk(l+1,k)/10;
  154. end
  155. end
  156. Pend = T;
  157. len(1,k) = len(1,k)+sqrt(sum((Pend-Pstart).^2));
  158. end
  159. %% 更新信息素
  160. % 寻找最短路径
  161. minlen = min(len);
  162. minlen = minlen(1);
  163. minant = find(len == minlen);
  164. minant = minant(1);
  165. % 更新全局最短路径
  166. if minlen < LL
  167. LL = minlen;
  168. bestPath=pathk(:,minant);
  169. end
  170. % 更新信息素
  171. for i=1:pathCount
  172. phePara(i,pathk(i,minant)) = (1-pheUpPara(1))* phePara(i,pathk(i,minant))+pheUpPara(1)*(1/minlen);
  173. end
  174. shortestpath(num) = minlen;
  175. end
  176. %% 画图
  177. Pstart = S;
  178. Pend = lines(1,1:2) + (lines(1,3:4)-lines(1,1:2))*bestPath(1)/10;
  179. plot([Pstart(1),Pend(1)],[Pstart(2),Pend(2)],'color','blue','LineWidth',3,'LineStyle','-.');
  180. for l=1:pathCount
  181. Pstart = Pend;
  182. if l<pathCount
  183. Pend = lines(l+1,1:2) + (lines(l+1,3:4)-lines(l+1,1:2))*bestPath(l+1)/10;
  184. plot([Pstart(1),Pend(1)],[Pstart(2),Pend(2)],'color','blue','LineWidth',3,'LineStyle','-.');
  185. end
  186. end
  187. Pend = T;
  188. plot([Pstart(1),Pend(1)],[Pstart(2),Pend(2)],'color','blue','LineWidth',3,'LineStyle','-.');
  189. figure;
  190. plot(1:NC,shortestpath,'color','blue');
  191. hold on
  192. % plot(1:NC,djpathlen,'color','red');
  193. ylabel('路径总长度');
  194. xlabel('迭代次数');

 D206

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/477042
推荐阅读
相关标签
  

闽ICP备14008679号