粒子群算法matlab(算法已經(jīng)調(diào)試)_第1頁
粒子群算法matlab(算法已經(jīng)調(diào)試)_第2頁
粒子群算法matlab(算法已經(jīng)調(diào)試)_第3頁
粒子群算法matlab(算法已經(jīng)調(diào)試)_第4頁
粒子群算法matlab(算法已經(jīng)調(diào)試)_第5頁
已閱讀5頁,還剩28頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、程序1當(dāng),。a)%主函數(shù)源程序(main.m)%-基本粒子群算法 (particle swarm optimization)%-名稱: 基本粒子群算法%-初始格式化clear all; %清除所有變量clc; %清屏format long; %將數(shù)據(jù)顯示為長整形科學(xué)計數(shù)%-給定初始條條件-N=40; %初始化群體個數(shù)D=10; %初始化群體維數(shù)T=100; %初始化群體最迭代次數(shù)c11=2; %學(xué)習(xí)因子1c21=2; %學(xué)習(xí)因子2c12=1.5;c22=1.5;w=1.2; %慣性權(quán)重eps=10(-6); %設(shè)置精度(在已知最小值的時候用)%-初始化種群個體(限定位置和速度)-x=zeros

2、(N,D);v=zeros(N,D);for i=1:N for j=1:D x(i,j)=randn; %隨機初始化位置 v(i,j)=randn; %隨機初始化速度 endend%-顯示群位置-figure(1)for j=1:D if(rem(D,2)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始位置) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),c

3、har(rem(j,10)+48),維); end title(tInfo)end%-顯示種群速度figure(2)for j=1:D if(rem(D,2)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始速度) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),維); end title(tInfo)endfigure(3)

4、%第一個圖subplot(1,2,1)%-初始化種群個體(在此限定速度和位置)-x1=x;v1=v;%-初始化個體最優(yōu)位置和最優(yōu)值-p1=x1;pbest1=ones(N,1);for i=1:N pbest1(i)=fitness(x1(i,:),D);end%-初始化全局最優(yōu)位置和最優(yōu)值-g1=1000*ones(1,D);gbest1=1000;for i=1:N if(pbest1(i)gbest1) g1=p1(i,:); gbest1=pbest1(i); endendgb1=ones(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for

5、j=1:N if (fitness(x1(j,:),D)pbest1(j) p1(j,:)=x1(j,:); pbest1(j)=fitness(x1(j,:),D); end if(pbest1(j)gbest1) g1=p1(j,:); gbest1=pbest1(j); end v1(j,:)=w*v1(j,:)+c11*rand*(p1(j,:)-x1(j,:)+c21*rand*(g1-x1(j,:); x1(j,:)=x1(j,:)+v1(j,:); end gb1(i)=gbest1;endplot(gb1)TempStr=sprintf(c1= %g ,c2=%g,c11,c2

6、1);title(TempStr);xlabel(迭代次數(shù));ylabel(適應(yīng)度值);%第二個圖subplot(1,2,2)%-初始化種群個體(在此限定速度和位置)-x2=x;v2=v;%-初始化種群個體最有位置和 最優(yōu)解-p2=x2;pbest2=ones(N,1);for i=1:N pbest2(i)=fitness(x2(i,:),D);end%-初始化種全局最有位置和 最優(yōu)解-g2=1000*ones(1,D);gbest2=1000;for i=1:N if(pbest2(i)gbest2) g2=p2(i,:); gbest2=pbest2(i); endendgb2=ones

7、(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x2(j,:),D)pbest2(j) p2(j,:)=x2(j,:); pbest2(j)=fitness(x2(j,:),D); end if(pbest2(j)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始位置) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,c

8、har(floor(j/10)+48),char(rem(j,10)+48),維); end title(tInfo)end%-顯示種群速度figure(2)for j=1:D if(rem(D,2)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始速度) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),維); end tit

9、le(tInfo)endfigure(3)%第一個圖subplot(1,2,1)%-初始化種群個體(在此限定速度和位置)-x1=x;v1=v;%-初始化個體最優(yōu)位置和最優(yōu)值-p1=x1;pbest1=ones(N,1);for i=1:N pbest1(i)=fitness(x1(i,:),D);end%-初始化全局最優(yōu)位置和最優(yōu)值-g1=1000*ones(1,D);gbest1=1000;for i=1:N if(pbest1(i)gbest1) g1=p1(i,:); gbest1=pbest1(i); endendgb1=ones(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度

10、或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x1(j,:),D)pbest1(j) p1(j,:)=x1(j,:); pbest1(j)=fitness(x1(j,:),D); end if(pbest1(j)gbest1) g1=p1(j,:); gbest1=pbest1(j); endv1(j,:)=w*v1(j,:)+c11*rand*(p1(j,:)-x1(j,:)+c21*rand*(g1-x1(j,:); x1(j,:)=x1(j,:)+v1(j,:); end gb1(i)=gbest1;endplot(gb1)TempStr=sprintf(

11、c1= %g ,c2=%g,c11,c21);title(TempStr);xlabel(迭代次數(shù));ylabel(適應(yīng)度值);%第二個圖subplot(1,2,2)%-初始化種群個體(在此限定速度和位置)-x2=x;v2=v;%-初始化種群個體最有位置和 最優(yōu)解-p2=x2;pbest2=ones(N,1);for i=1:N pbest2(i)=fitness(x2(i,:),D);end%-初始化種全局最有位置和 最優(yōu)解-g2=1000*ones(1,D);gbest2=1000;for i=1:N if(pbest2(i)gbest2) g2=p2(i,:); gbest2=pbest

12、2(i); endendgb2=ones(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x2(j,:),D)pbest2(j) p2(j,:)=x2(j,:); pbest2(j)=fitness(x2(j,:),D); end if(pbest2(j)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始位置) tInfo=strcat(第,char(j+48),維); if(

13、j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),維); end title(tInfo)end%-顯示種群速度figure(2)for j=1:D if(rem(D,2)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始速度) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j

14、,10)+48),維); end title(tInfo)endfigure(3)%第一個圖subplot(1,2,1)%-初始化種群個體(在此限定速度和位置)-x1=x;v1=v;%-初始化個體最優(yōu)位置和最優(yōu)值-p1=x1;pbest1=ones(N,1);for i=1:N pbest1(i)=fitness(x1(i,:),D);end%-初始化全局最優(yōu)位置和最優(yōu)值-g1=1000*ones(1,D);gbest1=1000;for i=1:N if(pbest1(i)gbest1) g1=p1(i,:); gbest1=pbest1(i); endendgb1=ones(1,T);%-

