Skip to content

Event-driven traffic simulation with priority-based vehicle scheduling, dynamic rerouting, and custom C++ data structures (no STL). Academic project for Data Structures course.

License

Notifications You must be signed in to change notification settings

Zeyad-nafea/Smart-traffic-control-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Smart Traffic Control System

Event-driven traffic simulation implementing priority-based vehicle scheduling, dynamic rerouting, and real-time intersection management using custom data structures in C++.

C++ License: MIT


πŸ‘₯ Team Members

Name Email
Zeyad Mohamed Fathy [email protected]
Omar Ahmed Fathy [email protected]
Shaza Kazem Mahmoud [email protected]
Ganna Allah Walid Helmy [email protected]

🎯 Overview

A sophisticated traffic control center simulation that manages multiple intersections, handles dynamic events (accidents, road closures, vehicle arrivals), and prioritizes emergency vehicles while ensuring fairness across all vehicle types. Built entirely from scratch using custom data structuresβ€”no STL containers.

Key Features

  • Priority-Based Scheduling: Emergency vehicles (EV) preempt normal traffic using urgency-weighted priority queues
  • Dynamic Event Handling: Real-time processing of arrivals, cancellations, promotions, accidents (ACC), and road closures (RC)
  • Adaptive Rerouting: Automatic vehicle rerouting during lane blockages using adjacency-based pathfinding
  • Auto-Promotion System: Public transport (PT) automatically promoted to EV status after waiting threshold
  • Fair Cancellation: Normal cars (NC) auto-cancel after exceeding wait limits to prevent indefinite blocking
  • Multi-Lane Management: Each intersection supports 4 lanes with independent queuing and switching cost optimization
  • Comprehensive Statistics: Detailed performance metrics including wait times, completion times, and intersection utilization

πŸ—οΈ Architecture

Core Components

TrafficControlCenter (Orchestrator)
β”œβ”€β”€ Event Queue (Priority Queue)
β”‚   β”œβ”€β”€ Arrival Events
β”‚   β”œβ”€β”€ Cancellation Events
β”‚   β”œβ”€β”€ Promotion Events
β”‚   β”œβ”€β”€ Accident Events
β”‚   └── Road Closure Events
β”‚
β”œβ”€β”€ Intersections (Array)
β”‚   └── Lanes (Per Intersection)
β”‚       β”œβ”€β”€ EV Queue (Priority Queue)
β”‚       β”œβ”€β”€ PT Queue (FIFO Queue)
β”‚       β”œβ”€β”€ NC Queue (FIFO Queue)
β”‚       └── FV Queue (FIFO Queue)
β”‚
└── Vehicle Registry (Queue)
    β”œβ”€β”€ All Vehicles
    β”œβ”€β”€ Completed Vehicles
    └── Canceled Vehicles

Vehicle Types & Priority

Type Priority Behavior
EV (Emergency) Highest Can interrupt green signals; Priority = (Urgency Γ— 2) - WT
PT (Public Transport) Medium FIFO within lane; Auto-promotes to EV if WT β‰₯ AP threshold
FV (Freight) Medium-Low Locks lane during crossing; Cannot be interrupted
NC (Normal Cars) Lowest FIFO; Auto-cancels if WT β‰₯ CancelT threshold

πŸš€ Implementation Highlights

Custom Data Structures (No STL)

  • Priority Queue: Max-heap implementation for event scheduling and EV prioritization
  • Queue: FIFO implementation for PT, NC, FV vehicle management
  • Node-based Linked Lists: Memory-efficient pointer sharing (no copying)
  • Adjacency Matrix: Intersection connectivity for rerouting pathfinding

Event-Driven Simulation

Events processed in strict order per timestep:

  1. Cancellations (highest priority)
  2. Promotions
  3. Accidents & Road Closures
  4. Arrivals (lowest priority)
  5. Lane assignment and green signal updates

Scheduling Algorithm

// EV Priority Calculation
priority = (urgency * 2) - waiting_time;

// Lane Selection Logic
for each lane:
  if (has_EV):
    select_lane_with_highest_EV_priority()
  else if (has_PT):
    select_lane_with_oldest_PT()
  else if (has_FV):
    select_lane_with_oldest_FV()
  else:
    select_lane_with_oldest_NC()

