A web-based traffic simulation system that combines SUMO (Simulation of Urban MObility) with PyPSA (Python for Power System Analysis) for real-time traffic visualization and analysis.
SUMOxPyPSA is a Flask-based web application that provides real-time traffic simulation for multiple cities using SUMO. The system allows users to visualize traffic flow, traffic lights, and vehicle movements in an interactive web interface.
- Multi-City Support: Simulate traffic in New York, Miami, and Los Angeles
- Real-Time Visualization: Web-based interface showing live traffic simulation
- Interactive Controls: Start, stop, and restart simulations
- Traffic Light Monitoring: Real-time traffic light state visualization with advanced synchronization
- Vehicle Tracking: Track individual vehicles and their movements
- WebSocket Communication: Real-time updates via Socket.IO
- Configurable Simulation: Adjustable simulation speed and update frequency
- Advanced Traffic Light Logic: Proper green-yellow-red cycling with randomized offsets and desynchronization
The application starts up and loads the main interface
Users can select between different cities (New York, Miami, Los Angeles) for simulation
Real-time switching between different city simulations with preserved traffic states
Advanced traffic light visualization showing proper synchronization and state management
The project includes significant improvements to traffic light behavior and synchronization:
-
Network Regeneration with Clean Intersections
- Regenerated all city networks using
netconvertwith specific options - Ensured clean intersections with separate signals for straight and left turns
- Applied
--tls.guess-signals truefor proper initial signal plans
- Regenerated all city networks using
-
Traffic Light Desynchronization
- Implemented randomized offsets and phase shifts to prevent unrealistic synchronization
- Created scripts to generate proper green-yellow-red cycling patterns
- Added random initial states to avoid all traffic lights starting in the same phase
-
Advanced Traffic Light Logic
- Fixed improper yellow-to-green transitions without red phases
- Implemented proper state duration tracking and phase management
- Added programmatic switching to use randomized traffic light programs (programID '1')
-
Configuration Enhancements
- Updated SUMO configurations to include traffic light additional files
- Set finer simulation time step (0.1 seconds) for more realistic behavior
- Implemented proper traffic light state monitoring and correction
- Traffic Light Programs: Each intersection uses programID '1' with randomized timing
- State Management: Proper tracking of current state, duration, and phase transitions
- Error Correction: Automatic detection and fixing of improper traffic light transitions
- Real-time Monitoring: Continuous monitoring of traffic light states with WebSocket updates
- Python 3.7+
- SUMO (Simulation of Urban MObility)
- Flask and Flask-SocketIO
- Required Python packages (see requirements.txt)
-
Install SUMO:
# Ubuntu/Debian sudo apt-get install sumo sumo-tools sumo-doc # macOS brew install sumo # Windows # Download from https://sumo.dlr.de/docs/Downloads.php
-
Clone the repository:
git clone <repository-url> cd SUMOxPyPSA
-
Install Python dependencies:
pip install -r tools/requirements.txt pip install flask flask-socketio traci
-
Configure SUMO path: Edit
config.pyand update theSUMO_PATHvariable to match your SUMO installation:# Linux (default) SUMO_PATH = "/usr/share/sumo" # Windows SUMO_PATH = "C:\\Program Files (x86)\\Eclipse\\Sumo" # macOS SUMO_PATH = "/opt/homebrew/Cellar/sumo/1.20.0/share/sumo"
-
Start the web server:
python app.py
-
Access the application: Open your web browser and navigate to
http://localhost:8080 -
Select a city: Use the city selector to choose between New York, Miami, or Los Angeles
-
Control the simulation:
- Click "Start" to begin the simulation
- Click "Restart" to reset and restart the simulation
SUMOxPyPSA/
├── app.py # Main Flask application
├── config.py # Configuration settings
├── sumo_config.py # SUMO-specific configurations
├── build.py # Network building script
├── map_to_power.py # Map conversion utilities
├── compress_net.py # Network compression utilities
├── test_db_connection.py # Database connection testing
├── tools/
│ └── requirements.txt # Python dependencies
├── templates/
│ └── index.html # Web interface template
├── static/ # Static web assets
├── gifs/ # Demo GIFs for documentation
├── new_york/ # New York city data
├── miami/ # Miami city data
├── los_angeles/ # Los Angeles city data
├── pypsa_network/ # PyPSA network data
└── LICENSE # MIT License
Edit config.py to customize simulation parameters:
SIMULATION_SPEED: Controls how fast the simulation runsUPDATE_FREQUENCY: How often to send updates to the web interfaceHOSTandPORT: Web server configuration
Each city has its own configuration in config.py:
CITY_CONFIGS = {
"newyork": {
"cfg_file": "path/to/newyork/osm.sumocfg",
"name": "New York, USA",
"working_dir": "path/to/newyork"
},
# ... other cities
}To build SUMO networks for cities:
python build.py [city_name]Where city_name can be:
newyorkmiamilosangeles
GET /: Main web interfaceWebSocket /socket.io: Real-time communicationchange_city: Switch between citiesrestart: Restart simulationupdate: Real-time simulation data
The application sends real-time data via WebSocket including:
- Vehicles: Position, angle, and ID of all vehicles
- Traffic Lights: Position, state, and ID of all traffic lights with proper synchronization
- SUMO not found: Ensure SUMO is properly installed and the path in
config.pyis correct - Port already in use: Change the
PORTinconfig.py - City data missing: Ensure city directories contain proper SUMO configuration files
- Traffic light synchronization issues: The system automatically handles traffic light logic and synchronization
To contribute to the project:
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- SUMO Development Team for the traffic simulation framework
- PyPSA Development Team for the power system analysis tools
- Flask and Socket.IO communities for the web framework
For issues and questions, please open an issue on the project repository.