粒子群最佳化演算法

來源:互聯網
上載者:User

標籤:fine   article   def   com   set   comm   color   minimum   col   

粒子群最佳化演算法屬於群智能(swarm intelligence)最佳化演算法。群智能分兩種,一種是粒群最佳化,另一種是蟻群最佳化。

群智能概念

       假設你和你的朋友正在尋寶,每個人有個探測器,這個探測器可以知道寶藏到探測器的距離。你們一群人在找,每個人都可以把資訊共用出去,就跟打dota時你可以有你隊友的視野,你可以知道其他所有人距離寶藏的距離,這樣,你看誰離寶藏最近,就向誰靠近,這樣會使你發現寶藏的機會變大,而且,這種方法比你單人找要快的多。

       這是一個群行為(swarm behavior)的簡單一實例,群中各個體互動作用,使用一個比單一個體更有效方法求解全域目標。可以把群(swarm)定義為某種互動作用的組織或Agent之結構集合,在群智能計算研究中,群的個體組織包括螞蟻,白蟻,蜜蜂,黃蜂,魚群,鳥群等。在這些群體中,個體在結構上是很簡單的,而它們的集體行為卻可能變得相當複雜。研究人員發現,螞蟻在鳥巢和食物之間的運輸路線,不管一開始多隨機,最後螞蟻總能找到一條最短路徑。

粒群最佳化概念

粒群最佳化(particle swarm optimization,PSO)演算法是一種基於群體搜尋的演算法,它建立在類比鳥群社會的基礎上。粒群概念的最初含義是通過圖形來類比鳥群優美和不可預測的舞蹈動作,發現鳥群支配同步飛行和以最佳隊形突然改變飛行方向並重新編隊的能力。這個概念已經被包含在一個簡單有效最佳化演算法中。

在粒群最佳化中,被稱為“粒子”(particle)的個體通過超維搜尋空間“流動”。粒子在搜尋空間中的位置變化是以個體成功地超過其他個體的社會心理意向為基礎的,因此,群中粒子的變化是受其鄰近粒子(個體)的經驗或知識影響的。一個粒子的搜尋行為受到群中其他粒子的搜尋行為的影響。由此可見,粒群最佳化是一種共生合作演算法。

演算法描述

先通過一個形象的情境來描述一下:5隻鳥覓食,每個鳥都知道自己與食物的距離,並將此資訊與其他鳥共用。

一開始,5隻鳥分散在不同的地方,假設沒只鳥每秒鐘更新自己的速度和方向,問題是怎麼更新呢?

每隻鳥記下自己離食物最近的位置,稱為pbest,pbest0,pbest1,..分別表示5隻鳥的pbest,從這裡面選一個gbest,組裡最好的。

每過一秒鐘,鳥更新自己的速度v(此處為向量),

v_new  = v_old + c1*rand()*(pbest-pcurrent) +c2*rand()*(gbest-pcurrent)

c1,c2一般為2,rand()產生0~1的隨機數。

然後以這個速度飛行1秒,再次更新,最終離食物越來越近。

 以下偽碼摘自百度百科

 程式的虛擬碼如下

 

  For each particle

 

  ____Initialize particle

 

  END

 

  Do

 

  ____For each particle

 

  ________Calculate fitness value

 

  ________If the fitness value is better than the best fitness value (pBest) in history

 

  ____________set current value as the new pBest

 

  ____End

 

  ____Choose the particle with the best fitness value of all the particles as the gBest

 

  ____For each particle

 

  ________Calculate particle velocity according equation (a)

 

  ________Update particle position according equation (b)

 

  ____End

 

  While maximum iterations or minimum error criteria is not attained

 

  在每一維粒子的速度都會被限制在一個最大速度Vmax,如果某一維更新後的速度超過使用者設定的Vmax,那麼這一維的速度就被限定為Vmax。

程式執行個體

