1
-
2
1
from libc.math cimport sin , cos, sqrt, fabs
3
2
import numpy as np
4
3
cimport numpy as cnp
@@ -14,30 +13,32 @@ cdef:
14
13
15
14
@ cython.boundscheck (False )
16
15
@ cython.wraparound (False )
17
- def findDepth (dict specificNode , list parsedLinkData ):
18
- cdef int temp = 0
16
+ def findDepth (dict specificNode , list parsedLinkData , dict nodeIDMapToParsedLinkDataIndexTarget ):
17
+ cdef int temp = 1
19
18
cdef float length = 0
20
19
cdef list linkWithTarget
21
20
cdef dict currentNode = specificNode
22
21
while True :
23
22
# # this can be imporved using hashmap
24
- linkWithTarget = [x for x in parsedLinkData if x[' target' ][' id' ] == currentNode[" id" ]]
23
+ # # linkWithTarget = [x for x in parsedLinkData if x['target']['id'] == currentNode["id"]]
24
+ linkWithTarget = [parsedLinkData[nodeIDMapToParsedLinkDataIndexTarget[currentNode[" id" ]]]] if nodeIDMapToParsedLinkDataIndexTarget.get(currentNode[" id" ], None ) is not None else []
25
25
if len (linkWithTarget)== 0 :
26
26
return (temp, length)
27
27
else :
28
- temp = temp + 1
28
+ temp += 1
29
29
length = length + linkWithTarget[0 ][' len' ]
30
30
currentNode = linkWithTarget[0 ][' source' ]
31
31
32
32
33
33
@ cython.boundscheck (False )
34
34
@ cython.wraparound (False )
35
- def findRootNode (dict randomNode , list parsedLinkData ):
35
+ def findRootNode (dict randomNode , list parsedLinkData , dict nodeIDMapToParsedLinkDataIndexTarget ):
36
36
cdef dict currentNode = randomNode
37
37
cdef list linkWithTarget
38
38
while True :
39
39
# # this can be imporved using hashmap
40
- linkWithTarget = [x for x in parsedLinkData if x[' target' ][' id' ] == currentNode[" id" ]]
40
+ # # linkWithTarget = [x for x in parsedLinkData if x['target']['id'] == currentNode["id"]]
41
+ linkWithTarget = [parsedLinkData[nodeIDMapToParsedLinkDataIndexTarget[currentNode[" id" ]]]] if nodeIDMapToParsedLinkDataIndexTarget.get(currentNode[" id" ], None ) is not None else []
41
42
if len (linkWithTarget)== 0 :
42
43
return currentNode
43
44
else :
@@ -46,12 +47,13 @@ def findRootNode(dict randomNode, list parsedLinkData):
46
47
# # find the nodes from "endNode" to node "startNode" without the startNode itself
47
48
@ cython.boundscheck (False )
48
49
@ cython.wraparound (False )
49
- def reversebfs (dict startNode , list parsedLinkData ):
50
+ def reversebfs (dict startNode , list parsedLinkData , dict nodeIDMapToParsedLinkDataIndexTarget ):
50
51
cdef list path = []
51
52
cdef dict currentNode = startNode , currentLink
52
53
while True :
53
54
# # this can be imporved using hashmap
54
- linkWithTarget = [x for x in parsedLinkData if x[' target' ][' id' ] == currentNode[" id" ]]
55
+ # # linkWithTarget = [x for x in parsedLinkData if x['target']['id'] == currentNode["id"]]
56
+ linkWithTarget = [parsedLinkData[nodeIDMapToParsedLinkDataIndexTarget[currentNode[" id" ]]]] if nodeIDMapToParsedLinkDataIndexTarget.get(currentNode[" id" ], None ) is not None else []
55
57
if len (linkWithTarget)== 0 :
56
58
break
57
59
else :
@@ -74,6 +76,7 @@ def updateCoordinatesX(double[:] x , double[:] y, float cX ,float cY, float rad,
74
76
return new_x
75
77
76
78
79
+
77
80
@ cython.boundscheck (False )
78
81
@ cython.wraparound (False )
79
82
def updateCoordinatesY (double[:] x , double[:] y , float cX , float cY , float rad , int l ):
@@ -126,6 +129,10 @@ def lossFunction(dict node,float realTheta,list parsedLinkData, dict nodeIDMapTo
126
129
deltaY = matchedLinkObject[' target' ][' y' ] - matchedLinkObject[' source' ][' y' ]
127
130
return ((middleX - cX)* (deltaX) + (middleY - cY)* (deltaY)) / sqrt(a* b)
128
131
132
+ @ cython.boundscheck (False )
133
+ @ cython.wraparound (False )
134
+ def lossFunctionWithDepth (dict node ,float realTheta , int depth , list parsedLinkData , dict nodeIDMapToParsedLinkDataIndexTarget ):
135
+ pass
129
136
130
137
# # can be destructured to function(hashTable, requiredNode)
131
138
@ cython.boundscheck (False )
@@ -225,7 +232,6 @@ cdef bint checkInfiniteCollision(float sourceX, float sourceY, float targetX, fl
225
232
dist = fabs(m* centreX - centreY + c) / sqrt(m* m + 1 )
226
233
return (dist <= R)
227
234
228
-
229
235
@ cython.boundscheck (False )
230
236
@ cython.wraparound (False )
231
237
def calIntersectionNum (list links , list requiredLinkIDs , list filteredLinksIDs ):
@@ -288,15 +294,17 @@ def findIntersection(list links):
288
294
289
295
# # all nodes inputed should not be root node here
290
296
@ cython.boundscheck (False )
297
+ @ cython.wraparound (False )
291
298
def mainAlgo (dict node , dict link , list parsedNodeData , list parsedLinkData , float THETA , float LAMBDA ,
292
299
dict nodeIDMapToParsedLinkDataIndexSource , dict nodeIDMapToParsedLinkDataIndexTarget , dict nodeIDMapToParsedNodeDataIndex ,
293
- dict linkIDMapToParsedNodeDataIndexSource , dict linkIDMapToParsedNodeDataIndexTarget , dict linkIDMapToParsedLinkDataIndex
300
+ dict linkIDMapToParsedNodeDataIndexSource , dict linkIDMapToParsedNodeDataIndexTarget , dict linkIDMapToParsedLinkDataIndex ,
301
+ dict depth
294
302
):
295
303
296
304
cdef:
297
305
int numOfIntersections, iterations = 0 , currentNodeIndex, nodeIndex, breakFromWhile = 0 , tempIndex = 1
298
306
list requiredUpdateNode = search(node[' id' ], nodeIDMapToParsedLinkDataIndexSource, nodeIDMapToParsedLinkDataIndexTarget, parsedLinkData)
299
- int l = len (requiredUpdateNode), N = len (parsedLinkData), recordLength
307
+ int l = len (requiredUpdateNode), N = len (parsedLinkData), recordLength = 0 , nodeDepth = depth[node[ " id " ]]
300
308
unsigned int indexing, index
301
309
long [:] orderedNodeIndex = np.zeros(l, dtype = long )
302
310
# # a numpy float is a C double.
@@ -312,10 +320,12 @@ def mainAlgo(dict node, dict link, list parsedNodeData, list parsedLinkData, flo
312
320
for index in range (l):
313
321
linkObjects[index] = nodeIDMapToParsedLinkDataIndexTarget[requiredUpdateNode[index]]
314
322
orderedNodeIndex[index] = nodeIDMapToParsedNodeDataIndex[requiredUpdateNode[index]]
315
- orderedNodeX[index] = float (parsedNodeData[orderedNodeIndex[index]][" x" ])
316
- orderedNodeY[index] = float (parsedNodeData[orderedNodeIndex[index]][" y" ])
323
+ # # orderedNodeX[index] = float(parsedNodeData[orderedNodeIndex[index]]["x"])
324
+ # # orderedNodeY[index] = float(parsedNodeData[orderedNodeIndex[index]]["y"])
325
+ orderedNodeX[index] = parsedNodeData[orderedNodeIndex[index]][" x" ]
326
+ orderedNodeY[index] = parsedNodeData[orderedNodeIndex[index]][" y" ]
317
327
318
- lastLinkObject = parsedLinkData[linkObjects[- 1 ]]
328
+ lastLinkObject = parsedLinkData[linkObjects[l - 1 ]]
319
329
R = sqrt((link[" source" ][" x" ] - lastLinkObject[" target" ][" x" ])** 2 + (link[" source" ][" y" ] - lastLinkObject[" target" ][" y" ])** 2 )
320
330
321
331
for index in range (N):
@@ -344,16 +354,17 @@ def mainAlgo(dict node, dict link, list parsedNodeData, list parsedLinkData, flo
344
354
if indexing == 0 :
345
355
records.append({' root' : { ' id' : requiredUpdateNode[indexing] ,' pos' :[new_x, new_y]} , ' childNodes' : {}})
346
356
parsedNodeData[currentNodeIndex].update([(" x" , new_x), (" y" , new_y)])
357
+ recordLength += 1
347
358
else :
348
- records[- 1 ][' childNodes' ].update([(requiredUpdateNode[indexing],[new_x, new_y])])
359
+ records[recordLength - 1 ][' childNodes' ].update([(requiredUpdateNode[indexing],[new_x, new_y])])
349
360
parsedNodeData[currentNodeIndex].update([(" x" ,new_x), (" y" ,new_y)])
350
-
361
+
351
362
numOfIntersections = calIntersectionNum(parsedLinkData, linkObjects, linkFilteredList)
352
363
if numOfIntersections == 0 :
353
364
breakFromWhile = 1
354
365
break
355
366
""" The loss function is defined as : #Intersections - lambda * dotproduct (which is for measuring the degree of parallelism)"""
356
- records[- 1 ][" loss" ] = numOfIntersections - LAMBDA* lossFunction(node, binaryVariable, parsedLinkData, nodeIDMapToParsedLinkDataIndexTarget)
367
+ records[recordLength - 1 ][" loss" ] = numOfIntersections - LAMBDA* lossFunction(node, binaryVariable, parsedLinkData, nodeIDMapToParsedLinkDataIndexTarget) / nodeDepth
357
368
iterations += 1
358
369
realTheta = iterations* THETA
359
370
0 commit comments