如何使用TraCI相扑中的Pandas数据帧设置速度?

2024-04-27 23:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

目标

我想使用预定义的速度矢量为个别车辆移动他们相扑模拟。你知道吗

数据和文件

模拟中有3辆车。对于其中的两辆车,我想指定速度。速度数据在Python中创建,如下所示:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

data = {'ADO_name':['car1','car1','car1','car2','car2','car2'],
        'Time_sec':[0,1,2,0,1,2],
        'Speed.kph':[50,51,52,0,0,52]}
dframe = DataFrame(data)  

此模拟的路由、网络和配置文件位于以下文件夹中:https://1drv.ms/f/s!AsMFpkDhWcnw61EI5wR6hPaaRBJI
我还将代码放在同一文件夹中的Script.py文件中。你知道吗

我试过的

以下是我一直尝试使用的代码以及错误:

#start sumo
sumoBinary = "C:/Users/Quinton/Desktop/Documents/Sumo/bin/sumo-gui"
sumoCmd = [sumoBinary, "-c", "C:/Users/Quinton/Desktop/Example2/example2.sumocfg"]


#importing libraries
import traci
import traci.constants as tc
traci.start(sumoCmd)

#subscribing to variables that we want to be printed once the copy has run
traci.vehicle.subscribe("car1", (tc.VAR_SPEED, tc.VAR_ROAD_ID, tc.VAR_LANE_ID, tc.VAR_LANEPOSITION))
traci.vehicle.subscribe("car2", (tc.VAR_SPEED, tc.VAR_ROAD_ID, tc.VAR_LANE_ID, tc.VAR_LANEPOSITION))
traci.vehicle.subscribe("car3", (tc.VAR_SPEED, tc.VAR_ROAD_ID, tc.VAR_LANE_ID, tc.VAR_LANEPOSITION))

#using traci.movetoXY to position car1 and car2 on network
traci.vehicle.moveToXY(vehID="car1", edgeID="highway1.1", lane=0, x=1000, y=-3.3, keepRoute=0)
traci.vehicle.moveToXY(vehID="car2", edgeID="highway1.1", lane=1, x=700, y=3.3, keepRoute=0)

#disallows car1 and car2 from changing lanes during simulation
traci.vehicle.setLaneChangeMode(vehID="car1", lcm=512)
traci.vehicle.setLaneChangeMode(vehID="car2", lcm=512)


#importing python modules
import numpy as np
import pandas as pd
from pandas import Series, DataFrame

#creating speed data
data = {'ADO_name':['car1','car1','car1','car2','car2','car2'],
        'Time_sec':[0,1,2,0,1,2],
        'Speed.kph':[50,51,52,0,0,52]}
dframe = DataFrame(data)
#print(dframe)

step = 0

#running traci
for ado in dframe.groupby('ADO_name'):
  ado_name = ado[1]["ADO_name"]
  adoID = ado_name.unique()

  while step <= 2:
    traci.simulationStep()
    traci.vehicle.setSpeed(adoID, ado[1][ado[1].Time_sec == step]['Speed.kph'])
    print (traci.vehicle.getSubscriptionResults("car1"), traci.vehicle.getSubscriptionResults("car2"))

  step += 1  

错误:

Traceback (most recent call last):
  File "C:\Users\Quinton\AppData\Local\Temp\Rtmp6jCqR4\chunk-code-16888822790.txt", line 41, in <module>
    traci.vehicle.setSpeed(adoID, ado[1][ado[1].Time_sec == step]['Speed.kph'])
  File "C:\Program Files (x86)\DLR\Sumo\tools\traci\_vehicle.py", line 927, in setSpeed
    tc.CMD_SET_VEHICLE_VARIABLE, tc.VAR_SPEED, vehID, speed)
  File "C:\Program Files (x86)\DLR\Sumo\tools\traci\connection.py", line 139, in _sendDoubleCmd
    self._beginMessage(cmdID, varID, objID, 1 + 8)
  File "C:\Program Files (x86)\DLR\Sumo\tools\traci\connection.py", line 127, in _beginMessage
    self._packString(objID, varID)
  File "C:\Program Files (x86)\DLR\Sumo\tools\traci\connection.py", line 66, in _packString
    self._string += struct.pack("!Bi", pre, len(s)) + s.encode("latin1")
AttributeError: 'numpy.ndarray' object has no attribute 'encode'  

注意事项

据我所知,问题出在代码的#running traci部分。我在没有调用TraCI和使用print()而不是traci.vehicle.setSpeed()的情况下测试了这段代码,没有出现错误。所以,似乎事情的Python方面是好的。你能帮我解决这个问题吗


Tags: nameinpyimportiddatavaras