πŸ“Š Simulation Modes

Mode Description Use Case
Interactive Manual step-through with console output Debugging, demonstrations
Step-by-Step Auto-advances with delay Presentations, live monitoring
Silent No console output, file-only Performance testing, batch processing

πŸ› οΈ Building & Running

Prerequisites

  • C++11 or higher
  • Any standard C++ compiler (GCC, Clang, MSVC)

Compilation

# Using g++
g++ -std=c++11 src/*.cpp -o traffic_sim

# Using Visual Studio (already configured via .vcxproj)
# Open in VS and build solution

Execution

./traffic_sim

The program will prompt you to:

  1. Select simulation mode (1-3)
  2. Choose input test file (1-6)
  3. Specify output filename

πŸ“ Input File Format

<num_intersections>
<switching_cost>
<auto_promote_threshold>
<cancel_threshold>
<rerouting_flag: ON/OFF>
Connections:
<intersection_id>: <connected_intersection_ids>
<num_events>
<event_lines>

Event Syntax

Event Type Format Example
Arrival A TYPE AT ID INT LN XD [PR] A EV 3 301 1 2 2 5
Cancellation X AT ID X 6 101
Promotion P AT ID P 5 201
Accident ACC AT INT LN DUR ACC 4 1 1 3
Road Closure RC AT INT DUR RC 8 2 2

πŸ“ˆ Output Statistics

The system generates comprehensive metrics:

  • Vehicle Counts: Total and per-type breakdown (EV, PT, NC, FV)
  • Average Wait Times: Overall and per vehicle type
  • Average Crossing Duration: System-wide XD analysis
  • Auto-Promotion Rate: % of PT vehicles promoted to EV
  • Signal Switches: Total lane changes across all intersections
  • Cancellation Rate: % of vehicles that timed out
  • Most Delayed Vehicle: ID and wait time of longest-waiting vehicle
  • Intersection Blockage: % of time each intersection was blocked (ACC/RC)

πŸ§ͺ Test Cases

Six comprehensive test files included:

Test File Vehicles Intersections Events Focus Area
TestFile1.txt 20-30 2-4 8-10 Basic arrivals, promotions
TestFile2.txt 40-60 4-6 15-20 Cancellations, accidents
TestFile3.txt 80-100 6-8 25-30 Road closures, rerouting
TestFile4.txt 100+ 8-10 35+ Heavy load, EV priority
TestFile5.txt 150+ 10+ 50+ Stress test, edge cases
TestFile6.txt 200+ 12+ 75+ Max capacity, all features

πŸŽ“ Academic Context

Course: CSAI 201 - Data Structures and Algorithms
Institution: Zewail City University
Semester: Fall 2025

Project Requirements Met

βœ… Custom data structures (Queue, Priority Queue, Linked Lists)
βœ… No global variables, no STL, no friendship
βœ… Pointer-based memory sharing (zero-copy architecture)
βœ… Object-oriented design with clear separation of concerns
βœ… Event-driven simulation with discrete timestep advancement
βœ… Comprehensive statistics and output file generation
βœ… Multiple simulation modes for different use cases


πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ” Technical Decisions

Why Custom Data Structures?

This project deliberately avoids STL containers to demonstrate:

  • Deep understanding of data structure internals
  • Memory management expertise (manual allocation, pointer manipulation)
  • Performance optimization (cache locality, minimal allocations)
  • Academic integrity (no black-box dependencies)

Design Patterns Used

  • Facade Pattern: TrafficControlCenter abstracts complexity from UI
  • Strategy Pattern: Different vehicle types implement varying priority logic
  • Observer Pattern: Events trigger state changes across intersections
  • State Pattern: Vehicle status transitions (WAITING β†’ PASSING β†’ FINISHED)

Note: This is an academic project. The simulation logic is designed for educational purposes and does not reflect real-world traffic control systems.

About

Event-driven traffic simulation with priority-based vehicle scheduling, dynamic rerouting, and custom C++ data structures (no STL). Academic project for Data Structures course.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages