Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/multipleConnections/subsecondSUMOstep/runner.py
169771 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file runner.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @author Leonhard Luecken
18
# @date 2010-02-20
19
20
from __future__ import absolute_import
21
from __future__ import print_function
22
23
import os
24
import subprocess
25
import sys
26
import time
27
import math
28
import multiprocessing
29
30
if "SUMO_HOME" in os.environ:
31
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
32
import sumolib # noqa
33
import traci # noqa
34
35
PORT = sumolib.miscutils.getFreeSocketPort()
36
DELTA_T = 1000
37
sumoBinary = sumolib.checkBinary(sys.argv[1])
38
39
40
def traciLoop(port, traciEndTime, index, SUMOsteplength, steplength=0):
41
orderTime = 0.25
42
time.sleep(orderTime * index) # assure ordering of outputs
43
if steplength == 0:
44
steplength = DELTA_T / 1000.
45
print("Starting process %s with steplength %s" % (index, steplength))
46
sys.stdout.flush()
47
traci.init(port)
48
traci.setOrder(index)
49
step = 1
50
nrEnteredVehicles = 0
51
sumoStop = False
52
try:
53
traciEndStep = math.ceil(traciEndTime / steplength)
54
while not step > traciEndStep:
55
traci.simulationStep(step * steplength)
56
# print(index, "asking for vehicles")
57
# sys.stdout.flush()
58
traci.vehicle.getIDList()
59
nrEnteredVehicles += traci.simulation.getDepartedNumber()
60
# ~ print(index, "Newly entered vehicles: ", traci.simulation.getDepartedNumber(), "(vehs: ", vehs, ")")
61
# ~ sys.stdout.flush()
62
step += 1
63
endTime = traci.simulation.getTime()
64
traci.close()
65
except traci.FatalTraCIError as e:
66
if str(e) == "connection closed by SUMO":
67
time.sleep(orderTime * index) # assure ordering of outputs
68
sumoStop = True
69
print("client %s: " % index, str(e), " (at TraCIStep %s)" % step)
70
sys.stdout.flush()
71
else:
72
raise
73
if not sumoStop:
74
time.sleep(orderTime * index) # assure ordering of outputs
75
print("Process %s ended at step %s" % (index, endTime))
76
print("Process %s was informed about %s entered vehicles" % (index, nrEnteredVehicles))
77
sys.stdout.flush()
78
79
80
def runSingle(sumoEndTime, traciEndTime, numClients, steplengths, runNr, SUMOsteplength):
81
fdi = open("sumo.sumocfg")
82
fdo = open("used.sumocfg", "w")
83
fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": SUMOsteplength})
84
fdi.close()
85
fdo.close()
86
sumoProcess = subprocess.Popen(
87
"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %
88
(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout)
89
# Alternate ordering
90
indexRange = range(numClients) if (runNr % 2 == 0) else list(reversed(range(numClients)))
91
procs = [multiprocessing.Process(target=traciLoop,
92
args=(PORT, traciEndTime, i + 1, SUMOsteplength, steplengths[indexRange[i]]))
93
for i in range(numClients)]
94
for p in procs:
95
p.start()
96
for p in procs:
97
p.join()
98
sumoProcess.wait()
99
sys.stdout.flush()
100
101
102
if __name__ == '__main__':
103
multiprocessing.set_start_method('spawn')
104
runNr = 2
105
clientRange = [4]
106
steplengths = [0.1, 1.0, 1.7, 2.0]
107
SUMOsteplengths = [0.1, 0.5]
108
print("----------- SUMO ends first -----------")
109
for numClients in clientRange:
110
print(" -------- numClients: %s --------" % numClients)
111
sys.stdout.flush()
112
for i in range(0, runNr):
113
print(" Run %s" % i, ", SUMO-steplength=", SUMOsteplengths[i])
114
sys.stdout.flush()
115
runSingle(50, 99, numClients, steplengths, i, SUMOsteplengths[i])
116
117
print("----------- TraCI ends first -----------")
118
for numClients in clientRange:
119
print(" -------- numClients: %s --------" % numClients)
120
sys.stdout.flush()
121
for i in range(0, runNr):
122
print(" Run %s" % i, ", SUMO-steplength=", SUMOsteplengths[i])
123
sys.stdout.flush()
124
runSingle(101, 99, numClients, steplengths, i, SUMOsteplengths[i])
125
126