1、%-程序正文-clear all;close all;%-变量部分-popsize=50;vartotal=2;%种群规模%变量个数%惯性因子%自身因子%全局因子%变异概率%限定代数inertia=0.5;selfw=2.0;globalw=2.0;mutatep=0.05;maxgen=500;%-数组部分-varrange(1,1)=-512; %第一变量最小值varrange(1,2)=512; %第一变量最大值varrange(2,1)=-512; %第二变量最小值%第二变量最大值varrange(2,2)=512;%-粒子位置初始化-for i=1:1:popsizefor j=1:
2、1:vartotalangle(i,j)=2*pi*rand;chrom(i,1,j)=cos(angle(i,j);chrom(i,2,j)=sin(angle(i,j);selfangle(i,j)=2*pi*rand;%第 i个粒子(量子染色体)的幅角余弦%第 i个粒子(量子染色体)的幅角正弦selfchrom(i,1,j)=cos(angle(i,j);%第 i 个粒子自身最优位置的幅角余弦selfchrom(i,2,j)=sin(angle(i,j);%第 i 个粒子自身最优位置的幅角正弦dangle(i,j)=0;endend1%-解空间变换-for i=1:1:popsizefo
3、r j=1:1:2for k=1:1:vartotalchromx(i,j,k)=0.5*(varrange(k,2)*(1+chrom(i,j,k)+varrange(k,1)*(1-chrom(i,j,k);selfchromx(i,j,k)=0.5*(varrange(k,2)*(1+selfchrom(i,j,k)+varrange(k,1)*(1-selfchrom(i,j,k);endendend1%-计算适应度-for i=1:1:popsizefor j=1:1:2fitness(i,j)=-chromx(i,j,1)*sin(abs(chromx(i,j,2)+1-chrom
4、x(i,j,1)0.5)*cos(abs(chromx(i,j,2)+1+chromx(i,j,1)0.5)-(chromx(i,j,2)+1)*cos(abs(chromx(i,j,2)+1-chromx(i,j,1)0.5)*sin(abs(chromx(i,j,2)+1+chromx(i,j,1)0.5);selffitness(i,j)=-selfchromx(i,j,1)*sin(abs(selfchromx(i,j,2)+1-selfchromx(i,j,1)0.5)*cos(abs(selfchromx(i,j,2)+1+selfchromx(i,j,1)0.5)-(selfch
5、romx(i,j,2)+1)*cos(abs(selfchromx(i,j,2)+1-selfchromx(i,j,1)0.5)*sin(abs(selfchromx(i,j,2)+1+selfchromx(i,j,1)0.5);endend%-适应度统计-minfit=fitness(1,1);if fitness(1,1)fitness(1,2)minfit=fitness(1,2);endmaxfit=fitness(1,2);bestchain=2;if fitness(1,1)fitness(1,2);maxfit=fitness(1,1);bestchain=1;endminfit
6、index=1;maxfitindex=1;avgfit=(fitness(1,1)+fitness(1,2)/(2*popsize);for i=2:1:popsizefor j=1:1:2if minfitfitness(i,j)minfit=fitness(i,j);minfitindex=i;endif maxfitpit1=t1-2*pi;endt2=gloangle(1,j)-angle(i,j);if t2pit2=t2-2*pi;enddangle(i,j)=inertia*dangle(i,j)+selfw*rand*t1+globalw*rand*t2;angle(i,j)
7、=angle(i,j)+dangle(i,j);chrom(i,1,j)=cos(angle(i,j);chrom(i,2,j)=sin(angle(i,j);%解空间变换%移动后的相位%移动后的位置%移动后的位置chromx(i,1,j)=0.5*(varrange(j,2)*(1+chrom(i,1,j)+varrange(j,1)*(1-chrom(i,1,j);chromx(i,2,j)=0.5*(varrange(j,2)*(1+chrom(i,2,j)+varrange(j,1)*(1-chrom(i,2,j);endfor j=1:1:2fitness(i,j)=-chromx(
8、i,j,1)*sin(abs(chromx(i,j,2)+1-chromx(i,j,1)0.5)*cos(abs(chromx(i,j,2)+1+chromx(i,j,1)0.5)-(chromx(i,j,2)+1)*cos(abs(chromx(i,j,2)+1-chromx(i,j,1)0.5)*sin(abs(chromx(i,j,2)+1+chromx(i,j,1)0.5);%计算适应度endend%-粒子位置变异-for i=1:1:popsizefor j=1:1:vartotalif randmutatepangle(i,j)=pi/2-angle(i,j);chrom(i,1,j)=cos(angle(i,j);chrom(i,2,j)=sin(angle(i,j);%解空间变换%变异后的相位%变异后的位置%变异后的位置chromx(i,1,j)=0.5*(varrange(j,2)*(1+chrom(i,1,j)+varrange(j,1)*(1-chrom(i,1,j);chromx(i,2,j)=0.5*(varrange(j,2)*(1+chrom(i,2,j)+varrange(j,1)*(1-chrom(i,2,j);