-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimtest.py
More file actions
383 lines (315 loc) · 10.7 KB
/
simtest.py
File metadata and controls
383 lines (315 loc) · 10.7 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
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
import ctypes
import os
import sys
import random
import pygame
import subprocess
import platform
import pickle
import numpy as np
<<<<<<< HEAD
from argparse import ArgumentParser
#from dqn import DQNAgent
from sim.simobjects import *
def main(control, sim_length, frequency, dmin, dmax, render):
global screen, clock, reverse, lanes, intersection, cars, intersection, total_wait, count, trial_reward, \
=======
from sim.simobjects import *
def main(control, sim_length, frequency, dmin, dmax, render):
global screen, clock, reverse, lanes, intersection, cars, intersection, total_wait, count, \
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
LANE_WIDTH, SPEED, SCREEN_SIZE
def approx(p1, p2):
if abs(p1[0] - p2[0]) < SPEED // 2:
if abs(p1[1] - p2[1]) < SPEED // 2:
return True
return False
def rotate(direction, turn):
return {
'right': {
'up': 'right',
'right': 'down',
'down': 'left',
'left': 'up'
},
'left': {
'up': 'left',
'left': 'down',
'down': 'right',
'right': 'up'
}
} [turn] [direction]
def get_turn(lane, turn):
return {
0: {
'right': 0,
'left': 2
},
2: {
'right': 1,
'left': 0
},
4: {
'right': 3,
'left': 1
},
6: {
'right': 2,
'left': 3
}
} [lane] [turn]
def get_reward():
result = 0
for l in lanes:
for c in l.cars:
if c.speed == 0:
result += 1
return (1 / (result + 1e-4))
i = 6
l = lanes[i]
car = Car(l.start, l.direction, SPEED, random.choice(['straight','right','left']), screen)
car.start = i
cars.add(car)
#total_wait = 0
for count in range(sim_length):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
quit()
elif event.key == 27:
pygame.quit()
quit()
if count % int(200 / SPEED) == 0:
i = random.choice(range(0, 7, 2))
l = lanes[i]
g = random.choice(['straight', 'right'])
c = Car(l.start, l.direction, SPEED, g, screen)
c.start = i
cars.add(c)
for c in cars.sprites():
c.speed = SPEED
if c.rect.colliderect(middle.rect):
if not middle.rect.contains(c.rect):
if middle.flow != c.orientation and c.rect.colliderect(lanes[c.start]):
c.speed = 0
if reverse(c.orientation) in [d.orientation for d in middle.incars] and c.rect.colliderect(lanes[c.start]):
c.speed = 0
else:
if c.goal != 'straight':
t = middle.turns[get_turn(c.start, c.goal)]
if approx(c.rect.center, t) and not c.turned:
c.direction = rotate(c.direction, c.goal)
c.turned = True
if c.speed == 0:
total_wait += 1
control(frequency, dmin, dmax, total_wait)
<<<<<<< HEAD
trial_reward += get_reward()
=======
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
intersection.update(cars.sprites())
cars.update(intersection.sprites())
screen.fill(YELLOW)
intersection.draw(screen)
cars.draw(screen)
if render: pygame.display.update()
clock.tick(180)
count += 1
''' Get information on the screen the program is running on '''
def get_screen_metrics():
if platform.system() == 'Windows':
user32 = ctypes.windll.user32
SCREEN_SIZE = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
COMBINED_SCREEN_SIZE = user32.GetSystemMetrics(78), user32.GetSystemMetrics(79)
SECOND_SCREEN_SIZE = (COMBINED_SCREEN_SIZE[0] - SCREEN_SIZE[0], COMBINED_SCREEN_SIZE[1])
DISPLAY_MODE = "single" if COMBINED_SCREEN_SIZE == SCREEN_SIZE else "dual"
RATIO = 1.0
if DISPLAY_MODE == "dual":
DISPLAY_WIDTH, DISPLAY_HEIGHT = tuple([int(i // RATIO) for i in list(SECOND_SCREEN_SIZE)])
x, y = ((COMBINED_SCREEN_SIZE[0] + SCREEN_SIZE[0] - DISPLAY_WIDTH) // 2, (COMBINED_SCREEN_SIZE[1] - DISPLAY_HEIGHT) // 2)
else:
DISPLAY_WIDTH, DISPLAY_HEIGHT = tuple([int(i // RATIO) for i in list(SCREEN_SIZE)])
x, y = (SCREEN_SIZE[0] - DISPLAY_WIDTH) // 2, (SCREEN_SIZE[1] - DISPLAY_HEIGHT) // 2
return x, y, DISPLAY_WIDTH, DISPLAY_HEIGHT
elif platform.system() == 'Linux':
output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',shell=True, stdout=subprocess.PIPE).communicate()[0]
resolution = [int(i) for i in output.split()[0].split(b'x')]
return resolution[0] // 2, 0, resolution[0], resolution[1]
return 683, 0, 1366, 768
if __name__ == '__main__':
def timed(frequency, *args):
''' Simple interval-based traffic control '''
if count % frequency == 0:
middle.flow = reverse(middle.flow)
def custom(frequency, *args):
''' Traffic control in which road with more cars gets the green light '''
hlanes = [lanes[2], lanes[6]]
vlanes = [lanes[0], lanes[4]]
if count % frequency == 0:
middle.flow = 'horizontal' if sum([len(l.cars) for l in hlanes]) > sum([len(l.cars) for l in vlanes]) else 'vertical'
def actuated(frequency, dmin, dmax, *args):
''' Actuated traffic control '''
global duration
def is_empty(lanes):
for l in lanes:
for c in l.cars:
position = [type(p) for p in c.position]
if Lane in position and Middle in position:
return False
return True
hlanes = [lanes[2], lanes[6]]
vlanes = [lanes[0], lanes[4]]
if count % frequency == 0:
switch = is_empty(hlanes) if middle.flow == 'horizontal' else is_empty(vlanes)
if (switch and duration > dmin and duration < dmax) or (duration > dmax):
middle.flow = reverse(middle.flow)
duration = 0
else:
duration += 1
def q_control(frequency, *args):
''' Traffic control using pretrained q table '''
if count % frequency != 0: return
def get_cars():
hcars = 0
vcars = 0
for l in hlanes:
hcars += len(l.cars)
for l in vlanes:
vcars += len(l.cars)
<<<<<<< HEAD
return (min(hcars, 7), min(vcars, 7))
return (hcars, vcars)
=======
<<<<<<< HEAD
return (min(hcars, 7), min(vcars, 7))
=======
return (hcars, vcars)
>>>>>>> d7063b3b83a5f01b86467f8c0c906e4a4acd70b5
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
hlanes = [lanes[2], lanes[6]]
vlanes = [lanes[0], lanes[4]]
state = get_cars()
action = np.argmax(table[state])
middle.flow = ACTIONS[action]
<<<<<<< HEAD
def deep_q_control(frequency, *args):
''' Traffic control using deep q learning '''
global action, old_state, episode_reward
if count % frequency != 0: return
def get_stat():
hcars = 0
vcars = 0
for l in hlanes:
hcars += len(l.cars)
for l in vlanes:
vcars += len(l.cars)
return np.array([min(hcars, 15), min(vcars, 15)])
def get_state():
cars = [len(l.cars) for l in [*hlanes, *vlanes]]
return np.array([min(c, 8) for c in cars])
hlanes = [lanes[2], lanes[6]]
vlanes = [lanes[0], lanes[4]]
action = np.argmax(agent.get_qs(get_state()))
middle.flow = ACTIONS[action]
ap = ArgumentParser()
ap.add_argument('-t', '--table', type=str, help='Path to the pretrained q table')
ap.add_argument('-m', '--model', type=str, help='Path to the pretrained neural network')
args = vars(ap.parse_args())
if args.get('table', None):
with open(args['table'], 'rb') as table_file:
table = pickle.load(table_file)
if args.get('model', None):
agent = DQNAgent('test')
agent.load(args['model'])
=======
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
reverse = lambda flow: {'horizontal': 'vertical'}.get(flow, 'horizontal')
x, y, DISPLAY_WIDTH, DISPLAY_HEIGHT = 640, 640, 1280, 1280#get_screen_metrics()
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
LANE_WIDTH = int(DISPLAY_WIDTH * 0.1)
VERT_LANE_LENGTH = DISPLAY_HEIGHT // 2 - LANE_WIDTH
HORZ_LANE_LENGTH = (DISPLAY_WIDTH // 2 - LANE_WIDTH)
CENTER = (DISPLAY_WIDTH // 2, DISPLAY_HEIGHT // 2)
<<<<<<< HEAD
SPEED = 8
TRIALS = 2
SIM_LENGTH = 300
ACTIONS = ['horizontal', 'vertical']
MAX_SIZE = 15
RENDER = True
controls = [timed, custom]
frequencies = [88]
=======
SPEED = 16
<<<<<<< HEAD
TRIALS = 20
=======
TRIALS = 5
>>>>>>> d7063b3b83a5f01b86467f8c0c906e4a4acd70b5
SIM_LENGTH = 500
ACTIONS = ['horizontal', 'vertical']
RENDER = True
with open(sys.argv[1], 'rb') as table_file:
table = pickle.load(table_file)
controls = [actuated, custom, q_control]
frequencies = [30]
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
pygame.init()
screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption('Simulation')
<<<<<<< HEAD
=======
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
# Iterate through each combination of the chosen control methods and frequencies
for control in controls:
rewards = []
waits = []
for frequency in frequencies:
count = 0
<<<<<<< HEAD
=======
total_wait = 0
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
duration = 0
for i in range(TRIALS):
trial_reward = 0
total_wait = 0
clock = pygame.time.Clock()
intersection = pygame.sprite.Group()
cars = pygame.sprite.Group()
middle = Middle(LANE_WIDTH * 2, LANE_WIDTH * 2, CENTER, screen)
intersection.add(middle)
lanes = []
lanes.append(Lane(LANE_WIDTH, VERT_LANE_LENGTH, 'down', ((DISPLAY_WIDTH - LANE_WIDTH) // 2, (DISPLAY_HEIGHT - VERT_LANE_LENGTH) // 2 - LANE_WIDTH), screen))
lanes.append(Lane(LANE_WIDTH, VERT_LANE_LENGTH, 'up', ((DISPLAY_WIDTH + LANE_WIDTH) // 2, (DISPLAY_HEIGHT - VERT_LANE_LENGTH) // 2 - LANE_WIDTH), screen))
lanes.append(Lane(LANE_WIDTH, HORZ_LANE_LENGTH, 'left', ((DISPLAY_WIDTH + HORZ_LANE_LENGTH) // 2 + LANE_WIDTH, (DISPLAY_HEIGHT - LANE_WIDTH) // 2), screen))
lanes.append(Lane(LANE_WIDTH, HORZ_LANE_LENGTH, 'right', ((DISPLAY_WIDTH + HORZ_LANE_LENGTH) // 2 + LANE_WIDTH, (DISPLAY_HEIGHT + LANE_WIDTH) // 2), screen))
lanes.append(Lane(LANE_WIDTH, VERT_LANE_LENGTH, 'up', ((DISPLAY_WIDTH + LANE_WIDTH) // 2, (DISPLAY_HEIGHT + VERT_LANE_LENGTH) // 2 + LANE_WIDTH), screen))
lanes.append(Lane(LANE_WIDTH, VERT_LANE_LENGTH, 'down', ((DISPLAY_WIDTH - LANE_WIDTH) // 2, (DISPLAY_HEIGHT + VERT_LANE_LENGTH) // 2 + LANE_WIDTH), screen))
lanes.append(Lane(LANE_WIDTH, HORZ_LANE_LENGTH, 'right', ((DISPLAY_WIDTH // 2 - LANE_WIDTH) // 2, (DISPLAY_HEIGHT + LANE_WIDTH) // 2), screen))
lanes.append(Lane(LANE_WIDTH, HORZ_LANE_LENGTH, 'left', ((DISPLAY_WIDTH // 2 - LANE_WIDTH) // 2, (DISPLAY_HEIGHT - LANE_WIDTH) // 2), screen))
intersection.add(*lanes)
main(control, SIM_LENGTH, frequency, 40, 150, RENDER)
<<<<<<< HEAD
rewards.append(trial_reward)
waits.append(total_wait)
print(f'Stats for {control.__name__}:')
print(f'Min: {min(rewards)}')
print(f'Avg: {sum(rewards)/TRIALS}')
print(f'Max: {max(rewards)}')
print(f'Wait: {sum(waits)/TRIALS}')
=======
print(f'\nAverage frames waited for {control.__name__}: {total_wait // TRIALS}')
>>>>>>> a5c2fc7961c043221138ed8771d55c24576bc46e
pygame.quit()
quit()