Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/contextSubscriptions/runner.py
169685 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
# @date 2012-10-19
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
22
import os
23
import sys
24
import math
25
26
if "SUMO_HOME" in os.environ:
27
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
28
import sumolib # noqa
29
import traci # noqa
30
31
32
def dist2(v, w):
33
return (v[0] - w[0]) ** 2 + (v[1] - w[1]) ** 2
34
35
36
def distToSegmentSquared(p, v, w):
37
l2 = dist2(v, w)
38
if l2 == 0:
39
return dist2(p, v)
40
t = ((p[0] - v[0]) * (w[0] - v[0]) + (p[1] - v[1]) * (w[1] - v[1])) / l2
41
if t < 0:
42
return dist2(p, v)
43
if t > 1:
44
return dist2(p, w)
45
return dist2(p, (v[0] + t * (w[0] - v[0]), v[1] + t * (w[1] - v[1])))
46
47
48
def runSingle(traciEndTime, viewRange, module, objID):
49
seen1 = 0
50
seen2 = 0
51
step = 0
52
traci.start([sumolib.checkBinary(sys.argv[1]), '-Q', "-c", "sumo.sumocfg"])
53
traci.poi.add("poi", 400, 500, (1, 0, 0, 0))
54
traci.polygon.add("poly", ((400, 400), (450, 400), (450, 400)), (1, 0, 0, 0))
55
subscribed = False
56
while not step > traciEndTime:
57
# print(step)
58
responses = traci.simulationStep()
59
near1 = set()
60
if objID in module.getAllContextSubscriptionResults():
61
for v in module.getContextSubscriptionResults(objID):
62
near1.add(v)
63
# print(objID, "context:", sorted(near1))
64
vehs = traci.vehicle.getIDList()
65
persons = traci.person.getIDList()
66
pos = {}
67
for v in vehs:
68
pos[v] = traci.vehicle.getPosition(v)
69
for p in persons:
70
pos[p] = traci.person.getPosition(p)
71
shape = None
72
egoPos = None
73
if hasattr(module, "getPosition"):
74
egoPos = module.getPosition(objID)
75
elif hasattr(module, "getShape"):
76
shape = module.getShape(objID)
77
elif module == traci.edge:
78
# it's a hack, I know, but do we really need to introduce edge.getShape?
79
shape = traci.lane.getShape(objID + "_0")
80
near2 = set()
81
for v in pos:
82
if egoPos:
83
if math.sqrt(dist2(egoPos, pos[v])) < viewRange:
84
near2.add(v)
85
if shape:
86
lastP = shape[0]
87
for p in shape[1:]:
88
if math.sqrt(distToSegmentSquared(pos[v], lastP, p)) < viewRange:
89
near2.add(v)
90
lastP = p
91
92
if not subscribed:
93
module.subscribeContext(objID, traci.constants.CMD_GET_VEHICLE_VARIABLE, viewRange,
94
[traci.constants.VAR_POSITION])
95
module.subscribeContext(objID, traci.constants.CMD_GET_PERSON_VARIABLE, viewRange,
96
[traci.constants.VAR_POSITION])
97
subscribed = True
98
else:
99
seen1 += len(near1)
100
seen2 += len(near2)
101
for v in near1:
102
if v not in near2:
103
print("timestep %s: %s is missing in surrounding objects" % (step, v))
104
for v in near2:
105
if v not in near1:
106
print("timestep %s: %s is missing in subscription results" % (step, v))
107
108
step += 1
109
module.unsubscribeContext(objID, traci.constants.CMD_GET_VEHICLE_VARIABLE, viewRange)
110
traci.simulationStep()
111
responses = list(module.getAllContextSubscriptionResults())
112
print(responses) # person subscription should still be active
113
module.unsubscribeContext(objID, traci.constants.CMD_GET_PERSON_VARIABLE, viewRange)
114
traci.simulationStep()
115
responses = module.getAllContextSubscriptionResults()
116
if responses:
117
print("Error: Unsubscribe did not work", responses)
118
else:
119
print("Ok: Unsubscribe successful")
120
print("Print ended at step %s" % traci.simulation.getTime())
121
traci.close()
122
sys.stdout.flush()
123
print("uncheck: seen %s vehicles via subscription, %s in surrounding" %
124
(seen1, seen2))
125
if seen1 == seen2:
126
print("Ok: Subscription and computed are same")
127
else:
128
print("Error: subscribed number and computed number differ")
129
130
131
sys.stdout.flush()
132
if sys.argv[3] == "vehicle":
133
runSingle(1000, float(sys.argv[2]), traci.vehicle, "ego")
134
elif sys.argv[3] == "edge":
135
runSingle(1000, float(sys.argv[2]), traci.edge, "1fi")
136
elif sys.argv[3] == "lane":
137
runSingle(1000, float(sys.argv[2]), traci.lane, "2si_0")
138
elif sys.argv[3] == "junction":
139
runSingle(1000, float(sys.argv[2]), traci.junction, "0")
140
elif sys.argv[3] == "poi":
141
runSingle(1000, float(sys.argv[2]), traci.poi, "poi")
142
elif sys.argv[3] == "polygon":
143
runSingle(1000, float(sys.argv[2]), traci.polygon, "poly")
144
elif sys.argv[3] == "person":
145
runSingle(1000, float(sys.argv[2]), traci.person, "p0")
146
147