Ranking Selection in Genetic Algorithm code

浪尽此生 提交于 2019-11-29 02:30:11

Rank selection is easy to implement when you already know on roulette wheel selection. Instead of using the fitness as probability for getting selected you use the rank. So for a population of N solutions the best solution gets rank N, the second best rank N-1, etc. The worst individual has rank 1. Now use the roulette wheel and start selecting.

The probability for the best individual to be selected is N/( (N * (N+1))/2 ) or roughly 2 / N, for the worst individual it is 2 / (N*(N+1)) or roughly 2 / N^2.

This is called linear rank selection, because the ranks form a linear progression. You can also think of ranks forming a geometric progression, such as e.g 1 / 2^n where n is ranging from 1 for the best individual to N for the worst. This of course gives much higher probability to the best individual.

You can look at the implementation of some selection methods in HeuristicLab.

Setu Kumar Basak

My code of Rank Selection in MatLab:

NewFitness=sort(Fitness);
NewPop=round(rand(PopLength,IndLength));

for i=1:PopLength
    for j=1:PopLength
        if(NewFitness(i)==Fitness(j))
            NewPop(i,1:IndLength)=CurrentPop(j,1:IndLength);
            break;
        end
    end
end
CurrentPop=NewPop;

ProbSelection=zeros(PopLength,1);
CumProb=zeros(PopLength,1);

for i=1:PopLength
    ProbSelection(i)=i/PopLength;
    if i==1
        CumProb(i)=ProbSelection(i);
    else
        CumProb(i)=CumProb(i-1)+ProbSelection(i);
    end
end

SelectInd=rand(PopLength,1);

for i=1:PopLength
    flag=0;
    for j=1:PopLength
        if(CumProb(j)<SelectInd(i) && CumProb(j+1)>=SelectInd(i))
            SelectedPop(i,1:IndLength)=CurrentPop(j+1,1:IndLength);
            flag=1;
            break;
        end
    end
    if(flag==0)
        SelectedPop(i,1:IndLength)=CurrentPop(1,1:IndLength);
    end
end

I've made a template genetic-algorithm class in C++.

My library of genetic algorithm is separated from GeneticAlgorithm and GAPopulation. Those are all template classes so that you can see its origin code in API Documents.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!