遗传算法中使用Golang实现的轮盘赌选择

huangapple go评论100阅读模式
英文:

Roulette Wheel Selection in Genetic Algorithm using Golang

问题

我正在为遗传算法构建一个模拟轮盘赌选择函数。首先,我想在主函数中计算fitnessScore的总和sum。在计算完fitnessScore的总和后,我想使用Go语言中的math/rand包从该总和中随机选择一个值。在这种情况下,我应该如何使用rand包?如何修复spin_wheel := rand.sum以随机选择一个值?

package main

import (
	"fmt"
	"time"
	"math/rand"
)

func rouletteWheel(fitnessScore []float64) []float64 {
	sum := 0.0
	for i := 0; i < len(fitnessScore); i++ {
		sum += fitnessScore[i]
	}

	rand.Seed(time.Now().UnixNano())
	spin_wheel := rand.Float64() * sum
	partial_sum := 0.0
	for i := 0; i < len(fitnessScore); i++ {
		partial_sum += fitnessScore[i]
		if partial_sum >= spin_wheel {
			return fitnessScore
		}
	}
	return fitnessScore
}

func main() {
	fitnessScore := []float64{0.1, 0.2, 0.3, 0.4}
	fmt.Println(rouletteWheel(fitnessScore))
}

在这个代码中,你可以使用rand.Float64()函数生成一个0到1之间的随机浮点数,并将其乘以sum来获取一个在0到sum之间的随机值。这样,你就可以使用spin_wheel来进行随机选择了。

英文:

I'm building a mock roulette wheel selection function for genetic algorithm. First of, I would want to add up the sum of the fitnessScore in the main function. After adding up the fitnessScore I wanted to randomize a value out of that sum using the math/rand package in Go. How should I use the rand package in this scenario how do I fix spin_wheel := rand.sum in order to random a value?

package main

import(
	&quot;fmt&quot;
	&quot;time&quot;
	&quot;math/rand&quot;
)

func rouletteWheel(fitnessScore []float64) []float64{
	sum := 0.0
	for i := 0; i &lt; len(fitnessScore); i++ {
		sum += fitnessScore[i]
	}
	
	rand.Seed(time.Now().UnixNano())
	spin_wheel := rand.sum
	partial_sum := 0.0
	for i := 0; i &lt; len(fitnessScore); i++{
		partial_sum += fitnessScore[i]
		if(partial_sum &gt;= spin_wheel){
			return fitnessScore
		}
	}
	return fitnessScore
}

func main(){
	fitnessScore := []float64{0.1, 0.2, 0.3, 0.4}
	fmt.Println(rouletteWheel(fitnessScore))
}

答案1

得分: 1

例如,

package main

import (
    "fmt"
    "math/rand"
    "time"
)

// 根据权重(概率)返回所选的权重
// 适应度比例选择:
// https://en.wikipedia.org/wiki/Fitness_proportionate_selection
func rouletteSelect(weights []float64) float64 {
    // 计算总权重
    sum := 0.0
    for _, weight := range weights {
        sum += weight
    }
    // 获取一个随机值
    value := rand.Float64() * sum
    // 根据权重定位随机值
    for _, weight := range weights {
        value -= weight
        if value <= 0 {
            return weight
        }
    }
    // 仅在出现舍入误差时
    return weights[len(weights)-1]
}

func main() {
    rand.Seed(time.Now().UnixNano())
    weights := []float64{0.1, 0.2, 0.3, 0.4}
    fmt.Println(rouletteSelect(weights))
}
英文:

For example,

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;time&quot;
)

// Returns the selected weight based on the weights(probabilities)
// Fitness proportionate selection:
// https://en.wikipedia.org/wiki/Fitness_proportionate_selection
func rouletteSelect(weights []float64) float64 {
	// calculate the total weights
	sum := 0.0
	for _, weight := range weights {
		sum += weight
	}
	// get a random value
	value := rand.Float64() * sum
	// locate the random value based on the weights
	for _, weight := range weights {
		value -= weight
		if value &lt;= 0 {
			return weight
		}
	}
	// only when rounding errors occur
	return weights[len(weights)-1]
}

func main() {
	rand.Seed(time.Now().UnixNano())
	weights := []float64{0.1, 0.2, 0.3, 0.4}
	fmt.Println(rouletteSelect(weights))
}

huangapple
  • 本文由 发表于 2015年10月8日 07:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/33003974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定