Route Optimizer App
Table of Contents
Project Overview
The Route Optimizer is a delivery routing and Traveling Salesperson Problem (TSP) web application. Designed for urban couriers and multi-stop delivery workflows, the application computes fast, turn-by-turn road routes across complex geographic waypoints using interactive map interfaces and local search heuristics.
Problem Statement
Multi-stop delivery routing is a classic instance of the NP-hard Traveling Salesperson Problem. For an n-stop route, the search space of possible permutations is:
A naive sequence of just 12 stops yields nearly 20 million possible permutations, leading to exponential computational bottlenecks and inefficient fuel consumption if ordered arbitrarily. Standard commercial routing APIs charge prohibitive per-waypoint fees and often lack lightweight, interactive stop reordering and local computational heuristics.
Solution Architecture
The application separates real-world road network matrix computation from client-side heuristic optimization.
Technical Deep Dives
Algorithmic Optimization (2-opt & Or-opt Heuristics)
- Rather than executing an exact brute-force search (
O(n!)), the application implements 2-opt and Or-opt approximation algorithms. - 2-opt Loop: Evaluates edge pairs (A, B) and (C, D). If reversing the sub-sequence between B and C yields a shorter total distance (
dist(A,C) + dist(B,D) < dist(A,B) + dist(C,D)), the sub-tour is inverted. - Or-opt Relocation: Consecutive segments of 1, 2, or 3 stops are excised and re-inserted elsewhere in the sequence if it shortens total circuit cost, escaping local 2-opt minimums without the complexity of simulated annealing.
Geospatial Engine & OSRM Integration
- Road network geometry is retrieved through the Open Source Routing Machine (OSRM) Table Service, which returns an N × N duration/distance matrix rather than simple Euclidean (Haversine) lines.
- Polyline paths are decoded client-side and dynamically bound to Leaflet layers, ensuring route curvature follows actual physical streets, one-way systems, and highway ramps.
Asynchronous Compute & Main-Thread Protection
- To ensure smooth map interactions during heuristic computation on larger routes (N ≥ 30), the search loop is broken into asynchronous execution batches using
requestAnimationFrame, preventing browser tab freezing during permutation evaluation.
Algorithm & Complexity Benchmarks
2-Opt / Or-Opt Local Search
vs O(N!) exact TSPO(N²) Space: O(N) Converges in <120ms for 40+ waypoints while cutting route length by 20–35%.
OSRM Matrix Lookup
vs O(N²) live HTTP callsO(N²) Space: O(N²) Caches symmetric road-network travel times across all heuristic passes.
Architecture Trade-Offs
Tour Optimization Strategy
Exact exponential solvers stall past ~15 stops; 2-opt/Or-opt converges in under 120ms for 40+ waypoints while cutting circuit distance by 20–35%.
Road Distance Matrix
Eliminates per-request API billing while computing real driving-time matrices over actual road geometry rather than straight-line Haversine estimates.
Project Results
Distance Reduction
Consistently produces 20% to 35% shorter total circuit distances compared to raw input ordering.
Sub-Second Execution
Runs 2-opt convergence in under 120ms for routes with up to 40 stops on standard consumer hardware.
Real-World Usability
Zero API subscription overhead by coupling public OSRM endpoints with custom client-side optimization logic.