Skip to content

Latest commit

ย 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— Route Optimizer

A modular route optimization system supporting multiple algorithms for different routing problems.

Route Optimizer Demo

๐Ÿ“‹ Description

Route Optimizer is a flexible framework for solving various route optimization problems using real street networks from OpenStreetMap. It provides a pluggable architecture where different algorithms can be applied depending on your use case.

Supported Algorithms

Algorithm Problem Type Best For
CPP (Chinese Postman) Edge coverage Street sweeping, patrol routes, inspections
TSP (Traveling Salesman) Node visits Deliveries, pickups, point-to-point routes

The system is designed to be extensible - new algorithms (VRP, CVRP, etc.) can be added easily.

โœจ Features

  • ๐Ÿงฎ Multiple algorithms - CPP, TSP, with extensible architecture
  • ๐Ÿ—บ๏ธ Real street networks - Downloads from OpenStreetMap
  • ๐Ÿ“ Zone-based routing - Define areas with polygons
  • ๐Ÿ“Š Excel export - Detailed route coordinates
  • ๐ŸŽจ Visualization - Map plots with matplotlib
  • โš™๏ธ JSON configuration - Easy setup and maintenance
  • ๐Ÿณ Docker support - Quick setup with all dependencies

๐Ÿš€ Installation

Option 1: Docker (Recommended) ๐Ÿณ

Docker handles all geospatial dependencies (GDAL, GEOS, PROJ) automatically.

git clone <repository-url>
cd route-optimizer
make build

Option A - Development (interactive shell):

Best for development and experimentation. Enter the container and run scripts manually.

make shell
# Now inside the container:
python parking_enforcement.py  # CPP example
python delivery_route.py       # TSP example

Option B - Quick execution (run directly):

Best for one-off runs without entering the container.

docker compose run --rm app python parking_enforcement.py
docker compose run --rm app python delivery_route.py

Option 2: Virtual Environment

# System dependencies (Ubuntu/Debian)
sudo apt-get install gdal-bin libgdal-dev libgeos-dev libproj-dev libspatialindex-dev

# Setup
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# Verify installation
python verify_install.py

# Run examples
python parking_enforcement.py  # CPP - cover all streets
python delivery_route.py       # TSP - visit points

See QUICKSTART.md for detailed installation instructions.

๐Ÿ“ Project Structure

route-optimizer/
โ”œโ”€โ”€ algorithms/                    # ๐Ÿงฎ Algorithm implementations
โ”‚   โ”œโ”€โ”€ base.py                   # Abstract base solver
โ”‚   โ”œโ”€โ”€ cpp/                      # Chinese Postman Problem
โ”‚   โ”‚   โ””โ”€โ”€ solver.py
โ”‚   โ””โ”€โ”€ tsp/                      # Traveling Salesman Problem
โ”‚       โ””โ”€โ”€ solver.py
โ”œโ”€โ”€ route_optimizer.py             # Main RouteOptimizer class
โ”œโ”€โ”€ config_loader.py               # JSON configuration loader
โ”œโ”€โ”€ zone_config.json               # Example configuration
โ”œโ”€โ”€ parking_enforcement.py # CPP example (cover all streets)
โ”œโ”€โ”€ delivery_route.py      # TSP example (visit points)
โ”œโ”€โ”€ verify_install.py              # Verify dependencies
โ”œโ”€โ”€ algorithms.md                  # Algorithm documentation
โ”œโ”€โ”€ Dockerfile                     # Docker setup
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ Makefile
โ””โ”€โ”€ requirements.txt

๐ŸŽฏ Quick Start

Basic Usage

from route_optimizer import RouteOptimizer, Zone
from shapely.geometry import Polygon
from datetime import time

# Define your area
zone = Zone(
    name="downtown",
    polygon=Polygon([
        (-73.99, 40.73), (-73.98, 40.73),
        (-73.98, 40.74), (-73.99, 40.74),
    ]),
    start_time=time(8, 0),
    end_time=time(18, 0),
    weekdays=[0, 1, 2, 3, 4],
    color="gold"
)

# Create optimizer
optimizer = RouteOptimizer(
    bbox=(-74.00, 40.72, -73.97, 40.75),
    start_point=(40.735, -73.985),
    zones=[zone]
)

# Download street network
optimizer.download_street_network()
optimizer.label_zones()

# Solve with CPP (cover all streets)
route, distance = optimizer.solve_cpp("full")

# Or solve with TSP (visit all intersections)
route, distance = optimizer.solve_tsp("full")

Using JSON Configuration

from config_loader import load_configuration
from route_optimizer import RouteOptimizer

