A route optimization system for public transit ticket inspectors that uses mathematical optimization to determine the most efficient inspection routes across transit networks.
Public transit systems need to efficiently deploy ticket inspectors to maximize route coverage while minimizing travel time and operational costs. This system solves the challenge of:
- Route Coverage Maximization: Visiting as many different transit routes as possible within time constraints
- Time Optimization: Minimizing total travel time while ensuring comprehensive coverage
- Operational Efficiency: Creating feasible inspection paths that start and end at a designated depot/stop
The system uses Mixed Integer Linear Programming (MILP) with the PuLP optimization library to solve a variant of the Traveling Salesman Problem (TSP) with additional constraints:
- Objective Function: Maximize the number of unique transit routes visited
- Constraints:
- Time window limitations (default: 240 minutes)
- Flow conservation (each stop has equal in/out edges)
- Single path requirement (no cycles except returning to start)
- Route feasibility (only use existing transit connections)
- GTFS Data Integration: Processes General Transit Feed Specification data including:
stops.txt- Stop locations and metadataroutes.txt- Transit route definitionstrips.txt- Trip-to-route mappingsstop_times.txt- Schedule and timing data
- Builds a 3D adjacency matrix
AdjMatrix[stop_i][stop_j][route_k] - Creates time matrix with travel times between consecutive stops
- Handles missing data with reasonable defaults
x[i][j][k]: Binary variable (1 if inspector travels from stop i to stop j via route k)y[k]: Binary variable (1 if route k is visited)t[i]: Continuous variable defining visit order for subtour elimination
Generates optimal inspection route starting from the specified stop ID.
Parameters:
sid(path parameter): Starting stop ID
Response:
{
"0": {
"stop_a_id": 123,
"stop_a_name": "Main Street Station",
"stop_a_lat": 40.7128,
"stop_a_lng": -74.0060,
"stop_a_time": 0,
"stop_b_id": 124,
"stop_b_name": "Central Plaza",
"stop_b_lat": 40.7589,
"stop_b_lng": -73.9851,
"stop_b_time": 15,
"route": 2
}
}- Node.js: Version 10.19.0
- npm: Version 6.14.4
- Python: Version 3.6+ with pip
- GTFS Data: Place your GTFS files in
./data/gtfscecil/
data/gtfscecil/
├── stops.txt
├── routes.txt
├── trips.txt
└── stop_times.txt
-
Clone the repository
git clone <repository-url> cd ticket-Inspector-backend
-
Install Node.js dependencies
npm install
-
Install Python dependencies
pip install -r requirements.txt
-
Prepare GTFS data
- Create the directory structure:
./data/gtfscecil/ - Place your GTFS files in this directory
- Ensure files follow the GTFS specification format
- Create the directory structure:
-
Start the server
npm start
The server will start on port 5000 (or the port specified in process.env.PORT).
# Example: Get route plan starting from stop ID 1
curl http://localhost:5000/api/1Modify the MaxTime variable in get_route_plan.py to adjust the inspection time window:
MaxTime = 240 # Time in minutesThe system adds time penalties for:
- Route transfers: +5 minutes
- Same-route connections: +2 minutes
- express: Web framework for API endpoints
- python-shell: Interface for executing Python optimization scripts
- fs: File system operations
- pandas: Data manipulation and CSV processing
- PuLP: Linear programming optimization
- pathlib: File path handling
- requests: HTTP requests (if needed for external data)
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Client │────│ Express.js API │────│ Python Optimizer│
│ Request │ │ (Node.js) │ │ (PuLP) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │
│ │
┌──────▼──────┐ ┌──────▼──────┐
│ Route │ │ GTFS Data │
│ Response │ │ Processing │
│ (JSON) │ │ & Graph │
└─────────────┘ │ Building │
└─────────────┘
The included Procfile supports Heroku deployment:
web: pip install -r requirements.txt && npm start
- Ensure sufficient memory for large GTFS datasets
- Consider caching optimization results for frequently requested routes
- Monitor Python script execution times for performance optimization
- Fork the repository
- Create a feature branch
- Make your changes
- Test with sample GTFS data
- Submit a pull request
ISC License - See package.json for details
Swati Ahuja