Skip to content

Commit

Permalink
Merge pull request #195 from oakmound/fix/weightedmap-choice-cumulative
Browse files Browse the repository at this point in the history
alg\selection: WeightedMapChoice previously did not appropriately use cumulative weights
  • Loading branch information
200sc authored Mar 12, 2022
2 parents 4cf652e + 0180c8b commit ed2b7a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion alg/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func weightedMapChoice(weightMap map[int]float64, rngFxn func() float64) int {
values[idx] = value
idx++
}

values = CumulativeWeights(values)
return keys[weightedChooseOne(values, rngFxn)]
}

Expand Down
13 changes: 13 additions & 0 deletions alg/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ func TestWeightedMapChoice(t *testing.T) {
if chosen < 0 || chosen > 9 {
t.Fatalf("WeightedMapChoiceSeeded returned impossible value")
}

m2 := map[int]float64{
0: 0,
1: 0.5,
2: 0,
4: 0,
5: 0.5,
}
chosen = WeightedMapChoiceSeeded(m2, rand.New(rand.NewSource(0)))
if chosen != 1 && chosen != 5 {
t.Fatalf("WeightedMapChoiceSeeded returned impossible value")
}

}

func TestWeightedChooseOne(t *testing.T) {
Expand Down

0 comments on commit ed2b7a1

Please sign in to comment.