All Projects

Route Optimizer App

Live Demo
Role
Full-Stack & Algorithm Engineering
Read
3 min read
Table of Contents
  1. Project Overview
  2. Problem Statement
  3. Solution Architecture
  4. Technical Deep Dives
    1. Algorithmic Optimization (2-opt & Or-opt Heuristics)
    2. Geospatial Engine & OSRM Integration
    3. Asynchronous Compute & Main-Thread Protection
  5. Algorithm & Benchmarks
  6. Architecture Trade-Offs
  7. Project Results

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:

(n − 1)! / 2

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.

Live architecture & logic flow
Service Data store External
coords N×N matrix optimal tour Waypoint Coordinates OSRM Distance Matrix 2-opt & Or-opt Engine rAF Batch Scheduler Leaflet Polyline Render
Click or focus any node to inspect architectural details.
Interactive 2-Opt Route Optimizer
Dist: 0 km Saved: 0.0% Crossings: 0
Live 2-opt local search heuristic untangling a multi-stop vehicle delivery route. Pairs of intersecting edges are reversed iteratively until reaching local optimality.

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 TSP
Time: O(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 calls
Time: O(N²) Space: O(N²)

Caches symmetric road-network travel times across all heuristic passes.

Architecture Trade-Offs

Tour Optimization Strategy

Chosen: Nearest-Neighbor + 2-opt / Or-opt Over: Exact Held-Karp DP Solver

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

Chosen: Public OSRM Table API + Leaflet Over: Proprietary Commercial Routing SDK

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.

Keyboard Shortcuts

Global Navigation

  • Open Command Palette Cmd / CtrlK
  • Toggle Bruce's AI Assistant Shell ~or`
  • Show keyboard shortcuts ?
  • Close active modal or dialog Esc

Case Studies & Diagrams

  • Jump to next article section J
  • Jump to previous article section K
  • Inspect focused architecture node Enter / Space

Interactive Shell

  • Autocomplete commands, routes, & slugs Tab
  • Cycle command history ↑↓