-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplesource.go
236 lines (205 loc) · 5.67 KB
/
simplesource.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package disttopk
import (
"fmt"
"math"
"math/rand"
)
var _ = fmt.Println
type SimpleZipfSource struct {
MaxItems uint32
zipParam float64
zipNorm float64
scale float64
}
func NewSimpleZipfSource(maxItems uint32, param float64, nlists int) SimpleZipfSource {
var norm float64
norm = 0
i := uint32(1)
for i < (maxItems + 1) {
norm += math.Pow(float64(i), -param)
i++
}
minItem := math.Pow(float64(maxItems), -param) / norm
//we want minitem score to be 1 so:
// minZipfValue*scale = 1
// scale = 1/MinZipfValue
scaleBy := 1.0 / minItem
maxItem := math.Pow(float64(1), -param) / norm
if maxItem*float64(nlists)*scaleBy > math.MaxUint32 {
fmt.Println("Have to rescale to fit in uint32")
// has to be maxItem*nlists*scaleby = (math.MaxUint32-1)
//scaleby = (math.MaxUint32-1)/(maxItem*nlists)
scaleBy = (math.MaxUint32 - 1) / (maxItem * float64(nlists))
}
return SimpleZipfSource{maxItems, param, norm, scaleBy}
}
func (src *SimpleZipfSource) GenerateItem(rank int) Item {
id := rand.Int()
zipfValue := math.Pow(float64(rank), -src.zipParam) / src.zipNorm
//score := (zipfValue * src.scale) + float64(id%10)
score := zipfValue * src.scale
//fmt.Println("gen", zipfValue, score, (zipfValue * src.scale), src.scale, id, id%100)
act_score := float64(score)
if act_score < 1.0 {
act_score = 1.0
}
return Item{id, float64(uint(act_score))}
}
func (src *SimpleZipfSource) GetList() ItemList {
l := make([]Item, 0, src.MaxItems)
i := uint32(1)
sum := 0.0
for i < (src.MaxItems + 1) {
l = append(l, src.GenerateItem(int(i)))
sum += l[len(l)-1].Score
i++
}
//fmt.Println("sum = ", sum)
return ItemList(l)
}
func GetDisjointSimpleList(nlists int, nitemsPerList uint32, param float64) []ItemList {
src := NewSimpleZipfSource(nitemsPerList, param, nlists)
lists := make([]ItemList, nlists)
for k, _ := range lists {
l := src.GetList()
l = MakeSureItemsUnique(l)
lists[k] = l
}
return lists
}
func GetFullOverlapSimpleList(nlists int, nitemsPerList uint32, param float64) []ItemList {
src := NewSimpleZipfSource(nitemsPerList, param, nlists)
reference_list := src.GetList()
reference_list = MakeSureItemsUnique(reference_list)
lists := make([]ItemList, nlists)
for k, _ := range lists {
copy_list := make([]Item, len(reference_list))
copy(copy_list, reference_list)
lists[k] = copy_list
}
return lists
}
func GetFullOverlapOrderPermutedSimpleList(nlists int, nitemsPerList uint32, param float64, reorder int) []ItemList {
return GetFullOverlapOrderPermutedSimpleListSeed(nlists, nitemsPerList, param, reorder, 99)
}
func GetFullOverlapOrderPermutedSimpleListSeed(nlists int, nitemsPerList uint32, param float64, reorder int, seed int64) []ItemList {
return GetFullOverlapOrderPermutedSimpleListSeedOverlap(nlists, nitemsPerList, param, reorder, seed, 1.0)
}
func GetFullOverlapOrderPermutedSimpleListSeedOverlap(nlists int, nitemsPerList uint32, param float64, reorder int, seed int64, overlap float64) []ItemList {
rand.Seed(seed)
lists := GetFullOverlapSimpleList(nlists, nitemsPerList, param)
//reference_list := ItemList(make([]Item, len(lists[0])))
//copy(reference_list, lists[0])
m := make(map[int]float64)
for k, list := range lists {
/*if k == 0 { this is wrong makes the overlap with one list greater than with the rest
continue
}*/
newItems := int(float64(len(list)) * (1.0 - overlap))
list.AddToMap(m)
replaced := make(map[int]bool, len(list))
for i := 0; i < newItems; {
pos := rand.Intn(len(list))
if !replaced[pos] {
i++
replaced[pos] = true
//new_id := rand.Int()
ok := true
new_id := 0
for ok {
new_id = rand.Int()
_, ok = m[new_id]
}
m[new_id] = list[pos].Score
list[pos].Id = new_id
}
}
lists[k] = list
}
if reorder > 0 {
for k, list := range lists {
for pos, _ := range list {
to_reorder := reorder
if pos+to_reorder > len(list)-1 {
to_reorder = (len(list) - 1) - pos
}
if to_reorder == 0 {
continue
}
pos_to_reorder := rand.Intn(to_reorder)
new_pos := pos + pos_to_reorder
list[pos].Id, list[new_pos].Id = list[new_pos].Id, list[pos].Id
//fmt.Println("reordering after", list[pos], list[new_pos])
}
lists[k] = list
}
}
for k, list := range lists {
list = MakeSureItemsUnique(list)
list.Sort()
lists[k] = list
//fmt.Println(list[:10])
}
/*
true_list := GetTrueList(lists)
true_list.Sort()
m = reference_list.AddToMap(nil)
count := 0
for _, item := range true_list[0:10] {
_, ok := m[item.Id]
if ok {
count++
//fmt.Println("Overlapping is", item.Id)
}
}
fmt.Println("Overlap of top-k with reference,", float64(count)/10.0)
*/
/*first := lists[1]
m := first.AddToMap(nil)
for _, list := range lists {
count := 0
for _, item := range list {
_, ok := m[item.Id]
if ok {
count++
}
}
fmt.Println("Overlap,", float64(count)/float64(len(list)))
} */
return lists
}
func MakeSureItemsUnique(list ItemList) ItemList {
m := list.AddToMap(nil)
l := MakeItemList(m)
l.Sort()
return l
}
/*
func GetListSet(nlists int, nitems uint32, param float64, overlap float64) []ItemList {
src := NewZipfSource(nitems, param)
lists := make([]ItemList, nlists)
for k, _ := range lists {
lists[k] = src.GetList(k)
}
nOver := int(overlap * float64(nitems))
//nOver := 10
for i := 0; i <= nOver; i++ {
first := lists[0]
index := rand.Int() % len(first)
id := first[index].Id
for _, l := range lists[1:] {
index := rand.Int() % len(l)
//println("over", id, index, k)
l[index].Id = id
}
}
for k, l := range lists {
m := ItemList(l).AddToMap(nil)
//v, ok := m[2553153660041385501]
//println(v, ok)
l := MakeItemList(m)
l.Sort()
lists[k] = l
}
return lists
}*/