当前位置:   article > 正文

python提取sumo各类型检测器数据实例_python traci输出所有sumo检测器流量值

python traci输出所有sumo检测器流量值
  1. import os
  2. import sys
  3. import optparse
  4. import random
  5. import pandas as pd
  6. import json
  7. from collections import OrderedDict
  8. from sumolib import checkBinary # noqa
  9. import traci # noqa
  10. import math
  11. import xml.dom.minidom as minidom
  12. import sumolib
  13. import subprocess
  14. import xml.etree.ElementTree as xee
  15. import matplotlib.pyplot as plt
  16. import time
  17. pd.set_option('expand_frame_repr', False) # 禁止换行
  18. # pd.set_option('display.max_columns', 20) # 显示所有行
  19. pd.set_option('display.max_rows', 10) # 设置Dataframe数据的显示长度,默认为50
  20. if 'SUMO_HOME' in os.environ:
  21. tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
  22. sys.path.append(tools)
  23. else:
  24. sys.exit("please declare environment variable 'SUMO_HOME'")
  25. def get_options():
  26. optParser = optparse.OptionParser()
  27. optParser.add_option("--nogui", action="store_true",
  28. default=False, help="run the commandline version of sumo")
  29. options, args = optParser.parse_args()
  30. return options
  31. if__show__gui = True
  32. if not if__show__gui:
  33. sumoBinary = checkBinary('sumo')
  34. else:
  35. sumoBinary = checkBinary('sumo-gui')
  36. dflist0 = []
  37. dfass = []
  38. dflist = []
  39. dflist1 = []
  40. dfass1 = []
  41. def miao(t1, simulationSteps):
  42. gwe = 45
  43. gl = 20
  44. gns = 20
  45. r = 3
  46. phase = traci.trafficlight.getPhase("J12")
  47. if simulationSteps <= t1 + int(gwe):
  48. traci.trafficlight.setPhase('J12', 0)
  49. print(simulationSteps, "东西向倒计时:", t1 + int(gwe) - simulationSteps)
  50. print("------------------------------------当前相位-------------------------:东西0", phase)
  51. if t1 + int(gwe) < simulationSteps <= t1 + int(gwe) + r:
  52. traci.trafficlight.setPhase('J12', 1)
  53. print(simulationSteps, "黄倒计时-----", t1 + int(gwe) + r - simulationSteps+1)
  54. print("------------------------------------当前相位-------------------------:黄1", phase)
  55. if t1 + int(gwe) + r < simulationSteps <= t1 + int(gwe) + r + int(gl):
  56. traci.trafficlight.setPhase('J12', 2)
  57. print(simulationSteps, "东西左转倒计时", t1 + int(gwe) + r + int(gl) - simulationSteps+1)
  58. print("-------------------------------------当前相位-----------------------:东西左2", phase)
  59. if t1 + int(gwe) + r + int(gl) < simulationSteps <= t1 + int(gwe) + 2 * r + int(gl):
  60. traci.trafficlight.setPhase('J12', 3)
  61. print(simulationSteps, "黄倒计时-------", t1 + int(gwe) + 2 * r + int(gl) - simulationSteps+1)
  62. print("------------------------------------当前相位--------------------------:黄3", phase)
  63. if t1 + int(gwe) + 2 * r + int(gl) < simulationSteps <= t1 + int(gwe) + 2 * r + int(gl) + int(gns):
  64. traci.trafficlight.setPhase('J12', 4)
  65. print(simulationSteps, "南北向倒计时", t1 + int(gwe) + 2*r + int(gns) + int(gl) - simulationSteps+1)
  66. print("-------------------------------------当前相位-----------------------:南北4", phase)
  67. if t1 + int(gwe) + 2 * r + int(gl) + int(gns) < simulationSteps < t1 + int(gwe) + 3 * r + int(gl) + int(gns):
  68. traci.trafficlight.setPhase('J12', 5)
  69. print(simulationSteps, "黄倒计时-------", t1 + int(gwe) + 3 * r + int(gns) + int(gl) - simulationSteps+1)
  70. print("------------------------------------当前相位--------------------------:黄5", phase)
  71. def run(): # 所需要进行的操作就放在这里面
  72. simulationSteps = 0
  73. time.sleep(0.001)
  74. jlv_w = []
  75. jlv_e = []
  76. jlv_n = []
  77. jlv_s = []
  78. occ_w = []
  79. occ_e = []
  80. occ_n = []
  81. occ_s = []
  82. jlv_ww = []
  83. jlv_ee = []
  84. jlv_nn = []
  85. jlv_ss = []
  86. occ_ww = []
  87. occ_ee = []
  88. occ_nn = []
  89. occ_ss = []
  90. while simulationSteps < 2000: # 仿真
  91. traci.simulationStep()
  92. jlv_w0_0 = traci.lanearea.getJamLengthVehicle('W_0') # 车道0
  93. occ_w0_0 = traci.lanearea.getLastStepOccupancy('W_0')
  94. jlv_e0_0 = traci.lanearea.getJamLengthVehicle('E_0')
  95. occ_e0_0 = traci.lanearea.getLastStepOccupancy('E_0')
  96. jlv_n0_0 = traci.lanearea.getJamLengthVehicle('N_0')
  97. occ_n0_0 = traci.lanearea.getLastStepOccupancy('N_0')
  98. jlv_s0_0 = traci.lanearea.getJamLengthVehicle('S_0')
  99. occ_s0_0 = traci.lanearea.getLastStepOccupancy('S_0')
  100. jlv_w0_1 = traci.lanearea.getJamLengthVehicle('W_1') # 车道1
  101. occ_w0_1 = traci.lanearea.getLastStepOccupancy('W_1')
  102. jlv_e0_1 = traci.lanearea.getJamLengthVehicle('E_1')
  103. occ_e0_1 = traci.lanearea.getLastStepOccupancy('E_1')
  104. jlv_n0_1 = traci.lanearea.getJamLengthVehicle('N_1')
  105. occ_n0_1 = traci.lanearea.getLastStepOccupancy('N_1')
  106. jlv_s0_1 = traci.lanearea.getJamLengthVehicle('S_1')
  107. occ_s0_1 = traci.lanearea.getLastStepOccupancy('S_1')
  108. jlv_wl0_2 = traci.lanearea.getJamLengthVehicle('W_2') # 车道2东西左转
  109. occ_wl0_2 = traci.lanearea.getLastStepOccupancy('W_2')
  110. jlv_el0_2 = traci.lanearea.getJamLengthVehicle('E_2')
  111. occ_el0_2 = traci.lanearea.getLastStepOccupancy('E_2')
  112. jlv_w.append(max(jlv_w0_0, jlv_w0_1))
  113. jlv_e.append(max(jlv_e0_0, jlv_e0_1))
  114. jlv_n.append(max(jlv_n0_0, jlv_n0_1))
  115. jlv_s.append(max(jlv_s0_0, jlv_s0_1))
  116. occ_w.append(max(occ_w0_0, occ_w0_1))
  117. occ_e.append(max(occ_e0_0, occ_e0_1))
  118. occ_n.append(max(occ_n0_0, occ_n0_1))
  119. occ_s.append(max(occ_s0_0, occ_s0_1))
  120. list0 = {'仿真时间': simulationSteps, '东向直行排队车辆数': max(jlv_w0_0, jlv_w0_1),
  121. '东直行占有率': max(occ_w0_0, occ_w0_1),
  122. '东左转排队车辆数': jlv_wl0_2, '东左转占有率': occ_wl0_2, '西向直行排队车辆数': max(jlv_e0_0, jlv_e0_1),
  123. '西向直行占有率': max(occ_e0_0, occ_e0_1),
  124. '西左转排队车辆数': jlv_el0_2, '西左转占有率': occ_el0_2, '北向排队车辆数': max(jlv_n0_0, jlv_n0_1),
  125. '北向占有率': max(occ_n0_0, occ_n0_1),
  126. '南向排队车辆数': max(jlv_s0_0, jlv_s0_1), '南向占有率': max(occ_s0_0, occ_s0_1)}
  127. # print(list0)
  128. dflist.append(list0) # 原方案
  129. pd.DataFrame(dflist).to_csv("优化方案车辆数占有率.csv", encoding="utf-8-sig")
  130. simulationSteps += 1
  131. traci.close() # 仿真结束 关闭TraCI
  132. if __name__ == "__main__":
  133. options = get_options()
  134. sumocfg_file = "1.sumocfg" # 这里输入仿真的cfg文件
  135. traci.start([sumoBinary, "-c", sumocfg_file]) # 这里通过traci接口启动仿真程序
  136. logic = traci.trafficlight.getAllProgramLogics("J12") # 获取控制方案
  137. print(logic)
  138. run()

