请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 22237|回复: 31

3-PSO算法(粒子群算法)的栅格路径寻优计算

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 2018-4-7 11:58:51 | 显示全部楼层 |阅读模式
3-PSO算法(粒子群算法)的栅格路径寻优计算
链接:https://pan.baidu.com/s/1hEzx9dh3-En7_fWFWKfmTQ 密码:ryqw

具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1

主程序如下:
  1. clc,clear,close all
  2. warning off
  3. %% MAP
  4. load('data2.mat');
  5. K = 4;             % 与节点自身相连的节点数
  6. [x,y,A,dij, citynum,a,b] = init_map(data2,K);
  7. %% 目标
  8. startpoint=39;  % 起始节点
  9. endpoint=18;    % 终止节点
  10. %% PSO
  11. c1 = 1.4995;  
  12. c2 = 1.4995;
  13. Vmin = -1;
  14. Vmax = 1;
  15. maxiter = 20;  % 迭代次数
  16. sizepop = 20;  % 种群数量
  17. popmin1 = 1;  popmax1 = 51; % x1
  18. popmin2 = 1;  popmax2 = 51; % x2
  19. % 初始化种群
  20. for i=1:sizepop
  21.     x1 = round( popmin1 + (popmax1-popmin1)*rand );
  22.     x2 = round( popmin2 + (popmax2-popmin2)*rand );
  23.     pop(i,1) = x1;
  24.     pop(i,2) = x2;
  25.     [path{i}, fitness(i)] = fun_dijstra( [startpoint, pop(i,:), endpoint], A, dij );
  26.     V(i,1) = 0;
  27.     V(i,2) = 0;
  28. end
  29. % 记录一组最优值
  30. [bestfitness,bestindex]=min(fitness);
  31. zbest.pop=pop(bestindex,:);   % 全局最佳
  32. gbest=pop;                % 个体最佳
  33. fitnessgbest=fitness;     % 个体最佳适应度值
  34. fitnesszbest=bestfitness; % 全局最佳适应度值
  35. zbest.path = path{bestindex};   % 最佳个体对应的最优路径
  36. wmax = 0.9;  wmin = 0.4;
  37. %% 迭代寻优
  38. for i=1:maxiter
  39.     for j=1:sizepop
  40.         % 自适应权重1
  41. %         w = wmin + (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( max(fitness)-min(fitness) );
  42.         % 自适应权重2
  43. %         w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
  44.         % 自适应权重3
  45.         if fitnessgbest(j)<=mean(fitness)
  46.             w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
  47.         else
  48.             w = wmax;
  49.         end
  50.         % 速度更新
  51.         V(j,:) = w*V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest.pop - pop(j,:));
  52.         % V--x1
  53.         if V(j,1)>Vmax
  54.             V(j,1)=Vmax;
  55.         end
  56.         if V(j,1)<Vmin
  57.             V(j,1)=Vmin;
  58.         end
  59.         % V--x2
  60.         if V(j,2)>Vmax
  61.             V(j,2)=Vmax;
  62.         end
  63.         if V(j,2)<Vmin
  64.             V(j,2)=Vmin;
  65.         end
  66.         % 个体更新
  67. %         pop(j,:) = pop(j,:) + 0.5 * V(j,:);
  68.         pop(j,1) = round( pop(j,1) + 1.5 * V(j,1) );
  69.         pop(j,2) = round( pop(j,2) + 0.5 * V(j,2) );
  70.         
  71.         % x1  越界限制
  72.         if pop(j,1)>popmax1
  73.             pop(j,1)=popmax1;
  74.         end
  75.         if pop(j,1)<popmin1
  76.             pop(j,1)=popmin1;
  77.         end
  78.         % x2  越界限制
  79.         if pop(j,2)>popmax2
  80.             pop(j,2)=popmax2;
  81.         end
  82.         if pop(j,2)<popmin2
  83.             pop(j,2)=popmin2;
  84.         end
  85.         
  86.         % 适应度更新
  87.         [path{j}, fitness(j)] = fun_dijstra( [startpoint, pop(j,:), endpoint], A, dij );
  88.         
  89.         % 比较  个体间比较
  90.         if fitness(j)<fitnessgbest(j)
  91.             fitnessgbest(j) = fitness(j);
  92.             gbest(j,:) = pop(j,:);
  93.         end
  94.         if fitness(j)<bestfitness
  95.             bestfitness = fitness(j);
  96.             zbest.pop =  pop(j,:);
  97.             zbest.path = path{j};
  98.         end
  99.         
  100.     end
  101.     fitness_iter(i) = bestfitness;
  102.    
  103. end
  104. toc ;
  105. times = toc;
  106. fprintf('\n')
  107. disp(['计算时间 Time =    ',   num2str(times) ])
  108. fprintf('\n')
  109. disp(['最优解   ', num2str(zbest.pop)])
  110. fprintf('\n')
  111. disp(['最优解对应的最优路径   ', num2str(zbest.path)])
  112. fprintf('\n')

  113. figure('color',[1,1,1])
  114. plot(fitness_iter,'ro-','linewidth',2)

  115. %% 绘图
  116. figure(3)
  117. colormap([0 0 0;1 1 1]),pcolor(0.5:size(a,2)+0.5,0.5:size(a,1)+0.5,b)
  118. hold on
  119. % 节点网络结构初始化
  120. for i=1:citynum
  121.     plot(x(i)+0.5,y(i)+0.5,'ro','MarkerEdgeColor','r','MarkerFaceColor','g','markersize',8);
  122.     hold on;
  123.     text(x(i)+0.5,y(i)+0.5+0.2,num2str(i),'Color',[1 0 0]);
  124. end
  125. % 连线
  126. for i=1:length(zbest.path)-1
  127.     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);
  128. end
  129. axis tight;
  130. axis off;
  131. hold off
