-
Notifications
You must be signed in to change notification settings - Fork 6
/
runner.py
347 lines (298 loc) · 14.1 KB
/
runner.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
"""
"""
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import optparse
import subprocess
import random
from collections import Counter
from pymongo import MongoClient
from dbFunction import dbFunction, initTrafficLight, initRunCount, saveStats, getRunCount
from globals import init
from helper import updateVehDistribution, plotGraph, savePlot, generate_routefile, getDBName
# we need to import python modules from the $SUMO_HOME/tools directory
try:
sys.path.append(os.path.join(os.path.dirname(
__file__), '..', '..', '..', '..', "tools")) # tutorial in tests
sys.path.append(os.path.join(os.environ.get("SUMO_HOME", os.path.join(
os.path.dirname(__file__), "..", "..", "..")), "tools")) # tutorial in docs
from sumolib import checkBinary
except ImportError:
sys.exit(
"please declare environment variable 'SUMO_HOME' as the root directory of your sumo installation (it should contain folders 'bin', 'tools' and 'docs')")
import traci
client = MongoClient()
def run(options):
initRunCount(options)
db = client[options.dbName]
tempStats = []
temp = []
# get list of traffic lights
trafficLights = traci.trafficlight.getIDList()
trafficLightsNumber = traci.trafficlight.getIDCount()
# we set every light to phase 0
for ID in trafficLights:
traci.trafficlight.setPhase(ID, 0)
initTrafficLight(ID)
tempStats.append(temp)
phaseVector = 6 * [None]
prePhase = trafficLightsNumber * [phaseVector]
preAction = trafficLightsNumber * [0]
currPhase = trafficLightsNumber * [0]
currTime = 0
dbStep = 10
avgQL = trafficLightsNumber * [0]
avgQLCurr = trafficLightsNumber * [0]
oldVeh = trafficLightsNumber * [None]
cumuDelay = trafficLightsNumber * [None]
ages = trafficLightsNumber * [0]
avgPlot = 0
# get age value from DB
i = 0
for ID in trafficLights:
qValues = db['qValues' + ID]
if (qValues.count_documents({"ageExists": True}) != 0):
ages[i] = qValues.find_one({"ageExists": True})['age']
i += 1
# execute the TraCI control loop
step = 1
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
# current traffic light index number
i = 0
for ID in trafficLights:
# get lanes for each traffic light
lanes = traci.trafficlight.getControlledLanes(ID)
lanesUniq = []
# get unique lanes
j = 0
while j < len(lanes):
lanesUniq.append(lanes[j])
j += 2
lanesUniq.append(lanes[j])
j += 3
lanes = lanesUniq
# get average queue length for current time step
queueLength = []
avgQLCurr[i] = 0
for lane in lanes:
queueLength.append(traci.lane.getLastStepHaltingNumber(lane))
avgQLCurr[i] += traci.lane.getLastStepHaltingNumber(lane)
avgQLCurr[i] = avgQLCurr[i] / (len(lanes) * 1.0)
# get average queue length till now
avgQL[i] = (avgQL[i] * step + avgQLCurr[i]) / ((step + 1) * 1.0)
# call plot graph with avg ql every 10*dbDtep
if(i == trafficLightsNumber - 1 and step % (dbStep * 30) == 0):
avgPlot /= dbStep * 10
plotGraph(step / (dbStep * 30), avgPlot)
avgPlot = 0
elif(i == trafficLightsNumber - 1):
avgQLTotal = 0
for avgQLC in avgQLCurr:
avgQLTotal += avgQLC
avgQLTotal = avgQLTotal / (trafficLightsNumber * 1.0)
avgPlot += avgQLTotal
options.bracket = int(options.bracket)
# run only for every db_step and when phase is not yellow
# yellow compulsory
currPhase[i] = traci.trafficlight.getPhase(ID)
condition = True
if (options.phasing == '1'):
condition = currPhase[i] != 2 and currPhase[i] != 4 and currPhase[i] != 7 and currPhase[i] != 9
if (step % dbStep == 0 and condition):
# emergency vehicle
for x in range(len(lanes)):
listVehicles = traci.lane.getLastStepVehicleIDs(lanes[x])
for veh in listVehicles:
if(traci.vehicle.getTypeID(veh) == "v1"):
queueLength[x] += 5
# print and save current stats
# print(avgQL[i], avgQLCurr[i], step, ID, "AvgQLs, step, ID")
tempStats[int(ID)].append({"step": step,
"curr_qL": avgQLCurr[i],
"avgQL": avgQL[i],
"ID": ID})
# skip everything and run according to default values
if (options.learn == '0'):
i += 1
continue
if (options.stateRep == '1'):
# generate current step's phase vector - with queueLength
phaseVector[0] = int(
round(max(queueLength[4], queueLength[5]) / options.bracket))
phaseVector[1] = int(
round(max(queueLength[0], queueLength[4]) / options.bracket))
phaseVector[2] = int(
round(max(queueLength[0], queueLength[1]) / options.bracket))
phaseVector[3] = int(
round(max(queueLength[3], queueLength[2]) / options.bracket))
phaseVector[4] = int(
round(max(queueLength[2], queueLength[6]) / options.bracket))
phaseVector[5] = int(
round(max(queueLength[6], queueLength[7]) / options.bracket))
elif (options.stateRep == '2'):
# get cumulative delay
cumulativeDelay = cumuDelay[i]
oldVehicles = oldVeh[i]
vehicles = []
for z in lanes:
vehicles.append(Counter())
if(cumulativeDelay is None):
cumulativeDelay = len(lanes) * [0]
if(oldVehicles is None):
oldVehicles = []
for z in lanes:
oldVehicles.append(Counter())
j = 0
for lane in lanes:
listVehicles = traci.lane.getLastStepVehicleIDs(lane)
for veh in listVehicles:
vehicles[j][veh] = oldVehicles[j][veh]
if (traci.vehicle.isStopped(veh)):
vehicles[j][veh] += 1
cumulativeDelay[j] += 1
vehToDelete = oldVehicles[j] - vehicles[j]
for veh, vDelay in vehToDelete.most_common():
cumulativeDelay[j] -= vDelay
oldVehicles[j] = vehicles[j]
j += 1
cumuDelay[i] = cumulativeDelay
oldVeh[i] = oldVehicles
# generate current step's phase vector - with
# cumulativeDelay
phaseVector[0] = int(
round(cumulativeDelay[4] + cumulativeDelay[5]) / options.bracket)
phaseVector[1] = int(
round(cumulativeDelay[0] + cumulativeDelay[4]) / options.bracket)
phaseVector[2] = int(
round(cumulativeDelay[0] + cumulativeDelay[1]) / options.bracket)
phaseVector[3] = int(
round(cumulativeDelay[3] + cumulativeDelay[2]) / options.bracket)
phaseVector[4] = int(
round(cumulativeDelay[2] + cumulativeDelay[6]) / options.bracket)
phaseVector[5] = int(
round(cumulativeDelay[6] + cumulativeDelay[7]) / options.bracket)
# update values
nextAction = dbFunction(
phaseVector, prePhase[i], preAction[i], ages[i], currPhase[i], ID, options)
ages[i] += 0.01
prePhase[i] = phaseVector[:]
if(options.phasing == '1'):
traci.trafficlight.setPhase(ID, nextAction)
if(nextAction != currPhase[i]):
nextAction = 1
else:
nextAction = 0
else:
if(nextAction != currPhase[i]):
oldRGYState = traci.trafficlight.getRedYellowGreenState(
ID)
traci.trafficlight.setPhase(ID, nextAction)
newRGYState = traci.trafficlight.getRedYellowGreenState(
ID)
# print(oldRGYState, newRGYState, "old new")
midRGYState = ""
for k, c in enumerate(oldRGYState):
if (c == 'G' and newRGYState[k] == 'r'):
midRGYState += 'Y'
elif(c == 'G' and newRGYState[k] == 'g'):
midRGYState += 'g'
else:
midRGYState += c
# print(midRGYState, "mid")
traci.trafficlight.setRedYellowGreenState(
ID, midRGYState)
tempCounter = 5
while(tempCounter > 0):
traci.simulationStep()
tempCounter -= 1
traci.trafficlight.setProgram(ID, 'custom2')
traci.trafficlight.setPhase(ID, nextAction)
else:
traci.trafficlight.setPhase(ID, nextAction)
preAction[i] = nextAction
# increment traffic light index
i += 1
step += 1
# update age in DB
i = 0
for ID in trafficLights:
qValues = db['qValues' + ID]
qValues.find_one_and_update(
{"ageExists": True}, {"$set": {"age": ages[i]}}, upsert=True)
i += 1
# print final average
avgQLTotal = 0
i = 0
for avgQLC in avgQL:
# print(avgQLC, "Final", i)
i += 1
avgQLTotal += avgQLC
avgQLTotal = avgQLTotal / (trafficLightsNumber * 1.0)
# print(avgQLTotal, "Final Total")
saveStats(trafficLightsNumber, tempStats)
savePlot(options.dbName + str(getRunCount()))
traci.close()
sys.stdout.flush()
return avgQLTotal
# this gets the input parameters specified to the program
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true",
default=False, help="run the commandline version of sumo")
optParser.add_option("--cars", "-C", dest="numberCars", default=1000, metavar="NUM",
help="specify the number of cars generated for simulation")
optParser.add_option("--bracket", dest="bracket", default=4, metavar="BRACKET",
help="specify the number with which to partition the range of queue length/cumulative delay")
optParser.add_option("--learning", dest="learn", default='1', metavar="NUM", choices=['0', '1', '2'],
help="specify learning method (0 = No Learning, 1 = Q-Learning, 2 = SARSA)")
optParser.add_option("--state", dest="stateRep", default='1', metavar="NUM", choices=['1', '2'],
help="specify traffic state representation to be used (1 = Queue Length, 2 = Cumulative Delay)")
optParser.add_option("--phasing", dest="phasing", default='1', metavar="NUM", choices=['1', '2'],
help="specify phasing scheme (1 = Fixed Phasing, 2 = Variable Phasing)")
optParser.add_option("--action", dest="actionSel", default='1', metavar="NUM", choices=['1', '2'],
help="specify action selection method (1 = epsilon greedy, 2 = softmax)")
optParser.add_option("--sublane", dest="sublaneNumber", default=2, metavar="FLOAT",
help="specify number of sublanes per edge (max=6) ")
optParser.add_option("--dbstep", dest="sbStep", default=10, metavar="NUM",
help="specify dbStep, default is 10 ")
optParser.add_option("--dbName", dest="dbName", default='tl', metavar="STRING",
help="specify dbName prefix")
options, args = optParser.parse_args()
return options
# this is the main entry point of this script
if __name__ == "__main__":
options = get_options()
# this script has been called from the command line. It will start sumo as a
# server, then connect and run
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
# generate the route file for this simulation
generate_routefile(options)
options.dbName = getDBName(options)
addFile = "data/cross.add.xml"
if (options.learn == '0'):
addFile = "data/cross_no_learn.add.xml"
elif (options.phasing == '2'):
addFile = "data/cross_variable.add.xml"
edgeWidth = 5
lateral_resolution_width = 2.5
if(int(options.sublaneNumber) <= 6.0):
lateral_resolution_width = float(
edgeWidth / int(options.sublaneNumber))
lateral_resolution_width = str(lateral_resolution_width)
# Sumo is started as a subprocess and then the python script connects and
# runs
traci.start([sumoBinary, "-c", "data/cross.sumocfg",
"-n", "data/cross.net.xml",
"-a", addFile,
"-r", "data/cross.rou.xml",
"--lateral-resolution", lateral_resolution_width,
"--queue-output", "queue.xml",
"--tripinfo-output", "tripinfo.xml"])
init(options)
run(options)