2021-04-05 14:48:01 +00:00
|
|
|
package connections
|
|
|
|
|
|
|
|
import "math/rand"
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Sampler implements weighted random number generation using Vose's Alias
|
|
|
|
// Method (https://www.keithschwarz.com/darts-dice-coins/).
|
2021-04-05 17:10:03 +00:00
|
|
|
type Sampler struct {
|
2021-04-05 14:48:01 +00:00
|
|
|
randomGenerator *rand.Rand
|
|
|
|
probabilities []float64
|
|
|
|
alias []int
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// NewSampler creates new Sampler with a given set of probabilities using
|
|
|
|
// given source of randomness. Created Sampler will produce numbers from
|
|
|
|
// 0 to len(probabilities).
|
2021-04-05 17:10:03 +00:00
|
|
|
func NewSampler(probabilities []float64, source rand.Source) *Sampler {
|
2021-04-05 17:13:49 +00:00
|
|
|
sampler := &Sampler{}
|
2021-04-05 14:48:01 +00:00
|
|
|
var (
|
|
|
|
small workList
|
|
|
|
large workList
|
|
|
|
)
|
|
|
|
n := len(probabilities)
|
2021-04-05 17:13:49 +00:00
|
|
|
sampler.randomGenerator = rand.New(source)
|
|
|
|
sampler.probabilities = make([]float64, n)
|
|
|
|
sampler.alias = make([]int, n)
|
2021-04-05 14:48:01 +00:00
|
|
|
// Compute scaled probabilities.
|
|
|
|
p := make([]float64, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
p[i] = probabilities[i] * float64(n)
|
|
|
|
}
|
|
|
|
for i, pi := range p {
|
|
|
|
if pi < 1 {
|
2021-04-06 09:06:11 +00:00
|
|
|
small.add(i)
|
2021-04-05 14:48:01 +00:00
|
|
|
} else {
|
2021-04-06 09:06:11 +00:00
|
|
|
large.add(i)
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-06 09:06:11 +00:00
|
|
|
for len(small) > 0 && len(large) > 0 {
|
|
|
|
l, g := small.remove(), large.remove()
|
2021-04-05 17:13:49 +00:00
|
|
|
sampler.probabilities[l] = p[l]
|
|
|
|
sampler.alias[l] = g
|
2021-04-06 09:06:11 +00:00
|
|
|
p[g] = p[g] + p[l] - 1
|
2021-04-05 14:48:01 +00:00
|
|
|
if p[g] < 1 {
|
2021-04-06 09:06:11 +00:00
|
|
|
small.add(g)
|
2021-04-05 14:48:01 +00:00
|
|
|
} else {
|
2021-04-06 09:06:11 +00:00
|
|
|
large.add(g)
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for len(large) > 0 {
|
2021-04-06 09:06:11 +00:00
|
|
|
g := large.remove()
|
2021-04-05 17:13:49 +00:00
|
|
|
sampler.probabilities[g] = 1
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
for len(small) > 0 {
|
2021-04-06 09:06:11 +00:00
|
|
|
l := small.remove()
|
2021-04-05 17:13:49 +00:00
|
|
|
sampler.probabilities[l] = 1
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
2021-04-05 17:13:49 +00:00
|
|
|
return sampler
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Next returns the next (not so) random number from Sampler.
|
2021-04-05 17:10:03 +00:00
|
|
|
func (g *Sampler) Next() int {
|
2021-04-05 14:48:01 +00:00
|
|
|
n := len(g.alias)
|
|
|
|
i := g.randomGenerator.Intn(n)
|
|
|
|
if g.randomGenerator.Float64() < g.probabilities[i] {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return g.alias[i]
|
|
|
|
}
|
2021-04-06 09:06:11 +00:00
|
|
|
|
|
|
|
type workList []int
|
|
|
|
|
|
|
|
func (wl *workList) add(e int) {
|
|
|
|
*wl = append(*wl, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wl *workList) remove() int {
|
|
|
|
l := len(*wl) - 1
|
|
|
|
n := (*wl)[l]
|
|
|
|
*wl = (*wl)[:l]
|
|
|
|
return n
|
|
|
|
}
|