复制代码
适应度函数如下:
  1. function [path, fitness] = fun_dijstra( pop, A, dij )
  2. path =[];
  3. for i=1:length(pop)-1
  4. %     path1 = find_path2(pop(i), pop(i+1), A);  % 找路径
  5.     path1 = dijkstra(pop(i), pop(i+1), dij);  % 找路径
  6.     path = [path,path1];
  7. end

  8. % 删除重复的节点
  9. index=[];
  10. for i=1:length(path)-1
  11.     if(path(i)==path(i+1))
  12.         index=[index,i];
  13.     end
  14. end
  15. path(index)=[];

  16. fitness = ca_tsp(path,dij);
复制代码
dijkstra最短路算法:
  1. function [r_path, r_cost] = dijkstra(pathS, pathE, transmat)
  2. % The Dijkstra's algorithm, Implemented by Yi Wang, 2005
  3. %   pathS: 所求最短路径的起点
  4. %   pathE :所求最短路径的终点
  5. %   transmat: 图的转移矩阵或者邻接矩阵,应为方阵
  6. if ( size(transmat,1) ~= size(transmat,2) )
  7.   error( 'detect_cycles:Dijkstra_SC', ...
  8.          'transmat has different width and heights' );
  9. end
  10. % 初始化:
  11. %  noOfNode-图中的顶点数
  12. %  parent(i)-节点i的父节点
  13. %  distance(i)-从起点pathS的最短路径的长度
  14. %  queue-图的广度遍历
  15. noOfNode = size(transmat, 1);
  16. for i = 1:noOfNode
  17.   parent(i) = 0;       % 初值操作
  18.   distance(i) = Inf;    % 初值操作
  19. end
  20. queue = [];           % 队列

  21. % 由路径开始最短路计算
  22. for i=1:noOfNode
  23.   if transmat(pathS, i)~=Inf
  24.     distance(i) = transmat(pathS, i);
  25.     parent(i)   = pathS;  % 当前路径
  26.     queue     = [queue i];
  27.   end
  28. end

  29. % 对图进行广度遍历
  30. while length(queue) ~= 0
  31.   hopS  = queue(1);
  32.   queue = queue(2:end);
  33.   
  34.   for hopE = 1:noOfNode
  35.     if distance(hopE) > distance(hopS) + transmat(hopS,hopE)   % 如果当前距离大于转换后的距离
  36.       distance(hopE) = distance(hopS) + transmat(hopS,hopE);  % 更新
  37.       parent(hopE)   = hopS;
  38.       queue         = [queue hopE];
  39.     end
  40.   end
  41.   
  42. end

  43. % 回溯进行最短路径的查找
  44. r_path = [pathE];   
  45. i = parent(pathE);

  46. while i~=pathS && i~=0
  47.   r_path = [i r_path];
  48.   i      = parent(i);
  49. end
  50. if i==pathS
  51.   r_path = [i r_path];   % 记录
  52. else
  53.   r_path = []          % 清空
  54. end
  55. % 返回最短路径的权和
  56. r_cost = distance(pathE);
复制代码
距离计算:
  1. % ca_tsp.m
  2. % 计算路径长度
  3. function ltsp=ca_tsp(c,dij)
  4.     i=1;
  5.     ltsp=0;
  6.     while i<length(c);
  7.         ltsp=ltsp+dij(c(i),c(i+1));
  8.         i=i+1;
  9.     end
  10. end
复制代码



参考:
【1】基于穷举法的机器人避障路径寻优(免费)
【2】智能车辆局部避障路径规划及横向运动控制研究_陈东



算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

0

主题

22

帖子

1

金钱

新手上路

Rank: 1

积分
23
发表于 2018-6-23 11:29:08 | 显示全部楼层
PSO算法(粒子群算法)的栅格路径寻优计算
回复 支持 反对

使用道具 举报

0

主题

6

帖子

10

金钱

新手上路

Rank: 1

积分
16
发表于 2018-9-4 09:58:39 | 显示全部楼层
楼主好人。。。。
回复

使用道具 举报

0

主题

6

帖子

10

金钱

新手上路

Rank: 1

积分
16
发表于 2018-9-4 10:49:58 | 显示全部楼层
值得学习。。。。。。
回复

使用道具 举报

0

主题

6

帖子

16

金钱

新手上路

Rank: 1

积分
22
发表于 2018-10-22 10:07:54 | 显示全部楼层
认真学习代码!
回复 支持 反对

使用道具 举报

0

主题

6

帖子

16

金钱

新手上路

Rank: 1

积分
22
发表于 2018-10-22 10:08:15 | 显示全部楼层
认真学习代码,努力毕业,加油。
回复 支持 反对

使用道具 举报

0

主题

7

帖子

16

金钱

新手上路

Rank: 1

积分
23
发表于 2018-11-29 12:23:32 | 显示全部楼层
认真学习一下
回复 支持 反对

使用道具 举报

0

主题

20

帖子

1

金钱

新手上路

Rank: 1

积分
21
发表于 2018-12-4 21:14:09 | 显示全部楼层
PSO算法(粒子群算法)
回复 支持 反对

使用道具 举报

0

主题

15

帖子

32

金钱

新手上路

Rank: 1

积分
47
发表于 2019-2-10 20:35:26 | 显示全部楼层
谢谢楼主。。。。。。
回复

使用道具 举报

0

主题

15

帖子

32

金钱

新手上路

Rank: 1

积分
47
发表于 2019-2-10 20:44:41 | 显示全部楼层
顶一下,新手刚开始学。。。。。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-28 23:53 , Processed in 0.225925 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表