Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/multipleConnections/orderDependentResults/runner.py
169735 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, i, runNr, steplength=0):
41
orderTime = 0.25
42
time.sleep(orderTime * i) # assure ordering of outputs
43
if steplength == 0:
44
steplength = DELTA_T / 1000.
45
# order index dependent on runNr
46
index = i if (runNr % 2 == 0) else 10 - i
47
print("Starting process %s (order: %s) with steplength %s" % (i, index, steplength))
48
sys.stdout.flush()
49
traci.init(port)
50
traci.setOrder(index)
51
step = 1
52
lastVehID = ""
53
traciEndStep = math.ceil(traciEndTime / steplength)
54
while not step > traciEndStep:
55
print("Process %s:" % (i))
56
print(" stepping (step %s)..." % step)
57
traci.simulationStep(step * steplength)
58
vehs = traci.vehicle.getIDList()
59
if len(vehs) != 0:
60
vehID = vehs[0]
61
if vehID != lastVehID and lastVehID != "":
62
print(" breaking execution: traced vehicle '%s' left." % lastVehID)
63
break
64
else:
65
lastVehID = vehID
66
print(" Retrieving position for vehicle '%s' -> %s on lane '%s'" %
67
(vehID, traci.vehicle.getLanePosition(vehID), traci.vehicle.getLaneID(vehID)))
68
print(" Retrieving speed for vehicle '%s' -> %s" % (vehID, traci.vehicle.getSpeed(vehID)))
69
traci.vehicle.setSpeedMode(vehID, 0)
70
newSpeed = i * 5
71
print(" Setting speed for vehicle '%s' -> %s" % (vehID, newSpeed))
72
print(" Retrieving speed for vehicle '%s' -> %s" % (vehID, traci.vehicle.getSpeed(vehID)))
73
traci.vehicle.setSpeed(vehID, newSpeed)
74
elif lastVehID != "":
75
print(" breaking execution: traced vehicle '%s' left." % lastVehID)
76
break
77
step += 1
78
sys.stdout.flush()
79
endTime = traci.simulation.getTime()
80
traci.close()
81
time.sleep(orderTime * i) # assure ordering of outputs
82
print("Process %s (order %s) ended at step %s" % (i, index, endTime))
83
sys.stdout.flush()
84
85
86
def runSingle(sumoEndTime, traciEndTime, numClients, runNr):
87
sumoProcess = subprocess.Popen(
88
"%s -v --num-clients %s -c sumo.sumocfg -S -Q --remote-port %s" %
89
(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout) # Alternate ordering
90
procs = [multiprocessing.Process(target=traciLoop, args=(PORT, traciEndTime, (i + 1), runNr))
91
for i in range(numClients)]
92
for p in procs:
93
p.start()
94
for p in procs:
95
p.join()
96
sumoProcess.wait()
97
sys.stdout.flush()
98
99
100
if __name__ == '__main__':
101
multiprocessing.set_start_method('spawn')
102
numClients = 2
103
runNr = 2
104
print(" Testing client order dependence ...")
105
for i in range(0, runNr):
106
print("\n###### Run %s ######" % i)
107
sys.stdout.flush()
108
runSingle(50, 500, numClients, i)
109
110