各类型检测器在1.det.xml

  1. <additional>
  2. <laneAreaDetector id="W_0" lane="1_0" pos="0" endPos="-0.5" freq="90" file="1.detout.xml"/>
  3. <laneAreaDetector id="E_0" lane="2_0" pos="0" endPos="-0.5" freq="90"
  4. file="1.detout.xml"/>
  5. <laneAreaDetector id="N_0" lane="3_0" pos="0" endPos="-0.5" freq="90"
  6. file="1.detout.xml"/>
  7. <laneAreaDetector id="S_0" lane="4_0" pos="0" endPos="-0.5" freq="90"
  8. file="1.detout.xml"/>
  9. <laneAreaDetector id="W_1" lane="1_1" pos="0" endPos="-0.5" freq="90" file="1.detout.xml"/>
  10. <laneAreaDetector id="E_1" lane="2_1" pos="0" endPos="-0.5" freq="90"
  11. file="1.detout.xml"/>
  12. <laneAreaDetector id="W_2" lane="1_2" pos="0" endPos="-0.5" freq="90" file="1.detout.xml"/>
  13. <laneAreaDetector id="E_2" lane="2_2" pos="0" endPos="-0.5" freq="90"
  14. file="1.detout.xml"/>
  15. <laneAreaDetector id="N_1" lane="3_1" pos="0" endPos="-0.5" freq="90"
  16. file="1.detout.xml"/>
  17. <laneAreaDetector id="S_1" lane="4_1" pos="0" endPos="-0.5" freq="90"
  18. file="1.detout.xml"/>
  19. <laneAreaDetector id="N_2" lane="3_2" pos="0" endPos="-0.5" freq="90"
  20. file="1.detout.xml"/>
  21. <laneAreaDetector id="S_2" lane="4_2" pos="0" endPos="-0.5" freq="90"
  22. file="1.detout.xml"/>
  23. </additional>

对应sumocfg文件修改为

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/sumoConfiguration.xsd">
  3. <input>
  4. <net-file value="1.net.xml"/>
  5. <route-files value="1.rou.xml"/>
  6. <additional-files value="1.det.xml"/>
  7. </input>
  8. <time>
  9. <begin value="0"/>
  10. <end value="3600"/>
  11. </time>
  12. <gui_only>
  13. <start value="t"/>
  14. <quit-on-end value="t"/>
  15. </gui_only>
  16. <scale value="20.0" />
  17. </configuration>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/645651
推荐阅读
相关标签
  

闽ICP备14008679号