|
楼主 |
发表于 2007-7-3 22:19
|
显示全部楼层
%函数6:染色体杂交(关键步骤)
- function sonChromosome=crossChromosome(fatherChromosome,parameter)
- [chromosomeSum,chromosomeLength]=size(fatherChromosome);
- %chromosomeSum:染色体的条数;chromosomeLength:染色体的长度
- switch parameter
- case 1%随机选择父染色体进行交叉重组
- for i=1:chromosomeSum/2
- crossDot=fix(rand(1)*chromosomeLength);%随机选择染色体的交叉点位
- randChromosomeSequence1=round(rand(1)*chromosomeSum);
- %随机产生第1条染色体的序号
- randChromosomeSequence2=round(rand(1)*chromosomeSum);
- %随机产生第2条染色体的序号,这两条染色体要进行杂交
- if randChromosomeSequence1==0%防止产生0序号
- randChromosomeSequence1=1;
- end
- if randChromosomeSequence2==0%防止产生0序号
- randChromosomeSequence2=1;
- end
- if crossDot==0 || crossDot==1
- sonChromosome(i*2-1,:)=fatherChromosome(randChromosomeSequence1,:);
- sonChromosome(i*2,:)=fatherChromosome(randChromosomeSequence2,:);
- else
- %执行两条染色体的交叉
- %把父染色体整条传给子染色体
- sonChromosome(i*2-1,:)=fatherChromosome(randChromosomeSequence1,:);
- sonChromosome(i*2-1,crossDot:chromosomeLength)=...
- fatherChromosome(randChromosomeSequence2,crossDot:chromosomeLength)
- %下一条父染色体上交叉点crossDot后的基因传给子染色体,完成前一条染色体的交叉
- sonChromosome(i*2,:)=fatherChromosome(randChromosomeSequence2,:);
- sonChromosome(i*2,crossDot:chromosomeLength)...
- =fatherChromosome(randChromosomeSequence1,crossDot:chromosomeLength)
- end
- end
- case 2 %父染色体的第i号与第chromosomeSum+1-i号交叉
- for i=1:chromosomeSum/2
- crossDot=fix(rand(1)*chromosomeLength);%随机选择染色体的交叉点位
- if crossDot==0 || crossDot==1
- sonChromosome(i*2-1,:)=fatherChromosome(i,:);
- sonChromosome(i*2,:)=fatherChromosome(chromosomeSum+1-i,:);
- else
- %执行两条染色体的交叉
- sonChromosome(i*2-1,:)=fatherChromosome(i,:);%把父染色体整条传给子染色体
- sonChromosome(i*2-1,crossDot:chromosomeLength)...
- =fatherChromosome(chromosomeSum+1-i,crossDot:chromosomeLength);
- %下一条父染色体上交叉点crossDot后的基因传给子染色体,完成前一条染色体的交叉
- sonChromosome(i*2,:)=fatherChromosome(chromosomeSum+1-i,:);
- sonChromosome(i*2,crossDot:chromosomeLength)...
- =fatherChromosome(i,crossDot:chromosomeLength);
- end
- end
- case 3 %父染色体的第i号与第i+chromosomeSum/2号交叉
- for i=1:chromosomeSum/2
- crossDot=fix(rand(1)*chromosomeLength);%随机选择染色体的交叉点位
- if crossDot==0 || crossDot==1
- sonChromosome(i*2-1,:)=fatherChromosome(i,:);
- sonChromosome(i*2,:)=fatherChromosome(i+chromosomeSum/2,:);
- else
- %执行两条染色体的交叉
- sonChromosome(i*2-1,:)=fatherChromosome(i,:);%把父染色体整条传给子染色体
- sonChromosome(i*2-1,crossDot:chromosomeLength)...
- =fatherChromosome(i+chromosomeSum/2,crossDot:chromosomeLength);
- %下一条父染色体上交叉点crossDot后的基因传给子染色体,完成前一条染色体的交叉
- sonChromosome(i*2,:)=fatherChromosome(i+chromosomeSum/2,:);
- sonChromosome(i*2,crossDot:chromosomeLength)...
- =fatherChromosome(i,crossDot:chromosomeLength);
- end
- end
- end
复制代码 |
|