• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Re-Optimization: New Tasks

NextBillion.ai’s Route Optimization Flexible API offers a powerful re-optimization feature which allows the users to re-optimize a set of existing or new tasks by readjusting some variables, if needed.

Let’s suppose a business which has an unpredictable frequency of incoming orders (grocery delivery, courier services etc.) creates a route plan to execute the orders at a given point of time. By the time the plan is created, delivery vehicles are loaded with inventory, and drivers are contracted, another bunch of high priority orders is received. Now either the users can rebuild the entire optimization problem again and scrap the plan that was already in motion or just re-optimize the existing solution to accommodate the new priority orders with minimal or no changes to the original plan.

To re-optimize a routing plan the users need the previous solution created using the given constraints at the time. For successfully adding new orders/tasks to the re-optimized solution, users need to decide if the existing constraints of number of vehicles, shift timings or capacities of the vehicles, or the existing task time windows or any other variables need to be updated or not. The users are expected to rebuild the request using some of the above strategies while specifying all the new and existing tasks. Upon receiving a re-optimization request, the solver will try to accommodate the new tasks into the original solution without many changes. This way businesses can make quick or last-minute adjustments to their route planning without needing to make extravagant changes to the existing plans.

We cover this example through 3 steps - in the first step, we execute a basic route optimization request and get an original solution. In the next step, we review the new tasks received and re-model our original optimization request. In the last step, we use the solution attribute, and re-optimize our existing solution so that all tasks are assigned. Let’s start with building the original request.

Get Started

Readers would need a valid NextBillion API key to try this example out. If you don’t have one, please contact us to get your API key now!

Setup

Once you have a valid API Key, you can start setting up the components to be used in this example. Let’s take a look at them below. Jobs & Shipments We start by defining 6 jobs and 2 shipments. For these tasks we add:

  • A unique identifier for each task

  • Location indexes 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.

  • The actual time taken to complete the tasks once the driver/vehicle is at the task’s location i.e. the service time for each task.

  • Skills needed to perform each task

  • Pickup and Delivery amounts for all jobs

Let’s take a look at the jobs JSON after the above properties are configured:

