数学建模社区-数学中国

标题: 基于MATLAB的标准粒群优化算法程序 [打印本页]

作者: sea_star666    时间: 2010-6-18 12:34
标题: 基于MATLAB的标准粒群优化算法程序
  1. %标准粒群优化算法[b][color=rgb(50, 70, 146)]程序[/color][/b]
  2. %测试函数:f(x,y)=100(x^2-y)^2+(1-x)^2,-2.048<x,y<2.048
  3. %求解函数最小值


  4. %种群规模
  5. %global popnum;      %种群数量
  6. global pop;         %种群
  7. %global c0;          %速度惯性系数,为0—1的随机数
  8. global c1;          %个体最优导向系数
  9. global c2;          %全局最优导向系数
  10. global gbest_x;       %全局最优解x轴坐标
  11. global gbest_y;       %全局最优解y轴坐标
  12. global best_fitness;    %最优解
  13. global best_in_history; %最优解变化轨迹
  14. global x_min;           %x的下限
  15. global x_max;           %x的上限
  16. global y_min;           %y的下限
  17. global y_max;           %y的上限
  18. globalgen;             %迭代次数
  19. global exetime;         %当前迭代次数
  20. global max_velocity;    %最大速度
  21. initial;       %初始化
  22. for exetime=1:gen
  23.     outputdata;     %实时输出结果
  24.     adapting;       %计算适应值
  25.     errorcompute(); %计算当前种群适值标准差
  26.     updatepop;      %更新粒子位置
  27.     pause(0.01);
  28. end

  29. clear i;
  30. clear exetime;
  31. clear x_max;
  32. clear x_min;
  33. clear y_min;
  34. clear y_max;
  35. %适值计算
  36. % 测试函数为f(x,y)=100(x^2-y)^2+(1-x)^2,-2.048<x,y<2.048
  37. %计算适应值并赋值
  38. for i=1:popsize
  39.     pop(i,8)=100*(pop(i,1)^2-pop(i,2))^2+(1-pop(i,1))^2;
  40.     if pop(i,7)>pop(i,8)    %若当前适应值优于个体最优值,则进行个体最优信息的更新
  41.        pop(i,7)=pop(i,8);          %适值更新
  42.        pop(i,5:6)=pop(i,1:2);      %位置坐标更新
  43.     end
  44. end
  45. %计算完适应值后寻找当前全局最优位置并记录其坐标
  46. if best_fitness>min(pop(:,7))
  47.     best_fitness=min(pop(:,7));     %全局最优值
  48.    gbest_x=pop(find(pop(:,7)==min(pop(:,7))),1);    %全局最优粒子的位置   
  49.     gbest_y=pop(find(pop(:,7)==min(pop(:,7))),2);
  50. end
  51. best_in_history(exetime)=best_fitness;%记录当前全局最优
  52. %[b][color=rgb(50, 70, 146)]程序[/color][/b]初始化
  53. gen=100;    %设置进化代数
  54. popsize=30;     %设置种群规模大小
  55. best_in_history(gen)=inf;   %初始化全局历史最优解
  56. best_in_history(:)=inf;   %初始化全局历史最优解
  57. max_velocity=0.3;       %最大速度限制
  58. best_fitness=inf;
  59. %popnum=1;       %设置种群数量
  60. pop(popsize,8)=0;   %初始化种群,创建popsize行8列的0矩阵
  61. %种群数组第1列为x轴坐标,第2列为y轴坐标,第3列为x轴速度分量,第4列为y轴速度分量
  62. %第5列为个体最优位置的x轴坐标,第6列为个体最优位置的y轴坐标
  63. %第7列为个体最优适值,第8列为当前个体适应值
  64. for i=1:popsize
  65.     pop(i,1)=4*rand()-2;     %初始化种群中的粒子位置,值为-2—2,步长为其速度
  66.     pop(i,2)=4*rand()-2;     %初始化种群中的粒子位置,值为-2—2,步长为其速度
  67.     pop(i,5)=pop(i,1); %初始状态下个体最优值等于初始位置
  68.     pop(i,6)=pop(i,2); %初始状态下个体最优值等于初始位置
  69.     pop(i,3)=rand()*0.02-0.01;    %初始化种群微粒速度,值为-0.01—0.01,间隔为0.0001
  70.     pop(i,4)=rand()*0.02-0.01;    %初始化种群微粒速度,值为-0.01—0.01,间隔为0.0001
  71.     pop(i,7)=inf;
  72.     pop(i,8)=inf;
  73. end
  74. c1=2;
  75. c2=2;
  76. x_min=-2;
  77. y_min=-2;
  78. x_max=2;
  79. y_max=2;
  80. gbest_x=pop(1,1);   %全局最优初始值为种群第一个粒子的位置
  81. gbest_y=pop(1,2);
  82. %实时输出结果
  83. %输出当前种群中粒子位置
  84. subplot(1,2,1);
  85. for i=1:popsize
  86.     plot(pop(i,1),pop(i,2),'b*');
  87.     hold on;
  88. end
  89. plot(gbest_x,gbest_y,'r.','markersize',20);axis([-2,2,-2,2]);
  90. hold off;
  91. subplot(1,2,2);
  92. axis([0,gen,-0.00005,0.00005]);
  93. if exetime-1>0
  94.    line([exetime-1,exetime],[best_in_history(exetime-1),best_fitness]);hold on;
  95. end
  96. %粒子群速度与位置更新

  97. %更新粒子速度
  98. for i=1:popsize
  99.    pop(i,3)=rand()*pop(i,3)+c1*rand()*(pop(i,5)-pop(i,1))+c2*rand()*(gbest_x-pop(i,1));   %更新速度
  100.    pop(i,4)=rand()*pop(i,4)+c1*rand()*(pop(i,6)-pop(i,2))+c2*rand()*(gbest_x-pop(i,2));
  101.     if abs(pop(i,3))>max_velocity
  102.         if pop(i,3)>0
  103.            pop(i,3)=max_velocity;
  104.         else
  105.            pop(i,3)=-max_velocity;
  106.         end
  107.     end
  108.     if abs(pop(i,4))>max_velocity
  109.         if pop(i,4)>0
  110.            pop(i,4)=max_velocity;
  111.         else
  112.            pop(i,4)=-max_velocity;
  113.         end
  114.     end
  115. end
  116. %更新粒子位置
  117. for i=1:popsize
  118.     pop(i,1)=pop(i,1)+pop(i,3);
  119.     pop(i,2)=pop(i,2)+pop(i,4);
  120. end
  121. [align=left] [/align]
复制代码


作者: linmatsas    时间: 2010-6-18 12:39
这个这么好……谢谢LZ分享啊~~~~~
作者: hrf5436    时间: 2010-6-26 18:46
谢谢了  很好 收下了~~~~
作者: mathcyang    时间: 2010-7-11 18:23
楼主解释一下




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5