/
runner.py
78 lines (55 loc) · 2.09 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
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import optparse
import subprocess
import random
import traci
THISDIR = os.path.dirname(__file__)
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')")
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
def run():
step = 0
while step < 5426:
traci.simulationStep()
step += 1
for veh_id in traci.simulation.getDepartedIDList():
print (traci.vehicle.subscribe(veh_id, [traci.constants.VAR_POSITION]))
# print (traci.vehicle.getElectricityConsumption(veh_id))
print (traci.vehicle.getIDList())
traci.close()
sys.stdout.flush()
def get_options():
"""define options for this script and interpret the command line"""
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true",
default=False, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
# this is the main entry point of this script
if __name__ == "__main__":
# load whether to run with or without GUI
options = get_options()
# If 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')
sumoCmd = [sumoBinary, "-c", "testing/thisisthefile.sumo.cfg"]
traci.start(sumoCmd)
run()