cross_chrom.m

来自「多智能体工具包」· M 代码 · 共 72 行

M
72
字号
function chrom1=cross_chrom(chrom,matenum)% CROSS_CHROM	Mate two individuals. %     Copyright (c) 1997-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 single point crossover Loop=0;for i=1:2:u-1  Is_Legal=0;  while ~Is_Legal    Must_Again=0;    while ~Must_Again      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)        Must_Again=1;      end    end    i1=mateind(i);     i2=mateind(i+1);    tempgene=chrom(i1,splitpos(1)+1:splitpos(2));    New_Chromosome1=chrom(i1,:);    New_Chromosome2=chrom(i2,:);    New_Chromosome1(splitpos(1)+1:splitpos(2))=chrom(i2,splitpos(1)+...                   1:splitpos(2));    New_Chromosome2(splitpos(1)+1:splitpos(2))=tempgene;    Is_Legal=islegal_chrom(New_Chromosome1);    if Is_Legal      Is_Legal=islegal_chrom(New_Chromosome2);    end    if Is_Legal      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 + =
减小字号Ctrl + -
显示快捷键?