This repository contains a solution to the Traffic Signaling optimization problem using Google OR-Tools CP-SAT solver. The problem was originally from Google Hash Code 2021 Qualification Round.
The Traffic Signaling problem involves optimizing traffic light schedules at intersections to minimize the total travel time of cars through a city. Each car follows a predetermined path through the city streets, and the goal is to coordinate traffic lights to allow cars to complete their journeys as efficiently as possible.
- Intersections: Points where multiple streets meet, controlled by traffic lights
- Streets: One-way roads connecting intersections, each with a travel time
- Cars: Vehicles following specific paths through the city
- Traffic Lights: Control which street has a green light at each intersection
├── main.py # Main entry point for solving problems
├── validate.py # Solution validation script
├── src/ # Core implementation
│ ├── __init__.py
│ ├── model.py # CP-SAT model implementation
│ └── utils.py # Input parsing and output utilities
├── inputs/ # Problem instances
├── outputs/ # Generated solutions
└── docs/ # Documentation and problem statements
├── hashcode_2021_qualification_round.pdf
├── hashcode_2021_qualification_round_scoreboard.json
└── traffic_signaling_CPSAT.pdf
- Python 3.10+
- Google OR-Tools 9.14.6206
-
Clone the repository:
git clone https://github.com/yllberisha/traffic-signaling-problem-ortools.git cd traffic-signaling-problem-ortools -
Create and activate a conda environment:
conda create -n pyortools_env python=3.10 conda activate pyortools_env
-
Install required packages:
pip install ortools==9.14.6206
Run the solver on a problem instance:
python main.py inputs/a_example.inThis will generate a solution file in the outputs/ directory.
Parameters:
input_file: Path to the input problem file (required)output_dir: Output directory for solutions (default:outputs)-t, --time: Time limit in seconds (default: 300)-j, --threads: Number of threads to use (default: 8)
Validate a generated solution:
python validate.py inputs/a_example.in outputs/a_example.outInput files contain:
-
Header:
D I S V Fwhere:D: Duration of the simulationI: Number of intersectionsS: Number of streetsV: Number of carsF: Bonus points for cars finishing before time D
-
Streets:
Slines withB E name Lwhere:B: Start intersectionE: End intersectionname: Street nameL: Time to travel the street
-
Cars:
Vlines withP street1 street2 ...where:P: Number of streets in the car's pathstreet1, street2, ...: Street names in the car's path
Output files contain:
- Number of intersections with scheduled traffic lights
- For each intersection:
- Intersection ID
- Number of streets with green light schedules
- For each street: street name and green light duration
The solver uses Google OR-Tools CP-SAT (Constraint Programming with SAT) to model and solve the traffic optimization problem. The key components include:
-
Variables:
- Start times for crossing events
- Green light schedules for intersections
- Car completion times
-
Constraints:
- Traffic light scheduling constraints
- Car movement constraints
- Timing and sequencing constraints
-
Objective: Maximize the total score (cars completing their journeys + bonus points)
This project is open source and available under the MIT License.