crossover.m
来自「用matlab编写的采用遗传算法进行图像分割的一个程序」· M 代码 · 共 23 行
M
23 行
function [newpop]=crossover(oldpop,stringlength,a,b,pc);
pops=size(oldpop,1);
newpop=zeros(size(oldpop));
for i=1:pops/2
if(rand<pc)
cpoint=round(rand*(stringlength-2))+1;
newpop(i,:)=[oldpop(i,1:cpoint),oldpop(pops/2+i,cpoint+1:stringlength),0,0];
newpop(pops/2+i,:)=[oldpop(pops/2+i,1:cpoint),oldpop(i,cpoint+1:stringlength),0,0];
else
newpop(i,:)=oldpop(i,:);
newpop(pops/2+i,:)=oldpop(pops/2+i,:);
end
end
for i=1:pops
sum=0;
for j=1:stringlength
sum=sum+newpop(i,j)*2^(j-1);
end
newpop(i,stringlength+1)=sum;%解码
x=newpop(i,stringlength+1);
newpop(i,stringlength+2)=fitness(x,pops,stringlength);
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?