Traveling Salesman Problem
- Last UpdatedMar 13, 2025
- 7 minute read
Let's explore solving the traveling salesman problem: given a set of locations, find the shortest route to visit each location and return to the original starting point.
Gather data to create a request
For the request URL we have:
POST https://tourplanning.hereapi.com/v3/problems
To calculate the results, we need to provide:
- the starting point
- the coordinates of the locations we want to visit
- information about our vehicle
Authentication
Tour Planning API supports authentication using both API Key
and Bearer Token
. To get started for free, you can create an account and get your API Key. For other authentication methods, see the Identity and Access Management Developer Guide.
Coordinates of locations to visit
The the locations we want to visit (jobs) are listed below in no particular order. Our goal is to find the optimal order to visit all of the following locations:
A: 47.620835, -122.333295
B: 47.623879, -122.335212
C: 47.620571, -122.339785
D: 47.623690, -122.333005
E: 47.622017, -122.336911
Our starting point (depot) is labeled S
is located at 47.623231,-122.340168
or 872 Republican Street, Seattle, WA. We will specify the coordinates of our depot later along with our fleet.
Sometimes we want to arrive during specific time windows that certain locations are open, in this example, we are not specifying time constraints that would impact our route. But if we wanted to set availability for depots, we could do that using the times
parameter.
Another parameter that we're not going to explore in this example is demand
. With this parameter, you can specify an array of flexible units that maps to a vehicle's capacity. For example, a demand
array of [2,1] for a delivery job can mean that two passengers and one suitcase will be dropped off. Another example would be setting [3] for a pick-up job, and that can mean 3 packages will be picked up. Demand in a job corresponds to capacity
in the fleet. In this example, we've set demand and capacity to [0] because we are not concerned about tracking shipments and capacity.
Information about the vehicle
The Tour Planning API can calculate routes for both trucks and normal passenger cars. It also provides information about the cost of the trip based on the details about the vehicle. Because of this, you have to provide some information about your fleet of vehicles upfront when you create a request.
Set up fleet profile
Each profile has to have a name. In general, all vehicles with more than 2 wheels and less than 3500 kg can typically share the same routing profile with type car, unless some of your cars or vans shall avoid toll roads and others not. In that case, you would need more than one profile. The vehicle type can be either set to car
, truck
, scooter
, bicycle
, pedestrian
, bus
or privateBus
. Trucks have driving restrictions and the route can be a little bit different to accommodate for things like height, weight, and turn limitations for trucks.
Set up vehicle details
Next, we need to set up vehicle details such as cost, profile information, and shift availability. The profile
is tied to the name of the profile that we defined earlier. It should not represent anything too specific about your vehicle due to privacy reasons.
With shifts
you can set the availability and the start and end location of your vehicles. For this example, we want to come back to our original starting point so we'll set the start and end location to the same coordinate.
The capacity allows you to define an array of unspecified units so for example, you can say use capacity [4,2] to show that your vehicle has capacity for 4 passengers and 2 suitcases. Capacity maps to demand
that we will set at the next step. For example, we won't worry about capacity because are not planning to accommodate any passengers or packages so we'll set capacity to [0].
When describing fleet, amount
represents the number of vehicles that you have that follow the same availability shift and same cost profile. For this example, we assume that we have only one vehicle so we set amount
to 1.
Create the request
Let's make the call to our API:
POST https://tourplanning.hereapi.com/v3/problems?apiKey=YOUR_API_KEY
And for the body, we have information about our jobs and fleet:
Evaluate the response
Below is the response showing the optimal order to visit each stop (C->E->A->D->B), the duration of the trip (504 seconds), the travel distance (2186 meters), and more:
For more information, see Submit a Vehicle Routing Problem to solve it synchronously and Submit a Vehicle Routing Problem to solve it asynchronously.