-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconflictsUtils.py
More file actions
139 lines (119 loc) · 4.64 KB
/
Copy pathconflictsUtils.py
File metadata and controls
139 lines (119 loc) · 4.64 KB
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
import networkx as nx
import networkx as nx
import numpy as np
def createMatrix(routes, laneNum):
"""Creates a matrix the same size as the numebr of lanes at the junction.
The routes are then used to fill the matrix with ones where the conflicts
are.
Args:
routes (list): List of routes. Each route is a tuple of two lanes.
laneNum (int): Number of lanes in the intersection.
Returns:
(list): Square matrix of size laneNum x laneNum with conflicts marked as 1.
"""
matrix = [[0] * laneNum for _ in range(laneNum)]
for r in routes:
ent= r[0]
ext = r[1]
if 0 <= ent < len(matrix) and 0 <= ext < len(matrix[0]):
matrix[ent][ext] = 1
else:
print(f"Invalid indices: ent={ent}, ext={ext}, laneNum={laneNum}")
return matrix
def entranceLanes(route, lanes):
""""Gets the potential conflict entrance lanes for that lanes routes in closewise order.
Args:
route (tuple): Tuple of a route.
lanes (int): Number of lanes in the intersection.
Returns:
(list): List of entrance lanes for the route.
"""
ent, ext = route
entrances = [ent]
currentLane = ent
while currentLane != ext:
currentLane = (currentLane + 1) % lanes
entrances.append(currentLane)
return entrances
def exitLanes(route, lanes):
"""Gets the potential conflict exit lanes for that lanes routes in clockwise order.
Args:
route (tuple): Tuple of a route.
lanes (int): Number of lanes in the intersection.
Returns:
(list): List of exit lanes for the route.
"""
ent, ext = route
exits = [ext]
currentLane = ext
while currentLane != ent:
currentLane = (currentLane + 1) % lanes
exits.append(currentLane)
return exits
def FindMatrixConflicts(matrix, entrances, exits, routes):
"""Find route conflicts in the matrix based on the entrance and exit lanes combinations.
Args:
matrix (list): Square matrix of size laneNum x laneNum with conflicts marked as 1.
entrances (list): List of conflict entrance lanes for the route.
exits (list): List of conflcit exit lanes for the route.
Returns:
(list): List of tuples representing the conflicts routes of a particulare entrance lane.
"""
conflicts = []
for e in entrances:
for ex in exits:
if matrix[e][ex] == 1:
conflicts.append((e,ex))
return conflicts
def getMatrixConflictList(matrix, routes, laneNum):
"""Get the list of conflicts for each route in the matrix.
Args:
matrix (list): Square matrix of size laneNum x laneNum with conflicts marked as 1.
routes (list): List of routes. Each route is a tuple of two lanes.
laneNum (int): Number of lanes in the intersection.
Returns:
(list): Conflict lanes for each lane in the routes.
"""
conflictList = []
for i in range(len(routes)):
forwardEntrance = entranceLanes(routes[i], laneNum)
forwardExit = exitLanes(routes[i], laneNum)
backwardEntrance = entranceLanes(routes[i][::-1], laneNum)
backwardExit = exitLanes(routes[i][::-1], laneNum)
matrix1 = FindMatrixConflicts(matrix, forwardEntrance, forwardExit, routes)
matrix2 = FindMatrixConflicts(matrix, backwardEntrance, backwardExit, routes)
matrix1.extend(matrix2)
matrix1 = list(set(matrix1))
conflictList.append((routes[i], matrix1))
return conflictList
def getGraphNodes(conflictList):
dictLane = {}
for startLane, conflictLanes in conflictList:
laneList = []
for conflicts in conflictLanes:
if conflicts[0] != startLane[0]:
laneList.append(conflicts[0])
dictLane[startLane[0]] = laneList
return dictLane
def getConflicts(routes, laneNum):
squarematrix = createMatrix(routes, laneNum)
conflicts = getMatrixConflictList(squarematrix, routes, laneNum)
graph = getGraphNodes(conflicts)
return graph
def getMIS(graph):
return nx.approximation.maximum_independent_set(graph)
def getMISList(graphData):
graph = nx.Graph()
for start in graphData:
graph.add_node(start)
for end in graphData[start]:
graph.add_edge(start, end)
misList = []
for node in graph.nodes():
subgraphNodes = set(graph.nodes()) - set(graph.neighbors(node)) - {node}
subgraph = graph.subgraph(subgraphNodes)
if len(list(getMIS(subgraph))) == 0:
misList.append([node, [node]])
else:
misList.append([node, list(getMIS(subgraph))])
return misList