-
Notifications
You must be signed in to change notification settings - Fork 6
/
aliasmethod.go
152 lines (123 loc) · 2.57 KB
/
aliasmethod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package aliasmethod
import (
"container/list"
"math/rand"
"time"
)
const (
kProbability = 1.0
)
type Item interface {
GetWeight() int32
}
type AliasMethod[T Item] struct {
r *rand.Rand
alias []int // 別名表
probs []float64 // 概率表
items []T
}
func New[T Item]() *AliasMethod[T] {
var m = &AliasMethod[T]{}
m.r = rand.New(rand.NewSource(time.Now().UnixNano()))
return m
}
func (m *AliasMethod[T]) Add(item T) {
m.items = append(m.items, item)
}
func (m *AliasMethod[T]) Prepare() bool {
if len(m.items) == 0 {
return false
}
var total = int32(0)
for _, item := range m.items {
total += item.GetWeight()
}
var scale = float64(total) / kProbability
var probs = make([]float64, 0, len(m.items))
for _, item := range m.items {
probs = append(probs, float64(item.GetWeight())/scale)
}
return m.process(probs)
}
func (m *AliasMethod[T]) process(probs []float64) bool {
var pLen = len(probs)
var avg = kProbability / float64(pLen)
m.alias = make([]int, pLen)
m.probs = make([]float64, pLen)
var smallList = list.New()
var largeList = list.New()
for index, value := range probs {
if value >= avg {
largeList.PushBack(index)
} else {
smallList.PushBack(index)
}
}
for {
var smallElement = smallList.Back()
var largeElement = largeList.Back()
if smallElement == nil || largeElement == nil {
break
}
var less = 0
var more = 0
if v, ok := smallElement.Value.(int); ok {
less = v
}
if v, ok := largeElement.Value.(int); ok {
more = v
}
m.probs[less] = probs[less] * float64(pLen)
m.alias[less] = more
probs[more] = probs[more] + probs[less] - avg
if probs[more] >= kProbability/float64(pLen) {
largeList.PushBack(more)
} else {
smallList.PushBack(more)
}
largeList.Remove(largeElement)
smallList.Remove(smallElement)
}
for {
var smallElement = smallList.Back()
if smallElement == nil {
break
}
if v, ok := smallElement.Value.(int); ok {
m.probs[v] = kProbability
}
smallList.Remove(smallElement)
}
for {
var largeElement = largeList.Back()
if largeElement == nil {
break
}
if v, ok := largeElement.Value.(int); ok {
m.probs[v] = kProbability
}
largeList.Remove(largeElement)
}
return true
}
func (m *AliasMethod[T]) Next() int {
var pLen = len(m.probs)
if pLen == 0 {
return -1
}
var index = m.r.Intn(pLen)
var value = m.r.Float64()
var coin = value < m.probs[index]
if coin {
return index
}
return m.alias[index]
}
func (m *AliasMethod[T]) NextItem() T {
var index = m.Next()
var item T
if index >= 0 {
item = m.items[index]
}
return item
}