Slide 18
Slide 18 text
GoConference’19
3. બʢSelectionʣ
ڥʹదԠͰ͖Δݸମ΄Ͳ࣍ੈʹࣗͷҨࢠΛͤΔΑ͏ʹ͢ΔͨΊʹɺ
ݸମͷదԠʹԠͯ͡ੜ͖ΔݸମΛબ͢Δ
18
τʔφϝϯτબʢUPVSOBNFOUTFMFDUJPOʣ
ϥϯμϜʹநग़ͨ͠4ݸͷݸମͷ͏ͪɺ
࠷దԠ͕ߴ͍ݸମΛબ
" # $ %
దԠ
$
func Select(indi Individuals) (Individuals, error) {
if len(indi) < t.NContestants {
return nil, errors.New("NContestants is too large")
}
selected := make(Individuals, len(indi))
for i := range selected {
winner := indi[rand.Intn(len(indi))]
for j := 0; j < t.NContestants; j++ {
next := indi[rand.Intn(len(indi))]
if winner.Fitness > next.Fitness {
winner = next
}
}
selected[i] = winner
}
return selected, nil
}