Skip to content

Commit ffa53ca

Browse files
committed
correct merge finction
1 parent 840164f commit ffa53ca

File tree

5 files changed

+108
-27
lines changed

5 files changed

+108
-27
lines changed

main.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ source("/home/amukam/thss/experiments/uniform-sample/dkl.r")
22

33
experiment = "KL1"
44
"--------------------------"
5-
times <- 2
6-
for (i in 1:times){
5+
d1 <- divergence("data/resultJul95trunk")
6+
times <- 5
7+
for (i in 3:times){
78
output = paste("data/output", i, sep="")
89
dKL(c(i, output, experiment))
910
}

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ func main() {
1515
input_path = *path
1616
switch *number {
1717
case 0:
18-
//test0()
19-
check_cms()
18+
test0()
19+
//check_cms()
2020
case 1:
2121
test_omniscient()
2222
case 2:
23-
test_Knowledge_free()
23+
//test_Knowledge_free()
24+
test_Knowledge_free_with_small_set()
2425
default:
2526
panic("Wrong number")
2627
}

network/countminsketch.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ func (cms *CMS) getMinMatrix() (min uint64) {
4848
return
4949
}
5050

51+
func getMinFreq() (min uint64) {
52+
min = 0
53+
for i := range in_memory {
54+
freq := PeerCMS.EstimateString(i)
55+
if freq < min || min == 0 {
56+
min = freq
57+
}
58+
}
59+
return
60+
}
61+
5162
func (cms *CMS) Knowledge_free(peer string) (output_choice string) {
5263
var cmsRand = rand.New(
5364
rand.NewSource(time.Now().UnixNano())) /* RNG generator */
@@ -59,25 +70,34 @@ func (cms *CMS) Knowledge_free(peer string) (output_choice string) {
5970
}
6071

6172
} else {
62-
freq := PeerCMS.EstimateString(peer)
63-
if freq == 0 {
64-
log.Panicf("Element %s has not been registered yet !!!\n", peer)
73+
74+
var prob float64
75+
if !in_memory[peer] {
76+
prob = 1.0
77+
} else {
78+
//min := cms.getMinMatrix()
79+
min := getMinFreq()
80+
freq := PeerCMS.EstimateString(peer)
81+
if freq == 0 {
82+
log.Panicf("Element %s has not been registered yet !!!\n", peer)
83+
}
84+
prob = float64(min) / float64(freq) // aj <= 1
85+
//fmt.Println("min", min, "prob", prob)
6586
}
66-
min := cms.getMinMatrix()
6787

68-
prob := float64(min) / float64(freq) // aj <= 1
69-
choice := cmsRand.Float64() // random choice in [0.0, 1.0[
88+
choice := cmsRand.Float64() // random choice in [0.0, 1.0[
89+
//fmt.Println("choice", choice)
90+
if choice < prob && !in_memory[peer] { // try to add only not known elements
7091

71-
if choice < prob {
7292
sample_choice_index := cmsRand.Intn(C) //uniform random choice
73-
if !in_memory[peer] {
74-
k := Sample_memory[sample_choice_index]
75-
delete(in_memory, k)
76-
Sample_memory[sample_choice_index] = peer // add j
77-
in_memory[peer] = true
78-
}
93+
k := Sample_memory[sample_choice_index]
94+
delete(in_memory, k)
95+
Sample_memory[sample_choice_index] = peer // add j
96+
in_memory[peer] = true
97+
//fmt.Println("change", k, peer)
7998
}
8099
}
100+
//fmt.Println("Memory", Sample_memory)
81101
output_choice_index := cmsRand.Intn(len(Sample_memory)) //uniform random choice
82102
output_choice = Sample_memory[output_choice_index] // k'
83103
//fmt.Printf("Sample memory : %v \n in_memory: %v\n", Sample_memory, in_memory)
@@ -170,18 +190,19 @@ func (s *CMS) Merge(other *CMS) error {
170190
return errors.New("countminsketch: matrix depth must match")
171191
}
172192

173-
if s.k != other.s {
193+
if s.k != other.k {
174194
return errors.New("countminsketch: matrix width must match")
175195
}
176196

177197
for i := uint(0); i < s.s; i++ {
178198
for j := uint(0); j < s.k; j++ {
179199
// s.mat[i][j] += other.mat[i][j] ADD
180-
val := uint64((s.mat[i][j] + other.mat[i][j]) / 2) /* MOY */
181-
if val < 1 {
200+
var val float64
201+
val = (float64(s.mat[i][j]) + float64(other.mat[i][j])) / 2 /* MOY */
202+
if 0.0 < val && val < 1.0 {
182203
s.mat[i][j] = 1
183204
} else {
184-
s.mat[i][j] = val
205+
s.mat[i][j] = uint64(val)
185206
}
186207
}
187208
}

scripts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ p="data/resultJul95"
66
#p="data/resultess_1"
77
for i in 2
88
do
9-
for j in {1..2}
9+
for j in {3..5}
1010
do
1111
./main -t=$i -n=$j -p=$p
1212
done

test.go

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func test0() {
5252

5353
d := 50
5454
w := 40
55-
network.C = 10 //int(d)
55+
network.C = 4 //int(d)
5656
network.PeerCMS = network.InitCMS(uint(d), uint(w))
5757
//fmt.Println(network.PeerCMS.MatToString())
5858

@@ -62,7 +62,7 @@ func test0() {
6262
//var listpeers = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
6363

6464
var output = []string{}
65-
fmt.Println("Memory", network.Sample_memory)
65+
fmt.Println("Memory", network.Sample_memory, network.C)
6666

6767
for i, elm := range listpeers {
6868
println(">>>>>>>>>>>>> ELEMENT", i+1, " ", elm)
@@ -106,8 +106,8 @@ func test_Knowledge_free() {
106106
n := network.Read_occurence(listpeers)
107107
/* CMS Parameters */
108108

109-
//k := int(math.Ceil(0.01 * float64(n))) // other test to do
110-
k := int(math.Ceil(math.Log(float64(n)))) // round to the next integer
109+
k := int(math.Ceil(0.01 * float64(n))) // other test to do
110+
//k := int(math.Ceil(math.Log(float64(n)))) // round to the next integer
111111
s := 10
112112
network.C = 300
113113

@@ -253,3 +253,61 @@ func check_cms() {
253253

254254
return
255255
}
256+
257+
func test_Knowledge_free_with_small_set() {
258+
259+
path := input_path
260+
var listpeers, err = readLines(path)
261+
if err != nil {
262+
log.Println("(check) Unable to read config file ", path)
263+
return
264+
}
265+
266+
m := len(listpeers)
267+
log.Println("Input of size ", m)
268+
269+
n := network.Read_occurence(listpeers)
270+
/* CMS Parameters */
271+
272+
k := int(math.Ceil(0.01 * float64(n))) // other test to do
273+
//k := int(math.Ceil(math.Log(float64(n)))) // round to the next integer
274+
s := 10
275+
network.C = 300
276+
277+
network.PeerCMS = network.InitCMS(uint(s), uint(k))
278+
279+
fmt.Println(network.PeerCMS.MatToString())
280+
281+
/* Knowledge free algorithm */
282+
var output = []string{}
283+
trunk := 1000
284+
train := 100000
285+
begin := m - trunk - train
286+
fmt.Printf("Read %d elements between from element %d \n", trunk, begin)
287+
for _, elm := range listpeers[begin : m-trunk] {
288+
//println(">elmt", i)
289+
network.PeerCMS.UpdateString(elm, 1)
290+
291+
}
292+
for _, elm := range listpeers[m-trunk:] { //only trunk elements in the output
293+
//println(">elmt", i)
294+
network.PeerCMS.UpdateString(elm, 1)
295+
296+
value := network.PeerCMS.Knowledge_free(elm)
297+
output = append(output, value)
298+
299+
}
300+
fmt.Println(network.PeerCMS.MatToString())
301+
/* summary */
302+
fmt.Printf("A matrix of size %d*%d with sample memory length of %d\n Output of size %d\n",
303+
s, k, len(network.Sample_memory), len(output))
304+
305+
out_path := "data/output" + strconv.Itoa(num_expe) // path of the unbiaised output stream
306+
err = writeLines(output, out_path)
307+
if err != nil {
308+
log.Println("(Write) Unable to read config file ", out_path)
309+
return
310+
}
311+
312+
return
313+
}

0 commit comments

Comments
 (0)