📄 gennew.asv
字号:
%a,b,c为KP,KI,KD的变化范围;popsize为群体规模;lchrom为表示(KP,KI,KD)的染色体串长度;maxgen为进化的最大代数
%pcross为交叉概率;pmutation为变异概率;min为上一代最小适应度;max为最大适应度;minpp为适应度最小个体的序号;
%maxpp为适应度最大个体的序号;
global a b oldpop newpop popsize lchrom maxgen pcross pmutation fitu;
global min max minpp maxpp k lc gen s kp ki kd;
% %******************************参数初始化************************************
a=[0 0 0];
b=[2 2 1];
popsize=6;
lchrom=18;
lc=lchrom/3;
maxgen=10;
pcross=[0.6 0.6 0.6];
pmutation=0.02;
gen=0;
k=1;
% % %********************************群体的初始化*******************************%
for s=1:popsize
oldpop{s}.chrom=randint(1,18);
% oldpop{s}.dvalue=decode(oldpop{s}.chrom);
oldpop{s}.fitness=0.0001; %计算每个个体的适应度%
oldpop{s}.parent1=0;
oldpop{s}.parent2=0;
oldpop{s}.xsite=0;
end
% statistics(oldpop);
sumfitness=oldpop{1}.fitness;
min=oldpop{1}.fitness;
max=poldpop{1}.fitness;
maxpp=1;
minpp=1;
for j=1:popsize
sumfitness=sumfitness+oldpop{j}.fitness;
if (oldpop{j}.fitness>max)
max=oldpop{j}.fitness;
maxpp=j;
end
if(oldpop{j}.fitness<min)
min=oldpop{j}.fitness;
minpp=j;
end
end
avg=sumfitness/popsize;
while(gen<maxgen)
% mate1=select;
index(1)=randint(1,1,[1,popsize]);
index(2)=randint(1,1,[1,popsize]);
if (oldpop{index(1)}.fitness > oldpop{index(2)}.fitness)
mate1=index(1);
else
mate1=index(2);
end
% mate2=select;
index(1)=randint(1,1,[1,popsize]);
index(2)=randint(1,1,[1,popsize]);
if (oldpop{index(1)}.fitness > oldpop{index(2)}.fitness)
mate2=index(1);
else
mate2=index(2);
end
%*******************判断是否交叉***************************
for i=1:3
pp=rand;
%************************确定交叉位置************************
if (pp<pcross(i))
jcross(i)=randint(1,1,[lc*(i-1)+1,lc*i]);
else
jcross(i)=lchrom/(4-i);
end
%**********************进行交叉与变异操作*********************
if(jcross(i) ~= lchrom/(4-i))
for j=(lc*(i-1)+1):jcross(i)
% newpop{k}.chrom(j)=mutation(oldpop{mate1}.chrom(j));
pp1=rand;
if (pp1<pmutation)
newpop{k}.chrom(j)=~oldpop{mate1}.chrom(j);
else
newpop{k}.chrom(j)=oldpop{mate1}.chrom(j);
end
% newpop{k+1}.chrom(j)=mutation(oldpop{mate2}.chrom(j));
pp1=rand;
if (pp1<pmutation)
newpop{k+1}.chrom(j)=~oldpop{mate2}}.chrom(j);
else
newpop{k+1}.chrom(j)=oldpop{mate2}.chrom(j);
end
end
for j=jcross(i):lc*i
% newpop{k}.chrom(j)=mutation(oldpop{mate2}.chrom(j));
pp1=rand;
if (pp1<pmutation)
newpop{k}.chrom(j)=~oldpop{mate2}.chrom(j);
else
newpop{k}.chrom(j)=oldpop{mate2}.chrom(j);
end
% newpop{k+1}.chrom(j)=mutation(oldpop{mate1}.chrom(j));
pp1=rand;
if (pp1<pmutation)
newpop{k+1}.chrom(j)=~oldpop{mate1}.chrom(j);
else
newpop{k+1}.chrom(j)=oldpop{mate1}.chrom(j);
end
end
else
for j=(lc*(i-1)+1):lc*i
% newpop{k}.chrom(j)=mutation(oldpop{mate1}.chrom(j));
pp1=rand;
if (pp1<pmutation)
newpop{k}.chrom(j)=~oldpop{mate1}.chrom(j);
else
newpop{k}.chrom(j)=oldpop{mate1}.chrom(j);
end
% newpop{k+1}.chrom(j)=mutation(oldpop{mate2}.chrom(j));
pp1=rand;
if (pp1<pmutation)
newpop{k}.chrom(j)=~oldpop{mate2}}.chrom(j);
else
newpop{k}.chrom(j)=oldpop{mate2}.chrom(j);
end
end
end
%*************************************************************
end
%*************************************************************
% newpop{k}.dvalue=decode(newpop{k}.chrom);
m=[0 0 0];
for i=1:3
for j=1:6
m(i)=m(i)+newpop{k}.chrom((i-1)*6+j)*(2^(6-j));
end
end
for i=1:3
newpop{k}.dval(i)=a(i)+m(i)*(b(i)-a(i))/(2^6-1);
end
newpop{k}.parent1=mate1;
newpop{k}.parent2=mate2;
newpop{k}.xsite=jcross;
disp('**************************************************')
disp('kp=');
kp=newpop{k}.dvalue(1);
disp('ki=')
ki=newpop{k}.dvalue(2);
disp('kd=')
kd=newpop{k}.dvalue(3);
disp('**************************************************')
sim('test1')
% keyboard
newpop{k}.fitness=1/J;
m=[0 0 0];
for i=1:3
for j=1:6
m(i)=m(i)+newpop{k+1}.chrom((i-1)*6+j)*(2^(6-j));
end
end
for i=1:3
newpop{k+1}.dval(i)=a(i)+m(i)*(b(i)-a(i))/(2^6-1);
end
newpop{k+1}.parent1=mate1;
newpop{k+1}.parent2=mate2;
newpop{k+1}.xsite=jcross;
disp('**************************************************')
disp('kp=');
kp= newpop{k+1}.dvalue(1);
disp('ki=');
ki=newpop{k+1}.dvalue(2);
disp('kd=');
kd=newpop{k+1}.dvalue(3);
disp('**************************************************')
sim('test1')
% keyboard
[d1 d2]=SIZE(J)
if c~=0
fit=1/J(d1);
else
fit=0;
end
=1/J;
k=k+2;
%***************************************************************
if mod(k,51)==0
k=1;
gen=gen+1
oldmax=max;
oldmaxpp=maxpp;
% statistics(newpop);
sumfitness=newpop{1}.fitness;
min=newpop{1}.fitness;
max=newpop{1}.fitness;
maxpp=1;
minpp=1;
for j=1:popsize
sumfitness=sumfitness+newpop{j}.fitness;
if (newpop{j}.fitness>max)
max=newpop{j}.fitness;
maxpp=j;
end
if(newpop{j}.fitness<min)
min=newpop{j}.fitness;
minpp=j;
end
end
avg=sumfitness/popsize;
if(max<oldmax)
newpop{minpp}.chrom=oldpop{oldmaxpp}.chrom;
newpop{minpp}.dvalue=oldpop{oldmaxpp}.dvalue;
newpop{minpp}.fitness=oldpop{oldmaxpp}.fitness;
% statistics(newpop);
end
for s=1:popsize
oldpop{s}.chrom=newpop{s}.chrom;
end
end
%***************************************************************
end
% %*********************译码*************************************
% function dval=decode(x)
% global a b;
% m=[0 0 0];
% for i=1:3
% for j=1:6
% m(i)=m(i)+x((i-1)*6+j)*(2^(6-j));
% end
% end
% for i=1:3
% dval(i)=a(i)+m(i)*(b(i)-a(i))/(2^6-1);
% end
% end
% %**************************************************************
%
%
% %***************************适应度函数**************************
% function fit=objfunc(c)
% % [d1 d2]=SIZE(c)
% % if c~=0
% % fit=1/c(d1);
% % else
% % fit=0;
% % end
% end
% %***************************************************************
%
% % %***************************选择操作****************************
% % function rs=select
% % global oldpop popsize;
% % index(1)=randint(1,1,[1,popsize]);
% % index(2)=randint(1,1,[1,popsize]);
% % if (oldpop{index(1)}.fitness > oldpop{index(2)}.fitness)
% % rs=index(1);
% % else
% % rs=index(2);
% % end
% % end
% % %***************************************************************
%
% %*******************群体适应度计算******************************
% function statistics(pop)
% global newpop oldpop popsize min max maxpp minpp;
% sumfitness=pop{1}.fitness;
% min=pop{1}.fitness;
% max=pop{1}.fitness;
% maxpp=1;
% minpp=1;
% for j=1:popsize
% sumfitness=sumfitness+pop{j}.fitness;
% if (pop{j}.fitness>max)
% max=pop{j}.fitness;
% maxpp=j;
% end
% if(pop{j}.fitness<min)
% min=pop{j}.fitness;
% minpp=j;
% end
% end
% avg=sumfitness/popsize;
% end
% %************************************************************
%
% %*****************************变异操作***********************
% function vmut=mutation(ch)
% global pmutation
% pp1=rand;
% if (pp1<pmutation)
% ch=~ch;
% end
% vmut=ch;
% end
% %************************************************************
%
%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -