forked from MdMouinulIslam/DivGetBatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AugSwap_ml.py
321 lines (248 loc) · 8.93 KB
/
AugSwap_ml.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from pyclustering.utils import euclidean_distance_square
from sklearn.datasets.samples_generator import make_blobs
from clustering_final import Clustering
import heapq
from distance import min_distance, max_distance
import numpy as np
import timeit
#from normalization import normalized_X
from sklearn import preprocessing
from sklearn.preprocessing import normalize
def createSimMatrix(q, X):
r = {}
i = 0
for p in X:
d = euclidean_distance_square(q,p)
r[i] = 1/(1+d)
i = i + 1
return r
def createDisMatrix(X):
d = []
i = 0
for p1 in X:
dd = []
for p2 in X:
dist = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
dd.append(dist)
d.append(dd)
return d
def topkitems(sortedrecitems, k):
return sortedrecitems[0:k]
def calculateDiRetlist(X, retlistkeys, i):
diret = 0
for j in retlistkeys:
diret = diret + euclidean_distance_square(X[i], X[j])
return diret
SortedRecItems = []
swapnumber = 0
def swap(X,recitems,k,ub):
global swapnumber
SortedRecItems = sorted(recitems.items(), key=lambda x: x[1], reverse=True)
retlist = topkitems(SortedRecItems, k)
pos = k
retlistkeys, retlistvalues = zip(*retlist)
retlistkeys = list(retlistkeys)
diretlist = []
diretlistMap = {}
for i in retlistkeys:
diretval = calculateDiRetlist(X, retlistkeys, i)
t = (diretval, i)
diretlist.append(t)
diretlistMap[i] = diretval
M = []
for item in diretlist:
heapq.heappush(M, item)
# print(M)
i = heapq.heappop(M)
#print(i)
while ((recitems[i[1]] - SortedRecItems[pos][1]) < ub):
rlist = [item for item in retlist if item[0] == i[1]]
retlist.remove(rlist[0])
retlistkeys, retlistvalues = zip(*retlist)
retlistkeys = list(retlistkeys)
dsortedrec = calculateDiRetlist(X, retlistkeys, SortedRecItems[pos][0])
if (i[0] < dsortedrec):
swapnumber = swapnumber + 1
retlist.append(SortedRecItems[pos])
retlistkeys.append(SortedRecItems[pos][0])
heapq.heappush(M, (dsortedrec, SortedRecItems[pos][0]))
# update d values
diretlist = []
diretlistMap = {}
M = []
for j in retlistkeys:
diretval = calculateDiRetlist(X, retlistkeys, j)
# print("item ", i, " di = ", diretval)
t = (diretval, j)
diretlist.append(t)
diretlistMap[j] = diretval
heapq.heappush(M, t)
i = heapq.heappop(M)
# print(i)
else:
retlist.append(rlist[0])
pos = pos + 1
if (pos == len(SortedRecItems)):
break
print("swap ",swapnumber)
swapnumber = 0
return retlist
def calculateDiRetlistCluster_new(cluster,l,minmaxDitc,insid,delid,id):
maxdf, mindf = minmaxDitc[(id,l)]
minin,maxin = cluster.dismatrixitem[l][insid][id]
minddel,maxdel = cluster.dismatrixitem[l][delid][id]
maxdf = maxdf + maxin - maxdel
mindf = mindf + minin - minddel
minmaxDitc[(id,l)] = (maxdf, mindf)
return (maxdf, mindf)
def calculateDiRetlistCluster(cluster,l,retlistkeys,id):
maxdf, mindf = (0.0, 0.0)
for j in retlistkeys:
mind,maxd = cluster.dismatrixitem[l][j][id]
mindf = mindf + mind
maxdf = maxdf + maxd
return (maxdf, mindf)
def getNext(cluster,numberOfLevels,minmaxDitc, i, retlistkeys,itdel,itin):
ret = []
retids = {-1}
if itin is None or itdel is None:
clusters = cluster.root.children
l = 1
while (l <= numberOfLevels):
clusterTocheck = []
for node in clusters:
maxdis, mindis = calculateDiRetlistCluster(cluster, l, retlistkeys, node.id)
minmaxDitc[(node.id,l)] = (maxdis,mindis)
if maxdis >= i[0]:
if l == numberOfLevels:
clusterTocheck.append(node)
else:
clusterTocheck.extend(node.children)
clusters = clusterTocheck
ret = clusters
l = l + 1
else:
clusters = cluster.root.children
l = 1
while (l < numberOfLevels):
clusterTocheck = []
for node in clusters:
maxdis, mindis = calculateDiRetlistCluster_new(cluster, l,minmaxDitc,itin,itdel, node.id)
if maxdis >= i[0]:
if l == numberOfLevels:
clusterTocheck.append(node)
else:
clusterTocheck.extend(node.children)
clusters = clusterTocheck
ret = clusters
l = l + 1
for n in ret:
retids.add(n.id)
return retids
skip = 0
def AugSwap(X,cluster,numberOfLevel,recitems,k,ub):
global swapnumber,skip
getNextTime = 0.0
SortedRecItems = sorted(recitems.items(), key=lambda x: x[1], reverse=True)
retlist = topkitems(SortedRecItems, k)
pos = k
retlistkeys, retlistvalues = zip(*retlist)
retlistkeys = list(retlistkeys)
diretlist = []
diretlistMap = {}
for i in retlistkeys:
diretval = calculateDiRetlist(X, retlistkeys, i)
# print("item ", i, " di = ", diretval )
t = (diretval, i)
diretlist.append(t)
diretlistMap[i] = diretval
M = []
for item in diretlist:
heapq.heappush(M, item)
# print(M)
i = heapq.heappop(M)
#print(i)
recalSkipList = True
candClsList = {}
minmaxdic = {}
newitem = None
while ((recitems[i[1]] - SortedRecItems[pos][1]) < ub):
rlist = [item for item in retlist if item[0] == i[1]]
retlist.remove(rlist[0])
retlistkeys, retlistvalues = zip(*retlist)
retlistkeys = list(retlistkeys)
if recalSkipList:
start = timeit.default_timer()
if newitem is None:
candClsList = getNext(cluster, numberOfLevel,minmaxdic, i, retlistkeys, None, None)
else:
candClsList = getNext(cluster,numberOfLevel,minmaxdic,i,retlistkeys,i[1],newitem[0])
stop = timeit.default_timer()
getNextTime = getNextTime + stop -start
if cluster.documentMap[tuple(X[SortedRecItems[pos][0]])][numberOfLevel] in candClsList:
dsortedrec = calculateDiRetlist(X, retlistkeys, SortedRecItems[pos][0])
else:
skip = skip + 1
pos = pos+1
recalSkipList = False
retlist.append(rlist[0])
if (pos == len(SortedRecItems)):
break
continue
if (i[0] < dsortedrec):
recalSkipList = True
swapnumber = swapnumber + 1
retlist.append(SortedRecItems[pos])
newitem = SortedRecItems[pos]
retlistkeys.append(SortedRecItems[pos][0])
minval = 10000000
candItem = None
for j in retlistkeys:
diretval = calculateDiRetlist(X, retlistkeys, j)
t = (diretval, j)
if minval > diretval:
minval = diretval
candItem = t
i = candItem
else:
recalSkipList = False
retlist.append(rlist[0])
pos = pos + 1
if (pos == len(SortedRecItems)):
break
print("awg swap number ",swapnumber)
print("skip ",skip)
print("getNext Time = ",getNextTime)
return retlist
def checkResult(augGmmResult, gmmResult):
if sorted(augGmmResult) == sorted(gmmResult):
print("array equal")
else:
print("array Not equal")
def run(numberOfSamples,numberofCluster,numberofLevel,k):
X, Y = make_blobs(n_samples=numberOfSamples, centers=1, n_features=3, random_state=2) #
X = X.tolist()
query = [0, 0,0]
r = createSimMatrix(query, X)
ub = 1000000
start = timeit.default_timer()
res = swap(X, r, k, ub)
stop = timeit.default_timer()
print('Time for calculate swap: ', stop - start)
print("swap: ",res)
indexMap = {}
index = 0
for e in X:
indexMap[tuple(e)] = index
index = index + 1
cluster = Clustering(X,numberofCluster,numberofLevel)
cluster.buildTree(cluster.root)
cluster.createLevelMatrix(cluster.root)
cluster.createDistanceMatrixforelements(numberofCluster,numberofLevel)
start = timeit.default_timer()
augres = AugSwap(X, cluster,numberofLevel, r, k, ub)
stop = timeit.default_timer()
print('Time for calculate Aug swap: ', stop - start)
print("AugSwap: ",augres)
checkResult(augres,res)
run(10000,5,3,20)