|
| 1 | +# allocator: Optimally Allocate Geographically Distributed Tasks |
| 2 | + |
| 3 | +[](https://pypi.python.org/pypi/allocator) |
| 4 | +[](https://pepy.tech/project/allocator) |
| 5 | +[](https://github.com/geosensing/allocator/actions/workflows/ci.yml) |
| 6 | +[](https://geosensing.github.io/allocator/) |
| 7 | + |
| 8 | +**Allocator v1.0** provides a modern, Pythonic API for geographic task allocation, clustering, and routing optimization. |
| 9 | + |
| 10 | +How can we efficiently collect data from geographically distributed locations? Whether you're coordinating crowdsourced data collection, optimizing delivery routes, or planning field research, allocator provides the tools you need. |
| 11 | + |
| 12 | +## ✨ What's New in v1.0 |
| 13 | + |
| 14 | +- **🎯 Modern Python API** - Clean, intuitive interface with type hints |
| 15 | +- **📦 Unified CLI** - Single command with subcommands (`allocator cluster`, `allocator route`, `allocator assign`) |
| 16 | +- **🚀 Performance** - Optimized algorithms with NumPy and scikit-learn |
| 17 | +- **📊 Rich Results** - Structured results with metadata and easy export |
| 18 | +- **🔧 No Backward Compatibility** - Clean slate, standards-compliant design |
| 19 | + |
| 20 | +## Core Functionality |
| 21 | + |
| 22 | +**1. Clustering** 🎯 |
| 23 | +Group geographic points into balanced clusters for task allocation. |
| 24 | + |
| 25 | +**2. Routing** 🛣️ |
| 26 | +Find optimal paths through sets of locations (TSP solving). |
| 27 | + |
| 28 | +**3. Assignment** 📍 |
| 29 | +Assign points to closest workers/centers with distance-based sorting. |
| 30 | + |
| 31 | +## Quick Start |
| 32 | + |
| 33 | +### Installation |
| 34 | + |
| 35 | +```bash |
| 36 | +pip install allocator |
| 37 | +``` |
| 38 | + |
| 39 | +### Python API Example |
| 40 | + |
| 41 | +```python |
| 42 | +import allocator |
| 43 | +import pandas as pd |
| 44 | + |
| 45 | +# Load your geographic data |
| 46 | +data = pd.DataFrame({ |
| 47 | + 'longitude': [101.0, 101.1, 101.2, 101.3], |
| 48 | + 'latitude': [13.0, 13.1, 13.2, 13.3], |
| 49 | + 'location_id': ['A', 'B', 'C', 'D'] |
| 50 | +}) |
| 51 | + |
| 52 | +# Cluster locations into groups |
| 53 | +result = allocator.cluster(data, n_clusters=2, method='kmeans') |
| 54 | +print(f"Cluster labels: {result.labels}") |
| 55 | +print(f"Centroids: {result.centroids}") |
| 56 | + |
| 57 | +# Find optimal route through locations |
| 58 | +route = allocator.shortest_path(data, method='ortools') |
| 59 | +print(f"Optimal route: {route.route}") |
| 60 | +print(f"Total distance: {route.total_distance}") |
| 61 | + |
| 62 | +# Assign points to closest centers |
| 63 | +centers = pd.DataFrame({ |
| 64 | + 'longitude': [101.05, 101.25], |
| 65 | + 'latitude': [13.05, 13.25] |
| 66 | +}) |
| 67 | +assignments = allocator.assign_to_closest(data, centers) |
| 68 | +print(assignments.data) |
| 69 | +``` |
| 70 | + |
| 71 | +### CLI Example |
| 72 | + |
| 73 | +```bash |
| 74 | +# Cluster geographic points |
| 75 | +allocator cluster data.csv --clusters 3 --method kmeans --output clusters.csv |
| 76 | + |
| 77 | +# Find optimal route |
| 78 | +allocator route locations.csv --method ortools --output route.csv |
| 79 | + |
| 80 | +# Assign points to centers |
| 81 | +allocator assign points.csv centers.csv --output assignments.csv |
| 82 | +``` |
| 83 | + |
| 84 | +## Distance Metrics |
| 85 | + |
| 86 | +All functions support multiple distance calculation methods: |
| 87 | + |
| 88 | +- **euclidean** - Fast Euclidean distance (good for local areas) |
| 89 | +- **haversine** - Great circle distance accounting for Earth's curvature |
| 90 | +- **osrm** - Real road network distances via OSRM API |
| 91 | +- **google** - Google Maps distance matrix (requires API key) |
| 92 | + |
| 93 | +## Algorithms |
| 94 | + |
| 95 | +**Clustering:** |
| 96 | +- **K-means**: Fast, well-balanced clusters |
| 97 | +- **KaHIP**: Graph partitioning for highly balanced clusters (requires external install) |
| 98 | + |
| 99 | +**Routing (TSP):** |
| 100 | +- **OR-Tools**: Exact solutions for small problems, heuristics for larger ones |
| 101 | +- **Christofides**: 1.5-approximation algorithm (requires external install) |
| 102 | +- **OSRM**: Real-world routing via road networks |
| 103 | +- **Google**: Google Maps Directions API |
| 104 | + |
| 105 | +## Data Format |
| 106 | + |
| 107 | +Input data must be pandas DataFrames or CSV files with these columns: |
| 108 | + |
| 109 | +- **longitude**: Geographic longitude (required) |
| 110 | +- **latitude**: Geographic latitude (required) |
| 111 | +- Additional columns are preserved in results |
| 112 | + |
| 113 | +## Examples and Use Cases |
| 114 | + |
| 115 | +- **Field Research**: Optimize survey routes for maximum efficiency |
| 116 | +- **Delivery/Logistics**: Plan optimal delivery routes and territories |
| 117 | +- **Crowdsourcing**: Assign tasks to workers based on geographic proximity |
| 118 | +- **Emergency Response**: Allocate resources to incident locations |
| 119 | +- **Urban Planning**: Analyze spatial patterns and optimize service locations |
| 120 | + |
| 121 | +## API Reference |
| 122 | + |
| 123 | +### Main Functions |
| 124 | + |
| 125 | +```python |
| 126 | +# High-level functions |
| 127 | +allocator.cluster(data, n_clusters=3, method='kmeans', distance='euclidean') |
| 128 | +allocator.shortest_path(data, method='ortools', distance='euclidean') |
| 129 | +allocator.assign_to_closest(points, workers, distance='euclidean') |
| 130 | + |
| 131 | +# Specific algorithms |
| 132 | +allocator.kmeans(data, n_clusters=3, distance='euclidean') |
| 133 | +allocator.kahip(data, n_clusters=3) # Requires KaHIP installation |
| 134 | +allocator.tsp_ortools(data, distance='euclidean') |
| 135 | +allocator.tsp_christofides(data) # Requires Christofides installation |
| 136 | +``` |
| 137 | + |
| 138 | +### Result Types |
| 139 | + |
| 140 | +- `ClusterResult`: Labels, centroids, convergence info, metadata |
| 141 | +- `RouteResult`: Route order, total distance, metadata |
| 142 | +- `SortResult`: Sorted assignments with distances, metadata |
| 143 | + |
| 144 | +## Requirements |
| 145 | + |
| 146 | +- Python 3.11+ |
| 147 | +- Core: pandas, numpy, matplotlib, networkx, scikit-learn |
| 148 | +- CLI: click, rich |
| 149 | +- Optional: ortools, googlemaps, requests (for OSRM) |
| 150 | + |
| 151 | +## Documentation |
| 152 | + |
| 153 | +Complete documentation: https://geosensing.github.io/allocator/ |
| 154 | + |
| 155 | +## Development |
| 156 | + |
| 157 | +This project uses modern Python development practices: |
| 158 | + |
| 159 | +- **uv** for dependency management |
| 160 | +- **pytest** for testing |
| 161 | +- **black** and **isort** for code formatting |
| 162 | +- **ruff** for linting |
| 163 | +- **GitHub Actions** for CI/CD |
| 164 | + |
| 165 | +## Contributing |
| 166 | + |
| 167 | +We welcome contributions! Please see our [Contributor Code of Conduct](http://contributor-covenant.org/version/1/0/0/). |
| 168 | + |
| 169 | +## Authors |
| 170 | + |
| 171 | +Suriyan Laohaprapanon and Gaurav Sood |
| 172 | + |
| 173 | +## License |
| 174 | + |
| 175 | +MIT License - see [LICENSE](https://opensource.org/licenses/MIT) for details. |
0 commit comments