eclipse-sumo / sumo

Eclipse SUMO is an open source, highly portable, microscopic and continuous traffic simulation package designed to handle large networks. It allows for intermodal simulation including pedestrians and comes with a large set of tools for scenario creation.
https://eclipse.dev/sumo
Eclipse Public License 2.0
2.49k stars 1.41k forks source link

[Question] Priority Junction at the intersection #15381

Closed TerzaSlayer2000 closed 2 weeks ago

TerzaSlayer2000 commented 2 weeks ago

Hello Sumo Community, in my four way roundabout intersection if i set the junction type as priority, will it prioritize road edges connected to the roundabout which have higher priority values? Actually my road edges are having the same priority values which is 10 for now, suppose if i set one of the edges with a higher priority value like 11, will SUMO allow that road edge to cross the roundabout intersection more frequently than the other edges which have low priority value? Thank you.

SUMO-version: 1.18.0

operating system: Windows 11

namdre commented 2 weeks ago

By default, netconvert detects roundabouts based on road geometry and topology and then assigns right-of-way to the edges inside the roundabout, regardless of the priority attribute of the edges. If you want to give some preference to one of the incoming edges, consider a roundabout with traffic lights or increase the number of lanes.

TerzaSlayer2000 commented 2 weeks ago

Thank you for your response. Can we use induction loop detectors just before the roundabout edges and gives priority to those detectors instead and implement the priority logic using TraCI in a python script. Below is the code :

import traci import sumolib

Initialize SUMO with the sumo-gui or sumo command, depending on your preference

sumoBinary = sumolib.checkBinary('sumo-gui') traci.start([sumoBinary, "-c", "sample.sumocfg"])

Define a function to control the roundabout with priority logic

def control_roundabout_with_priority():

Define detectors corresponding to each lane

high_priority_detectors = ["e1_1", "e1_5"]  # Lanes 835504481_1 and 835504479_1
low_priority_detectors = ["e1_2", "e1_6"]   # Lanes -123554671#0_1 and 835504475#2_1

# Initialize a list to keep track of which detector has a vehicle stopped
vehicle_waiting = {det: None for det in high_priority_detectors + low_priority_detectors}

# Define the maximum time step (in this case, 10,000)
max_timesteps = 10000

while traci.simulation.getMinExpectedNumber() > 0:
    current_step = traci.simulation.getTime()  # Get the current simulation time step

    if current_step >= max_timesteps:
        break  # Exit the loop if the max timestep is reached

    traci.simulationStep()

    # Process high-priority vehicles first
    for detector in high_priority_detectors:
        vehicles = traci.inductionloop.getLastStepVehicleIDs(detector)
        if vehicles:
            vehicle_id = vehicles[0]
            if vehicle_waiting[detector] is None:
                # Stop the vehicle at the detector
                traci.vehicle.slowDown(vehicle_id, 0, 1)  # Set speed to 0 over 1 second
                vehicle_waiting[detector] = vehicle_id
        else:
            if vehicle_waiting[detector] is not None:
                # Check if the vehicle is still in the simulation by checking if it's in the list of active vehicles
                active_vehicles = traci.vehicle.getIDList()
                if vehicle_waiting[detector] in active_vehicles:
                    # Allow the waiting vehicle to proceed
                    max_speed = traci.vehicle.getMaxSpeed(vehicle_waiting[detector])
                    traci.vehicle.slowDown(vehicle_waiting[detector], max_speed, 1)  # Reset speed to max_speed
                vehicle_waiting[detector] = None

    # Process low-priority vehicles if no high-priority vehicles are waiting
    high_priority_busy = any(vehicle_waiting[det] is not None for det in high_priority_detectors)
    if not high_priority_busy:
        for detector in low_priority_detectors:
            vehicles = traci.inductionloop.getLastStepVehicleIDs(detector)
            if vehicles:
                vehicle_id = vehicles[0]
                if vehicle_waiting[detector] is None:
                    # Stop the vehicle at the detector
                    traci.vehicle.slowDown(vehicle_id, 0, 1)  # Set speed to 0 over 1 second
                    vehicle_waiting[detector] = vehicle_id
            else:
                if vehicle_waiting[detector] is not None:
                    # Check if the vehicle is still in the simulation by checking if it's in the list of active vehicles
                    active_vehicles = traci.vehicle.getIDList()
                    if vehicle_waiting[detector] in active_vehicles:
                        # Allow the waiting vehicle to proceed
                        max_speed = traci.vehicle.getMaxSpeed(vehicle_waiting[detector])
                        traci.vehicle.slowDown(vehicle_waiting[detector], max_speed, 1)  # Reset speed to max_speed
                    vehicle_waiting[detector] = None

traci.close()

if name == "main": control_roundabout_with_priority()