bbox, start_point, zones, _ = load_configuration('zone_config.json')
optimizer = RouteOptimizer(bbox, start_point, zones)

๐Ÿงฎ Choosing the Right Algorithm

Your Problem Algorithm Method
Cover all streets in an area CPP solve_cpp()
Visit specific points TSP solve_tsp()
Patrol/sweep entire neighborhood CPP solve_cpp()
Deliver packages to addresses TSP solve_tsp()
Inspect all roads CPP solve_cpp()
Pick up items at locations TSP solve_tsp()

๐Ÿ“š See algorithms.md for detailed algorithm documentation.

๐Ÿ“Š Example Use Cases

1. Street Sweeping / Snow Plowing (CPP)

Cover every street in the city with minimum repeated travel.

route, distance = optimizer.solve_cpp("full")
print(f"Route covers all streets in {distance/1000:.1f} km")

2. Delivery Route (TSP)

Visit specific delivery points efficiently.

delivery_points = [node1, node2, node3, node4]
route, distance = optimizer.solve_tsp("full", nodes_to_visit=delivery_points)

3. Parking Enforcement Patrol (CPP)

Ensure all parking meters are checked.

route, distance = optimizer.solve_cpp("downtown_zone")
optimizer.export_results({"patrol": (route, distance)}, "patrol_routes.xlsx")

4. Utility Meter Reading (TSP)

Visit all meter locations.

route, distance = optimizer.solve_tsp("full")
detailed_route = solver.get_detailed_route(route)  # Include intermediate nodes

โš™๏ธ Configuration

Zone Definition (JSON)

{
  "configuration": {
    "bbox": {"west": -74.00, "south": 40.72, "east": -73.97, "north": 40.75},
    "start_point": {"latitude": 40.735, "longitude": -73.985}
  },
  "zones": [
    {
      "name": "downtown",
      "polygon": [[-73.99, 40.73], [-73.98, 40.73], [-73.98, 40.74], [-73.99, 40.74]],
      "schedule": {"start": "08:00", "end": "18:00"},
      "weekdays": [0, 1, 2, 3, 4],
      "color": "gold"
    }
  ],
  "route_types": {
    "full": {"zones": ["downtown", "midtown"]},
    "morning": {"zones": ["downtown"]}
  }
}

Getting Coordinates

  1. Google Maps: Right click โ†’ "What's here?"
  2. OpenStreetMap: Shift+Click
  3. GeoJSON.io: Draw polygon and export

โš ๏ธ Format: [longitude, latitude] (reversed from typical lat/lon!)

๐Ÿ“ˆ Output

Excel Export

results = {
    "full_coverage": optimizer.solve_cpp("full"),
    "delivery_route": optimizer.solve_tsp("full", nodes_to_visit=points)
}
optimizer.export_results(results, "routes.xlsx")

Output sheets:

  • Summary: Distance comparison for all routes
  • Per-route sheets: Sequence, Node_ID, Latitude, Longitude, Zone

Visualization

optimizer.visualize_zones(save_path="map.png")
optimizer.visualize_route(route, graph, "My Route", save_path="route.png")

๐Ÿ”ง Extending with New Algorithms

The system is built for extensibility. To add a new algorithm:

# algorithms/vrp/solver.py
from algorithms.base import BaseSolver

class VRPSolver(BaseSolver):
    name = "Vehicle Routing Problem"
    description = "Multiple vehicles, capacity constraints"
    use_cases = ["Fleet delivery", "Logistics"]
    
    def solve(self):
        # Your implementation
        route = [...]
        distance = self.calculate_route_distance(route)
        return route, distance

Then register in algorithms/__init__.py.

๐Ÿ”ง Troubleshooting

Issue Solution
Network doesn't download Check bbox coordinates and internet
"Not strongly connected" โœ… Handled automatically
Empty results Verify zone polygons cover the area
Import errors Install dependencies: pip install -r requirements.txt

๐Ÿ“š Documentation

๐Ÿ”œ Roadmap

  • VRP (Vehicle Routing Problem) - multiple vehicles
  • CVRP (Capacitated VRP) - vehicle capacity constraints
  • Time windows support
  • Interactive web interface
  • Google Maps/Mapbox integration
  • Real-time traffic consideration

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add your algorithm in algorithms/your_algorithm/
  4. Submit a pull request

๐Ÿ“„ License

MIT License - see LICENSE

๐Ÿ‘ฅ Author

Developed by Data Crew Consulting


Version: 1.0.0
Last updated: December 2025

About

Python-based route optimization for urban operations, helping reduce fuel usage and improve coverage efficiency through data-driven planning.

Topics

Resources

Stars

33 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages