第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Golang 在遺傳算法中選擇輪盤賭

使用 Golang 在遺傳算法中選擇輪盤賭

Go
江戶川亂折騰 2021-11-15 15:31:43
我正在為遺傳算法構(gòu)建一個(gè)模擬輪盤選擇函數(shù)。首先,我會(huì)想加起來(lái)sum的fitnessScore主功能。加起來(lái)后,fitnessScore我想sum使用math/randGo 中的包隨機(jī)化一個(gè)值。在這種情況下我應(yīng)該如何使用 rand 包如何修復(fù)spin_wheel := rand.sum以隨機(jī)化一個(gè)值?package mainimport(    "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 回答

?
千萬(wàn)里不及你

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

例如,


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))

}


查看完整回答
反對(duì) 回復(fù) 2021-11-15
  • 1 回答
  • 0 關(guān)注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)