1{
2 "jobs": [
3 {
4 "id": 1,
5 "location_index": 0,
6 "service": 120,
7 "pickup": [
8 3
9 ],
10 "skills": [
11 1
12 ],
13 "time_windows": [
14 [
15 1693386000,
16 1693386900
17 ]
18 ]

Vehicles

Next, we add 4 vehicles that are going to fulfill the tasks within the defined constraints. To describe the vehicles and their properties we add:

  • A unique ID for each vehicle

  • Vehicle shift time or the time window. We have added the same for both vehicles in the first step.

  • Capacity to denote the amount of load that the vehicle can take

  • Start_index to denote the point from where the vehicle would start

  • Skills for all vehicles

Once the vehicle and their properties are defined, the resulting vehicles JSON is:

1{
2 "vehicles": [
3 {
4 "id": 1,
5 "start_index": 10,
6 "skills": [
7 1
8 ],
9 "capacity": [
10 20
11 ],
12 "time_window": [
13 1693385100,
14 1693400400
15 ]
16 },
17 {
18 "id": 2,

Locations

And now, lastly we would define the locations object and add all the locations used in the problem along with a valid id. The locations object with all the points used in this example:

1"locations":
2{
3"id": 1,
4"location": ["34.083950,-118.318640","34.054927,-118.323726","34.075525,-118.361588","34.000215,-118.318803","33.945884,-118.325628","34.000895,-118.204929","34.076646,-118.376969","34.094986,-118.300885","34.018780,-118.317919","33.996658,-118.261708","34.057106,-118.361326","34.016137,-118.253523"]
5}

Step 1: Get the original Solution

Now that we have built the basic components of an optimization problem, we can submit this request using the Optimization POST method and retrieve the solution.

Optimized POST request

1curl --location 'https://api.nextbillion.io/optimization/v2?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4 "description": "Re-optimization (new-tasks) Example",
5 "jobs": [
6 {
7 "id": 1,
8 "location_index": 0,
9 "service": 120,
10 "pickup":[3],
11 "skills": [1],
12 "time_windows": [
13 [
14 1693386000,
15 1693386900
16 ]
17 ]
18 },

Optimized POST response

Once the request is made, we get a unique ID in the API response:

1{
2"id": "b25f9e62740c7cd5a1b9a7cb56ec604b",
3"message": "Optimization job created",
4"status": "Ok"
5}

Optimized GET request

We take the ID and use the Optimization GET request to retrieve the original result. Here is the GET request:

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

Following is the response

1{
2 "description": "Re-optimization (new-tasks) Example",
3 "result": {
4 "code": 0,
5 "summary": {
6 "cost": 7555,
7 "routes": 2,
8 "unassigned": 0,
9 "service": 720,
10 "duration": 7555,
11 "waiting_time": 3446,
12 "priority": 0,
13 "delivery": [
14 6
15 ],
16 "pickup": [
17 22
18 ],

Following is a visual representation of the initial locations of tasks, vehicles and the routes suggested after optimization as per the given constraints.

Step 2: Incorporate new changes

In the original response, we can see that there were 2 routes that took care of all the tasks and none of them remained unassigned. Readers are encouraged to study both routes and task distribution in this solution.

We will now add 4 new jobs to the mix and decide what changes to the original request are needed to ensure that all new jobs can be assigned. Let’s look at the updated components used in our original request.

New Jobs & Shipments

For the new jobs, We added jobs 4 & 5 with time windows between the existing task windows We added new jobs 6 & 7 with time windows starting after all the existing jobs got over in the original solution, but within the vehicle shift timings. These jobs should be appended to the existing routes.

Let’s look at how the job JSON looks like now with new changes:

1{
2 "jobs": [
3 {
4 "id": 1,
5 "location_index": 0,
6 "service": 120,
7 "pickup": [
8 3
9 ],
10 "skills": [
11 1
12 ],
13 "time_windows": [
14 [
15 1693386000,
16 1693386900
17 ]
18 ]

New Vehicles

Looking at the new tasks added, there is no need to add a new skill or extend vehicle time shifts. However, we do need to update the vehicle capacity so that the new tasks can be carried out. Following is the vehicle JSON after incorporating the new changes:

1{
2 "vehicles": [
3 {
4 "id": 1,
5 "start_index": 10,
6 "skills": [
7 1
8 ],
9 "capacity": [
10 30
11 ],
12 "time_window": [
13 1693385100,
14 1693400400
15 ]
16 },
17 {
18 "id": 2,

New Locations

The locations object also needs to be updated with the locations of the newly added tasks. Following is the new locations JSON:

1{
2 "locations": {
3 "id": 1,
4 "location": [
5 "34.083950,-118.318640",
6 "34.054927,-118.323726",
7 "34.075525,-118.361588",
8 "34.000215,-118.318803",
9 "33.945884,-118.325628",
10 "34.000895,-118.204929",
11 "34.076646,-118.376969",
12 "34.094986,-118.300885",
13 "34.018780,-118.317919",
14 "33.996658,-118.261708",
15 "34.057106,-118.361326",
16 "34.016137,-118.253523",
17 "34.076350,-118.338519",
18 "34.090425,-118.338933",

Step 3: Prepare for re-optimization

As a business operator when we get last minute new orders or changes to existing task time windows or any other change of sort in general, it might be in our interest to accommodate the new changes in the existing plan with minimal changes. In order to do that, we have updated our input request to add new tasks received and made other changes needed to our original optimization request to model the situation.

Now, in order to ensure that this new problem is not treated as a new request but as a re-optimization request instead, we add the original route plan to the solution section of the input request. This will ensure minimal changes to the existing route plan while taking care of the new tasks.

Solution

We build this object by adding the route details from the original solution as it is. Following is the JSON for solution part:

1{
2 "solution": [
3 {
4 "vehicle": 1,
5 "cost": 2866,
6 "steps": [
7 {
8 "type": "start",
9 "arrival": 1693386190,
10 "duration": 0,
11 "service": 0,
12 "waiting_time": 0,
13 "location": [
14 34.057106,
15 -118.361326
16 ],
17 "location_index": 10,
18 "load": [

Optimization POST Request

Now let’s put all these components together and create the final POST request that we will submit to the optimizer. Please ensure to add the new jobs, shipments, vehicles & locations attributes, reflecting the updated changes, instead of the original ones. Following is the final re-optimization request.

1curl --location 'https://api.nextbillion.io/optimization/v2?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data-raw '{
4 "description": "Re-optimization (new-tasks) Example",
5 "jobs": [
6 {
7 "id": 1,
8 "location_index": 0,
9 "service": 120,
10 "pickup":[3],
11 "skills": [1],
12 "time_windows": [
13 [
14 1693386000,
15 1693386900
16 ]
17 ]
18 },

Optimization POST Response

Once the request is made, we get a unique ID in the API response:

1{
2"id": "92b34b5c85baceafc1b200ca7e2e14db",
3"message": "Optimization job created",
4"status": "Ok",
5"warning": [
6"location_index[7] and 1 others are used for both pickups/deliveries as well as vehicle start/end"
7]
8}

Optimization GET Request

We take the ID and use the Optimization GET request to retrieve the result. Here is the GET request:

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

Optimization GET Response

Following is the optimized route plan:

1{
2 "description": "Re-optimization (new-tasks) Example",
3 "result": {
4 "code": 0,
5 "summary": {
6 "cost": 13663,
7 "routes": 2,
8 "unassigned": 0,
9 "service": 1200,
10 "duration": 13663,
11 "waiting_time": 4886,
12 "priority": 0,
13 "delivery": [
14 6
15 ],
16 "pickup": [
17 36
18 ],

Following is a visual representation of all the task locations (new + original), location of vehicles and the routes suggested after re-optimization as per the given constraints.

Analyzing the Solution

Looking at the new result we can observe some interesting insights:

  • All the newly added jobs - 4,5,6, & 7 were assigned within the 2 routes that were used in the original solution.

  • The new tasks (jobs 4 & 5 ) for vehicle 1 were added to the existing route without much change, while job 6 was fulfilled after all the original tasks were done owing to the time window of the job.

  • For vehicle 2, the job 7 was assigned after all the original tasks were completed owing to its time window, however important is to note that there was not much difference in the original and re-optimized until the delivery step of shipment 2 which is the last task before the vehicle heads towards the newly added task.

  • The optimizer proficiently adds all 4 tasks to the new solution with minimal changes to the original solution's existing parameters.

Above example showcases the powerful re-optimization capabilities of NextBillion.ai’s Route Optimization Flexible API. Users can leverage this feature to make last minute adjustments to their optimization plans with significant flexibility while improving overall task fulfillment. To explore this feature more, users can play around with strategies like adding more vehicles, changing the task time window and see how the API takes those changes into account without major changes to the original solution.

We hope this example was helpful. Check out some more use cases that Route Optimization Flexible API can handle for you!