Constraints : Maximum Waiting Time

In this example, we learn about the hard constraint of maximum waiting time. NextBillion.ai’s Route Optimization V2 provides options to control how long the driver must wait after arriving at the task's location. It is worth noting that this is a hard constraint and applies to all the tasks given in the problem. This feature is helpful when users want to manage the wait times of their drivers or when achieving the lowest wait times is a crucial business goal (for example, consumer-facing services). Let's look at how to incorporate this functionality into an optimisation 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 5 jobs and 1 shipment. For these tasks we add:

  1. A unique identifier for each task

  2. Location indexes for each task

  3. 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 is capable of handling complex task schedules as well.

  4. Skills for each task

  5. 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.

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": [0],
8      "skills": [1],
9      "time_windows": [[1693386000, 1693386900]]
10    },
11    {
12      "id": 2,
13      "location_index": 1,
14      "service": 180,
15      "pickup": [0],
16      "skills": [1],
17      "time_windows": [[1693387800, 1693388700]]
18    },
19    {
20      "id": 3,
21      "location_index": 2,
22      "service": 120,
23      "pickup": [0],
24      "skills": [2],
25      "time_windows": [[1693389600, 1693390500]]
26    },
27    {
28      "id": 4,
29      "location_index": 3,
30      "service": 120,
31      "pickup": [0],
32      "skills": [2],
33      "time_windows": [[1693391400, 1693392300]]
34    },
35    {
36      "id": 5,
37      "location_index": 4,
38      "service": 60,
39      "pickup": [0],
40      "skills": [3],
41      "time_windows": [[1693393200, 1693394100]]
42    }
43  ],
44  "shipments": [
45    {
46      "pickup": {
47        "description": "Shipment Pickup 1",
48        "id": 1,
49        "location_index": 5,
50        "service": 120,
51        "time_windows": [[1693395000, 1693395900]]
52      },
53      "delivery": {
54        "description": "Shipment Delivery 1",
55        "id": 1,
56        "location_index": 6,
57        "service": 120,
58        "time_windows": [[1693397700, 1693398600]]
59      },
60      "amount": [2],
61      "skills": [3]
62    }
63  ]
64}

Vehicles

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

  1. A unique ID for each vehicle

  2. Vehicle shift time or the time window

  3. Capacity to denote the amount of load that the vehicle can take

  4. start_index to denote the point from where the vehicle would start. Alternatively, the vehicles could also be assigned to a depot.

  5. costs for all vehicles. We have specified a fixed cost for both vehicles but the readers can use per_hour as well.

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

1{
2  "vehicles": [
3    {
4      "id": 1,
5      "start_index": 7,
6      "capacity": [
7        20
8      ],
9      "skills": [
10        1,
11        2,
12        3
13      ],
14      "costs": {
15        "fixed": 1000
16      },
17      "time_window": [
18        1693382400,
19        1693418400
20      ]
21    },
22    {
23      "id": 2,
24      "start_index": 8,
25      "capacity": [
26        20
27      ],
28      "skills": [
29        1,
30        2,
31        3
32      ],
33      "costs": {
34        "fixed": 3000
35      },
36      "time_window": [
37        1693382400,
38        1693418400
39      ]
40    }
41  ]
42}

Locations

Next, we 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    "id": 1,
3    "location": ["34.083950,-118.318640", "34.054927,-118.323726","34.075525,-118.361588","34.076350,-118.338519","34.090425,-118.338933", "34.076646,-118.376969", "34.094986,-118.300885", "34.057106,-118.361326", "34.016137,-118.253523"]
4}

Options

Lastly, we need to enable the max_activity_waiting_time constraint for our problem. We are configuring the constraint to keep the waiting time below 8 minutes for each activity.

1"options":{
2    "constraint":{
3    "max_activity_waiting_time": 480
4  }
5}

Optimization POST Request

Now let’s put all these components together and create the final POST request that we will submit to the optimizer.

