A traffic junction connects two major roads, forming a central point where vehicles must choose one of three alternative paths to continue (visual representation is shown in Figure 1). The traffic management system must handle the following scenarios:
- Vehicles at the junction are served equally from each lane. The system should ensure that vehicles are dispatched fairly.
-
If one of the roads (referred to as the priority road) accumulates more than 10 waiting vehicles, that road should be served first until the count drops below 5. Afterward, normal conditions resume. Lane
CL2
is considered as the lane having the highest priority.
To install and run the traffic simulator, follow these steps:
- Clone the repository on your local environment:
git clone https://github.com/SafalNarsingh/DSA-Queue-Simulator
-
Install SDL 2 library:
- For Linux/Unix:
git clone https://github.com/libsdl-org/SDL.git -b SDL2 cd SDL mkdir build cd build ../configure make sudo make install
- For macOS:
mkdir build cd build CC=/where/i/cloned/SDL/build-scripts/clang-fat.sh ../configure make
- For Windows: You do not need to install SDL 2 on your computer. You can include the SDL 2 path in your respective local environment.
- For Linux/Unix:
-
Build the project using:
gcc -o simulator simulator.c -I./src/include -L./src/lib -lmingw32 -lSDL2main -lSDL2 -lSDL2_ttf -pthread
-
Run the executable:
./simulator
Make sure mingw and pthread, POSIX threads library for MinGW, are installed on your device.
- C
- SDL2
- All lanes are normal lanes until explicitly declared as a priority. This lane will be served on normal conditions based on the average number of vehicles waiting on normal lanes.
Where,
n: Total number of lanes (in this case it is 3 ⇒(BL2, CL3, DL4))
|Li|: Total number of vehicle waiting on ith lane
|V|: Total number of vehicles served.
It is hard to count the number of vehicles passing the junction. To solve this problem we have to estimate the time required for passing m number of vehicles.
Total time of green light(state 2) = |V| ∗ t
Where,
T: Estimated time required to pass one vehicle
- This lane is a special lane where the waiting time should be low. When there are more than 5 vehicles waiting this lane should be served immediately after the running lighting condition ends.
- Special Case: For the vehicle less than 5, this lane also turn into the normal lane.
- For simplicity, there will be a total of four traffic lights each of which will instruct the vehicle of the opposite lane.
- Red Light: State 1 ⇒Stop
- Green Light: State 2 ⇒Go straight or turn
(Monitor the status of each lane to effectively track queue sizes)
Here is a brief of the code:
Vehicle
is the main structure used for vehicle generation. It contains the attributes required for the vehicle to be generated like its position(x,y), its color, its lane, etc.
typedef struct {
char id[9];
int x, y;
char lane;
int sublane;
bool active;
int direction;
int route_type;
char target_lane;
int target_sublane;
int choice;
SDL_Color color;
} Vehicle;
#define VEHICLE_SIZE 40
#define VEHICLE_LENGTH 60
#define MAX_VEHICLES 500
#define VEHICLE_SPEED 4
Constants defined for Vehicle Generation.
Vehicle vehicles[MAX_VEHICLES];
vehicles
has been declared as the structure variable.
Four major functions have been implemented for rendering the vehicle on the screen.
void drawVehicles(SDL_Renderer* renderer); //draws the vehicle
void initVehicles(); //initializes the vehicle in not moving state
void spawnVehicle(const char* id, char lane, int sublane) ; //positions the vehicle on the screen based on the lane and sublane
void updateVehicle(); //responsible for moving, turning and stopping of vehicle
with some helper functions to provide additional functionalities to the generated vehicle.
void countVehiclesPerLane(int laneQueue[], int sublane); //Counts the number of vehicles in sublane 2
TrafficLight
is implemented as the structure to define the state of the lights.
typedef struct {
bool green;
} TrafficLight;
TrafficLight trafficLights[4]
trafficLights
has be defined as the structure variable.
Major functions implemented for traffic light generation are:
void initTrafficLights(); //Initialize the lights to red as default
void drawTrafficLights(SDL_Renderer* renderer) //Render the traffic lights
void* updateTrafficLights(void* arg); //Traffic lights function for normal priority lanes
void* updateTrafficLightsAdvanced(void* arg); //Traffic lights function for high-priority lanes
In our queue implementation, we continuously monitor the number of vehicles waiting in each lane and prioritize dequeuing vehicles from lanes with higher vehicle counts, while simultaneously enqueuing vehicles from lanes at red lights. If the queue size of any lane exceeds 5 vehicles, that lane will be dequeued first. However, if lane Cl2
exceeds 5 vehicles, it receives top priority and will be dequeued first until its count drops back below 5 vehicles.
For Queue implementation, TrafficQueueu
structure has ben implemented.
typedef struct {
int vehicleIndices[MAX_QUEUE_SIZE]; // Stores indices of vehicles in queue
int front;
int rear;
int size;
} TrafficQueue;
laneQueues
has been defined as the structure variable.
TrafficQueue laneQueues[NUM_LANES]; // Create a queue for each lane to track vehicles waiting at lights
Functions used for traffic queue management has been listed below:
void initQueue(TrafficQueue* queue); //Initialize a new queue
bool isQueueEmpty(TrafficQueue* queue); //Check if queue is empty
bool isQueueFull(TrafficQueue* queue); //Check if queue is full
bool enqueue(TrafficQueue* queue, int vehicleIndex) //Add a vehicle index to the queue
int dequeue(TrafficQueue* queue; //Remove a vehicle index from the queue
int peek(TrafficQueue* queue); //Get front of queue without removing
void updateTrafficQueues() //Enqueues or dequeues the traffic and size calculation
- CPU usage needs to be optimized.
Last updated by Safal Narshing Shrestha on Feb 26, 2025, 23:53.
Copyright © 2025 Safal Narshing Shrestha. All rights reserved.