crossoper.m

来自「基本遗传算法的matlab源程序」· M 代码 · 共 29 行

M
29
字号
function geneCodCro=crossOper(geneCod,proCro)
%  crossover operator 
%  syntax:geneCodCro=crossOper(geneCod,proCro)
%
%  Output Arguments:
%      geneCode     ---- a matrix of chromosome codes
%  Input Arguments:
%      geneCodCro   ---- a matrix of chromosome codes after crossover
%      proCro       ---- the probability of cross-gene
%
%  Author:Yan Anxin
%  ID number:081810
%  Yax235 DreamWorks, SEE, SEU, 2# Sipailou Nanjing, 210096, P.R.China 

[sizePop,sizeGene]=size(geneCod);
matPool=randperm(sizePop);                  %to form the mating pool
geneCodCro=geneCod;

for ik=1:(sizePop/2)
    proRand=rand(1);
    if proRand>proCro,continue;end          %random selection
    father=geneCod(matPool(ik),:);mother=geneCod(matPool(ik+sizePop/2),:);
    crossPoint=unidrnd(sizeGene);           %random cross-matching
    child=father(crossPoint:sizeGene);
    father(crossPoint:sizeGene)=mother(crossPoint:sizeGene);
    mother(crossPoint:sizeGene)=child;      %single cross
    geneCodCro(matPool(ik),:)=father;
    geneCodCro(matPool(ik+sizePop/2),:)=mother;
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?