Quickstart

Vehicle Routing Problem (VRP)

Here’s an example JSON payload. When sent to the routing engine, these instructions will request the optimal route for a single vehicle, vehicle_1, starting at Killarney, East Vancouver and ending at VanDusen Botanical Gardens that needs to visit locations ABC-123, DEF-456 and GHI-789 in some order. The time windows, durations and location (latitude and longitude) of each visit are specified with the JSON object of each visit.

Create VRP Request

POST https://routing-engine.afi.io/vrp-long

The vrp-long endpoint finds an optimal assignment of visits to vehicles that minimizes the overall travel distance of the vehicle fleet while meeting constraints (capacities, time windows, shift times etc). This endpoint can solve both the Traveling Salesman (single vehicle) and Vehicle Routing (multiple vehicles) Problems.

Headers

JSON
{ 
  "job_id": "141df046-2bb7-4a3c-93a5-1253274673a5"
}
JSON
{
	"visits": {
		"ABC-123": {
			"location": {
				"name": "Brassneck Brewery",
				"lat": 49.2657714,
				"lng": -123.1017201
			},
			"start": "18:00",
			"end": "19:00",
			"duration": 30,
			"load": 1
		},
		"DEF-456": {
			"location": {
				"name": "Zakkushi on Denman",
				"lat": 49.2912,
				"lng": -123.1384817
			},
			"start": "18:00",
			"end": "20:00",
			"duration": 45,
			"load": 1
		},
		"GHI-789": {
			"location": {
				"name": "Granville Island Brewing",
				"lat": 49.2709294,
				"lng": -123.13623
			},
			"start": "20:00",
			"end": "21:00",
			"duration": 15,
			"load": 1
		}
	},
	"fleet": {
		"vehicle_1": {
			"start_location": {
				"id": "driver_start",
				"name": "Killarney, East Vancouver",
				"lat": 49.2287301,
				"lng": -123.0421047
			},
			"end_location": {
				"id": "driver_end",
				"name": "VanDusen Botanical Gardens",
				"lat": 49.2385564,
				"lng": -123.1309102
			},
			"shift_start": "17:00",
			"shift_end": "23:00",
			"capacity": 10
		}
	},
	"options": {
		"polylines": true
	}
}

The /vrp-long endpoint will trigger a long-running background task on our servers. This might take a while, so instead of returning the route solution immediately, the vrp-long endpoint returns a job_id:

JSON
{ 
  "job_id": "141df046-2bb7-4a3c-93a5-1253274673a5"
}

which you can use to obtain the route solution when the background task is complete. To get the full route solution, use the job_id and make a GET request to the following endpoint:

Retrieve route solution

GET https://routing-engine.afi.io/jobs/141df046-2bb7-4a3c-93a5-1253274673a5

