-
Notifications
You must be signed in to change notification settings - Fork 0
/
RLSimulation.py
399 lines (327 loc) · 14.9 KB
/
RLSimulation.py
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
'''
IMPORTS
'''
import itertools
import random as rand
import math
import matplotlib.pyplot as plt
import numpy as np
import copy
from Car import Car, Truck, Sedan, SUV, Sport
from ISD import ISD
'''
SIMULATION MECHANISM
'''
class Simulation:
CAR_LENGTH = 6
ROAD_LENGTH = CAR_LENGTH * 20
DESIRED_SPEED = 6
def __init__(self):
self.cars = []
self.init_cars()
self.iteration = 0
self.avg_speed = {}
def init_cars(self):
self.cars.append(self.generate_car())
def generate_car(self):
speed = 6
n = rand.randint(0, 3)
if n == 0:
car = Truck()
elif n == 1:
car = Sedan()
elif n == 2:
car = SUV()
else:
car = Sport()
car.set_speed(speed)
return car
def step(self):
if self.cars[0].x > self.ROAD_LENGTH:
self.cars.pop(0)
fender_dist = None
total_speed = self.cars[0].speed
if self.CAR_LENGTH * 1.25 < self.cars[-1].x:
self.cars.append(self.generate_car())
self.cars[0].drive()
self.cars[0].incr_speed()
for i in range(len(self.cars)):
self.cars[i].drive()
if i != 0:
fender_dist = self.cars[i - 1].x - self.cars[i].x
self.cars[i].incr_speed() if fender_dist > self.CAR_LENGTH * 1.25 else None
self.cars[i].decr_speed() if fender_dist < self.CAR_LENGTH * 1.25 else None
self.cars[i].brake() if fender_dist < self.CAR_LENGTH * 0.625 else None
total_speed += self.cars[i].speed
self.avg_speed[self.iteration] = total_speed / len(self.cars)
self.iteration += 1
def data_analysis(self):
self.avg_speed = sorted(self.avg_speed.items())
x, y = zip(*self.avg_speed)
plt.plot(x, y)
plt.xlabel("Simulation iteration")
plt.ylabel("Average System Speed (mi / h)")
plt.title("Traffic Simulation w/o ISDs")
plt.show()
'''
REINFORCED LEARNING TRAINED SIMULATION
'''
class RLSimulation(Simulation):
def __init__(self, alpha, gamma):
super().__init__()
self.alpha = alpha
self.gamma = gamma
self.n_actions = 4
self.n_sys_speed = 8
self.n_isd_speed = 7
self.n_car_type = 4
self.n_car_speed = 8
self.Qmat = [[[[{0: 0., 1: 0., 2: 0. , 3: 0.}
for _ in range(self.n_car_speed)]
for _ in range(self.n_car_type)]
for _ in range(self.n_isd_speed)]
for _ in range(self.n_sys_speed)]
self.speed_incr_reward = 50
self.speed_same_reward = 10
self.speed_decr_reward = -20
self.default_reward = -1
self.isd_present = False
self.isd = self.generate_isd()
self.isd.set_state(None, None, None, None)
self.curr_speed = 0
def generate_isd(self):
car = ISD()
car.set_speed(6)
return car
def setup_training(self):
self.train_isd = self.generate_isd()
self.train_isd.set_state(None, None, None, None)
self.train_isd_present = False
self.train_isd_index = -1
self.train_cars = []
self.train_iteration = 0
self.train_avg_speed = {}
self.train_cars.append(self.generate_car())
self.train_car_behind = False
def train(self, e = 0.1):
self.train_pop()
fender_dist = None
total_speed = self.train_cars[0].speed
self.train_add_car()
if self.train_car_behind:
action = self.train_each_action(e)
for i in range(len(self.train_cars)):
self.train_cars[i].drive()
if i != 0:
fender_dist = self.train_cars[i - 1].x - self.train_cars[i].x
if self.train_cars[i].car_type != 'isd' or self.train_car_behind == False:
self.train_cars[i].incr_speed() if fender_dist > self.CAR_LENGTH * 1.25 else None
self.train_cars[i].decr_speed() if fender_dist < self.CAR_LENGTH * 1.25 else None
self.train_cars[i].brake() if fender_dist < self.CAR_LENGTH * 0.625 else None
else:
if action == 0:
self.train_cars[i].incr_speed()
elif action == 1:
self.train_cars[i].decr_speed()
elif action == 2:
self.train_cars[i].brake()
else:
None
total_speed += self.train_cars[i].speed
self.train_avg_speed[self.train_iteration] = total_speed / len(self.train_cars)
self.curr_speed = int(self.train_avg_speed[self.train_iteration])
self.train_iteration += 1
def train_pop(self):
if self.train_cars[0].x > self.ROAD_LENGTH:
if self.train_cars[0].car_type == 'isd':
self.train_isd_present = False
self.train_isd_index = -1
self.train_car_behind = False
self.train_isd = self.generate_isd()
self.train_isd.set_state(None, None, None, None)
self.train_cars.pop(0)
if self.train_isd_present == True:
self.train_isd_index -= 1
def train_add_car(self):
if self.CAR_LENGTH * 1.25 < self.train_cars[-1].x:
if self.train_iteration > 0 and self.curr_speed < 6 and self.train_isd_present == False:
self.train_cars.append(self.train_isd)
self.train_isd_present = True
self.train_isd_index = len(self.train_cars) - 1
else:
self.train_cars.append(self.generate_car())
if self.train_isd_present:
car_behind = self.train_cars[self.train_isd_index + 1]
car_type = self.type_to_num(car_behind.car_type)
car_speed = car_behind.speed
self.train_isd.set_state(self.curr_speed, self.train_isd.speed, car_type, car_speed)
self.train_car_behind = True
def train_each_action(self, e):
[curr_sys_speed, curr_speed, curr_car_type, curr_car_speed] = self.train_isd.state
car_type = self.type_to_num(curr_car_type)
for i in range(self.n_actions):
train_copy = copy.deepcopy(self.train_isd)
train_cars_copy = self.train_cars.copy()
outcome = self.train_next_step(train_copy, self.train_isd_index, train_cars_copy, i)
reward = self.check_reward(curr_sys_speed, outcome[0])
action = train_copy.select_e_greedily(self.Qmat, e = e)
self.Qmat[curr_sys_speed - 1][curr_speed - 1][car_type][curr_car_speed - 1][i] += self.alpha * (reward + self.gamma * self.Qmat[int(outcome[0]) - 1][outcome[1] - 1][car_type][outcome[2] - 1][action] -
self.Qmat[curr_sys_speed - 1][curr_speed - 1][car_type][curr_car_speed - 1][i])
def train_next_step(self, isd, isd_index, cars, action):
fender_dist = None
total_speed = cars[0].speed
if self.CAR_LENGTH * 1.25 < cars[-1].x:
cars.append(self.generate_car())
cars[0].drive()
cars[0].incr_speed()
for i in range(len(cars)):
cars[i].drive()
if i != 0:
fender_dist = cars[i - 1].x - cars[i].x
if cars[i].car_type != 'isd':
cars[i].incr_speed() if fender_dist > self.CAR_LENGTH * 3 else None
cars[i].decr_speed() if fender_dist < self.CAR_LENGTH * 3 else None
cars[i].brake() if fender_dist < self.CAR_LENGTH * 1.5 else None
else:
if action == 0:
cars[i].incr_speed()
elif action == 1:
cars[i].decr_speed()
elif action == 2:
cars[i].brake()
else:
None
total_speed += cars[i].speed
sys_avg = total_speed / len(cars)
isd_speed = cars[isd_index].speed
car_speed = cars[isd_index + 1].speed
return sys_avg, isd_speed, car_speed
def check_reward(self, init_speed, curr_speed):
if curr_speed > init_speed:
return self.speed_incr_reward
elif curr_speed < init_speed:
return self.speed_decr_reward
elif abs(curr_speed - init_speed) <= 0.0001:
return self.speed_same_reward
else:
return self.default_reward
def training_data_analysis(self):
self.train_avg_speed = sorted(self.train_avg_speed.items())
x, y = zip(*self.train_avg_speed)
plt.plot(x, y)
plt.xlabel("Simulation iteration")
plt.ylabel("Average System Speed (mi / h)")
plt.title("Traffic Simulation Training w/ ISDs")
plt.show()
def type_to_num(self, car_type):
if car_type == 'truck':
return 0
elif car_type == 'sedan':
return 1
elif car_type == 'suv':
return 2
else:
return 3
def setup_step(self):
self.isd = self.generate_isd()
self.isd.set_state(None, None, None, None)
self.isd_present = False
self.isd_index = -1
self.cars = []
self.iteration = 0
self.avg_speed = {}
self.cars.append(self.generate_car())
self.car_behind = False
self.curr_speed = 0
def step_when_slow(self):
if self.cars[0].x > self.ROAD_LENGTH:
if self.cars[0].car_type == 'isd':
self.isd_present = False
self.isd_index = -1
self.car_behind = False
self.isd = self.generate_isd()
self.isd.set_state(None, None, None, None)
self.cars.pop(0)
if self.isd_present == True:
self.isd_index -= 1
fender_dist = None
total_speed = self.cars[0].speed
if self.CAR_LENGTH * 1.25 < self.cars[-1].x:
if self.iteration > 0 and self.curr_speed < 6 and self.isd_present == False:
self.cars.append(self.isd)
self.isd_present = True
self.isd_index = len(self.cars) - 1
else:
self.cars.append(self.generate_car())
if self.isd_present:
car_behind_isd = self.cars[self.isd_index + 1]
car_type = self.type_to_num(car_behind_isd.car_type)
car_speed = car_behind_isd.speed
self.isd.set_state(self.curr_speed, self.isd.speed, car_type, car_speed)
self.car_behind = True
self.cars[0].drive()
self.cars[0].incr_speed()
if self.car_behind:
action = self.isd.select_e_greedily(self.Qmat, e = 0.1)
for i in range(len(self.cars)):
self.cars[i].drive()
if i != 0:
fender_dist = self.cars[i - 1].x - self.cars[i].x
if self.cars[i].car_type != 'isd' or self.car_behind == False:
self.cars[i].incr_speed() if fender_dist > self.CAR_LENGTH * 1.25 else None
self.cars[i].decr_speed() if fender_dist < self.CAR_LENGTH * 1.25 else None
self.cars[i].brake() if fender_dist < self.CAR_LENGTH * 0.625 else None
else:
if action == 0:
self.cars[i].incr_speed()
elif action == 1:
self.cars[i].decr_speed()
elif action == 2:
self.cars[i].brake()
else:
None
total_speed += self.cars[i].speed
self.avg_speed[self.iteration] = total_speed / len(self.cars)
self.iteration += 1
def step(self):
if self.cars[0].x > self.ROAD_LENGTH:
self.cars.pop(0)
fender_dist = None
total_speed = self.cars[0].speed
if self.CAR_LENGTH * 1.25 < self.cars[-1].x:
self.cars.append(self.step_generate_car())
self.cars[0].drive()
self.cars[0].incr_speed()
for i in range(len(self.cars)):
self.cars[i].drive()
if i != 0:
fender_dist = self.cars[i - 1].x - self.cars[i].x
self.cars[i].incr_speed() if fender_dist > self.CAR_LENGTH * 1.25 else None
self.cars[i].decr_speed() if fender_dist < self.CAR_LENGTH * 1.25 else None
self.cars[i].brake() if fender_dist < self.CAR_LENGTH * 0.625 else None
total_speed += self.cars[i].speed
self.avg_speed[self.iteration] = total_speed / len(self.cars)
self.iteration += 1
def step_generate_car(self):
speed = 6
n = rand.randint(0, 4)
if n == 0:
car = Truck()
elif n == 1:
car = Sedan()
elif n == 2:
car = SUV()
elif n == 3:
car = Sport()
else:
car = ISD()
car.set_speed(speed)
return car
def data_analysis(self):
self.avg_speed = sorted(self.avg_speed.items())
x, y = zip(*self.avg_speed)
plt.plot(x, y)
plt.xlabel("Simulation iteration")
plt.ylabel("Average System Speed (mi / h)")
plt.title("Traffic Simulation w/ ISDs")
plt.show()