www.gusucode.com > 《模式识别与智能计算》matlab源代码 > 《模式识别与智能计算》matlab源代码/《模式识别与智能计算》部分函数的源程序/书程序/第9章/gacrossover.m

    function [new,xcount]=gacrossover(old,px,xtype)

[popsize,ndim]=size(old);
halfpop=popsize/2;
xcount=0;
if xtype==1
    randlist=rand((halfpop),1);
    for i=1:halfpop
        a=(i*2)-1;
        xpo=a+1;
        new(a,1:ndim)=old(a,1:ndim);
        new(xpo,1:ndim)=old(xpo,1:ndim);
        if (randlist(i)<px)
            xcount=xcount+1;
            xpoint=round((rand*ndim)+0.5);
            new(xpo,1:xpoint)=old(a,1:xpoint);
            new(a,1:xpoint)=old(xpo,1:xpoint);
        end
    end
end
if xtype==2
    randlist=rand((halfpop),1);
    for i=1:halfpop
        a=(i*2)-1;
        xpo=a+1;
        new(a,1:ndim)=old(a,1:ndim);
        new(xpo,1:ndim)=old(xpo,1:ndim);
        if (randlist(i)<px)
            xcount=xcount+1;
            [xpoint]=sort(round((rand(1,2)*ndim)+0.5));
            new(xpo,xpoint(1):xpoint(2))=old(a,xpoint(1):xpoint(2));
            new(a,xxpoint(1):xpoint(2))=old(xpo,xpoint(1):xpoint(2));
        end
    end
end
if xtype==3
      for i=1:halfpop
        a=(i*2)-1;
        xpo=a+1;
        new(a,1:ndim)=old(a,1:ndim);
        new(xpo,1:ndim)=old(xpo,1:ndim);
        for j=1:ndim
            test=rand;
            if test<px
                xcount=xcount+1;
                new(xpo,j)=old(a,j);
                new(a,j)=old(xpo,j);
            end
        end
     end
end