Optimize Trucking Routes

Product Used: Route Optimization Flexible API

NextBillion.ai’s Route Optimization API provides users with a high degree of customization by leveraging over 50+ parameters to closely emulate the real-world constraints of route planning. Users can make use of truck specific parameters to cater specifically to optimization problems involving trucks.

The optimization service takes into account all the variables specified in the input request and suggests optimal routes and task assignments to best meet the configured objective

To configure a Route Optimization request we will need to set up the locations involved, vehicles to be used and the tasks to be performed. Let’s address them one by one below.

Configure optimization request


First we define the locations object and add all the locations used in the problem along with a valid id. Following is the the locations object with all the coordinates to be used in this example:

2 "locations": {
3 "id": 1,
4 "location": [
5 "34.083950,-118.318640",
6 "34.054927,-118.323726",
7 "34.075525,-118.361588",
8 "34.076350,-118.338519",
9 "34.090425,-118.338933",
10 "34.076646,-118.376969",
11 "34.094986,-118.300885",
12 "34.018780,-118.317919",
13 "33.996658,-118.261708",
14 "34.059244,-118.376969",
15 "34.057106,-118.361326"
16 ]
17 }

Jobs & Shipments

We define a bunch of tasks that need to be performed in this example. Following are the constraints that we set for these tasks:

  • A unique id for each task

  • location_index for each task

  • Specify the schedule of tasks. This is done by adding time_windows within which a task must be completed. We have added a 15 min time window for all tasks for the sake of simplicity, but the Route Optimization V2 is fully capable of handling much complex task schedules involving multiple time_windows for each task.

  • skills needed to perform each task

  • pickup and delivery amounts for all tasks

  • The actual time taken to complete the task once the driver/vehicle is at the task’s location i.e. the service time for each task. We have added a uniform service time of 2 mins for all tasks, but the users can use varying service times instead.

Let’s take a look at the jobs and shipments JSONs after the above properties are configured:

2 "jobs":"[...]",


Next, we add 2 trucks that are responsible for fulfilling the above tasks which are to be performed at the locations mentioned above. To describe the trucks and their properties we add:

  • A unique ID for each truck

  • Truck’s or the driver’s shift timings using the time_window parameter

  • capacity of the load that the truck can take

  • Truck’s depot, if any.

  • start_index to denote the point from where the trucks would start. If this is left blank, the algorithm will assume the first task’s location as the start point of the truck.

  • skills of the driver or the vehicle to ensure appropriate tasks are only assigned to the respective truck.

Following is the vehicles JSON with all the above properties configured:

2 "vehicles":"[...]",


Next, we add a depot that we want to assign to one of the trucks. The assigned truck would start from the depot’s location. We assign an ID and a location_index to the depot:

1"depots": [
2 {
3 "description":"Depot 1",
4 "id":0,
5 "location_index":9
6 }
7 ]


Lastly we set the optimization objective and other routing constraints for the above optimization problem. We will set the following constraints:

  • mode option set as truck.

  • Objective of optimization will be configured to minimize the total travel distance, using the travel_cost attribute.

  • Define the truck dimensions using the truck_size & truck_weight option.

  • Configure the typical traffic conditions by providing the departure time for the trucks using traffic_timestamp.

Following is the options JSON with above properties configured:

2 "options": {
3 "objective": {
4 "travel_cost": "distance"
5 },
6 "routing": {
7 "mode": "truck",
8 "truck_size": "210, 210, 320",
9 "truck_wieght": 12000,
10 "traffic_timestamp": 1693387800
11 }
12 }

Route Optimization POST request

All the above components are now combined and submitted as a single JSON to the Route Optimization endpoint host:

1curl --location 'https://api.nextbillion.io/optimization/v2?key=<your_api_key>' --header 'Content-Type: application/json' --data '{...}'

Route Optimization POST response

Once the above POST request is successfully submitted, a response similar to the following is received:

2 "id": "12435679cc9cfc52e5b61b9171bb34b3",
3 "message": "Optimization job created",
4 "status": "Ok"

Retrieve optimization solution

Capture the task id from the POST response and use it in a subsequent GET request to retrieve the optimized solution asynchronously:

Route Optimization GET request

1curl --location 'https://api.nextbillion.io/optimization/v2/result?id=12435679cc9cfc52e5b61b9171bb34b3&key=<your_api_key>'

Route Optimization GET response

You would see the solution with all the suggested routes and the trucks that were assigned to each of the routes to take care of all the tasks. Some tasks might remain unassigned and the optimizer will also provide the reason for keeping the task unassigned.

2 "description": "Test",
3 "result": "{...}"

Visit Route Optimization API documentation to know more about the available features.