赞
踩
-
- import os
- import sys
- import optparse
- import random
- import pandas as pd
- import json
- from collections import OrderedDict
- from sumolib import checkBinary # noqa
- import traci # noqa
- import math
- import xml.dom.minidom as minidom
- import sumolib
- import subprocess
- import xml.etree.ElementTree as xee
- import matplotlib.pyplot as plt
- import time
-
- pd.set_option('expand_frame_repr', False) # 禁止换行
- # pd.set_option('display.max_columns', 20) # 显示所有行
- pd.set_option('display.max_rows', 10) # 设置Dataframe数据的显示长度,默认为50
-
-
- if 'SUMO_HOME' in os.environ:
- tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
- sys.path.append(tools)
- else:
- sys.exit("please declare environment variable 'SUMO_HOME'")
-
-
- def get_options():
- optParser = optparse.OptionParser()
- optParser.add_option("--nogui", action="store_true",
- default=False, help="run the commandline version of sumo")
- options, args = optParser.parse_args()
- return options
-
-
- if__show__gui = True
- if not if__show__gui:
- sumoBinary = checkBinary('sumo')
- else:
- sumoBinary = checkBinary('sumo-gui')
- dflist0 = []
- dfass = []
- dflist = []
- dflist1 = []
- dfass1 = []
-
-
- def miao(t1, simulationSteps):
- gwe = 45
- gl = 20
- gns = 20
- r = 3
- phase = traci.trafficlight.getPhase("J12")
- if simulationSteps <= t1 + int(gwe):
- traci.trafficlight.setPhase('J12', 0)
- print(simulationSteps, "东西向倒计时:", t1 + int(gwe) - simulationSteps)
- print("------------------------------------当前相位-------------------------:东西0", phase)
-
- if t1 + int(gwe) < simulationSteps <= t1 + int(gwe) + r:
- traci.trafficlight.setPhase('J12', 1)
- print(simulationSteps, "黄倒计时-----", t1 + int(gwe) + r - simulationSteps+1)
- print("------------------------------------当前相位-------------------------:黄1", phase)
- if t1 + int(gwe) + r < simulationSteps <= t1 + int(gwe) + r + int(gl):
- traci.trafficlight.setPhase('J12', 2)
- print(simulationSteps, "东西左转倒计时", t1 + int(gwe) + r + int(gl) - simulationSteps+1)
- print("-------------------------------------当前相位-----------------------:东西左2", phase)
- if t1 + int(gwe) + r + int(gl) < simulationSteps <= t1 + int(gwe) + 2 * r + int(gl):
- traci.trafficlight.setPhase('J12', 3)
- print(simulationSteps, "黄倒计时-------", t1 + int(gwe) + 2 * r + int(gl) - simulationSteps+1)
- print("------------------------------------当前相位--------------------------:黄3", phase)
- if t1 + int(gwe) + 2 * r + int(gl) < simulationSteps <= t1 + int(gwe) + 2 * r + int(gl) + int(gns):
- traci.trafficlight.setPhase('J12', 4)
- print(simulationSteps, "南北向倒计时", t1 + int(gwe) + 2*r + int(gns) + int(gl) - simulationSteps+1)
- print("-------------------------------------当前相位-----------------------:南北4", phase)
- if t1 + int(gwe) + 2 * r + int(gl) + int(gns) < simulationSteps < t1 + int(gwe) + 3 * r + int(gl) + int(gns):
- traci.trafficlight.setPhase('J12', 5)
- print(simulationSteps, "黄倒计时-------", t1 + int(gwe) + 3 * r + int(gns) + int(gl) - simulationSteps+1)
- print("------------------------------------当前相位--------------------------:黄5", phase)
-
-
- def run(): # 所需要进行的操作就放在这里面
- simulationSteps = 0
- time.sleep(0.001)
- jlv_w = []
- jlv_e = []
- jlv_n = []
- jlv_s = []
- occ_w = []
- occ_e = []
- occ_n = []
- occ_s = []
-
- jlv_ww = []
- jlv_ee = []
- jlv_nn = []
- jlv_ss = []
- occ_ww = []
- occ_ee = []
- occ_nn = []
- occ_ss = []
-
- while simulationSteps < 2000: # 仿真
- traci.simulationStep()
- jlv_w0_0 = traci.lanearea.getJamLengthVehicle('W_0') # 车道0
- occ_w0_0 = traci.lanearea.getLastStepOccupancy('W_0')
- jlv_e0_0 = traci.lanearea.getJamLengthVehicle('E_0')
- occ_e0_0 = traci.lanearea.getLastStepOccupancy('E_0')
-
- jlv_n0_0 = traci.lanearea.getJamLengthVehicle('N_0')
- occ_n0_0 = traci.lanearea.getLastStepOccupancy('N_0')
- jlv_s0_0 = traci.lanearea.getJamLengthVehicle('S_0')
- occ_s0_0 = traci.lanearea.getLastStepOccupancy('S_0')
-
- jlv_w0_1 = traci.lanearea.getJamLengthVehicle('W_1') # 车道1
- occ_w0_1 = traci.lanearea.getLastStepOccupancy('W_1')
- jlv_e0_1 = traci.lanearea.getJamLengthVehicle('E_1')
- occ_e0_1 = traci.lanearea.getLastStepOccupancy('E_1')
-
- jlv_n0_1 = traci.lanearea.getJamLengthVehicle('N_1')
- occ_n0_1 = traci.lanearea.getLastStepOccupancy('N_1')
- jlv_s0_1 = traci.lanearea.getJamLengthVehicle('S_1')
- occ_s0_1 = traci.lanearea.getLastStepOccupancy('S_1')
-
- jlv_wl0_2 = traci.lanearea.getJamLengthVehicle('W_2') # 车道2东西左转
- occ_wl0_2 = traci.lanearea.getLastStepOccupancy('W_2')
- jlv_el0_2 = traci.lanearea.getJamLengthVehicle('E_2')
- occ_el0_2 = traci.lanearea.getLastStepOccupancy('E_2')
-
- jlv_w.append(max(jlv_w0_0, jlv_w0_1))
- jlv_e.append(max(jlv_e0_0, jlv_e0_1))
- jlv_n.append(max(jlv_n0_0, jlv_n0_1))
- jlv_s.append(max(jlv_s0_0, jlv_s0_1))
-
- occ_w.append(max(occ_w0_0, occ_w0_1))
- occ_e.append(max(occ_e0_0, occ_e0_1))
- occ_n.append(max(occ_n0_0, occ_n0_1))
- occ_s.append(max(occ_s0_0, occ_s0_1))
- list0 = {'仿真时间': simulationSteps, '东向直行排队车辆数': max(jlv_w0_0, jlv_w0_1),
- '东直行占有率': max(occ_w0_0, occ_w0_1),
- '东左转排队车辆数': jlv_wl0_2, '东左转占有率': occ_wl0_2, '西向直行排队车辆数': max(jlv_e0_0, jlv_e0_1),
- '西向直行占有率': max(occ_e0_0, occ_e0_1),
- '西左转排队车辆数': jlv_el0_2, '西左转占有率': occ_el0_2, '北向排队车辆数': max(jlv_n0_0, jlv_n0_1),
- '北向占有率': max(occ_n0_0, occ_n0_1),
- '南向排队车辆数': max(jlv_s0_0, jlv_s0_1), '南向占有率': max(occ_s0_0, occ_s0_1)}
- # print(list0)
-
- dflist.append(list0) # 原方案
- pd.DataFrame(dflist).to_csv("优化方案车辆数占有率.csv", encoding="utf-8-sig")
- simulationSteps += 1
- traci.close() # 仿真结束 关闭TraCI
-
-
-
- if __name__ == "__main__":
- options = get_options()
- sumocfg_file = "1.sumocfg" # 这里输入仿真的cfg文件
- traci.start([sumoBinary, "-c", sumocfg_file]) # 这里通过traci接口启动仿真程序
- logic = traci.trafficlight.getAllProgramLogics("J12") # 获取控制方案
- print(logic)
-
- run()
各类型检测器在1.det.xml
- <additional>
- <laneAreaDetector id="W_0" lane="1_0" pos="0" endPos="-0.5" freq="90" file="1.detout.xml"/>
- <laneAreaDetector id="E_0" lane="2_0" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="N_0" lane="3_0" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="S_0" lane="4_0" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="W_1" lane="1_1" pos="0" endPos="-0.5" freq="90" file="1.detout.xml"/>
- <laneAreaDetector id="E_1" lane="2_1" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="W_2" lane="1_2" pos="0" endPos="-0.5" freq="90" file="1.detout.xml"/>
- <laneAreaDetector id="E_2" lane="2_2" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="N_1" lane="3_1" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="S_1" lane="4_1" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="N_2" lane="3_2" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- <laneAreaDetector id="S_2" lane="4_2" pos="0" endPos="-0.5" freq="90"
- file="1.detout.xml"/>
- </additional>
对应sumocfg文件修改为
- <?xml version="1.0" encoding="UTF-8"?>
-
- <configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/sumoConfiguration.xsd">
-
- <input>
- <net-file value="1.net.xml"/>
- <route-files value="1.rou.xml"/>
- <additional-files value="1.det.xml"/>
- </input>
-
- <time>
- <begin value="0"/>
- <end value="3600"/>
- </time>
-
- <gui_only>
- <start value="t"/>
- <quit-on-end value="t"/>
- </gui_only>
-
- <scale value="20.0" />
-
- </configuration>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。