遗传算法(一)基本遗传算法(SGA)及MATLAB源码

遗传算法(Genetic Algorithm)是常用的智能算法的一种,遗传算法GA(Genetic Algorithm)入门知识梳理 对GA进行了详尽的基本介绍,其结尾附的七个链接也让人很有启发。本文附上自己对遗传算法的学习理解与代码实现。

//挖坑


根据遗传算法基础知识,写出伪代码如下:

Initialization();
while less than maximum iterations
    Crossover(parent, pCross) -> childrenCross
    Mutation(parent, pMutation) -> childrenMutate
    [childrenCross; cihldrenMutate] -> children
    Calculate the fitness of children
    select(children) -> parent
    if Satisfy terminal condition
        break
    end
end

整体思路可以说是比较简单,生成种群->交叉、变异->遗传(自然选择)->下一代。生成种群需要一定的编码技巧(挖坑,以后填)。


下面给出具体MATLAB代码实现:

%%
%编码方式: 基本二进制编码
%Input:     FitFunc:    Any function
%           pCrossover: probability of crossover, default 0.5
%           pMutation:  probability of mutation,  default 0.04
%           GroupNum:   number of individuals of the virtual group, default 30
%           MaxIter:    maximum iterations
%           MaxRepeat:  (optional)determine the convergence standard 判断收敛
%parent.fitness
%parent.chrom

function Result = MyGA(FitFunc, pCrossover, pMutation, GroupNum, MaxIter, MaxRepeat)
    %Default parameters
    if nargin < 6
        MaxRepeat = 10;
        if nargin < 5
            MaxIter = 1000;
            if nargin < 4
                GroupNum = 50;
                if nargin < 3
                    pMutation = 0.04;
                    if nargin < 2
                        pCrossover = 0.5;
                    end
                end
            end
        end
    end

    Result  =  [];
    epsilon =  1e-5;
    iter    =  0;
    iRepeat =  1;
    bit     =  22;
    thisMax =  0;
    parent  =  InitGroup(GroupNum, FitFunc, bit);   %Generate initial population
    while iter < MaxIter
        children1 = Crossover(parent, pCrossover/iter^0.1);  %Return crossovered chromes
        children.chrom = [];
        children.fitness = [];
        children.chrom = Mutation([parent.chrom; children1], pMutation/iter^0.1);
        children.fitness = CalcFit(children.chrom, FitFunc, bit);
        children = select(children, GroupNum);
        parent = children;
        iter = iter + 1;
        %parent.chrom;
        %[m, I] = max(parent.fitness)
        if (thisMax-max(parent.fitness))/max(parent.fitness) < epsilon
            iRepeat = iRepeat + 1;
        else 
            iRepeat = 1;
        end
        thisMax = max(parent.fitness);
        disp(thisMax)
        Result = [Result; thisMax];
    end
end

%Encoding method: 普通二进制编码
function parent = InitGroup(GroupNum, FitFunc, bit)
    parent.fitness = [];
    parent.chrom = [];
    for i = 1:GroupNum
        item = dec2bin(fix(rand*2^bit));
        while size(item, 2) < bit
           item = ['0', item];
        end
        parent.chrom = [parent.chrom; item];
    end
    parent.fitness = CalcFit(parent.chrom, FitFunc, bit);
end

%User Define 自定义解码方式
function Decode = Decoding(Population, bit)
    Decode = bin2dec(Population) * 2 / (2^bit)+1;
end

%Calculate Fitness
function Fitness = CalcFit(Population, Fun, bit)
    DecodedPop = Decoding(Population, bit);
    Fitness = Fun(DecodedPop);
end

%roulette selcting method
function newPop = select(parent, PopNum)
    %Add: The best survive?
    cumFit = cumsum(parent.fitness)/sum(parent.fitness);
    %[M, I] = max(parent.fitness);
    %newPop.chrom(1,:) = parent.chrom(I, :);
    %newPop.fitness = parent.fitness(I);
    for i = 1 : PopNum
         index = find (cumFit - rand > 0);
         newPop.chrom(i,:) = parent.chrom(index(1),:);
         newPop.fitness(i) = parent.fitness(index(1));
    end
end

function childrenChrom = Crossover(parent, pCrossover)
    [PopNum, bit] = size(parent.chrom);
    childrenChrom = [];
    %[M, I] = max(parent.fitness);
    %childrenChrom = parent.chrom(I, :); %Parent with highest fitness
    for i = 1 : PopNum/2
        RandCross = rand(1);
        if RandCross < pCrossover
            i = fix(rand(1)*PopNum + 1);
            j = fix(rand(1)*PopNum + 1);
            while i == j
                i = fix(rand(1)*PopNum + 1);
                j = fix(rand(1)*PopNum + 1);
            end 
            BreakPoint = fix(rand(1)*bit + 1);
            temp = parent.chrom(i, 1:BreakPoint);
            parent.chrom(i, 1:BreakPoint) = parent.chrom(j, 1:BreakPoint);
            parent.chrom(j, 1:BreakPoint) = temp;
            childrenChrom = [childrenChrom; parent.chrom(i, :); parent.chrom(j, :)];
        end
    end
end

function childrenChrom = Mutation(chrom, pMutation)
    [PopNum, bit] = size(chrom);
    childrenChrom = chrom;
    for i = 1:PopNum
        for j = 1:bit
            if rand < pMutation
                childrenChrom(i, j) = '1'-childrenChrom(i, j)+'0';
            end
        end
    end   
end

解几个简单的例子

简单一元函数

y=x×sin(x)

[1,3] 区间上,用MATLAB求得在 x=2.0288 时函数有最大值 1.8197


a

此函数在指定区间上只有一个最大值,用遗传算法可以很容易地得到结果。

MyGA(@myFun)

其中myFun为上文所述的函数,即为遗传算法中的适应度函数。

MyGA(@myFun);
    1.8185
    1.8123
    1.8184
    1.8193
    1.8193
    1.8193
    1.8193
    1.8195
    1.8196
    1.8196
    1.8196
    1.8196
    1.8196
    1.8196
    1.8194
    1.8196
    1.8196
    1.8196
    1.8197
    1.8196
    1.8196
    1.8196
    1.8197
    1.8197
iter =
    25

随机选择一组输出如上所示。其中定义算法收敛的相对误差限epsilon为 1×103

猜你喜欢

转载自blog.csdn.net/qq_37043191/article/details/77528983