-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest_Simulation.py
241 lines (195 loc) · 8.5 KB
/
Test_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
import traci
import numpy as np
import random
import timeit
import os
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, maxSteps, numCars, greenDuration, yellowDuration, numStates, numActions):
self._Model = Model
self._step = 0
self._sumo_cmd = CMD
self._maxSteps = maxSteps
self._numCars = numCars
self._green_duration = greenDuration
self._yellow_duration = yellowDuration
self._num_states = numStates
self._num_actions = numActions
self._reward_episode = []
self._queue_length_episode = []
def run(self, episode):
"""
Runs the testing simulation
"""
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 = {}
old_total_wait = 0
old_action = -1 # dummy init
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
# choose the light phase to activate, based on the current state of the intersection
action = self.choose_action(current_state)
# 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._yellow_duration)
# execute the phase selected before
self.set_green_phase(action)
self.simulate(self._green_duration)
# saving variables for later & accumulate reward
old_action = action
old_total_wait = current_total_wait
self._reward_episode.append(reward)
#print("Total reward:", np.sum(self._reward_episode))
traci.close()
simulation_time = round(timeit.default_timer() - start_time, 1)
return simulation_time
def simulate(self, steps_todo):
"""
Proceed with the simulation in sumo
"""
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._queue_length_episode.append(queue_length)
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)
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):
"""
Pick the best action known based on the current state of the env
"""
return np.argmax(self._Model.predict_one(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._num_states)
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
@property
def queue_length_episode(self):
return self._queue_length_episode
@property
def reward_episode(self):
return self._reward_episode