Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/multipleConnections/differentSteplengths/runner.py
169727 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
from multiprocessing import Process
30
31
if "SUMO_HOME" in os.environ:
32
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
33
import sumolib # noqa
34
import traci # noqa
35
36
PORT = sumolib.miscutils.getFreeSocketPort()
37
DELTA_T = 1000
38
sumoBinary = sumolib.checkBinary(sys.argv[1])
39
40
41
def traciLoop(port, traciEndTime, index, steplength=0):
42
orderTime = 0.25
43
time.sleep(orderTime * index) # assure ordering of outputs
44
if steplength == 0:
45
steplength = DELTA_T / 1000.
46
print("Starting process %s with steplength %s" % (index, steplength))
47
sys.stdout.flush()
48
traci.init(port)
49
traci.setOrder(index)
50
step = 1
51
nrEnteredVehicles = 0
52
sumoStop = False
53
try:
54
traciEndStep = math.ceil(traciEndTime / steplength)
55
while not step > traciEndStep:
56
traci.simulationStep(step * steplength)
57
# print(index, "asking for vehicles")
58
# sys.stdout.flush()
59
traci.vehicle.getIDList()
60
nrEnteredVehicles += traci.simulation.getDepartedNumber()
61
# ~ print(index, "Newly entered vehicles: ", traci.simulation.getDepartedNumber(), "(vehs: ", vehs, ")")
62
# ~ sys.stdout.flush()
63
step += 1
64
endTime = traci.simulation.getTime()
65
traci.close()
66
except traci.FatalTraCIError as e:
67
if str(e) == "connection closed by SUMO":
68
time.sleep(orderTime * index) # assure ordering of outputs
69
sumoStop = True
70
print("client %s: " % index, str(e), " (at TraCIStep %s)" % step)
71
sys.stdout.flush()
72
else:
73
raise
74
if not sumoStop:
75
time.sleep(orderTime * index) # assure ordering of outputs
76
print("Process %s ended at step %s" % (index, endTime))
77
print("Process %s was informed about %s entered vehicles" % (index, nrEnteredVehicles))
78
sys.stdout.flush()
79
80
81
def runSingle(sumoEndTime, traciEndTime, numClients, steplengths, runNr):
82
fdi = open("sumo.sumocfg")
83
fdo = open("used.sumocfg", "w")
84
fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": DELTA_T / 1000.})
85
fdi.close()
86
fdo.close()
87
sumoProcess = subprocess.Popen(
88
"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %
89
(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout)
90
# Alternate ordering
91
indexRange = range(numClients) if (runNr % 2 == 0) else list(reversed(range(numClients)))
92
procs = [Process(target=traciLoop, args=(PORT, traciEndTime, i + 1, 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
print("----------- SUMO ends first -----------")
108
for numClients in clientRange:
109
print(" -------- numClients: %s --------" % numClients)
110
sys.stdout.flush()
111
for i in range(0, runNr):
112
print(" Run %s" % i)
113
sys.stdout.flush()
114
runSingle(50, 99, numClients, steplengths, i)
115
116
print("----------- TraCI ends first -----------")
117
for numClients in clientRange:
118
print(" -------- numClients: %s --------" % numClients)
119
sys.stdout.flush()
120
for i in range(0, runNr):
121
print(" Run %s" % i)
122
sys.stdout.flush()
123
runSingle(101, 99, numClients, steplengths, i)
124
125