Path: blob/main/tests/complex/traci/contextSubscriptions/runner.py
169685 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file runner.py14# @author Daniel Krajzewicz15# @author Michael Behrisch16# @date 2012-10-191718from __future__ import absolute_import19from __future__ import print_function2021import os22import sys23import math2425if "SUMO_HOME" in os.environ:26sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))27import sumolib # noqa28import traci # noqa293031def dist2(v, w):32return (v[0] - w[0]) ** 2 + (v[1] - w[1]) ** 2333435def distToSegmentSquared(p, v, w):36l2 = dist2(v, w)37if l2 == 0:38return dist2(p, v)39t = ((p[0] - v[0]) * (w[0] - v[0]) + (p[1] - v[1]) * (w[1] - v[1])) / l240if t < 0:41return dist2(p, v)42if t > 1:43return dist2(p, w)44return dist2(p, (v[0] + t * (w[0] - v[0]), v[1] + t * (w[1] - v[1])))454647def runSingle(traciEndTime, viewRange, module, objID):48seen1 = 049seen2 = 050step = 051traci.start([sumolib.checkBinary(sys.argv[1]), '-Q', "-c", "sumo.sumocfg"])52traci.poi.add("poi", 400, 500, (1, 0, 0, 0))53traci.polygon.add("poly", ((400, 400), (450, 400), (450, 400)), (1, 0, 0, 0))54subscribed = False55while not step > traciEndTime:56# print(step)57responses = traci.simulationStep()58near1 = set()59if objID in module.getAllContextSubscriptionResults():60for v in module.getContextSubscriptionResults(objID):61near1.add(v)62# print(objID, "context:", sorted(near1))63vehs = traci.vehicle.getIDList()64persons = traci.person.getIDList()65pos = {}66for v in vehs:67pos[v] = traci.vehicle.getPosition(v)68for p in persons:69pos[p] = traci.person.getPosition(p)70shape = None71egoPos = None72if hasattr(module, "getPosition"):73egoPos = module.getPosition(objID)74elif hasattr(module, "getShape"):75shape = module.getShape(objID)76elif module == traci.edge:77# it's a hack, I know, but do we really need to introduce edge.getShape?78shape = traci.lane.getShape(objID + "_0")79near2 = set()80for v in pos:81if egoPos:82if math.sqrt(dist2(egoPos, pos[v])) < viewRange:83near2.add(v)84if shape:85lastP = shape[0]86for p in shape[1:]:87if math.sqrt(distToSegmentSquared(pos[v], lastP, p)) < viewRange:88near2.add(v)89lastP = p9091if not subscribed:92module.subscribeContext(objID, traci.constants.CMD_GET_VEHICLE_VARIABLE, viewRange,93[traci.constants.VAR_POSITION])94module.subscribeContext(objID, traci.constants.CMD_GET_PERSON_VARIABLE, viewRange,95[traci.constants.VAR_POSITION])96subscribed = True97else:98seen1 += len(near1)99seen2 += len(near2)100for v in near1:101if v not in near2:102print("timestep %s: %s is missing in surrounding objects" % (step, v))103for v in near2:104if v not in near1:105print("timestep %s: %s is missing in subscription results" % (step, v))106107step += 1108module.unsubscribeContext(objID, traci.constants.CMD_GET_VEHICLE_VARIABLE, viewRange)109traci.simulationStep()110responses = list(module.getAllContextSubscriptionResults())111print(responses) # person subscription should still be active112module.unsubscribeContext(objID, traci.constants.CMD_GET_PERSON_VARIABLE, viewRange)113traci.simulationStep()114responses = module.getAllContextSubscriptionResults()115if responses:116print("Error: Unsubscribe did not work", responses)117else:118print("Ok: Unsubscribe successful")119print("Print ended at step %s" % traci.simulation.getTime())120traci.close()121sys.stdout.flush()122print("uncheck: seen %s vehicles via subscription, %s in surrounding" %123(seen1, seen2))124if seen1 == seen2:125print("Ok: Subscription and computed are same")126else:127print("Error: subscribed number and computed number differ")128129130sys.stdout.flush()131if sys.argv[3] == "vehicle":132runSingle(1000, float(sys.argv[2]), traci.vehicle, "ego")133elif sys.argv[3] == "edge":134runSingle(1000, float(sys.argv[2]), traci.edge, "1fi")135elif sys.argv[3] == "lane":136runSingle(1000, float(sys.argv[2]), traci.lane, "2si_0")137elif sys.argv[3] == "junction":138runSingle(1000, float(sys.argv[2]), traci.junction, "0")139elif sys.argv[3] == "poi":140runSingle(1000, float(sys.argv[2]), traci.poi, "poi")141elif sys.argv[3] == "polygon":142runSingle(1000, float(sys.argv[2]), traci.polygon, "poly")143elif sys.argv[3] == "person":144runSingle(1000, float(sys.argv[2]), traci.person, "p0")145146147