Modern traffic intersections often suffer from inefficiencies due to static traffic light timings, leading to unnecessary congestion. The goal of this system is to create a dynamic traffic light controller that adapts based on the number of cars waiting at each road. The system should:
- Detect the number of incoming cars per second on each road.
- Allocate green light time dynamically based on traffic volume.
- Ensure fairness and efficiency in managing road congestion.
This system simulates a smart intersection using an algorithm that:
- Tracks the number of cars arriving at each road per second.
- Determines which road has the highest traffic congestion.
- Allocates a green light dynamically, with a minimum of 5 seconds and a maximum of 30 seconds.
- Updates the system continuously as new cars arrive.
The system is implemented in TypeScript, using an Intersect
class to manage roads and their traffic conditions.
- Manages intersections and their traffic data.
- Stores historical data for decision-making.
- Dynamically updates light timing based on traffic conditions.
intersects
: An array representing each road's queue.totalSumOfCars
: Stores the total number of cars per road.lastIndexReserve
: Stores information about the last green light decision.
- Finds the road with the highest traffic.
- Allocates green light based on traffic throughput.
- Ensures fairness by not keeping any road waiting indefinitely.
green_time: The duration the traffic light stays green (e.g., 30 seconds).
car_length: The physical length of each car, in meters (e.g., 4.5m).
gap: The space between two cars when they are moving through the intersection, also in meters
alpha: weight for passable cars
beta: weight for total cars
rustc main.rs
./main