Roulette Selection in Genetic Algorithms

后端 未结 14 953
庸人自扰
庸人自扰 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 12:57

    This Swift 4 array extension implements weighted random selection, a.k.a Roulette selection from its elements:

    public extension Array where Element == Double {
    
        /// Consider the elements as weight values and return a weighted random selection by index.
        /// a.k.a Roulette wheel selection.
        func weightedRandomIndex() -> Int {
            var selected: Int = 0
            var total: Double = self[0]
    
            for i in 1..

    For example given the two element array:

    [0.9, 0.1]
    

    weightedRandomIndex() will return zero 90% of the time and one 10% of the time.

    Here is a more complete test:

    let weights = [0.1, 0.7, 0.1, 0.1]
    var results = [Int:Int]()
    let n = 100000
    for _ in 0..

    output:

    0.1 0.09906
    0.1 0.10126
    0.1 0.09876
    0.7 0.70092
    

    This answer is basically the same as Andrew Mao's answer here: https://stackoverflow.com/a/15582983/74975

提交回复
热议问题