simworld.traffic.base package
Submodules
simworld.traffic.base.crosswalk module
Crosswalk module for representing crosswalks in the simulation.
This module defines the crosswalk class used to represent pedestrian crossing paths that connect sidewalks across roads in the simulation.
- class simworld.traffic.base.crosswalk.Crosswalk(start: Vector, end: Vector, road_id: int)
Bases:
objectRepresents a crosswalk in the simulation.
A crosswalk connects two sidewalks and allows pedestrians to cross roads safely. It defines the start and end points of the crossing path.
- classmethod reset_id_counter()
Reset the ID counter for crosswalks.
Used when resetting the simulation to ensure IDs start from 0.
simworld.traffic.base.intersection module
Intersection module for representing and managing intersections in the simulation.
This module defines the intersection class which is responsible for managing the connections between roads, lanes, sidewalks, and controlling traffic signals.
- class simworld.traffic.base.intersection.Intersection(center: Vector, roads: List[Road])
Bases:
objectRepresents a road intersection in the simulation.
Manages the connection of multiple roads, including lanes, sidewalks, and traffic signals. Handles the traffic flow logic at the intersection.
- add_traffic_signals(config)
Add traffic signals to the intersection.
One signal for each road, controlling both lanes and crosswalks on that road.
- Args:
config: Configuration dictionary with traffic signal parameters.
- Raises:
ValueError: If there are no lanes to add traffic signals to.
- all_traffic_lights_red()
Check if all traffic lights at the intersection are red.
- Returns:
True if all traffic lights are red, False otherwise.
- connect_lanes(config)
Connect lanes at the intersection.
Creates mappings from incoming lanes to outgoing lanes.
- Args:
config: Configuration dictionary with lane parameters.
- connect_sidewalks(config)
Connect sidewalks at the intersection.
Creates mappings between sidewalks and determines crosswalk connections.
- Args:
config: Configuration dictionary with sidewalk parameters.
- Raises:
ValueError: If a road has an unexpected number of crosswalks.
- get_crosswalk_light_state(crosswalk: Crosswalk)
Get the state of the traffic light for a specific crosswalk.
- Args:
crosswalk: The crosswalk to get the traffic light state for.
- Returns:
A tuple of (state, left_time) for the crosswalk.
- get_traffic_light_state(incoming_lane: TrafficLane)
Get the state of the traffic light for a specific lane.
- Args:
incoming_lane: The lane to get the traffic light state for.
- Returns:
The state of the traffic light for the lane.
- has_completed_cycle()
Check if all traffic lights have cycled through green once.
- Returns:
True if a complete cycle has been completed, False otherwise.
- increment_cycle_count()
Increment the cycle count when a light turns green.
- init_intersection(config)
Initialize the intersection with configuration.
Sets up lane connections, sidewalk connections, and traffic signals.
- Args:
config: Configuration dictionary with simulation parameters.
- property lanes
Get the lane mapping for this intersection.
- Returns:
Dictionary mapping incoming lanes to outgoing lanes.
- reset_cycle_count()
Reset the cycle count when a light cycle is completed.
- classmethod reset_id_counter()
Reset the ID counter for intersections.
Used when resetting the simulation to ensure IDs start from 0.
- property sidewalks
Get the sidewalk mapping for this intersection.
- Returns:
Dictionary mapping sidewalks to their connections.
simworld.traffic.base.road module
Road module for representing roads in the simulation.
This module defines the road class which is responsible for generating and managing lanes, sidewalks, and crosswalks that make up the road infrastructure.
- class simworld.traffic.base.road.Road(start: Vector, end: Vector, num_lanes: int, lane_offset: float, intersection_offset: float, sidewalk_offset: float, crosswalk_offset: float)
Bases:
objectRepresents a road in the simulation.
A road consists of multiple lanes, sidewalks, and crosswalks. It connects two points in the simulation and provides the infrastructure for vehicles and pedestrians.
- add_crosswalks()
Create and add crosswalks to the road.
Creates crosswalks connecting the sidewalks at each end of the road.
- add_lanes()
Create and add traffic lanes to the road.
Creates forward and backward lanes for each lane number, offset appropriately from the road center line.
- add_sidewalks()
Create and add sidewalks to the road.
Creates forward and backward sidewalks on each side of the road, offset appropriately from the road edge.
- get_center()
Calculate and return the center point of the road.
- Returns:
A Vector representing the center point of the road.
- get_direction()
Calculate and return the normalized direction vector of the road.
- Returns:
A normalized Vector representing the road’s direction.
- init_road()
Initialize the road by creating lanes, sidewalks, and crosswalks.
This method should be called after creating a road to set up all its components.
- classmethod reset_id_counter()
Reset the ID counter for roads.
Used when resetting the simulation to ensure IDs start from 0.
simworld.traffic.base.sidewalk module
Sidewalk module for representing sidewalks in the simulation.
This module defines the sidewalk class used to represent pedestrian paths along the sides of roads in the simulation.
- class simworld.traffic.base.sidewalk.Sidewalk(road_id: int, start: Vector, end: Vector)
Bases:
objectRepresents a sidewalk in the simulation.
A sidewalk is a path along the side of a road where pedestrians can walk. It maintains its start and end points, direction, and a list of pedestrians on it.
- add_pedestrian(pedestrian)
Add a pedestrian to this sidewalk.
- Args:
pedestrian: The pedestrian object to add to the sidewalk.
- get_direction()
Calculate and return the normalized direction vector of the sidewalk.
- Returns:
A normalized Vector representing the sidewalk’s direction.
- remove_pedestrian(pedestrian)
Remove a pedestrian from this sidewalk.
- Args:
pedestrian: The pedestrian object to remove from the sidewalk.
- classmethod reset_id_counter()
Reset the ID counter for sidewalks.
Used when resetting the simulation to ensure IDs start from 0.
simworld.traffic.base.traffic_lane module
Traffic lane module for representing traffic lanes in the simulation.
This module defines the traffic lane class used to represent road lanes where vehicles can travel in the simulation.
- class simworld.traffic.base.traffic_lane.TrafficLane(road_id: int, start: Vector, end: Vector)
Bases:
objectRepresents a traffic lane in the simulation.
A traffic lane is a part of a road where vehicles can travel in one direction. It maintains its start and end points, direction, and a list of vehicles on it.
- add_vehicle(vehicle)
Add a vehicle to this lane.
- Args:
vehicle: The vehicle object to add to the lane.
- get_direction()
Calculate and return the normalized direction vector of the lane.
- Returns:
A normalized Vector representing the lane’s direction.
- remove_vehicle(vehicle)
Remove a vehicle from this lane.
- Args:
vehicle: The vehicle object to remove from the lane.
- classmethod reset_id_counter()
Reset the ID counter for traffic lanes.
Used when resetting the simulation to ensure IDs start from 0.
simworld.traffic.base.traffic_signal module
Traffic signal module for representing traffic lights in the simulation.
This module defines the traffic signal state enumeration and the traffic signal class used to represent and control traffic lights and pedestrian crossing signals.
- class simworld.traffic.base.traffic_signal.TrafficSignal(position: Vector, direction: Vector, lane_id: int | None = None, crosswalk_id: int | None = None, type: str = 'both')
Bases:
objectRepresents a traffic signal in the simulation.
This class models both vehicle traffic lights and pedestrian crossing signals, maintaining their state and position in the simulation.
- get_left_time()
Get the remaining time for the current signal state.
- Returns:
The time in seconds until the signal changes.
- get_state()
Get the current state of the traffic signal.
- Returns:
A tuple of (vehicle_state, pedestrian_state).
- classmethod reset_id_counter()
Reset the ID counter for traffic signals.
Used when resetting the simulation to ensure IDs start from 0.
- set_left_time(left_time)
Set the remaining time for the current signal state.
- Args:
left_time: The time in seconds until the signal changes.
- set_state(state)
Set the state of the traffic signal.
- Args:
state: A tuple of (vehicle_state, pedestrian_state).
- Raises:
ValueError: If the state is not a valid tuple of TrafficSignalState enums.
- class simworld.traffic.base.traffic_signal.TrafficSignalState(value)
Bases:
EnumEnumeration of possible traffic signal states.
Defines the possible states for both vehicle traffic lights and pedestrian signals.
- PEDESTRIAN_GREEN = 'pedestrian_green'
- PEDESTRIAN_RED = 'pedestrian_red'
- VEHICLE_GREEN = 'vehicle_green'
- VEHICLE_RED = 'vehicle_red'
Module contents
Base module for traffic simulation components.
This module contains the core classes and structures for representing and managing traffic elements in the simulation environment.