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

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

Roulette Wheel Selection in Genetic Algorithm using Golang

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "math/rand"
  6. )
  7. func rouletteWheel(fitnessScore []float64) []float64 {
  8. sum := 0.0
  9. for i := 0; i < len(fitnessScore); i++ {
  10. sum += fitnessScore[i]
  11. }
  12. rand.Seed(time.Now().UnixNano())
  13. spin_wheel := rand.Float64() * sum
  14. partial_sum := 0.0
  15. for i := 0; i < len(fitnessScore); i++ {
  16. partial_sum += fitnessScore[i]
  17. if partial_sum >= spin_wheel {
  18. return fitnessScore
  19. }
  20. }
  21. return fitnessScore
  22. }
  23. func main() {
  24. fitnessScore := []float64{0.1, 0.2, 0.3, 0.4}
  25. fmt.Println(rouletteWheel(fitnessScore))
  26. }

在这个代码中,你可以使用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?

  1. package main
  2. import(
  3. &quot;fmt&quot;
  4. &quot;time&quot;
  5. &quot;math/rand&quot;
  6. )
  7. func rouletteWheel(fitnessScore []float64) []float64{
  8. sum := 0.0
  9. for i := 0; i &lt; len(fitnessScore); i++ {
  10. sum += fitnessScore[i]
  11. }
  12. rand.Seed(time.Now().UnixNano())
  13. spin_wheel := rand.sum
  14. partial_sum := 0.0
  15. for i := 0; i &lt; len(fitnessScore); i++{
  16. partial_sum += fitnessScore[i]
  17. if(partial_sum &gt;= spin_wheel){
  18. return fitnessScore
  19. }
  20. }
  21. return fitnessScore
  22. }
  23. func main(){
  24. fitnessScore := []float64{0.1, 0.2, 0.3, 0.4}
  25. fmt.Println(rouletteWheel(fitnessScore))
  26. }

答案1

得分: 1

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. // 根据权重(概率)返回所选的权重
  8. // 适应度比例选择:
  9. // https://en.wikipedia.org/wiki/Fitness_proportionate_selection
  10. func rouletteSelect(weights []float64) float64 {
  11. // 计算总权重
  12. sum := 0.0
  13. for _, weight := range weights {
  14. sum += weight
  15. }
  16. // 获取一个随机值
  17. value := rand.Float64() * sum
  18. // 根据权重定位随机值
  19. for _, weight := range weights {
  20. value -= weight
  21. if value <= 0 {
  22. return weight
  23. }
  24. }
  25. // 仅在出现舍入误差时
  26. return weights[len(weights)-1]
  27. }
  28. func main() {
  29. rand.Seed(time.Now().UnixNano())
  30. weights := []float64{0.1, 0.2, 0.3, 0.4}
  31. fmt.Println(rouletteSelect(weights))
  32. }
英文:

For example,

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;math/rand&quot;
  5. &quot;time&quot;
  6. )
  7. // Returns the selected weight based on the weights(probabilities)
  8. // Fitness proportionate selection:
  9. // https://en.wikipedia.org/wiki/Fitness_proportionate_selection
  10. func rouletteSelect(weights []float64) float64 {
  11. // calculate the total weights
  12. sum := 0.0
  13. for _, weight := range weights {
  14. sum += weight
  15. }
  16. // get a random value
  17. value := rand.Float64() * sum
  18. // locate the random value based on the weights
  19. for _, weight := range weights {
  20. value -= weight
  21. if value &lt;= 0 {
  22. return weight
  23. }
  24. }
  25. // only when rounding errors occur
  26. return weights[len(weights)-1]
  27. }
  28. func main() {
  29. rand.Seed(time.Now().UnixNano())
  30. weights := []float64{0.1, 0.2, 0.3, 0.4}
  31. fmt.Println(rouletteSelect(weights))
  32. }

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:

确定