JSON
{
	"id": "6de367cc-2b3f-42db-a945-14b8888ec916",
	"status": "finished",
	"input": {
		"visits": {
			"ABC-123": {
				"location": {
					"name": "Brassneck Brewery",
					"lat": 49.2657714,
					"lng": -123.1017201,
					"id": "ABC-123"
				},
				"start": "18:00",
				"end": "19:00",
				"duration": 30,
				"load": 1
			},
			"DEF-456": {
				"location": {
					"name": "Zakkushi on Denman",
					"lat": 49.273423,
					"lng": -123.1275766,
					"id": "DEF-456"
				},
				"start": "18:00",
				"end": "20:00",
				"duration": 45,
				"load": 1
			},
			"GHI-789": {
				"location": {
					"name": "Earnest Ice Cream",
					"lat": 49.2493479,
					"lng": -123.0944052,
					"id": "GHI-789"
				},
				"start": "20:00",
				"end": "21:00",
				"duration": 15,
				"load": 1
			}
		},
		"fleet": {
			"vehicle_1": {
				"start_location": {
					"id": "home_start",
					"name": "Metrotown, Burnaby BC",
					"lat": 49.2287301,
					"lng": -123.0421047
				},
				"end_location": {
					"id": "home_end",
					"name": "Metrotown, Burnaby BC",
					"lat": 49.2287301,
					"lng": -123.0421047
				},
				"shift_start": "17:00",
				"shift_end": "23:00",
				"capacity": 10
			}
		},
		"options": {
			"polylines": true
		}
	},
	"output": {
		"status": "completed",
		"fitness": 0,
		"unserved": [],
		"solution": {
			"vehicle_1": [{
				"location_id": "home_start",
				"location_name": "Metrotown, Burnaby BC",
				"arrival_time": "17:00",
				"finish_time": "17:59",
				"distance": 0
			}, {
				"location_id": "ABC-123",
				"location_name": "Brassneck Brewery",
				"arrival_time": "17:59",
				"finish_time": "18:29",
				"distance": 5976,
				"duration": 30,
				"travel_mins": 11.96,
				"waiting_mins": 48.04,
				"working_mins": 41.96
			}, {
				"location_id": "DEF-456",
				"location_name": "Zakkushi on Denman",
				"arrival_time": "18:34",
				"finish_time": "19:19",
				"distance": 2061,
				"duration": 45,
				"travel_mins": 4.12,
				"waiting_mins": 0,
				"working_mins": 49.12
			}, {
				"location_id": "GHI-789",
				"location_name": "Earnest Ice Cream",
				"arrival_time": "19:59",
				"finish_time": "20:14",
				"distance": 3601,
				"duration": 15,
				"travel_mins": 7.21,
				"waiting_mins": 33.67,
				"working_mins": 22.21
			}, {
				"location_id": "home_end",
				"location_name": "Metrotown, Burnaby BC",
				"arrival_time": "20:23",
				"distance": 0
			}]
		},
		"polylines": {
			"vehicle_1": ["i_nkHft~mV@h@OJgA@ApCub@KUEs@WeBeBUYa@pKSnCUdBQz@_C~Ig@~AiJpWwBtF}GxRyCbIiH~SqJvVy@hC_CxGeM`]wBlGsEzNoAtCyBtEi@pAwTlm@cDbJ{ElM_BvEYjAMx@c@dEU`A]`AcFnKmBvDmB`CcEdEsDrDoDfD}CdCyArAGLw@n@gEGSNA~Cq@Ap@@CnFtDHG~JE`KYrh@E~CI`BeAAqYd@{@E{\\i@wAGm@NMHINGZ?RLj@hCzDTp@Pz@F|@?rA]nECpAB|@HtAL|@fAdHlDzRJnAFbDI~@UbAa@jAcAbBLVb@r@c@s@v@sAn@eBPq@R}AB_BQ}B{Fg]K{@GmAYBqEcDeA{@WK|BqDbEiHj@}@l@k@r@a@XKt@KZAbY^tAK~MMRGjFBNcCD_DXsh@DaKF_KDcKRFxACdFJfIH^DrDFtIFRK|OVn@?ZC`@KlAo@z@Ojb@d@LwYD_PqBA?fA?gApB@b@{bBFsA?yKHsSzHkT|AqEvPkd@jIqU~CcJ|P{e@dCyGnBcGzBmILy@TmBT}BJJlByGLYr@VTDtb@J@qCfAANKAi@"]
		},
		"num_unserved": 0,
		"total_distance": 16074.178624547105,
		"total_distance_miles": 9.988031532355755,
		"total_travel_time": 1929.6733042673586,
		"total_cost": 16074.178624547105,
		"total_operation_time": 12232.617166965356,
		"total_duration_time": 90,
		"summary": {
			"vehicle_1": {
				"distance": 16075,
				"travel_mins": 32.16,
				"waiting_mins": 81.72,
				"working_mins": 122.16,
				"working_percentage": 33.93,
				"visits": 3,
				"service_mins": 90
			}
		},
		"report": {
			"distance": 16075,
			"travel_mins": 32.16,
			"waiting_mins": 81.72,
			"working_mins": 122.16,
			"working_percentage": 33.93,
			"visits": 3,
			"service_mins": 90
		}
	},
	"fleet": 1,
	"visits": 3,
	"started_at": "2022-08-14T05:13:31.944Z",
	"finished_at": "2022-08-14T05:13:31.957Z",
	"type": "VRP",
	"network": {
		"home_start": {
			"id": "home_start",
			"name": "Metrotown, Burnaby BC",
			"lat": 49.2287301,
			"lng": -123.0421047
		},
		"home_end": {
			"id": "home_end",
			"name": "Metrotown, Burnaby BC",
			"lat": 49.2287301,
			"lng": -123.0421047
		},
		"ABC-123": {
			"name": "Brassneck Brewery",
			"lat": 49.2657714,
			"lng": -123.1017201,
			"id": "ABC-123"
		},
		"DEF-456": {
			"name": "Zakkushi on Denman",
			"lat": 49.273423,
			"lng": -123.1275766,
			"id": "DEF-456"
		},
		"GHI-789": {
			"name": "Earnest Ice Cream",
			"lat": 49.2493479,
			"lng": -123.0944052,
			"id": "GHI-789"
		}
	},
	"processing_time": "0.004"
}

When the job is finished, the status is set to finished and the solution to the request is now in the output object of the response. You can view the input, output, running time and error messages (if any) on the dashboard at https://dashboard.afi.io/requests. The dashboard also allows you to add or revoke API keys and generate an automated postman collection that you can use for testing.

The dashboard has a link (click the map icon on the right) to an interactive map based interface that you can use to explore your route solution in greater detail.

The map can also be accessed directly with this link: https://afi-viewer.herokuapp.com/jobs/141df046-2bb7-4a3c-93a5-1253274673a5?endpoint=https://routing-engine.afi.io. Both the dashboard and map interface come included with the out of the box installation of our routing engine and route optimization API (contact us for demo and pricing).

Last updated