forked from AC-Unicorn/Traffic-Flow-Optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.py
More file actions
159 lines (142 loc) · 5.9 KB
/
layout.py
File metadata and controls
159 lines (142 loc) · 5.9 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# layout.py
import os
from game import Info, Intersection, Crossroad, Road
class Layout(object):
"""
A Layout parse the map into Graph.
"""
def __init__(self, layoutText):
self.width = len(layoutText[0])
self.height = len(layoutText)
self.mapInfo = Info(self.width, self.height)
self.intersections = []
self.crossroads = []
self.roads = []
self.parseLayoutText(layoutText)
self.layoutText = layoutText
def parseLayoutText(self, layoutText):
"""
Parse all intersections and crossroads first, and then parse the roads in the map.
"""
self.__parseMap(layoutText)
self.__parseMap2(layoutText)
def __parseMap(self, layoutText):
for y in range(self.height):
for x in range(self.width):
layoutChar = layoutText[y][x]
if self.mapInfo.get(x, y) is not None: continue
if layoutChar == '%':
self.mapInfo.setField(x, y)
elif layoutChar == 'I':
number = len(self.intersections)
positions = self.__parseIntersection(layoutText, x, y, number)
self.intersections.append(Intersection(number, positions))
elif layoutChar == 'C':
number = len(self.crossroads)
positions = self.__parseCrossroad(layoutText, x, y, number)
self.crossroads.append(Crossroad(number, positions))
def __parseIntersection(self, layoutText, x, y, number):
self.mapInfo.setIntersection(x, y, number)
positions = [(x, y)]
for (nextX, nextY) in self.__getPosNearBy(x, y):
if layoutText[nextY][nextX] != 'I': continue
if self.mapInfo.get(nextX, nextY) is not None: continue
positions.extend(self.__parseIntersection(layoutText, nextX, nextY, number))
return positions
def __parseCrossroad(self, layoutText, x, y, number):
self.mapInfo.setCrossroad(x, y, number)
positions = [(x, y)]
for (nextX, nextY) in self.__getPosNearBy(x, y):
if layoutText[nextY][nextX] != 'C': continue
if self.mapInfo.get(nextX, nextY) is not None: continue
positions.extend(self.__parseCrossroad(layoutText, nextX, nextY, number))
return positions
def __parseMap2(self, layoutText):
for node in self.intersections + self.crossroads:
for pos in node.getPostions():
for (nextX, nextY) in self.__getPosNearBy(pos[0], pos[1]):
if self.mapInfo.get(nextX, nextY) is not None: continue
(testX, testY) = self.__getNextPos(nextX, nextY, layoutText[nextY][nextX])
if not (testX == pos[0] and testY == pos[1]):
number = len(self.roads)
(positions, ways) = self.__parseRoad(layoutText, nextX, nextY, number)
start = self.mapInfo.get(pos[0], pos[1])
end = positions.pop()
self.roads.append(Road(number, positions, ways, start, end))
self.__setInOutRoad(number, start, end)
def __parseRoad(self, layoutText, x, y, number):
self.mapInfo.setRoad(x, y, number)
positions = [(x, y)]
ways = [layoutText[y][x]]
(nextX, nextY) = self.__getNextPos(x, y, layoutText[y][x])
posInfo = self.mapInfo.get(nextX, nextY)
if posInfo is None:
(p, w) = self.__parseRoad(layoutText, nextX, nextY, number)
positions.extend(p)
ways.extend(w)
else:
positions.append(posInfo)
return (positions, ways)
def __setInOutRoad(self, number, start, end):
if start[0] == Info.INTERSECTION:
self.intersections[start[1]].addOutRoad(number)
elif start[0] == Info.CROSSROAD:
self.crossroads[start[1]].addOutRoad(number)
if end[0] == Info.INTERSECTION:
self.intersections[end[1]].addInRoad(number)
elif end[0] == Info.CROSSROAD:
self.crossroads[end[1]].addInRoad(number)
def __getPosNearBy(self, x, y):
positions = []
move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for m in move:
nextX = x + m[0]
nextY = y + m[1]
if 0 <= nextX < self.width and 0 <= nextY < self.height:
positions.append((nextX, nextY))
return positions
def __getNextPos(self, x, y, direction):
if direction == 'N':
return (x, y - 1)
elif direction == 'S':
return (x, y + 1)
elif direction == 'E':
return (x + 1, y)
elif direction == 'W':
return (x - 1, y)
def getTrafficLights(self):
return [(i.number, i.getInRoads()) for i in self.intersections]
# Call by Game?
def getLayout(name, back=2):
if name.endswith('.lay'):
layout = tryToLoad('layouts/' + name)
if layout is None: layout = tryToLoad(name)
else:
layout = tryToLoad('layouts/' + name + '.lay')
if layout is None: layout = tryToLoad(name + '.lay')
if layout is None and back >= 0:
curdir = os.path.abspath('.')
os.chdir('..')
layout = getLayout(name, back - 1)
os.chdir(curdir)
return layout
def tryToLoad(fullname):
if not os.path.exists(fullname): return None
f = open(fullname)
try: return Layout([line.strip() for line in f])
finally: f.close()
if __name__ == '__main__':
l = getLayout('double_cross')
# print('crossroads')
# for c in l.crossroads:
# print(c.getPostions())
# print('intersections')
# for it in l.intersections:
# print(it.getPostions())
# print(it.getOutRoads())
# print('roads')
# for r in l.roads:
# print(r.getPostions())
# print(r.getStart())
# print(r.getEnd())
print(l.getTrafficLights())