Roulette Selection in Genetic Algorithms

后端 未结 14 956
庸人自扰
庸人自扰 2020-11-27 12:19

Can anyone provide some pseudo code for a roulette selection function? How would I implement this:

14条回答
  •  独厮守ぢ
    2020-11-27 13:01

    Here's a compact java implementation I wrote recently for roulette selection, hopefully of use.

    public static gene rouletteSelection()
    {
        float totalScore = 0;
        float runningScore = 0;
        for (gene g : genes)
        {
            totalScore += g.score;
        }
    
        float rnd = (float) (Math.random() * totalScore);
    
        for (gene g : genes)
        {   
            if (    rnd>=runningScore &&
                    rnd<=runningScore+g.score)
            {
                return g;
            }
            runningScore+=g.score;
        }
    
        return null;
    }
    

提交回复
热议问题