1curl --location 'https://api.nextbillion.io/optimization/v2?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4   "description": "Max Waiting Time Example",
5   "jobs": [
6       {
7           "id": 1,
8           "location_index":0,
9           "service": 120,
10           "pickup":[0],
11           "skills":[1],
12           "time_windows": [
13               [
14                   1693386000,
15                   1693386900
16               ]
17           ]
18       },
19       {
20           "id": 2,
21           "location_index": 1,
22           "service": 180,
23           "pickup":[0],
24           "skills":[1],
25           "time_windows": [
26               [
27                   1693387800,
28                   1693388700
29               ]
30           ]
31       },
32       {
33           "id": 3,
34           "location_index": 2,
35           "service": 120,
36           "pickup":[0],
37           "skills":[2],
38           "time_windows": [
39               [
40                   1693389600,
41                   1693390500
42               ]
43           ]
44       },
45       {
46           "id": 4,
47           "location_index": 3,
48           "service": 120,
49           "pickup":[0],
50           "skills":[2],
51           "time_windows": [
52               [
53                   1693391400,
54                   1693392300
55               ]
56           ]
57       },
58       {
59           "id": 5,
60           "location_index": 4,
61           "service": 60,
62           "pickup":[0],
63           "skills":[3],
64           "time_windows": [
65               [
66                   1693393200,
67                   1693394100
68               ]
69           ]
70       }
71   ],
72    "shipments": [
73       {
74        "pickup":{
75           "description": "Shipment Pickup 1",
76           "id":1,
77           "location_index":5,
78           "service":120,
79           "time_windows":[[1693395000,1693395900]]
80        },
81        "delivery":{
82           "description": "Shipment Delivery 1",
83           "id":1,
84           "location_index":6,
85           "service":120,
86           "time_windows":[[1693397700,1693398600]]
87        },
88        "amount":[2],
89        "skills":[3]
90        }
91    ],
92   "vehicles": [
93       {
94           "id": 1,
95           "start_index": 7,
96           "capacity":[20],
97           "skills":[1,2,3],
98           "costs":{
99               "fixed":1000
100            },
101           "time_window": [
102               1693382400,
103                   1693418400
104           ]
105       },
106       {
107           "id": 2,
108           "start_index": 8,
109           "capacity":[20],
110           "skills":[1,2,3],
111           "costs":{
112               "fixed":3000
113            },
114           "time_window": [
115               1693382400,
116                   1693418400
117           ]
118       }
119   ],
120    "locations":
121        {
122            "id": 1,
123            "location": ["34.083950,-118.318640", "34.054927,-118.323726","34.075525,-118.361588","34.076350,-118.338519","34.090425,-118.338933", "34.076646,-118.376969", "34.094986,-118.300885", "34.057106,-118.361326", "34.016137,-118.253523"]
124        },
125    "options":{
126        "constraint":{
127            "max_activity_waiting_time": 480
128        }
129    }
130}
131'

Optimization POST Response

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

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

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=233c943dd6da56486f5cf6a36cddc5fa
2&key=<your_api_key>'

Optimization GET Response

Following is the optimized route plan:

