Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [sumo-user] Assistance with Extracting Traffic Signal Durations at Multiple Intersections in SUMO

please see https://sumo.dlr.de/docs/Simulation/Output/Traffic_Lights.html

Am Di., 25. Juni 2024 um 11:44 Uhr schrieb Manohara C V via sumo-user <sumo-user@xxxxxxxxxxx>:
Dear SUMO Support Team,
I hope this email finds you well. I am currently working on a traffic management project that involves extracting and analyzing traffic signal durations at multiple intersections using the SUMO traffic simulation software.
I have successfully set up my simulation environment and have been able to visualize the traffic signals in the SUMO-GUI. However, I am facing difficulties in exporting the traffic signal phase durations for multiple intersections over time. Specifically, I aim to capture the duration of each signal phase (red, yellow, green) at every phase change and export this data for further analysis.
To provide some context, I have attached a screenshot of the traffic signal visualization for one intersection. My goal is to automate the extraction of these signal phase durations for all intersections in my network.

Could you kindly provide guidance or recommend a method to achieve this? Below is a Python script I have been working on, which uses TraCI to interact with the SUMO simulation. Any feedback or suggestions to improve the script or alternative approaches would be greatly appreciated.
import os
import sys
import traci
import pickle

if "SUMO_HOME" in os.environ:
    tools = os.path.join(os.environ["SUMO_HOME"], "tools")
    sys.path.append(tools)
else:
    sys.exit("Please declare the environment variable 'SUMO_HOME'")

def get_tls_phase_durations():
    traci.start(["sumo-gui", "-c", "your_sumo_config_file.sumocfg"])
   
    tls_ids = traci.trafficlight.getIDList()
    phase_durations = {tls_id: [] for tls_id in tls_ids}
    phase_start_times = {tls_id: traci.simulation.getTime() for tls_id in tls_ids}
    previous_phases = {tls_id: None for tls_id in tls_ids}

    step = 0
    while step < 3600:  # Run simulation for 3600 steps (1 hour)
        traci.simulationStep()

        for tls_id in tls_ids:
            current_phase = traci.trafficlight.getPhase(tls_id)
            current_time = traci.simulation.getTime()

            if previous_phases[tls_id] is None:
                previous_phases[tls_id] = current_phase
            elif previous_phases[tls_id] != current_phase:
                phase_duration = current_time - phase_start_times[tls_id]
                phase_durations[tls_id].append({
                    'phase': previous_phases[tls_id],
                    'duration': phase_duration
                })
                previous_phases[tls_id] = current_phase
                phase_start_times[tls_id] = current_time
       
        step += 1
   
    traci.close()

    # Save the phase durations to a file
    with open('phase_durations.pkl', 'wb') as f:
        pickle.dump(phase_durations, f)

    # Print the phase durations
    for tls_id, phases in phase_durations.items():
        print(f"Traffic Light ID: {tls_id}")
        for phase in phases:
            print(f"Phase: {phase['phase']}, Duration: {phase['duration']} seconds")

get_tls_phase_durations()



Thanks and regards,
Manohar 
_______________________________________________
sumo-user mailing list
sumo-user@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/sumo-user

Back to the top