Skip to content
oslew
Get a free key

APIs

Route optimisation API

A route optimisation API puts a list of stops into the order that takes the least time to travel. oslew solves this for one vehicle — the travelling salesman problem — for up to 500 stops, as a round trip back to the start or a one-way path, and returns the order, the time of every leg and the routed path. Small sets are solved exactly and larger ones by local improvement. Time windows, vehicle capacities and multi-vehicle fleets are not modelled.

Last reviewed

The request is a list of stops, GET /v1/optimize?points=lat,lon;lat,lon;…, between 2 stops and 500 stops. The answer is the order to visit them, the time of each leg, the total, and the road path joining them. It is the endpoint for a courier’s round, a technician’s calls for the day or a sales rep’s visits — anything where one vehicle must reach every stop and the only question is the sequence.

Choosing where the run starts and ends

Three parameters shape the trip. roundtrip defaults to true: start at the first point you sent, visit the rest, and come back. With roundtrip=false, source and destination decide the ends.

roundtrip source destination The run
true — — Starts at the first point, visits the others, returns to it
false first any Starts at the first point and ends wherever is quickest — a courier who does not go back
false first last Starts at the first point and ends at the last — depot to home with calls between
false any last Ends at the last point and starts wherever is quickest
false any any The quickest path through every point, from anywhere to anywhere

An open path is not simply the round trip with its last leg removed. The best way to end somewhere is not always the best way to come back from it, so the order can change when the return is dropped.

How the order is found

The optimiser first builds a travel-time matrix between every pair of stops on the chosen network — the same computation the distance matrix exposes on its own. For a small number of stops it then tries every order and returns the exact best. For more, it builds a first order by nearest neighbour (from the start, always go to the closest stop not yet visited; with a free start, it tries every starting stop and keeps the best) and improves it with two kinds of move: 2-opt, which reverses a stretch of the order, and Or-opt, which lifts one, two or three consecutive stops and puts them elsewhere. It repeats until no single move helps.

The result past the exact range is a local optimum: no single reversal or relocation improves it. It is deterministic — the same input gives the same order — and it is not proven best. Why a proof is out of reach at these sizes, and what a solver gives up to be fast, is the subject of TSP vs VRP.

Reading the answer

Field What it holds
order Indices into your points in visiting order; a round trip repeats the start at the end
waypoints Your points, as sent, rearranged into that order
legs_s Each leg’s time in whole seconds
duration_s The total of the legs
nearest_neighbour_s The unimproved nearest-neighbour order’s time, for comparison
distance_m Metres along the routed legs
geometry Every leg’s path, one after another, as [lat, lon] pairs
engine Which engine routed the legs

The times come from the matrix, which places each stop at its nearest junction; the distance and path come from routing each leg, which starts at the nearest point along a road. The two can disagree by a little at every stop, so distance_m divided by duration_s is not a speed to rely on. geometry is one continuous list with no marker where one leg ends and the next begins. When a driver’s map needs a separate line per leg, ask for the order with geometry=false and then route each consecutive pair — the last-mile delivery workflow does exactly that.

What it does not model

This is a travelling salesman solver, and it says so. It has no time windows, no vehicle capacities, no driver shifts or breaks, no priorities, no pickup-and-delivery pairs and no second vehicle. Service time at each stop is not part of the search, which does no harm: a fixed allowance per stop adds the same amount to every order, so the fastest order is unchanged and you add the allowance when computing arrival times.

For a fleet, or for any of those constraints, the work splits in two. Request a matrix covering the depot and every stop, and give it to a vehicle routing solver that models your constraints; it decides which vehicle serves which stops. Each vehicle’s list can then come back here for its order, or be routed leg by leg in the sequence the solver chose.

Traffic and timing

Leg times are free-flow, like every oslew route: the posted limit or a class speed, nothing for signals or congestion. Where an Enterprise operator has applied a traffic feed, traffic=live times the matrix — and so the order, legs_s and duration_s — on those current speeds, but the legs are still routed on the release’s speeds, so distance_m and geometry describe the free-flow path. engine names the engine that routed the legs, and reads base/hierarchy even when the times came from a feed.

What it costs

One request against the routing rate, however many stops it orders. On the Routing plan, $299 a month, that rate is 5 requests a second sustained, burst 25; routing each leg afterwards is one request per leg on the same rate. The pricing page lists the faster plans.

Limits, stated plainly

  • One vehicle, between 2 stops and 500 stops per request.
  • Exact only for small sets; above that, a deterministic local optimum.
  • One unreachable pair fails the whole request with HTTP 400.
  • A round trip always starts at the first point you sent.
  • Answers derive from road data licensed ODbL-1.0; see the licensing page before distributing stored plans.

Bottom line

Use this endpoint when one vehicle has to reach a list of stops and the order is the whole problem. Compare duration_s with nearest_neighbour_s to show what the ordering saved, route each leg for per-stop lines and times, and reach for a matrix and your own solver the moment a second vehicle or a time window appears.

Questions

Can the optimisation API plan routes for several vehicles?

No. It orders the stops of one vehicle. For a fleet, request a travel-time matrix covering the depots and every stop, give it to a vehicle routing solver of your choice to divide the stops between vehicles, and then send each vehicle’s share back here for its order, or route its legs in the order the solver chose.

Does it support delivery time windows?

No. There are no time windows, capacities, driver breaks or stop priorities. Time spent at each stop is not modelled either, but a fixed allowance per stop does not change which order is fastest, so add it to the clock yourself when you work out arrival times.

Is the order it returns the best possible one?

For small sets, yes: every order is tried. Beyond that, a nearest-neighbour order is improved by 2-opt and Or-opt moves until neither finds anything faster, which gives a local optimum — usually very good, and not proven best. The same points in the same order always return the same answer.

What does nearest_neighbour_s tell me?

How long the obvious order — always drive to the closest stop not yet visited — would have taken before improvement. The gap between it and duration_s is what the optimisation bought. On small sets that are solved exactly no such order is built and the two are reported equal, so the gap says nothing there.

What happens if one stop cannot be reached?

The whole request fails with HTTP 400, because no order can visit every stop. Route from the start to each stop, or request a matrix of the same points and look for its nulls, to find the unreachable one; then correct its position or leave it out and try again.

Browse every page →