英文:
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(
"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.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))
}
答案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 (
"fmt"
"math/rand"
"time"
)
// 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 <= 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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论