/
runner.py
executable file
·94 lines (82 loc) · 3.5 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
#!/usr/bin/env python
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
# Copyright (C) 2008-2019 German Aerospace Center (DLR) and others.
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v20.html
# SPDX-License-Identifier: EPL-2.0
# @file runner.py
# @author Daniel Krajzewicz
# @author Michael Behrisch
# @author Leonhard Luecken
# @date 2012-10-19
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
sumoHome = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', ".."))
sys.path.append(os.path.join(sumoHome, "tools"))
import sumolib # noqa
import traci # noqa
if sys.argv[1] == "sumo":
sumoCall = [sumolib.checkBinary('sumo')]
else:
sumoCall = [sumolib.checkBinary('sumo-gui')] # , '-S', '-Q']
egoID = "ego"
def runSingle(traciEndTime, downstreamDist, upstreamDist, lanes, opposite, vTypes, vClasses):
step = 0
traci.start(sumoCall + ["-n", "input_net.net.xml", "-r", "input_routes.rou.xml", "--no-step-log", "true"])
subscribed = False
while not step > traciEndTime:
responses = traci.simulationStep()
near1 = set()
if subscribed:
print("Context results for veh '%s':" % egoID)
for v in sorted(traci.vehicle.getContextSubscriptionResults(egoID) or []):
print(v)
near1.add(v)
if not subscribed:
print("Subscribing to context of vehicle '%s'" % (egoID))
traci.vehicle.subscribeContext(egoID, traci.constants.CMD_GET_VEHICLE_VARIABLE, 0.0,
[traci.constants.VAR_POSITION])
print("""Adding subscription filters ...
(downstreamDist=%s, upstreamDist=%s, lanes=%s, opposite=%s
vTypes:%s, vClasses:%s)""" % (downstreamDist, upstreamDist, lanes, opposite, vTypes, vClasses))
sys.stdout.flush()
traci.vehicle.addSubscriptionFilterDownstreamDistance(downstreamDist)
traci.vehicle.addSubscriptionFilterUpstreamDistance(upstreamDist)
traci.vehicle.addSubscriptionFilterLanes(lanes)
if vClasses:
traci.vehicle.addSubscriptionFilterVClass(vClasses)
if vTypes:
traci.vehicle.addSubscriptionFilterVType(vTypes)
if not opposite:
traci.vehicle.addSubscriptionFilterNoOpposite()
subscribed = True
step += 1
traci.vehicle.unsubscribeContext(egoID, traci.constants.CMD_GET_VEHICLE_VARIABLE, 0.0)
responses = traci.simulationStep()
if responses:
print("Error: Unsubscribe did not work")
else:
print("Ok: Unsubscribe successful")
print("Print ended at step %s" % traci.simulation.getTime())
traci.close()
sys.stdout.flush()
if len(sys.argv) < 8:
print("Usage: runner <sumo/sumo-gui> <downstreamDist> <upstreamDist> <lanes(csv)> " +
"<opposite{0,1}> <vTypes> <vClasses>")
sys.exit("")
sys.stdout.flush()
opposite = bool(int(sys.argv[5]))
lanes = [int(s.strip()) for s in sys.argv[4].split(",")]
if sys.argv[6].strip() != "":
vTypes = [s.strip() for s in sys.argv[6].split(",")]
else:
vTypes = []
if sys.argv[7].strip() != "":
vClasses = [s.strip() for s in sys.argv[7].split(",")]
else:
vClasses = []
runSingle(1, float(sys.argv[2]), float(sys.argv[3]), lanes, opposite, vTypes, vClasses)