mutation.m

来自「用简单遗传算法SGA实现的关于控制图的程序」· M 代码 · 共 75 行

M
75
字号
function ret=Mutation(pmutation,lenchrom,chrom,sizepop,opts,pop,bound)
% In this function,it fulfils a mutation among chromosomes
% pcorss    input  : probability of mutation
% lenchrom  input  : length of a chromosome
% chrom     input  : set of all chromosomes
% sizepop   input  : size of population
% opts      input  : tag for choosing method of crossover
% pop       input  : current serial number of generation and maximum gemeration
% ret       output : new set of chromosome
switch opts
    case 'simple' % mutation at single position
        for i=1:sizepop
            % select child at random
            pick=rand;
            while pick==0
                pick=rand;
            end
            index=ceil(pick*sizepop);
            pick=rand;
            if pick>pmutation
                continue;
            end
            % mutation position
            pick=rand;
            while pick==0
                pick=rand;
            end
            pos=ceil(pick*sum(lenchrom));
            v=bitget(chrom(index),pos);
            v=~v;
            chrom1=bitset(chrom(index),pos,v);
        end

        ret=chrom;
    case 'float'  % multiple position mutation
        for i=1:sizepop
            flag=1;
            while(flag)
                % select child at random
                pick=rand;
                while pick==0
                    pick=rand;
                end
                index=ceil(pick*sizepop);
                pick=rand;
                if pick>pmutation
                    continue;
                end
                % mutation position
                pick=rand;
                while pick==0
                    pick=rand;
                end
                pos=ceil(pick*sum(lenchrom));
                v=chrom(i,pos);
                v1=v-bound(pos,1);
                v2=bound(pos,2)-v;
                pick=rand;
                if pick>0.5
                    delta=v2*(1-pick^((1-pop(1)/pop(2))^2));
                else
                    delta=v1*(1-pick^((1-pop(1)/pop(2))^2));
                    delta=-delta;
                end
                chrom1=chrom(i,:);
                chrom1(pos)=v+delta;
                if tiaojian(chrom1)
                    chrom(i,pos)=v+delta;
                    flag=0;
                end
            end
        end
        ret=chrom;
        ret(:,1)=fix(ret(:,1));
end

⌨️ 快捷键说明

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