引用:
作者: ai_hinemon
L=ceil(log2((b-a)/eps+1)); %根据离散精度,确定二进制编码需要的码长
x=zeros(NP,L);
for i=1:NP
x(i,  =Initial(L);%种群初始化
fx(i)=fitness(Dec(a,b,x(i,  ,L)); %个体适应值
end
for k=1:NG
sumfx=sum(fx); %所有个体适应值之和
px=fx/sumfx; %所有个体适应值的平均值
ppx=0;
ppx(1)=px(1);
for i=2:NP %用于轮盘赌策略的累加
ppx(i)=ppx(i-1)+px(i);
end
for i=1:NP
sita=rand();
for n=1:NP
if sita<=ppx(n)
SelFather=n; %根据轮盘赌策略确定的父亲
break;
end
|
end
Selmother=floor(rand()*(NP-1))+1; %随机选择母亲
posCut=floor(rand()*(L-2))+1; %随机选择交叉点
r1=rand();
if r1<=pc %交叉
nx(i,1

osCut)=x(SelFather,1

osCut);
nx(i,(posCut+1):L)=x(Selmother,(posCut+1):L);
r2=rand();
if r2<=pm %变异
posMut=round(rand()*(L-1)+1);
nx(i,posMut)=~nx(i,posMut);
end