• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

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 a depot.

  • 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
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
{
  "description": "Max Waiting Time Example",
  "result": {
    "code": 0,
    "summary": {
      "cost": 7272,
      "routes": 2,
      "unassigned": 2,
      "service": 540,
      "duration": 3272,
      "waiting_time": 595,
      "priority": 0,
      "delivery": [0],
      "pickup": [0],
      "distance": 22806.9
    },
    "unassigned": [
      {
        "id": 5,
        "type": "job",
        "location": [34.090425, -118.338933]
      },
      {
        "id": 1,
        "type": "pickup",
        "location": [34.076646, -118.376969]
      },
      {
        "id": 1,
        "type": "delivery",
        "location": [34.094986, -118.300885]
      }
    ],
    "routes": [
      {
        "vehicle": 1,
        "cost": 1821,
        "steps": [
          {
            "type": "start",
            "arrival": 1693390020,
            "duration": 0,
            "service": 0,
            "waiting_time": 0,
            "location": [34.057106, -118.361326],
            "location_index": 7,
            "load": [0]
          },
          {
            "type": "job",
            "arrival": 1693390500,
            "duration": 480,
            "service": 120,
            "waiting_time": 0,
            "location": [34.075525, -118.361588],
            "location_index": 2,
            "id": 3,
            "load": [0]
          },
          {
            "type": "job",
            "arrival": 1693390961,
            "duration": 821,
            "service": 120,
            "waiting_time": 439,
            "location": [34.07635, -118.338519],
            "location_index": 3,
            "id": 4,
            "load": [0]
          },
          {
            "type": "end",
            "arrival": 1693391520,
            "duration": 821,
            "service": 0,
            "waiting_time": 0,
            "location": [34.07635, -118.338519],
            "location_index": 3,
            "load": [0]
          }
        ],
        "service": 240,
        "duration": 821,
        "waiting_time": 439,
        "priority": 0,
        "delivery": [0],
        "pickup": [0],
        "distance": 5302.6,
        "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@???"
      },
      {
        "vehicle": 2,
        "cost": 5451,
        "steps": [
          {
            "type": "start",
            "arrival": 1693385073,
            "duration": 0,
            "service": 0,
            "waiting_time": 0,
            "location": [34.016137, -118.253523],
            "location_index": 8,
            "load": [0]
          },
          {
            "type": "job",
            "arrival": 1693386900,
            "duration": 1827,
            "service": 120,
            "waiting_time": 0,
            "location": [34.08395, -118.31864],
            "location_index": 0,
            "id": 1,
            "load": [0]
          },
          {
            "type": "job",
            "arrival": 1693387644,
            "duration": 2451,
            "service": 180,
            "waiting_time": 156,
            "location": [34.054927, -118.323726],
            "location_index": 1,
            "id": 2,
            "load": [0]
          },
          {
            "type": "end",
            "arrival": 1693387980,
            "duration": 2451,
            "service": 0,
            "waiting_time": 0,
            "location": [34.054927, -118.323726],
            "location_index": 1,
            "load": [0]
          }
        ],
        "service": 300,
        "duration": 2451,
        "waiting_time": 156,
        "priority": 0,
        "delivery": [0],
        "pickup": [0],
        "distance": 17504.3,
        "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|@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Dc@v@cDpFbCb@RJFFB@@BFBD?F?L?LbF|Bf@RzGbDTJVHRHDo@D}@BS_@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 22806 meters covered within a total duration of 3272 seconds to complete all the assigned tasks.

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

  • 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!