-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrain_Simulation.py
302 lines (248 loc) · 11.7 KB
/
Train_Simulation.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
import traci
import numpy as np
import random
import timeit
from Helpers import Traffic_Route_Generator
# phase codes based on environment.net.xml
PHASE_NS_GREEN = 0 # action 0 code 00
PHASE_NS_YELLOW = 1
PHASE_NSL_GREEN = 2 # action 1 code 01
PHASE_NSL_YELLOW = 3
PHASE_EW_GREEN = 4 # action 2 code 10
PHASE_EW_YELLOW = 5
PHASE_EWL_GREEN = 6 # action 3 code 11
PHASE_EWL_YELLOW = 7
class Simulation:
def __init__(self, Model, CMD, gamma, maxSteps, numCars, greenDuration, yellowDuration, numStates, numActions, trainingEpochs):
self._Model = Model
self._sumo_cmd = CMD
self._gamma = gamma
self._maxSteps = maxSteps
self._numCars = numCars
self._greenDuration = greenDuration
self._yellowDuration = yellowDuration
self._numStates = numStates
self._numActions = numActions
self._step = 0
self._reward_store = []
self._cumulative_wait_store = []
self._avg_queue_length_store = []
self._training_epochs = trainingEpochs
def run(self, episode, epsilon):
"""
Runs an episode of simulation, then starts a training session
"""
start_time = timeit.default_timer()
# first, generate the route file for this simulation and set up sumo
Traffic_Route_Generator(self._numCars, self._maxSteps, seed=episode)
traci.start(self._sumo_cmd)
print("Simulating...")
# inits
self._step = 0
self._waiting_times = {}
self._sum_neg_reward = 0
self._sum_queue_length = 0
self._sum_waiting_time = 0
old_total_wait = 0
old_state = -1
old_action = -1
while self._step < self._maxSteps:
# get current state of the intersection
current_state = self.get_state()
# calculate reward of previous action: (change in cumulative waiting time between actions)
# waiting time = seconds waited by a car since the spawn in the environment, cumulated for every car in incoming lanes
current_total_wait = self.collect_waiting_times()
reward = old_total_wait - current_total_wait
# saving the data into the memory
if self._step != 0:
self._Model.add_sample((old_state, old_action, reward, current_state))
# choose the light phase to activate, based on the current state of the intersection
action = self.choose_action(current_state, epsilon)
# if the chosen phase is different from the last phase, activate the yellow phase
if self._step != 0 and old_action != action:
self.set_yellow_phase(old_action)
self.simulate(self._yellowDuration)
# execute the phase selected before
self.set_green_phase(action)
self.simulate(self._greenDuration)
# saving variables for later & accumulate reward
old_state = current_state
old_action = action
old_total_wait = current_total_wait
# saving only the meaningful reward to better see if the agent is behaving correctly
if reward < 0:
self._sum_neg_reward += reward
self.save_episode_stats()
print("Total reward:", self._sum_neg_reward, "- Epsilon:", round(epsilon, 2))
traci.close()
simulation_time = round(timeit.default_timer() - start_time, 1)
print("Training...")
start_time = timeit.default_timer()
for _ in range(self._training_epochs):
self.replay()
training_time = round(timeit.default_timer() - start_time, 1)
return simulation_time, training_time
def simulate(self, steps_todo):
"""
Execute steps in sumo while gathering statistics
"""
if (self._step + steps_todo) >= self._maxSteps: # do not do more steps than the maximum allowed number of steps
steps_todo = self._maxSteps - self._step
while steps_todo > 0:
traci.simulationStep() # simulate 1 step in sumo
self._step += 1 # update the step counter
steps_todo -= 1
queue_length = self.get_queue_length()
self._sum_queue_length += queue_length
self._sum_waiting_time += queue_length # 1 step while wating in queue means 1 second waited, for each car, therefore queue_lenght == waited_seconds
def collect_waiting_times(self):
"""
Retrieve the waiting time of every car in the incoming roads
"""
incoming_roads = ["E2TL", "N2TL", "W2TL", "S2TL"]
car_list = traci.vehicle.getIDList()
for car_id in car_list:
wait_time = traci.vehicle.getAccumulatedWaitingTime(car_id) # getWaitingTime
road_id = traci.vehicle.getRoadID(car_id) # get the road id where the car is located
if road_id in incoming_roads: # consider only the waiting times of cars in incoming roads
self._waiting_times[car_id] = wait_time
else:
if car_id in self._waiting_times: # a car that was tracked has cleared the intersection
del self._waiting_times[car_id]
total_waiting_time = sum(self._waiting_times.values())
return total_waiting_time
def choose_action(self, state, epsilon):
"""
This function uses an epsilon-greedy policy to decide whether to perform an explorative or exploitative action
"""
if random.random() < epsilon:
return random.randint(0, self._numActions - 1) # random action
else:
return np.argmax(self._Model.predict_one(state)) # the best action given the current state
def set_yellow_phase(self, old_action):
"""
Activate the correct yellow light combination in sumo
"""
yellow_phase_code = old_action * 2 + 1 # obtain the yellow phase code, based on the old action (ref on environment.net.xml)
traci.trafficlight.setPhase("TL", yellow_phase_code)
def set_green_phase(self, action_number):
"""
Activate the correct green light combination in sumo
"""
if action_number == 0:
traci.trafficlight.setPhase("TL", PHASE_NS_GREEN)
elif action_number == 1:
traci.trafficlight.setPhase("TL", PHASE_NSL_GREEN)
elif action_number == 2:
traci.trafficlight.setPhase("TL", PHASE_EW_GREEN)
elif action_number == 3:
traci.trafficlight.setPhase("TL", PHASE_EWL_GREEN)
def get_queue_length(self):
"""
Retrieve the number of cars with speed = 0 in every incoming lane
"""
halt_N = traci.edge.getLastStepHaltingNumber("N2TL")
halt_S = traci.edge.getLastStepHaltingNumber("S2TL")
halt_E = traci.edge.getLastStepHaltingNumber("E2TL")
halt_W = traci.edge.getLastStepHaltingNumber("W2TL")
queue_length = halt_N + halt_S + halt_E + halt_W
return queue_length
def get_state(self):
"""
Retrieve the state of the intersection from sumo, in the form of cell occupancy
"""
state = np.zeros(self._numStates)
car_list = traci.vehicle.getIDList()
for car_id in car_list:
lane_pos = traci.vehicle.getLanePosition(car_id)
lane_id = traci.vehicle.getLaneID(car_id)
lane_pos = 750 - lane_pos # inversion of lane pos, so if the car is close to the traffic light -> lane_pos = 0 --- 750 = max len of a road
# distance in meters from the traffic light -> mapping into cells
if lane_pos < 7:
lane_cell = 0
elif lane_pos < 14:
lane_cell = 1
elif lane_pos < 21:
lane_cell = 2
elif lane_pos < 28:
lane_cell = 3
elif lane_pos < 40:
lane_cell = 4
elif lane_pos < 60:
lane_cell = 5
elif lane_pos < 100:
lane_cell = 6
elif lane_pos < 160:
lane_cell = 7
elif lane_pos < 400:
lane_cell = 8
elif lane_pos <= 750:
lane_cell = 9
# finding the lane where the car is located
# x2TL_3 are the "turn left only" lanes
if lane_id == "W2TL_0" or lane_id == "W2TL_1" or lane_id == "W2TL_2":
lane_group = 0
elif lane_id == "W2TL_3":
lane_group = 1
elif lane_id == "N2TL_0" or lane_id == "N2TL_1" or lane_id == "N2TL_2":
lane_group = 2
elif lane_id == "N2TL_3":
lane_group = 3
elif lane_id == "E2TL_0" or lane_id == "E2TL_1" or lane_id == "E2TL_2":
lane_group = 4
elif lane_id == "E2TL_3":
lane_group = 5
elif lane_id == "S2TL_0" or lane_id == "S2TL_1" or lane_id == "S2TL_2":
lane_group = 6
elif lane_id == "S2TL_3":
lane_group = 7
else:
lane_group = -1
if lane_group >= 1 and lane_group <= 7:
car_position = int(str(lane_group) + str(lane_cell)) # composition of the two postion ID to create a number in interval 0-79
valid_car = True
elif lane_group == 0:
car_position = lane_cell
valid_car = True
else:
valid_car = False # flag for not detecting cars crossing the intersection or driving away from it
if valid_car:
state[car_position] = 1 # write the position of the car car_id in the state array in the form of "cell occupied"
return state
def replay(self):
"""
Retrieve a group of samples from the memory and for each of them update the learning equation, then train
"""
batch = self._Model.get_samples(self._Model.batch_size)
if len(batch) > 0: # if the memory is full enough
states = np.array([val[0] for val in batch]) # extract states from the batch
next_states = np.array([val[3] for val in batch]) # extract next states from the batch
# prediction
q_s_a = self._Model.predict_batch(states) # predict Q(state), for every sample
q_s_a_d = self._Model.predict_batch(next_states) # predict Q(next_state), for every sample
# setup training arrays
x = np.zeros((len(batch), self._numStates))
y = np.zeros((len(batch), self._numActions))
for i, b in enumerate(batch):
state, action, reward, _ = b[0], b[1], b[2], b[3] # extract data from one sample
current_q = q_s_a[i] # get the Q(state) predicted before
current_q[action] = reward + self._gamma * np.amax(q_s_a_d[i]) # update Q(state, action)
x[i] = state
y[i] = current_q # Q(state) that includes the updated action value
self._Model.train_batch(x, y) # train the NN
def save_episode_stats(self):
"""
Save the stats of the episode to plot the graphs at the end of the session
"""
self._reward_store.append(self._sum_neg_reward) # how much negative reward in this episode
self._cumulative_wait_store.append(self._sum_waiting_time) # total number of seconds waited by cars in this episode
self._avg_queue_length_store.append(self._sum_queue_length / self._maxSteps) # average number of queued cars per step, in this episode
@property
def reward_store(self):
return self._reward_store
@property
def cumulative_wait_store(self):
return self._cumulative_wait_store
@property
def avg_queue_length_store(self):
return self._avg_queue_length_store