Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include<iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int population,iteration;
  8. float weight,c1,c2;
  9. int i,j;
  10. float x[2][100],v[2][100];
  11. float fitness[100];
  12. float pbest[100],gbest=0;
  13. float precord[2][100],grecord[2];
  14. int count=0;
  15. float tempV[2];
  16. float check[2][100];
  17. float k=1,kmax=0;
  18.  
  19. //輸入參數
  20. cout<<"粒子群演算法PSO\n請輸入母體個數:";
  21. cin>>population;
  22. cout<<"請輸入迭代次數:";
  23. cin>>iteration;
  24. cout<<"請輸入慣性權重:";
  25. cin>>weight;
  26. cout<<"請輸入學習因子C1:";
  27. cin>>c1;
  28. cout<<"請輸入學習因子C2:";
  29. cin>>c2;
  30.  
  31. //隨機產生S0、V0
  32. srand(time(NULL));
  33. for(i=0;i<population;i++)
  34. { rands:
  35. x[0][i]=(rand()%401+200)*0.01; //x1=2~6
  36. x[1][i]=(rand()%201)*0.01; //x2=0~2
  37. if(6*x[0][i]+4*x[1][i]>24 || x[0][i]+2*x[1][i]>6 || -x[0][i]+x[1][i]>1 || x[1][i]>2 || x[1][i]<0 || x[0][i]<0)
  38. { goto rands;
  39. }
  40. }
  41. for(i=0;i<2;i++)
  42. { for(j=0;j<population;j++)
  43. { randv0:
  44. v[i][j]=(rand()%601+1-300)*0.01; //v=-3~3
  45. if(v[i][j]==0)
  46. {goto randv0;}
  47. }
  48. }
  49. do{
  50. //評估各粒子適應函數值、記錄pbest&gbest
  51. for(i=0;i<2;i++)
  52. { for(j=0;j<population;j++)
  53. { fitness[j]=5*x[0][j]+4*x[1][j];
  54. if(fitness[j]>pbest[j])
  55. { pbest[j]=fitness[j];
  56. precord[0][j]=x[0][j];
  57. precord[1][j]=x[1][j];
  58. }
  59. if(fitness[j]>gbest)
  60. { gbest=fitness[j];
  61. grecord[0]=x[0][j];
  62. grecord[1]=x[1][j];
  63. }
  64. }
  65. }
  66.  
  67. //印出表格
  68. cout<<"x1\tx2\tv1\tv2\tfitness\tpbest\n";
  69. for(i=0;i<population;i++)
  70. { cout<<x[0][i]<<"\t"<<x[1][i]<<"\t"<<v[0][i]<<"\t"<<v[1][i]<<"\t"
  71. <<fitness[i]<<"\t"<<pbest[i]<<endl;
  72. }
  73. cout<<"第"<<count<<"代 gbest = "<<gbest<<endl<<endl;
  74.  
  75.  
  76. //更新速度、位置
  77. for(i=0;i<population;i++)
  78. { decrease:
  79. tempV[0]=((weight*v[0][i])+
  80. (c1*((rand()%10000)*0.0001)*(precord[0][i]-x[0][i]))+
  81. (c2*((rand()%10000)*0.0001)*(grecord[0]-x[0][i]))
  82. )/k;
  83. tempV[1]=((weight*v[1][i])+
  84. (c1*((rand()%10000)*0.0001)*(precord[1][i]-x[1][i]))+
  85. (c2*((rand()%10000)*0.0001)*(grecord[1]-x[1][i]))
  86. )/k;
  87.  
  88. check[0][i]=x[0][i]+tempV[0];
  89. check[1][i]=x[1][i]+tempV[1];
  90.  
  91. if(6*check[0][i]+4*check[1][i]<=24 && check[0][i]+2*check[1][i]<=6 && -check[0][i]+check[1][i]<=1 && check[1][i]<=2 && check[1][i]>=0 && check[0][i]>=0)
  92. { x[0][i]=check[0][i];
  93. x[1][i]=check[1][i];
  94. v[0][i]=tempV[0];
  95. v[1][i]=tempV[1];
  96. }
  97. else
  98. { k++;
  99. if(k>kmax)
  100. {kmax=k;}
  101. goto decrease;
  102. }
  103. k=1;
  104. }
  105. count++;
  106. }while(count<=iteration);
  107. cout<<"kmax="<<kmax<<endl;
  108. system("pause");
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement