上一个问题的链接
我正在使用一个名为 TraCI 的工具,它允许用户使用 Python 控制一个名为 SUMO 的模拟软件。因此,我的代码包含来自 Python 中的 traci 模块的函数。我之前问过question关于您可能希望看到的 traci 代码。但是,我想澄清一下,这个问题是关于 Python 的。我提到 traci 只是因为您会看到该模块的功能。
我想做什么?
我想将多个单元的输出字典组合成一个pandas数据框。以下是我的输入数据:
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
data = {'ADO_name':['car1','car1','car1','car1','car1','car1',
'car2','car2','car2','car2','car2','car2'],
'Time_sec':[0,1,2,3,4,5,
0,1,2,3,4,5],
'Speed.mps':[13.9,15,18,20,21,22,
0, 0, 13.9,13.9,13.9,14.5]}
dframe = DataFrame(data)
dframe = dframe.sort_values('Time_sec')
数据是如何生成的?
上面的输入 dframe 用于在给定的时间步长和给定的速度下循环运行两辆车,如下所示:
step = 0
#running traci
while step <= 1:
traci.simulationStep() # traci function to start a simulation step
for ado in dframe["ADO_name"].unique():
traci.vehicle.setSpeed(ado, dframe[(dframe.Time_sec == step) & (dframe.ADO_name == ado)]['Speed.kph']) # traci function to set the speed of both cars
print(traci.vehicle.getSubscriptionResults(ado)) # traci function to get results
step += 1
输出
{64: 0.0, 80: 'highway1.1', 81: 'highway1.1_0', 86: 400.0}
{64: 0.0, 80: 'highway1.1', 81: 'highway1.1_1', 86: 100.0}
{64: 13.9, 80: 'highway1.1', 81: 'highway1.1_0', 86: 413.9}
{64: 0.0, 80: 'highway1.1', 81: 'highway1.1_1', 86: 100.0}
函数 traci.vehicle.getSubscriptionResults() 产生速度(key = 64)、roadID(80)、laneID(81)和车道位置(86)变量作为每次的输出步骤和每辆车。在上面的结果中,前 2 个字典表示 car1 和 car2(一个接一个)在时间步长 0 中的输出。同样,最后 2 个字典表示在时间步长 1。我想将此输出合并到一个数据框中。
预期输出
ADO_name Speed.mps laneID lane_pos roadID step
0 car1 0.0 highway1.1_0 400.0 highway1.1 0
1 car2 0.0 highway1.1_1 100.0 highway1.1 0
2 car1 13.9 highway1.1_0 413.9 highway1.1 1
3 car2 0.0 highway1.1_0 100.0 highway1.1 1
我尝试了什么?
我是 Python 和 pandas 的新手,所以尽管我花了几个小时来解决这个问题,但我还没有成功。我知道我可以使用字典函数,例如traci.vehicle.getSubscriptionResults(ado).get(64) 获取速度变量。但我不知道如何在每个循环中附加结果。我也试过以下:
print(DataFrame(traci.vehicle.getSubscriptionResults(ado), index = [step]))
> 64 80 81 86
0 0.0 highway1.1 highway1.1_0 400.0
64 80 81 86
0 0.0 highway1.1 highway1.1_1 100.0
64 80 81 86
1 13.9 highway1.1 highway1.1_0 413.9
64 80 81 86
1 0.0 highway1.1 highway1.1_1 100.0
但这不是我所期望的。请指导我如何处理这个问题?如何收集结果并清理字典以获得最终的预期输出?任何相关示例/博客都会有所帮助。谢谢。
请您参考如下方法:
我看到的解决方案是修改您的模拟循环以将每个字典附加到一个列表中,为 ADO_name 和 step 插入新键,然后传递该字典列表循环后到 DataFrame。
这是修改后的循环。这是未经测试的,但我相信这个想法是正确的:
step = 0
# Create an empty list before the loop
list_of_dicts = []
#running traci
while step <= 1:
traci.simulationStep() # traci function to start a simulation step
for ado in dframe["ADO_name"].unique():
traci.vehicle.setSpeed(ado, dframe[(dframe.Time_sec == step) & (dframe.ADO_name == ado)]['Speed.kph']) # traci function to set the speed of both cars
print(traci.vehicle.getSubscriptionResults(ado)) # traci function to get results
# A block of new code
d = traci.vehicle.getSubscriptionResults(ado)
d['ADO_name'] = ado
d['step'] = step
list_of_dicts.append(d)
step += 1
假设循环运行没有错误,你应该得到这样一个列表:
ds = [{64: 0.0, 80: 'highway1.1', 81: 'highway1.1_0', 86: 400.0, 'ADO_name': 'car1', 'step': 0},
{64: 0.0, 80: 'highway1.1', 81: 'highway1.1_1', 86: 100.0, 'ADO_name': 'car2', 'step': 0},
{64: 13.9, 80: 'highway1.1', 81: 'highway1.1_0', 86: 413.9, 'ADO_name': 'car1', 'step': 1},
{64: 0.0, 80: 'highway1.1', 81: 'highway1.1_1', 86: 100.0, 'ADO_name': 'car2', 'step': 1}]
您想要的输出的最后一步:
result = pd.DataFrame(ds).rename(columns={64: 'Speed.mps',
80: 'roadID',
81: 'laneID',
86: 'lane_pos'})
result
Speed.mps roadID laneID lane_pos ADO_name step
0 0.0 highway1.1 highway1.1_0 400.0 car1 0
1 0.0 highway1.1 highway1.1_1 100.0 car2 0
2 13.9 highway1.1 highway1.1_0 413.9 car1 1
3 0.0 highway1.1 highway1.1_1 100.0 car2 1