15、浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x1(j,:),D)pbest1(j) p1(j,:)=x1(j,:); pbest1(j)=fitness(x1(j,:),D); end if(pbest1(j)gbest1) g1=p1(j,:); gbest1=pbest1(j); end v1(j,:)=w*v1(j,:)+c11*rand*(p1(j,:)-x1(j,:)+c21*rand*(g1-x1(j,:); x1(j,:)=x1(j,:)+v1(j,:); end gb1(i)=gbest1;endplot

16、(gb1)TempStr=sprintf(c1= %g ,c2=%g,c11,c21);title(TempStr);xlabel(迭代次數(shù));ylabel(適應(yīng)度值);%第二個圖subplot(1,2,2)%-初始化種群個體(在此限定速度和位置)-x2=x;v2=v;%-初始化種群個體最有位置和 最優(yōu)解-p2=x2;pbest2=ones(N,1);for i=1:N pbest2(i)=fitness(x2(i,:),D);end%-初始化種全局最有位置和 最優(yōu)解-g2=1000*ones(1,D);gbest2=1000;for i=1:N if(pbest2(i)gbest2) g2=

17、p2(i,:); gbest2=pbest2(i); endendgb2=ones(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x2(j,:),D)pbest2(j) p2(j,:)=x2(j,:); pbest2(j)=fitness(x2(j,:),D); end if(pbest2(j)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始位置) tInfo=strcat(第,c

18、har(j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),維); end title(tInfo)end%-顯示種群速度- figure(2)for j=1:D if(rem(D,2)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始速度) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,char(floor(j

19、/10)+48),char(rem(j,10)+48),維); end title(tInfo)end figure(3)subplot(1,2,1)%-初始化種群個體(在此限定速度和位置)-x1=x;v1=v;%-初始化個體最優(yōu)位置和最優(yōu)值-p1=x1;pbest1=ones(N,1);for i=1:N pbest1(i)=fitness(x1(i,:),D);end%-初始化全局最優(yōu)位置和最優(yōu)值-g1=1000*ones(1,D);gbest1=1000;for i=1:N if(pbest1(i)gbest1) g1=p1(i,:); gbest1=pbest1(i); endendg

20、b1=ones(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x1(j,:),D)pbest1(j) p1(j,:)=x1(j,:); pbest1(j)=fitness(x1(j,:),D); end if(pbest1(j)gbest1) g1=p1(j,:); gbest1=pbest1(j); endv1(j,:)=w1*v1(j,:)+c1*rand*(p1(j,:)-x1(j,:)+c2*rand*(g1-x1(j,:); x1(j,:)=x1(j,:)+v1(j,:); end gb1(i)=g

21、best1;endplot(gb1)TempStr=sprintf(w= %g ,w1);title(TempStr);xlabel(迭代次數(shù));ylabel(適應(yīng)度值);subplot(1,2,2)%-初始化種群個體(在此限定速度和位置)-x2=x;v2=v;%-初始化種群個體最有位置和 最優(yōu)解-p2=x2;pbest2=ones(N,1);for i=1:N pbest2(i)=fitness(x2(i,:),D);end%-初始化種全局最有位置和 最優(yōu)解-g2=1000*ones(1,D);gbest2=1000;for i=1:N if(pbest2(i)gbest2) g2=p2(i

22、,:); gbest2=pbest2(i); endendgb2=ones(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x2(j,:),D)pbest2(j) p2(j,:)=x2(j,:); pbest2(j)=fitness(x2(j,:),D); end if(pbest2(j)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始位置) tInfo=strcat(第,char(

23、j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),維); end title(tInfo)end%-顯示種群速度- figure(2)for j=1:D if(rem(D,2)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始速度) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)

24、+48),char(rem(j,10)+48),維); end title(tInfo)end figure(3)subplot(1,2,1)%-初始化種群個體(在此限定速度和位置)-x1=x;v1=v;%-初始化個體最優(yōu)位置和最優(yōu)值-p1=x1;pbest1=ones(N,1);for i=1:N pbest1(i)=fitness(x1(i,:),D);end%-初始化全局最優(yōu)位置和最優(yōu)值-g1=1000*ones(1,D);gbest1=1000;for i=1:N if(pbest1(i)gbest1) g1=p1(i,:); gbest1=pbest1(i); endendgb1=o

