Add/remove SUMO 中的车辆在运行时通过 TraCI
Add/remove vehicles in SUMO via TraCI at runtime
我知道 traci.vehicle API 具有添加和删除功能,即 traci 车辆删除或 traci 车辆添加。我在 Windows 上使用 SUMO。我有网络,路线和网络中固定数量的37000辆车。我在 python 开始模拟,并在模拟为 运行ning 时尝试如下移除车辆:
import traci
traci.start(sumoCmd)
stepz = 0
while traci.simulation.getMinExpectedNumber() > 0 and stepz < 7000:
traci.simulationStep()
traci.vehicle.remove("2773")
traci.close()
当我 运行 时,我收到此错误消息:
Traceback (most recent call last):
File "", line 9, in
traci.vehicle.remove("2773")
File "C:\Program Files (x86)\Eclipse\Sumo\tools\traci_vehicle.py", line 1579, in remove
tc.CMD_SET_VEHICLE_VARIABLE, tc.REMOVE, vehID, reason)
File "C:\Program Files (x86)\Eclipse\Sumo\tools\traci\connection.py", line 149, in _sendByteCmd
self._sendExact()
File "C:\Program Files (x86)\Eclipse\Sumo\tools\traci\connection.py", line 112, in _sendExact
raise TraCIException(err, prefix[1], _RESULTS[prefix[2]])
TraCIException: Vehicle '2773' is not known.
“2773”是我从 mytrip.trips.xml 文件中获得的车辆 ID 之一。我不知道这段代码有什么问题以及如何修复它。我也想知道如何找到 RouteID 添加车辆。
任何帮助将不胜感激。
您只能删除已在网络中的车辆。所以也许您的车辆还没有离开。路由 ID 是来自 XML 定义或来自较早的 traci.route.add 调用的路由 ID。
问题可能是(正如 Michael 所指出的)您正在尝试移除尚未在模拟中的车辆。 ID 为 "2773"
的车辆仅在您的 .rou.xml
文件中定义。 SUMO 在运行时解析 .rou.xml
文件并在模拟 运行 时加载车辆。车辆尚未在模拟中,因为它要么被定义为在不同的时间出发,要么它当前正在尝试进入网络,但在它之前有更多的车辆(它正在排队)。
解决方法是检查车辆是否已经出发:
if "2773" in traci.vehicle.getIDList():
traci.vehicle.remove("2773")
或者,尝试删除并捕获:
try:
traci.vehicle.remove("2773")
except TraCIException:
pass # just do nothing
关于您在评论中关于添加 route
的问题。 traci.route.add("1", ["5367021#0", "-133806981#1"])
行中的 "1"
是一个新 ID,您正试图将其分配给由边 "5367021#0"
和 "-133806981#1"
组成的路由。确保您没有再次添加相同的路线。路由 ID 必须是唯一的。在许多情况下,命令在循环中重复执行导致错误。
您可以在此处的文档中找到所需的一切:https://sumo.dlr.de/pydoc/
我知道 traci.vehicle API 具有添加和删除功能,即 traci 车辆删除或 traci 车辆添加。我在 Windows 上使用 SUMO。我有网络,路线和网络中固定数量的37000辆车。我在 python 开始模拟,并在模拟为 运行ning 时尝试如下移除车辆:
import traci
traci.start(sumoCmd)
stepz = 0
while traci.simulation.getMinExpectedNumber() > 0 and stepz < 7000:
traci.simulationStep()
traci.vehicle.remove("2773")
traci.close()
当我 运行 时,我收到此错误消息:
Traceback (most recent call last): File "", line 9, in traci.vehicle.remove("2773")
File "C:\Program Files (x86)\Eclipse\Sumo\tools\traci_vehicle.py", line 1579, in remove tc.CMD_SET_VEHICLE_VARIABLE, tc.REMOVE, vehID, reason)
File "C:\Program Files (x86)\Eclipse\Sumo\tools\traci\connection.py", line 149, in _sendByteCmd self._sendExact() File "C:\Program Files (x86)\Eclipse\Sumo\tools\traci\connection.py", line 112, in _sendExact raise TraCIException(err, prefix[1], _RESULTS[prefix[2]]) TraCIException: Vehicle '2773' is not known.
“2773”是我从 mytrip.trips.xml 文件中获得的车辆 ID 之一。我不知道这段代码有什么问题以及如何修复它。我也想知道如何找到 RouteID 添加车辆。 任何帮助将不胜感激。
您只能删除已在网络中的车辆。所以也许您的车辆还没有离开。路由 ID 是来自 XML 定义或来自较早的 traci.route.add 调用的路由 ID。
问题可能是(正如 Michael 所指出的)您正在尝试移除尚未在模拟中的车辆。 ID 为 "2773"
的车辆仅在您的 .rou.xml
文件中定义。 SUMO 在运行时解析 .rou.xml
文件并在模拟 运行 时加载车辆。车辆尚未在模拟中,因为它要么被定义为在不同的时间出发,要么它当前正在尝试进入网络,但在它之前有更多的车辆(它正在排队)。
解决方法是检查车辆是否已经出发:
if "2773" in traci.vehicle.getIDList():
traci.vehicle.remove("2773")
或者,尝试删除并捕获:
try:
traci.vehicle.remove("2773")
except TraCIException:
pass # just do nothing
关于您在评论中关于添加 route
的问题。 traci.route.add("1", ["5367021#0", "-133806981#1"])
行中的 "1"
是一个新 ID,您正试图将其分配给由边 "5367021#0"
和 "-133806981#1"
组成的路由。确保您没有再次添加相同的路线。路由 ID 必须是唯一的。在许多情况下,命令在循环中重复执行导致错误。
您可以在此处的文档中找到所需的一切:https://sumo.dlr.de/pydoc/