SUMO uses E2 detector to obtain lane information and signal control scheme of signalized intersection


I Xiaobai, the blog is only for recording my following learning process and results, for your reference only. Welcome to exchange and correct.

1, What is the Traci interface

TraCI is the Traffic Control Interface of micro traffic flow simulation software, The full name is "Traffic Control Interface" ". SUMO itself can realize the simulation of many actual traffic scenes. When SUMO is used as the test platform of intelligent traffic control algorithm, it needs to interact well with external programs / algorithms. For example, the user-defined control algorithm can obtain real-time traffic information from SUMO, and then control the vehicle status and signal light status in real time. TraCI is the interface to realize this kind of interaction.

Function: obtain the data in SUMO traffic simulation environment, modify and control it in real time.

2, Acquisition method

1. Establish road network, traffic flow and detector

It is common to establish basic operations such as road network and traffic flow. I won't introduce it too much. Here is a brief introduction to the layout method of the detector I use.
The SUMO installation package comes with an automatic deployment tool for detectors, which can generate road detectors with one click, as shown in the figure below. They are the methods of automatically generating E1, E2 and E3 detectors respectively.

The use of the above automatic deployment tools requires the integration of road network documents and As for the py tool, for the same directory or adding a relatively complete file path, the specific call commands are:

python -n -o E2.add.xml

This will generate an E2 add. XML detector file. In sumocfg, you need to add this file in < additional - File > (as shown in the figure below), otherwise you cannot call the E2 detector just generated

The complete road network is shown in the figure below:

2. Call traci interface to start simulation

The code is as follows (example):

import os
import sys
import optparse
import random
from sumolib import checkBinary  # noqa
import traci  # noqa
import pandas as pd

if 'SUMO_HOME' in os.environ:
    tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
    sys.exit("please declare environment variable 'SUMO_HOME'")
sumocfg_file = "demo.sumocfg"#Input the simulated cfg file here
if__show__gui =True
if not if__show__gui :
    sumoBinary = checkBinary('sumo')
else :
    sumoBinary = checkBinary('sumo-gui')
traci.start([sumoBinary, "-c", sumocfg_file])#Here, start the simulation program through traci interface

After the program is started successfully, click the start button in the cfg interface to get a feedback

3. Obtain Lane turning information

In the research of signal control, it may be necessary to obtain the turning information of the lane released in each phase and calculate the lane attribute according to the lane turning. Since the information obtained by traci's own interface contains some other invalid information (as shown in the figure below), a filtering scheme for information is proposed here, as shown below

The code is as follows (example):

def get_dir(lane_list):#Incoming lane information to be judged
    dir = {}
    for lane in lane_list:
        if len(traci.lane.getLinks(lane)) == 2:#Because there is only one two-way Lane straight to the right in the example, it is defined this way
            dir[lane] = 'sr'
        elif len(traci.lane.getLinks(lane)) == 1:#In case of single turn lane
            if traci.lane.getLinks(lane)[0][7] !=0:#And the last item is not 0. If it is 0, it is the lane in the junction
                dir[lane] = traci.lane.getLinks(lane)[0][6]#This item is the turning information of the lane
    dir_d = pd.DataFrame.from_dict(dir,orient='index')#Convert dir to dataframe format
    dir_d.rename(columns={0:"dir"},inplace = True)
    return dir_d

The format of the information obtained is as follows:

4. Obtain lane information through E2 detector

E2 detector corresponds to the same energy of the camera similar to the intersection in the display, and can obtain the vehicle queue length, road occupancy and other information within the detection signal range. Because in the actual application process, using the interface to obtain a single piece of data is not conducive to the processing of information, this paper proposes an information acquisition method.

def get_info(decid):#Incoming detector id information
    quene_lenth={}#Define queue length
    occ = {}#Define occupancy
    lane_length = {}#Define lane length
    for dets in decid:#Ergodic detector
        lane_id = traci.lanearea.getLaneID(dets)#Obtain Lane id through detector
        lane_length[lane_id] = traci.lane.getLength(lane_id)#Obtain the lane length and record it
        quene_lenth[lane_id] = traci.lanearea.getJamLengthMeters(dets)#Get the queue length and record it
        occ[lane_id] = traci.lanearea.getLastStepOccupancy(dets)#Capture and record share
    quene_lenth_d = pd.DataFrame.from_dict(quene_lenth,orient='index')#Convert queue length to dataframe
    quene_lenth_d.rename(columns={0:"quene_lenth"},inplace = True)#Change data label
    occ_d = pd.DataFrame.from_dict(occ,orient='index')
    occ_d.rename(columns={0:"occ"},inplace = True)
    lane_length_d = pd.DataFrame.from_dict(lane_length,orient='index')
    lane_length_d.rename(columns={0:"length"},inplace = True)
    data = pd.concat([quene_lenth_d,occ_d,lane_length_d],axis=1)#Fusion of acquired data information
    return data

The intersection information obtained is as follows:

The above information can be fused through pandas operation:

5. Obtain the release lane information of each phase

Due to Traci trafficlight. Getphase cannot directly obtain the release information of each phase. Here, the control scheme of each phase is obtained by traversing the control scheme of the signal lamp. The lane of 'G' is selected and recorded to form the release lane information, as shown below:
The signal scheme obtained through Traci interface is as follows:

logic = traci.trafficlight.getAllProgramLogics("gneJ0")#Acquisition control scheme
program = logic[1]
phase = {}#Define null phase	
for i in range(len(program)):#Ergodic signal phase
    if i%2 ==0:#Because there is a yellow light signal in the middle of the phase, the green light signals are all even
        phase[i] = program[i].state#The signal is in logic format state is the specific signal control scheme
incoming_lanes_all = {}#Define the entry Lane set
outcoming_lanes_all = {}#Define exit lane set
for i in phase:#Ergodic phase
    incoming_lanes = []#Define the empty lane at the entrance
    outcoming_lanes = []#Define empty exit lane
    k = 0#Number of control phases
    for j in phase[i]:
        if j =='G':#Record the lane corresponding to 'G'
        k = k+1
    incoming_lanes_all[i] = incoming_lanes#Save lanes for each phase
    outcoming_lanes_all[i] = outcoming_lanes

The entrance lane information and exit lane information obtained through the above code are as follows:

Consistent with the information obtained by the signal control interface


This paper mainly introduces some ways and methods of using traci interface to obtain traffic flow information and intersection information during my study. It is only for your reference. If you have any questions or opinions, you are welcome to exchange and discuss in the comment area or private letter.

Tags: Python SUMO

Posted by SetToLoki on Thu, 19 May 2022 22:20:49 +0300