歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

C語言實現基本PSO算法

介紹看此文:http://www.linuxidc.com/Linux/2012-11/73567.htm

粒子群算法的流程圖如上,看了好多版本,這個最靠譜,我的main函數完全按照這個來,好理解過程:

  1. int main(int argc, constchar *argv[])
  2. {
  3. int n=0;
  4. //printf("Random Initialization of the swarm:\n\n");
  5. RandInitofSwarm();
  6. //printf("Computation of the fitness of each particle:\n");
  7. ComputFitofSwarm();
  8. //printf("FirstComputPandGbest:\n");
  9. FirstComputPandGbest();
  10. while(n++!=N)
  11. {
  12. printf("The %dth time to calculate .\n",n );
  13. //printf("Updated of the swarm:\n\n");
  14. UpdateofVandX();
  15. //printf("Updated of the swarm's Fitness:\n");
  16. ComputFitofSwarm();
  17. //printf("Replaced of P and Gbest:\n\n");
  18. UpdatePandGbest();
  19. }
  20. getchar();
  21. return 0;
  22. }

各種printf用來調試自己的信息,可以去掉看看中間值~~~

先來pso.h
 
現在執行最簡單的功能,用粒子群搜索二維空間:y=x^2+y^+3的最大值~~~x,y的范圍屬於(-100,100),那最大值就是20003,偽隨機粒子數20個,最大迭代次數為40次,最大搜索速度為2,慣性權重W設為1.4,學習因子c1=c2=2,看能不能找到最大值~~~

  1. #ifndef _PSO_H_
  2. #define _PSO_H_
  3. #define Dim 2
  4. #define PNum 20
  5. #define N 40
  6. typedefstruct PARTICLE{
  7. double X[Dim];
  8. double P[Dim];
  9. double V[Dim];
  10. double Fitness;
  11. }particle;
  12. typedefstruct SWARM{
  13. particle Particle[PNum];
  14. int GBestIndex;
  15. double GBest[Dim];
  16. double W;
  17. double C1;
  18. double C2;
  19. double Xup[Dim];
  20. double Xdown[Dim];
  21. double Vmax[Dim];
  22. }swarm;
  23. void RandInitofSwarm(void);
  24. void ComputFitofSwarm(void);
  25. void FirstComputPandGbest(void);
  26. void UpdateofVandX(void);
  27. void UpdatePandGbest(void);
  28. double InertWeight(void);
  29. int CriteriaofStop(void);
  30. #endif