25、nes(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x1(j,:),D)pbest1(j) p1(j,:)=x1(j,:); pbest1(j)=fitness(x1(j,:),D); end if(pbest1(j)gbest1) g1=p1(j,:); gbest1=pbest1(j); end v1(j,:)=w1*v1(j,:)+c1*rand*(p1(j,:)-x1(j,:)+c2*rand*(g1-x1(j,:); x1(j,:)=x1(j,:)+v1(j,:); end gb1(i)=gbes

26、t1;endplot(gb1)TempStr=sprintf(w= %g ,w1);title(TempStr);xlabel(迭代次數(shù));ylabel(適應(yīng)度值);subplot(1,2,2)%-初始化種群個體(在此限定速度和位置)-x2=x;v2=v;%-初始化種群個體最有位置和 最優(yōu)解-p2=x2;pbest2=ones(N,1);for i=1:N pbest2(i)=fitness(x2(i,:),D);end%-初始化種全局最有位置和最優(yōu)解-g2=1000*ones(1,D);gbest2=1000;for i=1:N if(pbest2(i)gbest2) g2=p2(i,:);

27、 gbest2=pbest2(i); endendgb2=ones(1,T);%-浸入主循環(huán),按照公式依次迭代直到滿足精度或者迭代次數(shù)-for i=1:T for j=1:N if (fitness(x2(j,:),D)pbest2(j) p2(j,:)=x2(j,:); pbest2(j)=fitness(x2(j,:),D); end if(pbest2(j)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始位置) tInfo=strcat(第,char(

28、j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),維); end title(tInfo)end%-顯示種群速度-figure(2)for j=1:D if(rem(D,2)0) subplot(D+1)/2,2,j) else subplot(D/2,2,j) end plot(x(:,j),b*);grid on xlabel(粒子) ylabel(初始速度) tInfo=strcat(第,char(j+48),維); if(j9) tInfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),維); end title(tInfo)end figure(3) %-初始化群體個體最有位置和最優(yōu)解-p=x;pbest=ones(N,1);for i=1:N pbest(i)=fitness(x(i,:),D);end%-初始化全局最優(yōu)位置和最優(yōu)解-g=1000*o

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論