-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNNrefractored.py
976 lines (914 loc) · 26 KB
/
NNrefractored.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
import heapq, time, sys, csv
class node(object):
def __init__(self, Region = None, index = None, father = None, MinKey = None, MaxKey = None, Children = None, prob = 1):
self.Region = Region
self.index = index
self.father = father
self.MinKey = NumMinKey
self.MaxKey = NumMaxKey
self.Children = []
self.prob = prob
def RegionArea(Region):
Area = 1
for i in range(NumDimension):
minstr = str(i) + 'min'
maxstr = str(i) + 'max'
Area *= Region[maxstr] - Region[minstr]
return Area
pass
def IncreasedRegion(Region1, Region2):
EnclosingDict = dict.fromkeys(DefaultKeys, None)
for i in range(NumDimension):
minstr = str(i) + 'min'
maxstr = str(i) + 'max'
EnclosingDict[minstr] = min(Region1[minstr], Region2[minstr])
EnclosingDict[maxstr] = max(Region1[maxstr], Region2[maxstr])
X = RegionArea(EnclosingDict)
Y = RegionArea(Region1)
return X - Y
pass
def MergeRegion(Region1, Region2):
if Region1 == None:
return Region2
elif Region2 == None:
return Region1
else:
EnclosingDict = dict.fromkeys(DefaultKeys, None)
for i in range(NumDimension):
minstr = str(i) + 'min'
maxstr = str(i) + 'max'
EnclosingDict[minstr] = min(Region1[minstr], Region2[minstr])
EnclosingDict[maxstr] = max(Region1[maxstr], Region2[maxstr])
return EnclosingDict
pass
def ChooseLeaf(root, node):
if len(root.Children) == 0:
return root
elif len(root.Children[0].Children) == 0:
return root
else:
TempIndex = 0
Best = float("inf")
for i in range(len(root.Children)):
if IncreasedRegion(root.Children[i].Region, node.Region) < Best:
Best = IncreasedRegion(root.Children[i].Region, node.Region)
TempIndex = i
return ChooseLeaf(root.Children[TempIndex], node)
pass
def SplitNode(Node):
if Node.father == None:
Node.father = node()
Node.father.Children.append(Node)
Child1 = node(father = Node.father)
Child2 = node(father = Node.father)
SeedList = PickSeeds(Node, Child1, Child2)
Seed2 = Node.Children.pop(SeedList[1])
Seed1 = Node.Children.pop(SeedList[0])
Child1.Children.append(Seed1)
Child2.Children.append(Seed2)
Seed1.father = Child1
Seed2.father = Child2
Child1.Region = MergeRegion(Child1.Region, Seed1.Region)
Child2.Region = MergeRegion(Child2.Region, Seed2.Region)
while len(Node.Children) > 0:
Next = PickNext(Node, Child1, Child2)
if len(Child1.Children) > len(Child2.Children) and len(Child2.Children) + len(Node.Children) == Node.MinKey:
for child in Node.Children:
Child2.Region = MergeRegion(Child2.Region, child.Region)
Child2.Children.append(child)
child.father = Child2
Node.Children = []
break
if len(Child2.Children) > len(Child1.Children) and len(Child1.Children) + len(Node.Children) == Node.MinKey:
for child in Node.Children:
Child1.Region = MergeRegion(Child1.Region, child.Region)
Child1.Children.append(child)
child.father = Child1
Node.Children = []
break
if Next[1] == 1:
child = Node.Children.pop(Next[0])
Child2.Region = MergeRegion(Child2.Region, child.Region)
child.father = Child2
Child2.Children.append(child)
else:
child = Node.Children.pop(Next[0])
Child1.Region = MergeRegion(Child1.Region, child.Region)
child.father = Child1
Child1.Children.append(child)
Node.father.Children.remove(Node)
Node.father.Children.append(Child1)
Node.father.Children.append(Child2)
Node.father.Region = MergeRegion(Node.father.Region, Child1.Region)
Node.father.Region = MergeRegion(Node.father.Region, Child2.Region)
CalculateNodeProb(Child1)
CalculateNodeProb(Child2)
pass
def PickNext(node, Child1, Child2):
WhichChild = 0
Index = 0
MaxDiffArea = 0
for i in range(len(node.Children)):
Temp1 = IncreasedRegion(Child1.Region, node.Children[i].Region)
Temp2 = IncreasedRegion(Child2.Region, node.Children[i].Region)
if abs(Temp1 - Temp2) > MaxDiffArea:
MaxDiffArea = abs(Temp1 - Temp2)
Index = i
if Temp1 - Temp2 > 0:
WhichChild = 1
else:
WhichChild = 0
return [Index, WhichChild]
pass
def PickSeeds(node, Child1, Child2):
Index1 = 0
Index2 = 0
MaxArea = 0
for i in range(len(node.Children)):
for j in range(i + 1, len(node.Children)):
if RegionArea(MergeRegion(node.Children[i].Region, node.Children[j].Region)) - RegionArea(node.Children[i].Region) - RegionArea(node.Children[j].Region) > MaxArea:
Index1 = i
Index2 = j
MaxArea = RegionArea(MergeRegion(node.Children[i].Region, node.Children[j].Region)) - RegionArea(node.Children[i].Region) - RegionArea(node.Children[j].Region)
return [Index1, Index2]
pass
def CalculateNodeProb(node):
maxprob = 0
for child in node.Children:
if child.prob > maxprob:
maxprob = child.prob
node.prob = maxprob
pass
def AdjustTree(node):
Temp = node
while Temp != None:
if len(Temp.Children) > Temp.MaxKey:
SplitNode(Temp)
else:
CalculateNodeProb(Temp)
if Temp.father != None:
Temp.father.Region = MergeRegion(Temp.father.Region, Temp.Region)
Temp = Temp.father
return
pass
def Insert(root, node):
NodeToInsert = ChooseLeaf(root, node)
node.father = NodeToInsert
NodeToInsert.Children.append(node)
NodeToInsert.Region = MergeRegion(NodeToInsert.Region, node.Region)
AdjustTree(NodeToInsert)
if root.father != None:
root = root.father
return root
pass
def Mindist(Region):
Distance = 0
for i in range(NumDimension):
key = str(i) + 'min'
Distance += Region[key]
return Distance
pass
def MinList(Region):
TempList = []
for i in range(NumDimension):
key = str(i) + 'min'
TempList.append(Region[key])
return TempList
pass
def IsDominate(pointList, elementList):
for i in range(NumDimension):
if elementList[i] < pointList[i]:
return False
return True
pass
def Dominate(element):
global NumofComp
elementList = MinList(element.Region)
for point in SkylinePoints:
NumofComp += 1
pointList = MinList(point.Region)
if IsDominate(pointList, elementList):
return True
return False
pass
def BBS(root):
for child in root.Children:
heapq.heappush(Heap, (Mindist(child.Region), child))
while True:
try:
element = heapq.heappop(Heap)[1]
if Dominate(element):
continue
elif len(element.Children) != 0:
for child in element.Children:
if not Dominate(child):
heapq.heappush(Heap, (Mindist(child.Region),child))
else:
SkylinePoints.append(element)
except:
# print "GG"
# for points in SkylinePoints:
# print points.Region
# print len(SkylinePoints)
break
pass
def MINDISTNN(point, rectangle):
ans = 0
for i in range(NumDimension):
mindim = str(i) + 'min'
maxdim = str(i) + 'max'
pi = point[mindim]
if pi < rectangle[mindim]:
ri = rectangle[mindim]
elif pi > rectangle[maxdim]:
ri = rectangle[maxdim]
else:
ri = pi
ans += (pi - ri) ** 2
return ans
pass
def MAXDISTNN(point, rectangle):
ans = 0
for i in range(NumDimension):
mindim = str(i) + 'min'
maxdim = str(i) + 'max'
pi = point[mindim]
if pi < (rectangle[mindim] + rectangle[maxdim])/2:
ri = rectangle[maxdim]
# elif pi > rectangle[maxdim]:
# ri = rectangle[maxdim]
else:
ri = rectangle[mindim]
ans += (pi - ri) ** 2
return ans
pass
# def MINMAXDISTNN(point, rectangle):
# ans = float("inf")
# for i in range(NumDimension):
# temp = 0
# mindim = str(i) + 'min'
# maxdim = str(i) + 'max'
# pi = point[mindim]
# if pi <= (rectangle[mindim] + rectangle[maxdim])/2:
# temp += (pi - rectangle[mindim]) ** 2
# else:
# temp += (pi - rectangle[maxdimdim]) ** 2
# for j in range(NumDimension):
# if i != j:
# mindim = str(j) + 'min'
# maxdim = str(j) + 'max'
# pi = point[mindim]
# if pi >= (rectangle[mindim] + rectangle[maxdim])/2:
# temp += (pi - rectangle[mindim]) ** 2
# else:
# temp += (pi - rectangle[maxdimdim]) ** 2
# if temp < ans:
# ans = temp
# return ans
# pass
def MINMAXDISTNNalternative(point, rectangle):
S = 0
for i in range(NumDimension):
mindim = str(i) + 'min'
maxdim = str(i) + 'max'
pi = point[mindim]
if pi < (rectangle[mindim] + rectangle[maxdim])/2:
ri = rectangle[maxdim]
else:
ri = rectangle[mindim]
S += (pi - ri) ** 2
ans = float("inf")
for k in range(NumDimension):
mindim = str(k) + 'min'
maxdim = str(k) + 'max'
pi = point[mindim]
if pi <= (rectangle[mindim] + rectangle[maxdim])/2:
rmk = rectangle[mindim]
else:
rmk = rectangle[maxdim]
if pi >= (rectangle[mindim] + rectangle[maxdim])/2:
rMk = rectangle[mindim]
else:
rMk = rectangle[maxdim]
temp = S - (pi - rMk) ** 2 + (pi - rmk) ** 2
if temp < ans:
ans = temp
return ans
pass
# def pruneNN(ABL):
# list2 = ABL
# list3 = ABL.sort(key= lambda x: x[3])
# for element in list3:
# toprune = (element[0],element[3])
# flag = 0
# for element2 in list2:
# if
# pass
# def pruneNNquad(ABL):
# indexlist = []
# for i in range(len(ABL)):
# for j in range(i+1, len(ABL)):
# if ABL[i][3] < ABL[j][2]:
# indexlist.append(j)
# else:
# if ABL[j][3] < ABL[i][2]:
# indexlist.append(i)
# i = len(ABL)-1
# while i >= 0:
# if i in indexlist:
# ABL.pop(i)
# i = i - 1
# return ABL
# pass
def pruneNNnew(ABL):
min_minmaxdist = min(ABL, key = lambda x: x[3])[3]
# print min_minmaxdist
if len(ABL) == 1:
return ABL
prune_index = 0
for index, item in enumerate(ABL):
if item[2] > min_minmaxdist:
prune_index = index
break
# print prune_index
# print ABL[0: prune_index][0][1].Region
return ABL[0: prune_index]
pass
def objectdistance(point, Object):
ans = 0
for i in range(NumDimension):
mindim = str(i)+"min"
ans += (point[mindim] - Object[mindim]) ** 2
return ans
pass
def pruneNNafter(ABL, i):
global NN
temp = NN[-1][1]
# min_minmaxdist = min(ABL, key = lambda x: x[3])
# indexlist = []
prune_index = i + 1
j = i + 1
while j < len(ABL):
if ABL[j][2] > temp:
prune_index = j
break
# print ABL[0: prune_index]
return ABL[0: prune_index]
def makeRtree(filename):
global Root
with open(filename,'r') as csvfile:
f = csv.reader(csvfile, delimiter=',', quotechar='"')
for Object in f:
# print Object
Object2 = [float(Object[i].strip()) for i in Dimension]
i = 0
TempDict = {}
TempDict = dict.fromkeys(DefaultKeys, None)
while i < NumDimension:
key0 = str(i) + 'min'
key1 = str(i) + 'max'
TempDict[key0] = Object2[i]
TempDict[key1] = Object2[i]
i += 1
Root = Insert(Root, node(Region = TempDict, index = int(Object[0]), prob = float(Object[-2])))
pass
# def makeRtree(filename):
# global Root
# with open(filename,'r') as f:
# for Object in f:
# Object = Object.strip().split(",")
# # Object2 = [float(Object[i].strip()) for i in Dimension]
# Object2 = [float(Object[-4]), float(Object[-3])]
# i = 0
# TempDict = {}
# TempDict = dict.fromkeys(DefaultKeys, None)
# while i < NumDimension:
# key0 = str(i) + 'min'
# key1 = str(i) + 'max'
# TempDict[key0] = Object2[i]
# TempDict[key1] = Object2[i]
# i += 1
# Root = Insert(Root, node(Region = TempDict, index = int(Object[0]), prob = float(Object[-2])))
# pass
def InsertSortedNN(tup):
global NN, k
dis = tup[1]
# print tup
# print dis, tup[0]
temp = 0
for index, item in enumerate(NN):
if item[1] > dis:
temp = index
break
print NN
NN.insert(temp, tup)
# if len(NN) < k:
# NN.insert(temp, tup)
# else:
# NN.insert(temp, tup)
# del NN[-1]
# print NN
pass
def NNall(Root, point):
global NN
# print DiskPageSize
# print len(Root.Children)
if len(Root.Children) == 0:
print "root is empty"
elif len(Root.Children[0].Children) == 0:
# if len(Root.Children[0].Children) == 0:
# At leaf level
for child in Root.Children:
# print child.Region
# InsertSortedNN((child.Region, objectdistance(point, child.Region)))
# print child.Region, objectdistance(point, child.Region)
NN.append((child, objectdistance(point, child.Region)))
# tempdist = objectdistance(point, child.Region)
# if(tempdist < NearestPointDistances):
# NearestPointDistances = tempdist
# NearestPoint = child.Region
else:
# print Root.Region
ABL = []
i = 1
for child in Root.Children:
# print child.Region
ABL.append((i,child, MINDISTNN(point,child.Region), MINMAXDISTNNalternative(point, child.Region)))
i += 1
# ABL.sort(key= lambda x: x[2])
# print ABL
# print ABL[0][2],ABL[0][3],ABL[1][2],ABL[1][3]
# ABL = pruneNNnew(ABL)
# length = len(ABL)
# # print length
# # print ABL[0][1].Children[0].Children[0]
# i = 0
# while i < length:
# NNall(ABL[i][1], point)
# # ABL = pruneNNafter(ABL,i)
# length = len(ABL)
# i += 1
for item in ABL:
NNall(item[1],point)
pass
# def PNN(p):
# global NN
# index = 0
# while p > 0 and index < len(NN):
# x = NN[index]
# print (x[0].Region, p * x[0].prob)
# p = p * (1 - x[0].prob)
# index += 1
# pass
# def PNNThres(p):
# global NN,k
# index = 0
# while p >= k and index < len(NN):
# x = NN[index]
# if p * x[0].prob >= k:
# print (x[0].Region, p * x[0].prob)
# p = p * (1 - x[0].prob)
# index += 1
# pass
def insertsorted(minheap, x):
index = 0
while index < len(minheap):
if x[1] > minheap[index][1]:
break
index += 1
minheap.insert(index, x)
# print minheap
return minheap
pass
# def PNNRank(p):
# global NN,k
# minheap = []
# pm = 0
# index = 0
# while p >= pm and index <len(NN):
# x = NN[index]
# if p * x[0].prob >= pm:
# minheap = insertsorted(minheap, (x[0], p * x[0].prob))
# # print minheap
# if len(minheap) < k:
# pm = 0
# else:
# pm = minheap[k-1][0].prob
# p = p * (1 - x[0].prob)
# index += 1
# print [(i[0].Region, i[1]) for i in minheap[0:int(k)]]
# pass
def PNNIncRank(root, point):
global k,queue, returnList
heapq.heappush(queue, (0,Root))
minheap = []
p = 1
pm = 0
index = 0
while p >= pm and len(queue) > 0:
y = IncNN(root, point)
x = y[1]
if p * x.prob >= pm:
minheap = insertsorted(minheap, (x, p * x.prob,y[0]))
# print minheap
if len(minheap) < k:
pm = 0
else:
pm = minheap[k-1][1]
p = p * (1 - x.prob)
index += 1
a = minheap[0:int(k)]
a.sort(key = lambda x:x[1], reverse = True)
# a = [(i[0].Region, i[1]) for i in minheap[0:int(k)]]
for item in a:
# print item[0].index, item[1], item[2]
returnList.append(item[0].index)
pass
def PNNIncThres(root, point):
global k,queue, returnList
heapq.heappush(queue, (0,Root))
p = 1
minheap = []
index = 0
while p >= k and len(queue) > 0:
gg = IncNN(root, point)
x = gg[1]
if p * x.prob >= k:
# print (x[0].Region, p * x[0].prob)
# print x.index, p * x.prob, gg[0]
# returnList.append(x.index)
minheap.append([x.index, p * x.prob, gg[0]])
p = p * (1 - x.prob)
minheap.sort(key = lambda x:x[1], reverse = True)
for item in minheap:
# print item[0],item[1]
returnList.append(item[0])
pass
def IncNN(root, point):
global queue
while len(queue) != 0:
element = heapq.heappop(queue)
if len(element[1].Children) == 0:
# print element[1].Region
return element
# elif len(element[1].Children[0].Children) == 0:
# for child in element[1].Children:
# heapq.heappush(queue, (objectdistance(point,child.Region),child))
else:
for child in element[1].Children:
heapq.heappush(queue, (MINDISTNN(point,child.Region),child))
pass
def PickAndRemove():
global discarded
minindex = 0
temp = discarded[0][0]
for index, item in enumerate(discarded):
if item[0] < temp:
minindex = index
return discarded.pop(index)
pass
def CalculatePminmax(p,x):
global discarded
p_xmin = p * x.prob
p_xmax = p * x.prob
for item in discarded:
if item[0] <= objectdistance(x.Region, QueryDict):
p_xmin = p_xmin * (1 - item[1].prob)**len(item[1].Children)
for item in discarded:
if MAXDISTNN(QueryDict, item[1].Region) <= objectdistance(x.Region, QueryDict):
p_xmax = p_xmax * (1 - item[1].prob)
return [p_xmin, p_xmax]
pass
def PNNIncAugThres(root, point):
global queue, k, discarded, returnList
heapq.heappush(queue, (0,Root))
minheap = []
p = 1
while p >= k and len(queue) > 0:
gg = IncNNAugThres(root, point, p)
x = gg[1]
temp = CalculatePminmax(p,x)
p_xmin = temp[0]
p_xmax = temp[1]
while p_xmin < k <= p_xmax:
E = PickAndRemove()
if len(E[1].Children) == 0:
p = p * (1 - E[1].prob)
else:
for child in E[1].Children:
discarded.append((MINDISTNN(point, child.Region), child))
temp = CalculatePminmax(p,x)
p_xmin = temp[0]
p_xmax = temp[1]
if p_xmin >= k:
# print x.index, p_xmin, p_xmax, gg[0]
minheap.append([x.index, p_xmin, p_xmax, gg[0]])
p = p * (1 - x.prob)
minheap.sort(key = lambda x:x[1], reverse = True)
for item in minheap:
returnList.append(item[0])
pass
def PNNIncAugRank(root, point):
global queue, k, discarded, returnList
heapq.heappush(queue, (0,Root))
minheap = []
p = 1
pm = 0
hflag = 0
lflag = 0
while p >= pm and len(queue) > 0:
gg = IncNNAug(root, point, p, pm)
x = gg[1]
temp = CalculatePminmax(p,x)
p_xmin = temp[0]
p_xmax = temp[1]
while p_xmin < pm <= p_xmax:
E = PickAndRemove()
lflag = 1
if len(E[1].Children) == 0:
p = p * (1 - E[1].prob)
# Do something
for index,item in enumerate(minheap):
if objectdistance(E[1].Region, QueryDict) <= objectdistance(item[0].Region):
item[3] = item[3] * (1 - E[1].prob)
hflag = 1
else:
for child in E[1].Children:
discarded.append((MINDISTNN(point, child.Region), child))
lflag = 1
temp = CalculatePminmax(p,x)
p_xmin = temp[0]
p_xmax = temp[1]
if p_xmin > pm:
minheap = insertsorted(minheap, [x,p_xmin,p_xmax,p,gg[0]])
hflag = 1
if hflag == 1 or lflag == 1:
for item in minheap:
temp = CalculatePminmax(item[3],item[0])
item[1] = temp[0]
item[2] = temp[1]
if len(minheap) < k:
pm = 0
else:
pm = minheap[k-1][1]
temp = []
for index, item in enumerate(minheap):
if item[2] < pm:
temp.append(index)
temp.sort(reverse = True)
for index in temp:
minheap.pop(index)
hflag = 0
lflag = 0
p = p * (1 - x.prob)
while len(minheap) > 0 and len(discarded) > 0:
E = PickAndRemove()
lflag = 1
if len(E[1].Children) == 0:
p = p * (1 - E[1].prob)
# Do something
for index,item in enumerate(minheap):
if objectdistance(E[1].Region, QueryDict) <= objectdistance(item[0].Region):
item[3] = item[3] * (1 - E[1].prob)
hflag = 1
else:
for child in E[1].Children:
discarded.append((MINDISTNN(point, child.Region), child))
lflag = 1
temp = CalculatePminmax(p,x)
p_xmin = temp[0]
p_xmax = temp[1]
for item in minheap:
temp = CalculatePminmax(item[3],item[0])
item[1] = temp[0]
item[2] = temp[1]
if len(minheap) < k:
pm = 0
else:
pm = minheap[k-1][1]
temp = []
for index, item in enumerate(minheap):
if item[2] < pm:
temp.append(index)
temp.sort(reverse = True)
for index in temp:
minheap.pop(index)
maxdist = 0
for item in minheap:
if objectdistance(QueryDict, item[0].Region) > maxdist:
maxdist = objectdistance(QueryDict, item[0].Region)
temp = []
for index,item in enumerate(discarded):
if item[0] > maxdist:
temp.append(index)
temp.sort(reverse = True)
for index in temp:
discarded.pop(index)
a = minheap[0:int(k)]
a.sort(key = lambda x:x[1], reverse = True)
# a = [(i[0].Region, i[1]) for i in minheap[0:int(k)]]
for item in a:
# print item[0].index, item[1], item[2]
returnList.append(item[0].index)
pass
def IncNNAugThres(root, point, pfirst):
global queue, discarded, k
while len(queue) != 0:
element = heapq.heappop(queue)
if len(element[1].Children) == 0:
return element
else:
if pfirst * element[1].prob < k:
discarded.append(element)
else:
for child in element[1].Children:
heapq.heappush(queue, (MINDISTNN(point,child.Region),child))
def IncNNAug(root, point, pfirst, pm):
global queue, discarded, k
while len(queue) != 0:
element = heapq.heappop(queue)
if len(element[1].Children) == 0:
return element
else:
if pfirst * element[1].prob < pm:
discarded.append(element)
else:
for child in element[1].Children:
heapq.heappush(queue, (MINDISTNN(point,child.Region),child))
if __name__ == "__main__":
# Start_Time = 0
# End_Time = 0
# NumofComp = 0
# Start_Time = time.time()
QueryLat = 0
QueryLong = 0
QueryDict = {}
DiskPageSize = 120
KeySize = 8
PointerSize = 4
Dimension = [14,15]
NumDimension = 2
NumMaxKey = 10
NumMinKey = 5
DefaultDict = {}
DefaultKeys = []
for i in range(NumDimension):
DefaultKeys.append(str(i) + 'min')
DefaultKeys.append(str(i) + 'max')
DefaultDict = dict.fromkeys(DefaultKeys, None)
Heap = []
BloodGroup = ''
BloodGroupList = ['A+','AB+','B+','O+']
returnList = []
# SkylinePoints = []
Root = None
NN = []
queue = []
discarded = []
k = 0
# with open('query_real.txt','r') as f:
# # Dimension = [i- 1 for i in map(int, f.readline().strip().split())]
# Dimension = map(int, f.readline().strip().split())
# NumDimension = len(Dimension)
# DiskPageSize = int(f.readline().strip())
# string = f.readline().strip().split()
# # k = int(f.readline().strip())
# PointerSize = int(string[0])
# KeySize = int(string[1])
# NumMaxKey = divmod(DiskPageSize, PointerSize + KeySize)[0]
# NumMinKey = NumMaxKey/2
# DefaultKeys = []
# for i in range(NumDimension):
# DefaultKeys.append(str(i) + 'min')
# DefaultKeys.append(str(i) + 'max')
# DefaultDict = dict.fromkeys(DefaultKeys, None)
Root = node()
arglist = sys.argv
if arglist[1] not in BloodGroupList:
print "Wrong BloodGroup"
else:
BloodGroup = arglist[1]
try:
QueryLat = float(arglist[2])
QueryLong = float(arglist[3])
QueryDict = dict.fromkeys(DefaultKeys, None)
QueryDict['0min'] = QueryLat
QueryDict['0max'] = QueryLat
QueryDict['1min'] = QueryLong
QueryDict['1max'] = QueryLong
except:
print 'wrong lat long args'
if arglist[4] == 'threshold':
try:
k = float(arglist[5])
except:
print 'wrong threshold argument'
elif arglist[4] == 'kbest':
try:
if arglist[5].isdigit() == True:
k = int(arglist[5])
else:
raise ValueError
except:
print "wrong kbest argument"
else:
print "wrong output type argument"
if BloodGroup == 'A+':
makeRtree('newdonordb_a.csv')
elif BloodGroup == 'AB+':
makeRtree('newdonordb_ab.csv')
elif BloodGroup == 'B+':
makeRtree('newdonordb_b.csv')
else:
makeRtree('newdonordb_o.csv')
# makeRtree('newdonordb_old.csv')
# print '-------------------'
if arglist[4] == 'threshold' and arglist[6] == '0':
PNNIncThres(Root, QueryDict)
elif arglist[4] == 'kbest' and arglist[6] == '0':
PNNIncRank(Root, QueryDict)
elif arglist[4] == 'kbest' and arglist[6] == '1':
PNNIncAugRank(Root, QueryDict)
elif arglist[4] == 'threshold' and arglist[6] == '1':
PNNIncAugThres(Root, QueryDict)
else:
print 'problem'
temp = []
# with open('newdonordb_old.csv','r') as f:
# for Object in f:
# Object = Object.strip().split(",")
# if int(Object[0]) in returnList:
# # print [Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()]
# temp.append([int(Object[0]),Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()])
# for index in returnList:
# for item in temp:
# if item[0] == index:
# print item
# break
if BloodGroup == 'A+':
temp = []
with open('newdonordb_a.csv','r') as f:
for Object in f:
Object = Object.strip().split(",")
if int(Object[0]) in returnList:
# print [Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()]
temp.append([int(Object[0]),Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()])
for index in returnList:
for item in temp:
if item[0] == index:
print item
break
elif BloodGroup == 'AB+':
temp = []
with open('newdonordb_ab.csv','r') as f:
for Object in f:
Object = Object.strip().split(",")
if int(Object[0]) in returnList:
# print [Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()]
temp.append([int(Object[0]),Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()])
for index in returnList:
for item in temp:
if item[0] == index:
print item
break
elif BloodGroup == 'B+':
temp = []
with open('newdonordb_b.csv','r') as f:
for Object in f:
Object = Object.strip().split(",")
if int(Object[0]) in returnList:
# print [Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()]
temp.append([int(Object[0]),Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()])
for index in returnList:
for item in temp:
if item[0] == index:
print item
break
else:
temp = []
with open('newdonordb_o.csv','r') as f:
for Object in f:
Object = Object.strip().split(",")
if int(Object[0]) in returnList:
# print [Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()]
temp.append([int(Object[0]),Object[1].strip(),Object[4].strip(),Object[6].strip(),Object[11].strip(),Object[13].strip()])
for index in returnList:
for item in temp:
if item[0] == index:
print item
break
# heapq.heappush(queue, (0,Root))
# IncNN(Root, {"0min":0, "0max":0, "1min":0, "1max":0})
# temp(Root, {"0min":0, "0max":0, "1min":0, "1max":0})
# temp(Root, {"0min":28.65, "0max":28.65, "1min":77.777, "1max":77.777, "2min":0, "2max":0})
# PNNIncRank(Root, {"0min":0, "0max":0, "1min":0, "1max":0, "2min":0, "2max":0})
# PNNIncRank(Root, {"0min":28.6139422, "0max":28.6139422, "1min":77.274811, "1max":77.274811})
# BBS(Root)
# NNall(Root, {"0min":0, "0max":0, "1min":0, "1max":0})
# NNall(Root, {"0min":0, "0max":0, "1min":0, "1max":0})
# NN.sort(key= lambda x: x[1])
# PNN(1)
# print '--------'
# # PNNThres(1)
# print '--------'
# PNNRank(1)