mutation.m

来自「MATLAB的遗传算法工具」· M 代码 · 共 66 行

M
66
字号
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
               % 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));
                    chrom(i,pos)=v+delta;
                else
                    delta=v1*(1-pick^((1-pop(1)/pop(2))^2));
                    chrom(i,pos)=v-delta;
                end
        end
        ret=chrom;
end

⌨️ 快捷键说明

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