-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulations_binary_code.py
133 lines (123 loc) · 4.61 KB
/
simulations_binary_code.py
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
import binary_code as bc
import numpy as np
from matplotlib import pyplot as plt
import pickle
import time
def singleTest(numRow, numCol, dtype='float32'):
k = numRow * numCol # number of original tasks
# dtype = 'uint8' # binary operationration
# dtype = 'float32' # real number operation
# print('rlnc')
enc1 = bc.RLNCEncoder(numRow, numCol, sysPhase=True, dtype=dtype)
dec1 = bc.Decoder()
decodable = False # a flag indicating whether the k tasks can be decoded
counter1 = 0
n = k * 3
order = np.random.permutation(n)
coeffs = [enc1.getCoeff() for i in range(n)]
while not decodable:
useful, decodable = dec1.receive(coeffs[order[counter1]])
counter1 += 1
# print('lt')
enc2 = bc.LTEncoder(numRow, numCol, dtype=dtype)
dec2 = bc.Decoder()
decodable = False # a flag indicating whether the k tasks can be decoded
counter2 = 0
while not decodable:
useful, decodable = dec2.receive(enc2.getCoeff())
counter2 += 1
return counter1 - k, counter2 - k
def simulations(testNum, dtype='uint8'):
kTest = range(100, 1001, 100)
validComb = {}
print(dtype)
for k in kTest:
validComb[k] = bc.decompose2D(k, k, k)
print(validComb[k])
print("initializaed")
redun = {}
redun['rlnc'] = {}
redun['lt'] = {}
start = time.time()
for k in kTest:
print(k)
redun['rlnc'][k] = np.zeros(testNum)
redun['lt'][k] = np.zeros(testNum)
combs = validComb[k]
lenCombs = len(combs)
for t in range(testNum):
print(t)
numRow, numCol = combs[np.random.randint(0, lenCombs)]
r1, r2 = singleTest(numRow, numCol, dtype)
redun['rlnc'][k][t] = r1
redun['lt'][k][t] = r2
print(np.mean(redun['rlnc'][k]), np.mean(redun['lt'][k]))
print("duration: ", time.time() - start)
#############################################
# numRow = range(5, 41, 5)
# redun = {}
# redun['rlnc'] = {}
# redun['lt'] = {}
# testNum = 10
# start = time.time()
# for r in numRow:
# redun['rlnc'][r ** 2] = []
# redun['lt'][r ** 2] = []
# print(r ** 2)
# for t in range(testNum):
# print(t)
# redun1, redun2 = singleTest(r, r)
# redun['rlnc'][r ** 2].append(redun1)
# redun['lt'][r ** 2].append(redun2)
# print("duration: ", time.time() - start)
#############################################
redun['kList'] = kTest
with open('20171204_' + dtype +'.pickle', 'wb') as handle:
pickle.dump(redun, handle)
assert False
kTest = range(100, 1001, 100)
with open('./results/20171129_float.pickle', 'rb') as handle:
redun1 = pickle.load(handle)
with open('20171204_binary.pickle', 'rb') as handle:
redun2 = pickle.load(handle)
# with open('20171129_binary.pickle', 'rb') as handle:
# redun2 = pickle.load(handle)
plt.plot(redun1['kList'], [(np.mean(redun1['rlnc'][k]) * 50 + np.mean(redun1['rlnc'][k]) * 10) / 60 for k in redun1['kList']],
label='RLNC - real', linewidth=3)
# plt.plot(redun2['kList'], [np.mean(redun2['rlnc'][k]) for k in redun2['kList']],
# label='RLNC - binary', linewidth=3)
plt.plot(redun1['kList'], [(np.mean(redun1['lt'][k]) * 50 + np.mean(redun1['rlnc'][k]) * 10) / 60 for k in redun1['kList']],
label='LT code - real', linewidth=3)
# plt.plot(redun2['kList'], [np.mean(redun2['lt'][k]) for k in redun2['kList']],
# label='LT code - binary', linewidth=3)
plt.legend(loc='best', fontsize=16)
plt.xlabel("$K=s*t$", fontsize=18)
plt.ylabel('Average number of extra workers $\delta$', fontsize=18)
plt.title("Synthesis Binary RLNC and LT codes over $\mathbb{R}$\n" +
"can both asymptotically achieve the recovery threshold $K$ " +
"as $K+\delta$", fontsize=18)
plt.xlim(0, 1000)
plt.ylim(0, 10)
plt.grid()
plt.show()
testNum = 10
dtype = 'float32'
# simulations(testNum, dtype)
# numRow = range(5, 41, 5)
# with open('r1.pickle', 'rb') as handle:
# redun = pickle.load(handle)
# for key in ['rlnc', 'lt']:
# plt.plot(redun['kList'], [np.mean(redun[key][k]) for k in redun['kList']],
# label=key)
# plt.legend(loc='best')
# plt.xlabel("$r^2$")
# plt.ylabel('average number of extra workers')
# plt.grid()
# plt.show()
# with open('r1.pickle', 'rb') as handle:
# redun = pickle.load(handle)
# # print(redun[1225])
# numRow = range(5, 40, 5)
# kList = [r ** 2 for r in numRow[1:]]
# plt.plot(kList, [np.mean(redun[k]) / k for k in kList])
# plt.show()