Route Optimization Engine

Same duty, shorter route.
Every time.

Route Optimization Engine re-sequences a crew's stops with Haversine distance, a Nearest-Neighbor seed tour, and 2-Opt local search — then hands back a shorter route and a ready-to-render map, in one call.

Haversine + 2-Opt Live Route Map Request / Response Crew Routing Screen
Haversine + 2-Opt

An approximate TSP
solver built for real crews

Every "Optimize Route" click runs the same three-step heuristic: measure real-world distance between every stop, build a fast greedy tour, then locally refine it until no swap shortens it further.

  • Haversine distance — great-circle km between any two lat/lng points, using Earth's real radius (6,371 km)
  • Nearest-Neighbor seed — starts at the origin hub, always jumps to the closest unvisited stop next
  • 2-Opt refinement — repeatedly reverses tour segments whenever that shortens total distance, undoing the seed tour's bad jumps
  • O(n²) — instant for the 2–15 stop routes a real field crew actually runs in a day
Open Crew Routing
totalapp.app / energy-crew-routing
Before — Unordered Stops
3
1
5
2
4
Total distance: 78.4 km
After — Optimized Order
1
2
3
4
5
Total distance: 51.2 km
−34.7% distance
Live Route Map

A map payload,
ready to render

The optimized tour comes back pre-packaged in AnyChart connector-map format — numbered points plus a flat lat/lng connector series — so the client never has to transform anything before drawing it.

  • map.points — origin + every stop, each with an order index for numbered pins
  • map.connector — a flat [lat,lng,lat,lng,...] array plus short/full tooltip text
  • Feeds the same shared route-map component the Fleet module already uses
  • "View on Map" stays independent — it draws the current stops without calling the optimizer at all
View a Route on the Map
totalapp.app / fleet-route-map
H 1 2 3 4
Route: Istanbul Hub → 4 stops · 51.2 km
Request / Response

One call in,
one route out

A single POST /api/en/crew-routing/optimize takes the route's stops and returns the re-sequenced list, the new total distance, and the ready-to-draw map — no side effects, no partial writes.

  • Send routeId, stops[], and an optional origin lat/lng
  • Get back optimizedStops, totalDistanceKm, and map in one response
  • No origin coordinates supplied? Defaults to Istanbul Hub automatically
  • Pure calculation — the engine never writes to disk; the client saves the result
Read the Full API Docs
POST /api/en/crew-routing/optimize
// request
{ "routeId": "rt_204",
  "stops": [ {...} x5 ] }
// response
{ "optimizedStops": [...],
  "totalDistanceKm": 51.2,
  "map": { points, connector } }
401 if unauthenticated · 400 on bad JSON
Crew Routing Screen

Built for
Energy & Utilities field crews

Crew Routing (Energy & Utilities → Field & Dispatch Operations) is the one screen that calls this engine. One "Optimize Route" click per route row does the rest.

  • Optimize Route — sends the row's stops to the engine, re-sequences them, and saves the new order + distance
  • View on Map — draws the route's current stops directly, independent of optimization
  • Server-side calculation keeps route data and the optimizer in the same backend layer
  • Swapping in a real Directions API later only touches this one engine file
Explore Energy & Utilities
totalapp.app / energy-crew-routing
RouteStopsDist.Action
Route — North Grid551.2km 🪄 Optimize
Route — Substation B322.7km View Map
Route — Meter Sweep764.9km 🪄 Optimize
3 active routes · Energy & Utilities → Field & Dispatch Operations