粒子群算法python實現

jopen 12年前發布 | 25K 次閱讀 算法

1、 概述

粒子群算法作為一種優化算法,在很多領域都有應用。所謂優化,我的理解是對一個問題求出它足夠好的解,目前的優化算法有很多,如蟻群算法、遺傳算法等。粒子群算法相對于這些算法來說,它更簡單,而且有很快的收斂速度。

2、 算法描述

舉一個優化問題的例子,若求粒子群算法python實現 的最小值,當然可以通過數學手段求出當向量粒子群算法python實現 時的最小值為0 ,但是對于更復雜的函數表達式來說,運用數學手段求解是復雜的,而且在實際應用中,并不一定要求出它的精確解,只要是滿足足夠精度的精確解就夠了,這時通過一定的優化算法求解就很方便。

粒子群算法思想來源于實際生活中鳥捕食的過程。假設在一個n維的空間中,有一群鳥(m只)在捕食,食物位于n維空間的某個點上,對于第i只鳥某一時刻來說,有兩個向量描述,一個是鳥的位置向量粒子群算法python實現 ,第二個是鳥的速度粒子群算法python實現 i=1,2...m)。 假設鳥能夠判斷一個位置的好壞,所謂“好壞”,就是離食物更近了還是更遠了。鳥在捕食的過程中會根據自己的經驗以及鳥群中的其他鳥的位置決定自己的速度, 根據當前的位置和速度,可以得到下一刻的位置,這樣每只鳥通過向自己和鳥群學習不斷的更新自己的速度位置,最終找到食物,或者離食物足夠近的點。更新速度 和位置的表達式如下。

更新速度:粒子群算法python實現

粒子群算法python實現

對應的python實現如下:

import random
import copy

birds=int(raw_input('Enter count of bird: ')) xcount=int(raw_input('Enter count of x: ')) pos=[] speed=[] bestpos=[] birdsbestpos=[] w=0.8 c1=2 c2=2 r1=0.6 r2=0.3 for i in range(birds): pos.append([]) speed.append([]) bestpos.append([])

def GenerateRandVec(list): for i in range(xcount): list.append(random.randrange(1,100))

def CalDis(list): dis=0.0 for i in list: dis+=i**2 return dis

for i in range(birds): #initial all birds' pos,speed GenerateRandVec(pos[i]) GenerateRandVec(speed[i]) bestpos[i]=copy.deepcopy(pos[i])

def FindBirdsMostPos(): best=CalDis(bestpos[0]) index=0 for i in range(birds): temp=CalDis(bestpos[i]) if temp<best: best=temp index=i return bestpos[index]

birdsbestpos=FindBirdsMostPos() #initial birdsbestpos

def NumMulVec(num,list): #result is in list for i in range(len(list)): list[i]*=num return list

def VecSubVec(list1,list2): #result is in list1 for i in range(len(list1)): list1[i]-=list2[i] return list1

def VecAddVec(list1,list2): #result is in list1 for i in range(len(list1)): list1[i]+=list2[i] return list1

def UpdateSpeed():

#global speed
for i in range(birds):
    temp1=NumMulVec(w,speed[i][:])
    temp2=VecSubVec(bestpos[i][:],pos[i])
    temp2=NumMulVec(c1*r1,temp2[:])
    temp1=VecAddVec(temp1[:],temp2)
    temp2=VecSubVec(birdsbestpos[:],pos[i])
    temp2=NumMulVec(c2*r2,temp2[:])
    speed[i]=VecAddVec(temp1,temp2)

def UpdatePos(): global bestpos,birdsbestpos for i in range(birds): VecAddVec(pos[i],speed[i]) if CalDis(pos[i])<CalDis(bestpos[i]): bestpos[i]=copy.deepcopy(pos[i]) birdsbestpos=FindBirdsMostPos()

for i in range(100):

#print birdsbestpos
print CalDis(birdsbestpos)
UpdateSpeed()
UpdatePos()


raw_input()</pre>

若搜索空間鳥群中鳥的個數為30,問題規模x的個數為5個,100次迭代運行的結果如下,可以看到最終的值已經非常接近0這個最優解了。

Enter count of bird: 30

Enter count of x: 5

6300.0

6300.0

5286.56

253.7792

253.7792

169.750784

169.750784

29.27174656

29.27174656

14.3572668416

14.3572668416

10.7095755489

10.7095755489

10.4166336974

10.4166336974

10.3952346067

10.3952346067

10.38162211

10.38162211

10.38162211

10.38162211

10.38162211

10.38162211

10.3816078435

10.3816078435

10.3815990193

10.3815990193

10.3815990193

10.3815990193

10.3815990193

10.3815990193

10.3815990038

8.61600314002

6.75814610104

0.697031173541

0.697031173541

0.586257672539

0.319653330666

0.308201304448

0.277551198357

0.152964935388

0.11330877896

0.0897094795931

0.0849797134585

0.0824510053969

0.0824510053969

0.0817642679444

0.0293278926344

0.0101946030255

0.0101946030255

0.00880640894843

0.00517872172034

0.00517872172034

0.00517872172034

0.00517872172034

0.00517872172034

0.00514217487311

0.00511832820187

0.00510609755302

0.00510609755302

0.00510609755302

0.0039233096712

0.00319253923712

0.00142224947992

0.000847531318472

0.000682710187325

0.000126289737569

0.000126289737569

0.000109415873528

0.000109415873528

0.000106080598721

0.000106080598721

0.000105801137376

0.000105801137376

0.000105654750511

0.000105654750511

0.000105654750511

0.000105654750511

0.000105654750511

0.000105654750511

0.000105653808938

0.000105653808938

0.000105653808938

7.63547737464e-05

2.56599311271e-05

6.88805040513e-06

6.88805040513e-06

2.93943099726e-07

2.93943099726e-07

2.93943099726e-07

1.65997040259e-07

1.49983822855e-07

1.45620647032e-07

1.30809105417e-07

1.30631326401e-07

1.29726054702e-07

1.2360770395e-07

1.2360770395e-07

1.2360770395e-07

1.23467030689e-07

轉自:http://blog.csdn.net/chen_jp/article/details/7947059

</div>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!