pso.c

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "time.h"
  4. #include "math.h"
  5. #include "pso.h"
  6. #include "stdio.h"
  7. swarm *s=(swarm *)malloc(sizeof(swarm));
  8. particle *p=(particle *)malloc(sizeof(particle));
  9. int main(int argc, constchar *argv[])
  10. {
  11. int n=0;
  12. //printf("Random Initialization of the swarm:\n\n");
  13. RandInitofSwarm();
  14. //printf("Computation of the fitness of each particle:\n");
  15. ComputFitofSwarm();
  16. //printf("FirstComputPandGbest:\n");
  17. FirstComputPandGbest();
  18. while(n++!=N)
  19. {
  20. printf("The %dth time to calculate .\n",n );
  21. //printf("Updated of the swarm:\n\n");
  22. UpdateofVandX();
  23. //printf("Updated of the swarm's Fitness:\n");
  24. ComputFitofSwarm();
  25. //printf("Replaced of P and Gbest:\n\n");
  26. UpdatePandGbest();
  27. }
  28. getchar();
  29. return 0;
  30. }
  31. //Random Initialization of the swarm
  32. void RandInitofSwarm(void)
  33. {
  34. int i,j;
  35. s->W=1.4;
  36. s->C1=2.0;
  37. s->C2=2.0;
  38. for(j=0;j<Dim;j++)
  39. {
  40. s->Xdown[j] = -100;
  41. s->Xup[j] = 100;
  42. s->Vmax[j] = 2;
  43. }
  44. srand((unsigned)time(NULL));
  45. for(i=0; i<PNum; i++)
  46. {
  47. //printf(" The %dth of X is: ",i);
  48. for(j=0; j<Dim; j++)
  49. {
  50. s->Particle[i].X[j] = rand()/(double)RAND_MAX*(s->Xup[j]-s->Xdown[j])+s->Xdown[j]; //-100~100
  51. s->Particle[i].V[j] = rand()/(double)RAND_MAX*s->Vmax[j]*2-s->Vmax[j]; //-2~2
  52. //printf(" %.2f \n ",s->Particle[i].X[j]);
  53. }
  54. }
  55. }
  56. //Computation of the fitness of each particle
  57. void ComputFitofSwarm(void)
  58. {
  59. int i;
  60. srand((unsigned)time(NULL));
  61. for(i=0; i<PNum; i++)
  62. {
  63. //printf(" The Fitness of %dth Particle: ",i);
  64. s->Particle[i].Fitness = s->Particle[i].X[0]*s->Particle[i].X[0]+s->Particle[i].X[1]*s->Particle[i].X[1]+3;
  65. //printf(" %.2f\n",s->Particle[i].Fitness);
  66. }
  67. }
  68. void FirstComputPandGbest(void)
  69. {
  70. int i,j;
  71. //P=X;
  72. for(i=0; i<PNum; i++)
  73. {
  74. for(j=0;j<Dim;j++)
  75. {
  76. s->Particle[i].P[j]=s->Particle[i].X[j];
  77. }
  78. }
  79. //Computation of GBest
  80. s->GBestIndex = 0;
  81. for(i=0; i<PNum; i++)
  82. if(s->Particle[i].Fitness>=s->Particle[s->GBestIndex].Fitness)
  83. s->GBestIndex = i;
  84. for(j=0;j<Dim;j++)
  85. {
  86. s->GBest[j]=s->Particle[s->GBestIndex].P[j];
  87. }
  88. printf("GBestIndex , GBest , Fitness of GBest:%d ,%.2f ,%.2f ,%.2f \n",
  89. s->GBestIndex ,s->GBest[0],s->GBest[1],
  90. s->Particle[s->GBestIndex].Fitness);
  91. }
  92. //update V and X
  93. void UpdateofVandX(void)
  94. {
  95. int i,j;
  96. srand((unsigned)time(NULL));
  97. for(i=0; i<PNum; i++)
  98. {
  99. //printf(" The %dth of X is: ",i);
  100. for(j=0; j<Dim; j++)
  101. s->Particle[i].V[j] = s->W*s->Particle[i].V[j]+
  102. rand()/(double)RAND_MAX*s->C1*(s->Particle[i].P[j] - s->Particle[i].X[j])+
  103. rand()/(double)RAND_MAX*s->C2*(s->GBest[j] - s->Particle[i].X[j]);
  104. for(j=0; j<Dim; j++)
  105. {
  106. if(s->Particle[i].V[j]>s->Vmax[j])
  107. s->Particle[i].V[j] = s->Vmax[j];
  108. if(s->Particle[i].V[j]<-s->Vmax[j])
  109. s->Particle[i].V[j] = -s->Vmax[j];
  110. }
  111. for(j=0; j<Dim; j++)
  112. {
  113. s->Particle[i].X[j] += s->Particle[i].V[j];
  114. if(s->Particle[i].X[j]>s->Xup[j])
  115. s->Particle[i].X[j]=s->Xup[j];
  116. if(s->Particle[i].X[j]<s->Xdown[j])
  117. s->Particle[i].X[j]=s->Xdown[j];
  118. }
  119. //printf(" %.2f %.2f \n",s->Particle[i].X[0],s->Particle[i].X[1]);
  120. }
  121. }
  122. staticdouble ComputAFitness(double X[])
  123. {
  124. return X[0]*X[0]+X[1]*X[1]+3;
  125. }
  126. void UpdatePandGbest(void)
  127. {
  128. int i,j;
  129. //update of P if the X is bigger than current P
  130. for (i = 0; i < PNum; i++)
  131. {
  132. //printf(" The %dth of P is: ",i);
  133. if (s->Particle[i].Fitness > ComputAFitness(s->Particle[i].P))
  134. {
  135. for(j=0;j<Dim;j++)
  136. {
  137. s->Particle[i].P[j] = s->Particle[i].X[j];
  138. }
  139. }
  140. //printf(" %.2f %.2f \n",s->Particle[i].P[0],s->Particle[i].P[1]);
  141. }
  142. for (i = 0; i < PNum; i++)
  143. {
  144. //printf("The %dth of P's Fitness is : %.2f \n",i,ComputAFitness(s->Particle[i].P));
  145. }
  146. //update of GBest
  147. for(i=0; i<PNum; i++)
  148. if(ComputAFitness(s->Particle[i].P) >= s->Particle[s->GBestIndex].Fitness)
  149. s->GBestIndex = i;
  150. for(j=0;j<Dim;j++)
  151. {
  152. s->GBest[j]=s->Particle[s->GBestIndex].P[j];
  153. }
  154. printf("GBestIndex , GBest , Fitness of GBest:%d ,%.2f ,%.2f ,%.2f \n",
  155. s->GBestIndex ,s->GBest[0],s->GBest[1],
  156. ComputAFitness(s->GBest));
  157. }
  158. double InertWeight(void)
  159. {
  160. return 1.0;
  161. }
  162. int CriteriaofStop()
  163. {
  164. int n=N;
  165. return (n--==0);
  166. }
Copyright © Linux教程網 All Rights Reserved