1{
2  "description": "Max Waiting Time Example",
3  "result": {
4    "code": 0,
5    "summary": {
6      "cost": 7272,
7      "routes": 2,
8      "unassigned": 2,
9      "service": 540,
10      "duration": 3272,
11      "waiting_time": 595,
12      "priority": 0,
13      "delivery": [0],
14      "pickup": [0],
15      "distance": 22806.9
16    },
17    "unassigned": [
18      {
19        "id": 5,
20        "type": "job",
21        "location": [34.090425, -118.338933]
22      },
23      {
24        "id": 1,
25        "type": "pickup",
26        "location": [34.076646, -118.376969]
27      },
28      {
29        "id": 1,
30        "type": "delivery",
31        "location": [34.094986, -118.300885]
32      }
33    ],
34    "routes": [
35      {
36        "vehicle": 1,
37        "cost": 1821,
38        "steps": [
39          {
40            "type": "start",
41            "arrival": 1693390020,
42            "duration": 0,
43            "service": 0,
44            "waiting_time": 0,
45            "location": [34.057106, -118.361326],
46            "location_index": 7,
47            "load": [0]
48          },
49          {
50            "type": "job",
51            "arrival": 1693390500,
52            "duration": 480,
53            "service": 120,
54            "waiting_time": 0,
55            "location": [34.075525, -118.361588],
56            "location_index": 2,
57            "id": 3,
58            "load": [0]
59          },
60          {
61            "type": "job",
62            "arrival": 1693390961,
63            "duration": 821,
64            "service": 120,
65            "waiting_time": 439,
66            "location": [34.07635, -118.338519],
67            "location_index": 3,
68            "id": 4,
69            "load": [0]
70          },
71          {
72            "type": "end",
73            "arrival": 1693391520,
74            "duration": 821,
75            "service": 0,
76            "waiting_time": 0,
77            "location": [34.07635, -118.338519],
78            "location_index": 3,
79            "load": [0]
80          }
81        ],
82        "service": 240,
83        "duration": 821,
84        "waiting_time": 439,
85        "priority": 0,
86        "delivery": [0],
87        "pickup": [0],
88        "distance": 5302.6,
89        "geometry": "ywznEjmlqUBG@G@GAEEEMIw@[q@WLoA@KB[JmAB_@?U@[?a@?iABiDGAKCICyBw@uJ}CaCu@_A]KE}@[QGSIEAKEMEWIYKa@MKEGAGAGA?c@@uA?U?_@?QWIMG]KuAi@W?g@?w@?{@A]@a@?m@?Q?M@K@I@?fA?b@@dC?xB?R?~A?~@?dG?P?zA?N?j@@rCA`@?T?\\?\\AhBg@@e@?U?cA?W?oC?U?mC?w@@c@?a@?O?[@g@?k@?i@?c@?e@?o@?a@@Y?[?Y?U?S?cA?]?U?[A_A@m@?i@?uBAY?_A?g@?g@?u@?Q?{@@U?_@?e@?a@?uB?{@@M?Q?G?U?]?u@??kA?W?_@AS?S?c@?U?M?qA?i@?a@?]AQ?g@?mAAi@?c@?gA?i@?S?_@?sA?g@?c@?m@?y@?c@?Y?MAY?g@?]?[?i@?u@?_@?]?_@?}@?c@?_@Am@?g@?}@?g@?UAa@?a@?e@?c@?qA?a@@i@?oA?U?g@?g@?a@?c@?eA?g@?e@?M?c@?k@?yA?c@?k@?]?_@?gA?e@AaA@a@Aa@?y@?e@?s@?a@?c@?Y?e@?M?w@?q@?u@?w@?U?e@Ao@@yEAgB?}@?o@?s@?uB?o@?m@?a@Ac@?m@?q@?Ya@???"
90      },
91      {
92        "vehicle": 2,
93        "cost": 5451,
94        "steps": [
95          {
96            "type": "start",
97            "arrival": 1693385073,
98            "duration": 0,
99            "service": 0,
100            "waiting_time": 0,
101            "location": [34.016137, -118.253523],
102            "location_index": 8,
103            "load": [0]
104          },
105          {
106            "type": "job",
107            "arrival": 1693386900,
108            "duration": 1827,
109            "service": 120,
110            "waiting_time": 0,
111            "location": [34.08395, -118.31864],
112            "location_index": 0,
113            "id": 1,
114            "load": [0]
115          },
116          {
117            "type": "job",
118            "arrival": 1693387644,
119            "duration": 2451,
120            "service": 180,
121            "waiting_time": 156,
122            "location": [34.054927, -118.323726],
123            "location_index": 1,
124            "id": 2,
125            "load": [0]
126          },
127          {
128            "type": "end",
129            "arrival": 1693387980,
130            "duration": 2451,
131            "service": 0,
132            "waiting_time": 0,
133            "location": [34.054927, -118.323726],
134            "location_index": 1,
135            "load": [0]
136          }
137        ],
138        "service": 300,
139        "duration": 2451,
140        "waiting_time": 156,
141        "priority": 0,
142        "delivery": [0],
143        "pickup": [0],
144        "distance": 17504.3,
145        "geometry": "uwrnEbkwpUSKkBcAoAq@aB_AeAk@a@SsA{@{A{@g@a@a@_@mAeAmAkAmAgAmAiAoAgAsAgAUS_BoAkAy@w@m@SOGE{BcBqAaAOKm@c@[Ww@m@GHc@h@IJIJOTs@pAuAdCINS^OZQXO\\_@bAEv@CNKf@Sz@GTMb@Y~@Wx@?@GRO`@O`@Ul@I^]p@Yn@Sb@GVADk@nAq@nBc@tBgBzD_AtBSb@Q`@OZO^KRaAdCUl@e@pAUp@GROd@Md@M`@K^GTGVOj@CJKd@Sx@I`@SdAQv@Ot@S|@Ml@Oj@Kb@K`@St@KXGRITIVMZKXEJIRMZKTGNGNy@dBYn@S`@Ud@MVYn@o@tAO^MV]v@a@|@[v@Q\\g@hACFIPs@zAg@`AMVu@xAILS\\e@t@o@`AINc@r@c@p@]j@[h@MRi@`Aw@tAQ`@Q\\Sb@Qb@Ul@M\\Qd@GRGNK`@Oj@CHCHCLEPKf@CLCNCJAJAJ?DCL?FAJCr@?J?j@@^M~A@T?V@^?b@@t@H`D@bABdB@v@LlD@P?HB^BRHtA@HLdBFl@@LBRL~@Hp@Hl@DXv@lGJz@Ff@D\\Fb@BXDZ@NHx@LbBDrAAp@@bFBbF@z@?R?\\?P?T?rCA~B?hBA`B?tBAzBAzA?vCErC?bAAtAEjE?L?p@?d@?LB|@?T@\\AT@t@KnAOrDQ|ECr@A^e@AY?W@_@?U@g@?]?yA@g@?S?I?UA[@C?a@@K?K@?v@?b@?R?l@?P@X?n@?d@?|C?f@M@_N?gF??tB?lA?lA?`@@`E?dD?tA@zA@bC?xC?\\?tA?pA?l@?X?dAkJ@kA?{@@i@?sA?aB@sA?_C?cB@m@?y@?G?GAKAECE?EAqB?wCCoIB[?Q?eD?k@?]?eB?yB?}C@}A?yC?_E@?jEiE@}B?eE?{B?e@?k@??nA?dAIR?|C?rA?T?L@JHT?R?JAN?f@?`@?fA?\\?f@?h@@^E?E?_ADg@BO@U@K@}BHa@BsAFcERyBJ?tBkJ@iJ??fB?fAu@?{J@?nCC`@Ol@M\\[BsBBcB@cAAe@AW?_B?sGDoF@?t@{G@mEAY?]?aP?iJ@?k@?e@?wA?s@_B@A`@@B?@?@?@DDBB@@@B?P?R?N?b@?d@?J?K?e@?c@?O?S?QACAACCEE?A?A?AAC@a@~AA?r@nJCxO?`B?|B?xEAhB??`@?f@?l@nFArGE~A?V?d@@bA@bBArBCZC`@?^AtB?|@@|@Dd@DXBZBTBV@V@?|@?`C?d@?z@hA?rD?jB?hJA|C@dEAE`HbB?bB?D?R?n@?jA?H?D@B@NPx@\\D@tHbDDc@v@cDpFbCb@RJFFB@@BFBD?F?L?LbF|Bf@RzGbDTJVHRHDo@D}@BS_@C??"
146      }
147    ]
148  },
149  "status": "Ok",
150  "message": ""
151}

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

Analyzing the Solution

Looking at the result we can observe some interesting insights:

  1. We get an overview of the overall result with a total distance of 22806 meters covered within a total duration of 3272 seconds to complete all the assigned tasks.

  2. Important to highlight here is the total wait time of 595 seconds (< 10 minutes) for both vehicles, however 2 tasks remained unassigned as the wait time for them was higher than the prescribed limit of 8 minutes.

  3. As evident in the solution, the maximum waiting time strictly adheres to the configured value and that might lead to some tasks remaining unassigned for some cases.

We can experiment with the wait time limit, number of vehicles and time windows etc to explore how job sequencing and assignments change under different scenarios presented to Route Optimization V2 API.

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

DIDN'T FIND WHAT YOU LOOKING FOR?