-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.py
More file actions
223 lines (188 loc) · 8.36 KB
/
create.py
File metadata and controls
223 lines (188 loc) · 8.36 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
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
import random
from intersections import intersection
from cars import car
import numpy as np
from new_position import find_new_position
class create_world():
def __init__(self, cellMap, NUM_CARS, MAX_SPEED, cycle):
self.cycle = cycle
self.MAX_SPEED = MAX_SPEED
self.NUM_CARS = NUM_CARS
self.traffic_map, self.cars, self.intersections = self.build_map(cellMap)
self.flow = []
def build_map(self, cellMap):
traffic_map = self.create_traffic_map(cellMap)
traffic_map, cars = self.create_cars(traffic_map)
intersections, traffic_map = self.create_intersections(traffic_map)
return traffic_map, cars, intersections
def create_traffic_map(self, cellMap):
for row in range(cellMap.shape[0]):
for column in range(cellMap.shape[1]):
cellMap[row, column] = -1
cellMap[15, :] = 0
cellMap[16, :] = 0
cellMap[35, :] = 0
cellMap[36, :] = 0
cellMap[:, 25] = 0
cellMap[:, 45] = 0
cellMap[:, 26] = 0
cellMap[:, 46] = 0
return cellMap
def create_cars(self, traffic_map):
cars = []
temp = 0
while temp != self.NUM_CARS:
rand_x = random.randint(0, traffic_map.shape[0]-1)
rand_y = random.randint(0, traffic_map.shape[0]-1)
if traffic_map[rand_x, rand_y] == 0:
traffic_map[rand_x, rand_y] = 1
cars.append(car((rand_x, rand_y)))
temp += 1
return traffic_map, cars
def create_intersections(self, traffic_map):
# Define intersections
intersections = []
intersections.append(intersection(15, (14, 25), "GREEN", (14, 24)))
intersections.append(intersection(19, (14, 45), "GREEN", (14, 44)))
intersections.append(intersection(35, (34, 25), "GREEN", (34, 24)))
intersections.append(intersection(39, (34, 45), "GREEN", (34, 44)))
intersections.append(intersection(67, (17, 26), "GREEN", (17, 27)))
intersections.append(intersection(71, (17, 46), "GREEN", (17, 47)))
intersections.append(intersection(89, (37, 26), "GREEN", (37, 27)))
intersections.append(intersection(93, (37, 46), "GREEN", (37, 47)))
intersections.append(intersection(16, (15, 27), "GREEN", (14, 27)))
intersections.append(intersection(20, (15, 47), "GREEN", (14, 47)))
intersections.append(intersection(36, (35, 27), "GREEN", (34, 27)))
intersections.append(intersection(40, (35, 47), "GREEN", (34, 47)))
intersections.append(intersection(66, (16, 24), "GREEN", (17, 24)))
intersections.append(intersection(70, (16, 44), "GREEN", (17, 44)))
intersections.append(intersection(86, (36, 24), "GREEN", (37, 24)))
intersections.append(intersection(90, (36, 44), "GREEN", (37, 44)))
for i in intersections:
if i.state == "GREEN":
traffic_map[i.visual_position_x, i.visual_position_y] = 5
return intersections, traffic_map
def update_map(self):
new_list = self.cars.sort(key=lambda x: x.position_x)
cars1 = []
cars2 = []
cars3 = []
cars4 = []
cars5 = []
cars6 = []
cars7 = []
cars8 = []
while(self.cars != []):
if self.cars[0].position_x == 16:
cars1.append(self.cars[0])
elif self.cars[0].position_x == 36:
cars2.append(self.cars[0])
elif self.cars[0].position_x == 15:
cars3.append(self.cars[0])
elif self.cars[0].position_x == 35:
cars4.append(self.cars[0])
elif self.cars[0].position_y == 26:
cars5.append(self.cars[0])
elif self.cars[0].position_y == 46:
cars6.append(self.cars[0])
elif self.cars[0].position_y == 25:
cars7.append(self.cars[0])
elif self.cars[0].position_y == 45:
cars8.append(self.cars[0])
self.cars.pop(0)
cars1.sort(key=lambda x: x.position_y, reverse=True)
cars2.sort(key=lambda x: x.position_y, reverse=True)
cars3.sort(key=lambda x: x.position_y)
cars4.sort(key=lambda x: x.position_y)
cars5.sort(key=lambda x: x.position_x)
cars6.sort(key=lambda x: x.position_x)
cars7.sort(key=lambda x: x.position_x, reverse=True)
cars8.sort(key=lambda x: x.position_x, reverse=True)
self.cars.extend(cars1)
self.cars.extend(cars2)
self.cars.extend(cars3)
self.cars.extend(cars4)
self.cars.extend(cars5)
self.cars.extend(cars6)
self.cars.extend(cars7)
self.cars.extend(cars8)
flow = 0
for c in self.cars:
previous_position = [c.position_x, c.position_y]
self.traffic_map[c.position_x, c.position_y] = 0
c = self.update_car_state(c)
previous_lane = c.lane
new_position = [c.position_x, c.position_y]
if c.position_x == 0 and c.position_y == 0:
self.cars.remove(c)
if previous_lane == 1:
self.cars.append(car((15, 49)))
self.traffic_map[15, 49] = 1
elif previous_lane == 2:
self.cars.append(car((16, 0)))
self.traffic_map[16, 0] = 1
elif previous_lane == 3:
self.cars.append(car((35, 49)))
self.traffic_map[35, 49] = 1
elif previous_lane == 4:
self.cars.append(car((36, 0)))
self.traffic_map[36, 0] = 1
elif previous_lane == 5:
self.cars.append(car((0, 25)))
self.traffic_map[0, 25] = 1
elif previous_lane == 6:
self.cars.append(car((49, 26)))
self.traffic_map[49, 26] = 1
elif previous_lane == 7:
self.cars.append(car((0, 45)))
self.traffic_map[0, 45] = 1
else:
self.cars.append(car((49, 46)))
self.traffic_map[49, 46] = 1
else:
self.traffic_map[c.position_x, c.position_y] = 1
for intersection in self.intersections:
if previous_position[0] == intersection.position_x and previous_position[1] == intersection.position_y:
if new_position[0] != intersection.position_x or new_position[1] != intersection.position_y:
flow += 1
self.flow.append(flow)
def update_car_state(self, car):
if car.state == "NONE":
car.update_state(random.randint(0, 1)) # 0:Non turning vehicle, 1: Turning Vehicle
if car.state == "LEFT" or car.state == "RIGHT":
return self.turning(car)
else:
return self.non_turning(car)
def turning(self, car):
for i in self.intersections:
if car.position_x == i.position_x and car.position_y == i.position_y:
# car is at intersection
if car.light == "RED" and i.counter < self.cycle:
i.update_counter(1)
i.update_light_state("RED")
self.traffic_map[i.visual_position_x, i.visual_position_y] = 6
return car # The vehicle does not move
elif car.light == "RED" and i.counter >= self.cycle:
i.update_counter(0)
i.update_light_state("GREEN")
car.update_light("GREEN")
self.traffic_map[i.visual_position_x, i.visual_position_y] = 5
return car
else:
car = find_new_position(car, self.traffic_map, self.intersections)
car.update_state(random.randint(0, 1))
return car
# Assume red light at intersection
car.update_light("RED")
car = self.non_turning(car)
return car
def non_turning(self, car):
# Acceleration step
if car.speed < self.MAX_SPEED:
car.update_speed(car.speed+1)
# Randomization step
if car.speed > 0:
if random.randint(0, 1) == 1:
car.update_speed(car.speed-1)
car = find_new_position(car, self.traffic_map, self.intersections)
return car