📄 crosover.m
字号:
function chrom1=crosover(chrom,matenum)% CROSSOVER Mate two individuals. % Copyright (c) 1998-2000 Jiming Liu and Jianbing Wumateind=[]; chrom1=chrom;[pop bitlength]=size(chrom); ind=1:pop; u=floor(pop*matenum);if floor(u/2)~=u/2 u=u-1;end%select percentage to mate randomlywhile length(mateind)~=u i=round(rand*pop); if i==0 i=1; end if ind(i)~=-1 mateind=[mateind i]; ind(i)=-1; endend%perform dual point crossover for i=1:2:u-1 Loop=0; IsLegal=0; while ~IsLegal MustAgain=0; while ~MustAgain splitpos=sort(floor(rand(1,2)*bitlength)); % crossover point for mm=1:2 if splitpos(mm)==0 splitpos(mm)=1; end end if splitpos(1)~=splitpos(2) MustAgain=1; end end i1=mateind(i); i2=mateind(i+1); tempgene=chrom(i1,splitpos(1)+1:splitpos(2)); NewChromosome1=chrom(i1,:); NewChromosome2=chrom(i2,:); NewChromosome1(splitpos(1)+1:splitpos(2))=chrom(i2,splitpos(1)+... 1:splitpos(2)); NewChromosome2(splitpos(1)+1:splitpos(2))=tempgene; IsLegal=forlegal(NewChromosome1); if IsLegal IsLegal=forlegal(NewChromosome2); end if IsLegal chrom1(i1,splitpos(1)+1:splitpos(2))=chrom(i2,splitpos(1)+1:... splitpos(2)); chrom1(i2,splitpos(1)+1:splitpos(2))=tempgene; end Loop=Loop+1; % avoid deadlock. if Loop>200 break; end endend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -