版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、例 函数对于适应度函数fitness对其参数,做出不同方式的比较以测试其对函数结果影响。当,。 (适应函数)程序1当,。a)%主函数源程序(main.m)%-基本粒子群算法 (particle swarm optimization)%-名称: 基本粒子群算法%-初始格式化clear all; %清除所有变量clc; %清屏format long; %将数据显示为长整形科学计数%-给定初始条条件-n=40; %初始化群体个数d=10; %初始化群体维数t=100; %初始化群体最迭代次数c11=2; %学习因子1c21=2; %学习因子2c12=1.5;c22=1.5;w=1.2; %惯性权重e
2、ps=10(-6); %设置精度(在已知最小值的时候用)%-初始化种群个体(限定位置和速度)-x=zeros(n,d);%x是位置,初始化位置空间(矩阵)v=zeros(n,d);%v是速度,初始化速度空间(矩阵)for i=1:n for j=1:d x(i,j)=randn; %随机初始化位置,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)
3、,b*);grid on%b*表示颜色是绿的,用*显示在图上 xlabel(粒子) ylabel(初始位置) tinfo=strcat(第,char(j+48),维);%strcat使括号里的东西连成字符串 if(j9) tinfo=strcat(第,char(floor(j/10)+48);%floor向负无穷方向取整char(rem(j,10)+48,维);%rem 取余 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
4、(v(:,j),b*);grid on%是不是应该是v(:,j) 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)%第一个图subplot(1,2,1)%-初始化种群个体(在此限定速度和位置)-x1=x;v1=v;%-初始化个体最优位置和最优值-p1=x1;pbest1=ones(n,1);for i=1:n pbest1(i)=fitness(
5、x1(i,:),d);%适应度函数end%-初始化全局最优位置和最优值-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);%-进入主循环,按照公式依次迭代直到满足精度或者迭代次数-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
6、=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,c21);title(tempstr);xlabel(迭代次数);ylabel(适应度值);%第二个图subplot(1,2,2)%-初始化种群个体(在此限定速度和位置)-x2=x;v2=v;%-初始化种群个体最有位置和 最优
7、解-p2=x2;pbest2=ones(n,1);for i=1:n pbest2(i)=fitness(x2(i,:),d);end%-初始化种全局最优位置和 最优解-g2=1000*ones(1,d);gbest2=1000;for i=1:n if(pbest2(i)gbest2) g2=p2(i,:);%最优位置 gbest2=pbest2(i);%最优解 endendgb2=ones(1,t);%t为迭代次数t=100%-进入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for j=1:n if (fitness(x2(j,:),d)pbest2(j)%个体最
8、优 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(第,char(floor(j/10)+48),char(rem(j,10)+48),维); end title(tinfo)end%-显示种群速度figure(2)for j=
9、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)%第一个图subplot(1,2,1)%-初始化种群个体(在此限定速度和位置)-x1=x;v1=v;%-初始化个体最优位置和最优
10、值-p1=x1;pbest1=ones(n,1);for i=1:n pbest1(i)=fitness(x1(i,:),d);end%-初始化全局最优位置和最优值-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);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for j=1:n if (fitness(x1(j,:),d)pbest1(j) p1(j,:)=x1(j,:); pbest1(j)
11、=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(c1= %g ,c2=%g,c11,c21);title(tempstr);xlabel(迭代次数);ylabel(适应度值);%第二个图subplot(1,2,2)%-
12、初始化种群个体(在此限定速度和位置)-x2=x;v2=v;%-初始化种群个体最有位置和 最优解-p2=x2;pbest2=ones(n,1);for i=1:n pbest2(i)=fitness(x2(i,:),d);end%-初始化种全局最有位置和 最优解-g2=1000*ones(1,d);gbest2=1000;for i=1:n if(pbest2(i)gbest2) g2=p2(i,:); gbest2=pbest2(i); endendgb2=ones(1,t);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for j=1:n if (fitnes
13、s(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(第,char(floor(j/10)+48),char(rem(j,10)+48),维); end title(tinfo)end%-显
14、示种群速度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)%第一个图subplot(1,2,1)%-初始化种群个体(在此限定速度和位置)-x1=x
15、;v1=v;%-初始化个体最优位置和最优值-p1=x1;pbest1=ones(n,1);for i=1:n pbest1(i)=fitness(x1(i,:),d);end%-初始化全局最优位置和最优值-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);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for j=1:n if (fitness(x1(j,:),d)pbest1(j) p1(j,:
16、)=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,c21);title(tempstr);xlabel(迭代次数);ylabel(适应度值);
17、%第二个图subplot(1,2,2)%-初始化种群个体(在此限定速度和位置)-x2=x;v2=v;%-初始化种群个体最有位置和 最优解-p2=x2;pbest2=ones(n,1);for i=1:n pbest2(i)=fitness(x2(i,:),d);end%-初始化种全局最有位置和 最优解-g2=1000*ones(1,d);gbest2=1000;for i=1:n if(pbest2(i)gbest2) g2=p2(i,:); gbest2=pbest2(i); endendgb2=ones(1,t);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t
18、 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(j+48),维); if(j9) tinfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),维); end t
19、itle(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)subplot(1,2,1)%-初始化种群个体(
20、在此限定速度和位置)-x1=x;v1=v;%-初始化个体最优位置和最优值-p1=x1;pbest1=ones(n,1);for i=1:n pbest1(i)=fitness(x1(i,:),d);end%-初始化全局最优位置和最优值-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);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for j=1:n if (fitness(x1(j,:),d)p
21、best1(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)=gbest1;endplot(gb1)tempstr=sprintf(w= %g ,w1);title(tempstr);xlabel(迭代次数);ylabel(适应度值)
22、;subplot(1,2,2)%-初始化种群个体(在此限定速度和位置)-x2=x;v2=v;%-初始化种群个体最有位置和 最优解-p2=x2;pbest2=ones(n,1);for i=1:n pbest2(i)=fitness(x2(i,:),d);end%-初始化种全局最有位置和 最优解-g2=1000*ones(1,d);gbest2=1000;for i=1:n if(pbest2(i)gbest2) g2=p2(i,:); gbest2=pbest2(i); endendgb2=ones(1,t);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for
23、 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(j+48),维); if(j9) tinfo=strcat(第,char(floor(j/10)+48),char(rem(j,10)+48),维); end title
24、(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)subplot(1,2,1)%-初始化种群个体(在此限定
25、速度和位置)-x1=x;v1=v;%-初始化个体最优位置和最优值-p1=x1;pbest1=ones(n,1);for i=1:n pbest1(i)=fitness(x1(i,:),d);end%-初始化全局最优位置和最优值-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);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for j=1:n if (fitness(x1(j,:),d)pbest
26、1(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)=gbest1;endplot(gb1)tempstr=sprintf(w= %g ,w1);title(tempstr);xlabel(迭代次数);ylabel(适应度值);su
27、bplot(1,2,2)%-初始化种群个体(在此限定速度和位置)-x2=x;v2=v;%-初始化种群个体最有位置和 最优解-p2=x2;pbest2=ones(n,1);for i=1:n pbest2(i)=fitness(x2(i,:),d);end%-初始化种全局最有位置和最优解-g2=1000*ones(1,d);gbest2=1000;for i=1:n if(pbest2(i)gbest2) g2=p2(i,:); gbest2=pbest2(i); endendgb2=ones(1,t);%-浸入主循环,按照公式依次迭代直到满足精度或者迭代次数-for i=1:t for j=1
28、: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(第,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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2023年硫代硫酸盐资金筹措计划书
- 2023年广告资金需求报告
- 2024年工业喷墨打印头项目投资申请报告代可行性研究报告
- 交管12123学法减分(学法免分)试题及答案646
- 医院外科工作总结
- 公司年度绩效总结反思总结报告评价
- 消防工作总结报告:安全演练与危险应对
- 医院工作总结我的工作自我认知和反思
- 公司工作总结及社会责任担当
- 医院工作总结:安全管理与医疗质量的总结
- 施工现场每周月安全检查记录
- 俄罗斯方块C语言实现
- 领导干部离任交接书(共10页)
- 外架、满堂架钢管、扣件用量计算_图文
- 爱惜粮食主题班会课件:节约粮食光盘行动
- TSG91-2021锅炉节能环保技术规程
- NC系统库存及调拨业务操作手册
- EPC总承包项目组织结构
- 氧合血红蛋白(HbO2)和还原血红蛋白(Hb)吸收光谱
- 车辆档案一车一档
- 合规管理岗位职责
评论
0/150
提交评论