Constraints : Maximum Waiting Time
In this example, we learn about the hard constraint of maximum waiting time. NextBillion.ai’s Route Optimization Flexible API 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 optimization 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:
-
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 for the sake of simplicity, but the Route Optimization is capable of handling complex task schedules as well.
-
Skills for each task
-
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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{ "jobs": [ { "id": 1, "location_index": 0, "service": 120, "pickup": [0], "skills": [1], "time_windows": [[1693386000, 1693386900]] }, { "id": 2, "location_index": 1, "service": 180, "pickup": [0], "skills": [1], "time_windows": [[1693387800, 1693388700]] }, { "id": 3, "location_index": 2, "service": 120, "pickup": [0], "skills": [2], "time_windows": [[1693389600, 1693390500]] }, { "id": 4, "location_index": 3, "service": 120, "pickup": [0], "skills": [2], "time_windows": [[1693391400, 1693392300]] }, { "id": 5, "location_index": 4, "service": 60, "pickup": [0], "skills": [3], "time_windows": [[1693393200, 1693394100]] } ], "shipments": [ { "pickup": { "description": "Shipment Pickup 1", "id": 1, "location_index": 5, "service": 120, "time_windows": [[1693395000, 1693395900]] }, "delivery": { "description": "Shipment Delivery 1", "id": 1, "location_index": 6, "service": 120, "time_windows": [[1693397700, 1693398600]] }, "amount": [2], "skills": [3] } ] }
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:
-
A unique ID for each vehicle
-
Vehicle shift time or the time window
-
Capacity to denote the amount of load that the vehicle can take
-
start_index
to denote the point from where the vehicle would start. Alternatively, the vehicles could also be assigned to adepot
. -
costs
for all vehicles. We have specified afixed
cost for both vehicles but the readers can useper_hour
as well.
Once the vehicle and their properties are defined, the resulting vehicles
JSON is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
{ "vehicles": [ { "id": 1, "start_index": 7, "capacity": [ 20 ], "skills": [ 1, 2, 3 ], "costs": { "fixed": 1000 }, "time_window": [ 1693382400, 1693418400 ] }, { "id": 2, "start_index": 8, "capacity": [ 20 ], "skills": [ 1, 2, 3 ], "costs": { "fixed": 3000 }, "time_window": [ 1693382400, 1693418400 ] } ] }
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 2 3 4
"locations":{ "id": 1, "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"] }
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 2 3 4 5
"options":{ "constraint":{ "max_activity_waiting_time": 480 } }
Optimization POST Request
Now let’s put all these components together and create the final POST request that we will submit to the optimizer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
curl --location 'https://api.nextbillion.io/optimization/v2?key=<your_api_key>' \ --header 'Content-Type: application/json' \ --data '{ "description": "Max Waiting Time Example", "jobs": [ { "id": 1, "location_index":0, "service": 120, "pickup":[0], "skills":[1], "time_windows": [ [ 1693386000, 1693386900 ] ] }, { "id": 2, "location_index": 1, "service": 180, "pickup":[0], "skills":[1], "time_windows": [ [ 1693387800, 1693388700 ] ] }, { "id": 3, "location_index": 2, "service": 120, "pickup":[0], "skills":[2], "time_windows": [ [ 1693389600, 1693390500 ] ] }, { "id": 4, "location_index": 3, "service": 120, "pickup":[0], "skills":[2], "time_windows": [ [ 1693391400, 1693392300 ] ] }, { "id": 5, "location_index": 4, "service": 60, "pickup":[0], "skills":[3], "time_windows": [ [ 1693393200, 1693394100 ] ] } ], "shipments": [ { "pickup":{ "description": "Shipment Pickup 1", "id":1, "location_index":5, "service":120, "time_windows":[[1693395000,1693395900]] }, "delivery":{ "description": "Shipment Delivery 1", "id":1, "location_index":6, "service":120, "time_windows":[[1693397700,1693398600]] }, "amount":[2], "skills":[3] } ], "vehicles": [ { "id": 1, "start_index": 7, "capacity":[20], "skills":[1,2,3], "costs":{ "fixed":1000 }, "time_window": [ 1693382400, 1693418400 ] }, { "id": 2, "start_index": 8, "capacity":[20], "skills":[1,2,3], "costs":{ "fixed":3000 }, "time_window": [ 1693382400, 1693418400 ] } ], "locations": { "id": 1, "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"] }, "options":{ "constraint":{ "max_activity_waiting_time": 480 } } } '
Optimization POST Response
Once the request is made, we get a unique ID in the API response:
1 2 3 4 5
{ "id": "233c943dd6da56486f5cf6a36cddc5fa", "message": "Optimization job created", "status": "Ok" }
Optimization GET Request
We take the ID and use the Optimization GET request to retrieve the result. Here is the GET request:
1 2
curl --location 'https://api.nextbillion.io/optimization/v2/result?id=233c943dd6da56486f5cf6a36cddc5fa &key=<your_api_key>'
Optimization GET Response
Following is the optimized route plan:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{ "description": "Max Waiting Time Example", "result": { "code": 0, "summary": { "cost": 6276, "routes": 2, "unassigned": 3, "setup": 0, "service": 540, "duration": 2276, "waiting_time": 718, "priority": 0, "delivery": [ 0 ], "pickup": [ 0 ], "distance": 24826 }, "unassigned": [ { "id": 1, "type": "pickup", "location": [ 34.076646, -118.376969 ], "reason": "task unassigned because of max activity waiting time" }, { "id": 1, "type": "delivery", "location": [ 34.094986, -118.300885 ], "reason": "task unassigned because of max activity waiting time" }, { "id": 5, "type": "job", "location": [ 34.090425, -118.338933 ], "reason": "task unassigned because of max activity waiting time" } ], "routes": [ { "vehicle": 1, "cost": 1599, "steps": [ { "type": "start", "arrival": 1693390207, "duration": 0, "service": 0, "waiting_time": 0, "location": [ 34.057106, -118.361326 ], "location_index": 7, "load": [ 0 ], "distance": 0 }, { "type": "job", "arrival": 1693390500, "duration": 293, "service": 120, "waiting_time": 0, "location": [ 34.075525, -118.361588 ], "location_index": 2, "id": 3, "load": [ 0 ], "distance": 2213 }, { "type": "job", "arrival": 1693390926, "duration": 599, "service": 120, "waiting_time": 474, "location": [ 34.07635, -118.338519 ], "location_index": 3, "id": 4, "load": [ 0 ], "distance": 4410 }, { "type": "end", "arrival": 1693391520, "duration": 599, "service": 0, "waiting_time": 0, "location": [ 34.07635, -118.338519 ], "location_index": 3, "load": [ 0 ], "distance": 4410 } ], "service": 240, "duration": 599, "waiting_time": 474, "priority": 0, "delivery": [ 0 ], "pickup": [ 0 ], "distance": 4410, "geometry": "ywznEjmlqUBG@G@GAEEEeAlBWb@KPMTU^_@p@MPOXMROXIJe@x@e@v@i@U[MCAWKc@Qa@Qq@Yy@[c@OwAm@c@S_@Oe@SSIg@U_@Os@Yc@S_@OMG]MCA[MQIEA[KWGOAKAO?e@?Q?Q?S?S@Q?Q?e@?e@?O?g@?W?Q?g@@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@???" }, { "vehicle": 2, "cost": 4677, "steps": [ { "type": "start", "arrival": 1693385759, "duration": 0, "service": 0, "waiting_time": 0, "location": [ 34.016137, -118.253523 ], "location_index": 8, "load": [ 0 ], "distance": 0 }, { "type": "job", "arrival": 1693386900, "duration": 1141, "service": 120, "waiting_time": 0, "location": [ 34.08395, -118.31864 ], "location_index": 0, "id": 1, "load": [ 0 ], "distance": 16628 }, { "type": "job", "arrival": 1693387556, "duration": 1677, "service": 180, "waiting_time": 244, "location": [ 34.054927, -118.323726 ], "location_index": 1, "id": 2, "load": [ 0 ], "distance": 20416 }, { "type": "end", "arrival": 1693387980, "duration": 1677, "service": 0, "waiting_time": 0, "location": [ 34.054927, -118.323726 ], "location_index": 1, "load": [ 0 ], "distance": 20416 } ], "service": 300, "duration": 1677, "waiting_time": 244, "priority": 0, "delivery": [ 0 ], "pickup": [ 0 ], "distance": 20416, "geometry": "uwrnEbkwpUSKkBcAoAq@aB_Ak@`Bk@bBq@hBa@jAK\\c@nAm@QWIQGQC}@[YKa@Ku@U[KMEIGKIMKy@u@OOgAaA]Yg@e@KIeAaA}@w@IIQOkAeAQOy@u@o@i@k@g@WUMMUSSSc@_@a@_@OO_@YiAaASSUQWWm@k@\\i@PYPYPYJQLWP]x@cBd@sAVc@`@{@Vc@Jm@Fc@NcAViB@G`@aDB[Fm@Ba@@]?m@?e@?g@Ae@Iw@?]AYASCk@Ae@G{@Iw@K{@Q_AG]G[EMESUw@Uw@Uq@Wy@EQIWOc@Ma@g@_BOk@GQY}@aA}Ca@qAa@yASm@Ss@_@oA_@gACMK[M_@{@qCQo@q@oB]kAa@oAK[g@eBc@{A[wA]_BMo@Km@Kk@Gg@M_AE_@Ea@CWY}A[}BQsAIs@Ky@Gc@Eg@COCYGe@Iq@EQGc@Ki@ESIa@IQCIUq@O]Yg@Ye@W_@i@q@QQMKKIwIkFe@YsBoAGCECIGGCSKQMSMQKKGIESKOKMGKGOKKEGCK_@GKGI]UGEMQKOGMCIEKESCSAY?YBUBUg@KcAQaB]IASt@c@nAUp@GPWv@W?M?I?I@E@A@ABABAHGTALCHGXI\\GRKNKLCBGFOHOFSDUB[@cA@e@P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c@jA_@dAOb@GNGNs@nA[j@a@v@Uf@_@z@Yj@c@|@IPIJGHIDMBE?@v@?b@?b@?p@?RA`@@v@?j@?J?X@nB?h@?r@?pA?N?B?P?^?b@?n@@Z?F?N?L?nB?l@?f@?n@?X?hA?j@?f@?n@?j@@t@?n@?f@@j@?xB?r@?x@?hD?l@@lA?R?@?N?|@?n@?t@?`A?\\?TAl@?J@d@?R?j@?lA?d@?d@?T?pA?`@?f@?T?R?x@?T@x@?b@?p@?x@?X?pA?xA?X?d@?^?b@?n@?j@?\\?hC?\\?zA_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??y@?uA`AAdJ?vEAzJ?zJAt@??f@?t@?|B@~@dA?`D?zAAd@?hJ?R?l@?lC?lB?l@?hEAfA?~@?RA?p@?HAfAHBb@Ld@LTJXLtGxC\\NZLv@^dAd@ZN~Ar@bJ`ETLtAn@xAn@PHNHHBRJXLv@\\I`@CPAHC\\ARAT_@C??" } ] }, "status": "Ok", "message": "" }
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:
-
We get an overview of the overall result with a total distance of 24826 meters covered within a total duration of 2276 seconds of driving time to complete all the assigned tasks.
-
Important to highlight here is the total wait time of 718 seconds (~12 minutes) for both vehicles, however 3 tasks remained unassigned as the wait time for them was higher than the prescribed limit of 8 minutes. Please note that readers might see a different number of tasks getting unassigned when trying the example. This would be because of the prevailing traffic conditions at the time of executing the POST request and is totally expected.
-
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 Flexible API.
We hope this example was helpful. Check out some more use cases that Route Optimization Flexible API can handle for you!