1、 opti=population(sequ,: endclear sequ;fmax=22000;%=for gen=1:maxgen%选择操作%将求最小值的函数转化为适应度函数for indivi=1: obj1(indivi)=1/obj(indivi);clear indivi;%适应度函数累加总合total=0; total=total+obj1(indivi);%每条染色体被选中的几率 fitness1(indivi)=obj1(indivi)/total;%各条染色体被选中的范围 fitness(indivi)=0; for j=1:indivi fitness(indivi)=f
2、itness(indivi)+fitness1(j);clear j;fitness;%选择适应度高的个体for ranseti=1: ran=rand; while (ran1|ran0) ran; if ranfitness(fet-1)&(ran=fitness(fet)=population(fet,:clear ran;newpopulation;%交叉for int=1:2:popsize-1 popmoth=newpopulation(int,: popfath=newpopulation(int+1,: popcross(int,:)=popmoth; popcross(int
3、+1,:)=popfath; randnum=rand; if(randnum cpoint1=round(rand*hromlength); cpoint2=round(rand*hromlength); while (cpoint2=cpoint1) if cpoint1cpoint2 tem=cpoint1; cpoint1=cpoint2; cpoint2=tem; cpoint1; cpoint2; for term=cpoint1+1: for ss=1:hromlength if popcross(int,ss)=popfath(term) tem1=popcross(int,s
4、s); popcross(int,ss)=popcross(int,term); popcross(int,term)=tem1; clear tem1; if popcross(int+1,ss)=popmoth(term) tem1=popcross(int+1,ss); popcross(int+1,ss)=popcross(int+1,term); popcross(int+1,term)=tem1; clear tem1; clear term;clear randnum;popcross;%变异操作newpop=popcross; if randnum cpoint12=round
5、(rand*hromlength); cpoint22=round(rand*hromlength); if (cpoint12=0) cpoint12=1; if (cpoint22=0) cpoint22=1; while (cpoint22=cpoint12) if cpoint22=0; temp=newpop(int,cpoint12); newpop(int,cpoint12)=newpop(int,cpoint22); newpop(int,cpoint22)=temp;newpop;clear cpoint12;clear cpoint22;clear int;for ium=
6、1: vector1=newpop(ium,: obj1(ium)=hanshu(hromlength,vector1,phen);clear ium;obj1max=max(obj1);for ar=1: if obj1(ar)=obj1max newpop(ar,:)=opti;%遗传操作结束二粒子群算法源程序:%-初始格式化-clear all;format long;%-给定初始化条件-c1=1.4962;%学习因子1c2=1.4962;%学习因子2w=0.7298;%惯性权重MaxDT=100;%最大迭代次数D=2;%搜索空间维数(未知数个数)N=40;%初始化群体个体数目eps=1
7、0(-6);%设置精度(在已知最小值时候用)%-初始化种群的个体(可以在这里限定位置和速度的范围)-for i=1:ND x(i,j)=randn;%随机初始化位置 v(i,j)=randn;%随机初始化速度%-先计算各个粒子的适应度,并初始化Pi和Pg- p(i)=fitness(x(i,:),D); y(i,:)=x(i,:pg=x(1,:%Pg为全局最优for i=2: if fitness(x(i,:),D)FITNESS(pg,D) pg=x(i,:%-进入主要循环,按照公式依次迭代,直到满足精度要求-for t=1:MaxDT t for i=1: v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:)+c2*rand*(pg-x(i,:); x(i,:)+v(i,:p(i) if p(i) pg=y(i,: Pbest(t)=fitness(pg,D);%-最后给出计算结果disp(*)函数的全局最优位置为:Solution=pg最后得到的优化极值为:Result=fitness(pg,D)X,Y=meshgrid(-500:500);Z=X.*sin(sqrt(X)+Y.*(sin(sqrt(Y);hold oncontour(X,Y,Z)plot(x(:,1),x(:,2),*hold off
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1