粒子群算法简单实现

快来打我* 2022-03-07 01:22 345阅读 0赞

PSO算法相关定义

PSO中的每个粒子P包含两个向量: x, v

o位置向量x:粒子在解空间中的当前位置: x[x1, x2, …, xdim]

o速度向量v:粒子在解空间中的飞行速度: v[v1, v2, …, vdim]

pBest :粒子自身的历史最优位置

gBest :群体全局最优向量

粒子速度与位置更新:

P.v=omega*P.v+c1*r1*(P.pBest-P.x)+c2*r2*(gBest-P.x);

P.x=P.x+P.v;

其中omega为惯性权重,c1,c2为学习因子,r1,r2为[0, 1]上的随机数

算法流程:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0RYSDkyNA_size_16_color_FFFFFF_t_70watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0RYSDkyNA_size_16_color_FFFFFF_t_70 1

实例测试:

  1. // 测试实例1
  2. // f=x1*x1+x2*x2+x3*x3, 求fmin
  3. // N=40, G=500
  4. // -10<=x<=-4
  5. #include<cstdio>
  6. #include<algorithm>
  7. #include<vector>
  8. #include<cstdlib>
  9. #include<ctime>
  10. #include<cmath>
  11. using namespace std;
  12. const int dim=3;
  13. const int N=40;
  14. const int G=500;
  15. const int low=-10, high=-4, wid=high-low;;
  16. const double INF=99999.9;
  17. double omega=0.729;
  18. double c1=1.49445, c2=c1;
  19. vector<double> gBest;
  20. // 定义粒子
  21. struct pp{
  22. vector<double> x;
  23. vector<double> v;
  24. vector<double> pBest;
  25. double value;
  26. }P[N];
  27. // 比较函数
  28. bool cmp(struct pp A, struct pp B) {
  29. return A.value<B.value;
  30. }
  31. int random() {
  32. int temp=rand()%(wid+1);
  33. return low+temp;
  34. }
  35. // 目标函数(适应度函数)
  36. double f(struct pp &P) {
  37. double vl=0;
  38. for (int i=0; i<dim; i++)
  39. vl+=P.x[i]*P.x[i];
  40. return vl;
  41. }
  42. // 更新操作
  43. void update(struct pp &P) {
  44. double r1=rand()/double(RAND_MAX);
  45. double r2=rand()/double(RAND_MAX);
  46. //P.v=omiga*P.v+c1*r1*(P.pBest-P.x)+c2*r2*(gBest-P.x);
  47. for (int i=0; i<dim; i++)
  48. P.v[i]=omega*P.v[i]+c1*r1*(P.pBest[i]-P.x[i])+c2*r2*(gBest[i]-P.x[i]);
  49. //P.x=P.x+P.v;
  50. for (int i=0; i<dim; i++) {
  51. P.x[i]=P.x[i]+P.v[i];
  52. if (P.x[i]>high)
  53. P.x[i]=high;
  54. else if (P.x[i]<low)
  55. P.x[i]=low;
  56. }
  57. }
  58. // 初始化
  59. void Init() {
  60. srand((unsigned)time(NULL));
  61. for (int i=0; i<N; i++) {
  62. for (int j=0; j<dim; j++) {
  63. P[i].v.push_back(random());
  64. P[i].x.push_back(random());
  65. }
  66. P[i].pBest=P[i].x;
  67. P[i].value=INF;
  68. P[i].value=min(f(P[i]), P[i].value);
  69. }
  70. sort(P, P+N, cmp);
  71. gBest=P[0].pBest;
  72. }
  73. // 运行
  74. void Run() {
  75. for (int i=0; i<G; i++) {
  76. for (int j=0; j<N; j++) {
  77. update(P[j]);
  78. double temp=f(P[j]);
  79. if (temp<P[j].value) {
  80. P[j].pBest=P[j].x;
  81. P[j].value=temp;
  82. }
  83. if (temp<P[0].value)
  84. gBest=P[j].pBest;
  85. }
  86. sort(P, P+N, cmp);
  87. }
  88. for (int i=0; i<dim; i++)
  89. printf("x%d = %.2f\n", i+1, gBest[i]);
  90. }
  91. // 测试
  92. int main() {
  93. Init();
  94. Run();
  95. return 0;
  96. }
  97. /**********输出**********/
  98. x1 = -4.00
  99. x2 = -4.00
  100. x3 = -4.00
  101. //实例测试2
  102. // f=sinx1+cosx2-cos(2*x3),求fmin
  103. // -10<=x<=-4
  104. // 目标函数(适应度函数)
  105. double f(struct pp &P) {
  106. double vl=0;
  107. vl=sin(P.x[0])+cos(P.x[1])-cos(2*P.x[2]);
  108. return vl;
  109. }
  110. /**********输出**********/
  111. x1 = -7.85
  112. x2 = -9.42
  113. x3 = -6.28
  114. x1 = -7.85
  115. x2 = -9.42
  116. x3 = -9.42
  117. 对于cos(2*x3)来说周期为pie=3.14,因此两组输出等价

发表评论

表情:
评论列表 (有 0 条评论,345人围观)

还没有评论,来说两句吧...

相关阅读

    相关 粒子算法(PSO)

    1.粒子群算法概述 粒子群算法属于群智能算法的一种,使用过模拟鸟群捕食行为设计的。假设区域里只有一块食物(即通常优化问题的最优解)鸟群的任务是找到这个任务源。鸟群在整个搜寻

    相关 粒子算法

    粒子群算法:通过模拟鸟群觅食过程中的迁徙和群聚行为而提出的一种基于群体智能的全局随机搜索算法,用于解决优化问题。 设想这样一个场景:一群鸟在随机的搜索食物。在这个区域里只有一