計算f(x) = x*x - 20x + 100 的最小值及取最小值時的x

  1. #include <iostream>  
  2. #include <cmath>  
  3. #include <cstdlib>  
  4. using namespace std;  
  5.  
  6. #define C1 2  
  7. #define C2 2  
  8. #define VMAX 5.0  
  9. #define MAX_ITERATIONS 100  
  10. float rand01()  
  11. {  
  12.         return (float) (rand()/(double)RAND_MAX);  
  13. }  
  14. struct particle{  
  15.         float current;  
  16.         float pbest;  
  17. };  
  18.  
  19. float fitness(float x)  
  20. {  
  21.         return x*x - 20*x + 100;  
  22. }  
  23.  
  24. float gbest = 10000;  
  25. struct particle p[5];  
  26. float v[5] = {0};  
  27.  
  28. void init_particles()  
  29. {  
  30.         int i;  
  31.         for(i = 0; i < 5; i++)  
  32.         {  
  33.                 p[i].current = -2+i;  
  34.                 p[i].pbest = p[i].current;  
  35.         }  
  36. }  
  37. void find_gbest()  
  38. {  
  39.         int i;  
  40.         for(i = 0; i < 5; i++)  
  41.         {  
  42.                         if(fitness(gbest) > fitness(p[i].current))  
  43.                                 gbest = p[i].current;  
  44.         }  
  45. }  
  46. void adjust_v()  
  47. {  
  48.         int i ;  
  49.         for(i = 0; i < 5; i++)  
  50.         {  
  51.                 v[i] = v[i] + C1*rand01()*(p[i].pbest - p[i].current) + C2*rand01()*(gbest - p[i].current);  
  52.                 if(v[i] > VMAX)  
  53.                         v[i] = VMAX;  
  54.         }  
  55. }  
  56. void pso()  
  57. {  
  58.         int i,iter_num;  
  59.         iter_num = 1;  
  60.         while(iter_num < MAX_ITERATIONS)  
  61.         {  
  62.                 /*for(i = 0; i < 5; i++)  
  63.                 {  
  64.                         cout <<"p"<<i<<":current "<<p[i].current<<" pbest "<<p[i].pbest<<endl;  
  65.                 }  
  66.                 cout <<"gbest:"<<gbest<<endl;  
  67.                 cout <<endl;  
  68.                 getchar();*/ 
  69.                 for(i = 0; i < 5; i++)  
  70.                 {  
  71.                         if(fitness(p[i].current) < fitness(p[i].pbest))  
  72.                                 p[i].pbest = p[i].current;  
  73.                 }  
  74.                 find_gbest();  
  75.                 adjust_v();  
  76.                 for(i = 0; i < 5; i++)  
  77.                         p[i].current += v[i];  
  78.                 iter_num ++;  
  79.         }  
  80. }  
  81. int main()  
  82. {  
  83.  
  84.         init_particles();  
  85.         pso();  
  86.         printf("After %d iterations,gbest is %f\n",MAX_ITERATIONS,gbest);  
  87.         return 0;  
  88. }  

運行結果

下面是每次迭代後的結果,可以看到,經過5次迭代,結果就很好了。

  1. After 1 iterations  
  2. p0:current -2 pbest -2  
  3. p1:current -1 pbest -1  
  4. p2:current 0 pbest 0  
  5. p3:current 1 pbest 1  
  6. p4:current 2 pbest 2  
  7. gbest:10000  
  8.  
  9.  
  10. After 2 iterations  
  11. p0:current 1.15506 pbest -2  
  12. p1:current 3.79064 pbest -1  
  13. p2:current 0.790205 pbest 0  
  14. p3:current 2.53646 pbest 1  
  15. p4:current 2 pbest 2  
  16. gbest:2  
  17.  
  18.  
  19. After 3 iterations  
  20. p0:current 6.15506 pbest 1.15506  
  21. p1:current 8.58128 pbest 3.79064  
  22. p2:current 5.79021 pbest 0.790205  
  23. p3:current 5.87216 pbest 2.53646  
  24. p4:current 4.17373 pbest 2  
  25. gbest:3.79064  
  26.  
  27.  
  28. After 4 iterations  
  29. p0:current 11.1551 pbest 6.15506  
  30. p1:current 13.3719 pbest 8.58128  
  31. p2:current 10.7902 pbest 5.79021  
  32. p3:current 9.79741 pbest 5.87216  
  33. p4:current 8.27141 pbest 4.17373  
  34. gbest:8.58128  
  35.  
  36.  
  37. After 5 iterations  
  38. p0:current 13.8766 pbest 11.1551  
  39. p1:current 10.1764 pbest 8.58128  
  40. p2:current 14.7492 pbest 10.7902  
  41. p3:current 13.7227 pbest 9.79741  
  42. p4:current 13.2714 pbest 8.27141  
  43. gbest:9.79741  
  44.  
  45.  
  46. After 6 iterations  
  47. p0:current 8.03327 pbest 11.1551  
  48. p1:current 6.98078 pbest 10.1764  
  49. p2:current 13.2414 pbest 10.7902  
  50. p3:current 4.78856 pbest 9.79741  
  51. p4:current 11.6974 pbest 8.27141  
  52. gbest:10.1764  
  53.  
  54.  
  55. After 7 iterations  
  56. p0:current 5.84287 pbest 11.1551  
  57. p1:current 9.25245 pbest 10.1764  
  58. p2:current 5.23059 pbest 10.7902  
  59. p3:current -3.28694 pbest 9.79741  
  60. p4:current 9.93147 pbest 11.6974  
  61. gbest:10.1764  

粒子群最佳化演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.