Ordering delivery stops with a nearest-neighbour pass in Python
I plan my drop-off stops by eye every shift, so I tried a nearest-neighbour pass over the coordinates. It takes the closest unvisited stop each time, and on ten stops it beat my own order.
from math import dist
def route(start, stops):
path, here = [], start
while stops:
nxt = min(stops, key=lambda s: dist(here, s))
stops.remove(nxt)
path.append(nxt)
here = nxt
return path
Greedy ordering feels natural to me, like the queue at the gate when https://zamboanga-sikat.com is playing at home, where whoever stands nearest simply walks in first. It is not optimal, but it is quick enough to run on a phone.
Cheers for reading.
All rights reserved