Skip to content

Commit 8687c4e

Browse files
committed
Multi-Itinerary Optimization sample
1 parent d7cde19 commit 8687c4e

4 files changed

Lines changed: 146 additions & 79 deletions

File tree

‎Samples/REST Services/MIO/mio.html‎

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
<script src="https://samples.azuremaps.com/lib/azure-maps/azure-maps-helper.min.js"></script>
2222

2323
<script>
24-
var map, stopSource, depotSource, routeSource, stops, depots;
24+
var map, stopDataSource, depotDataSource, routeDataSource;
25+
var stops, depots, vehicles;
26+
27+
// Colors for the different routes.
28+
var routeColors = ['#2272B9', '#ff7b25', '#6b5b95', '#d64161', '#00cc66', '#000000'];
2529

2630
// Your Azure Maps client id for accessing your Azure Maps account.
2731
var azureMapsClientId = 'e6b6ab59-eb5d-4d25-aa57-581135b927f0';
@@ -50,7 +54,7 @@
5054
map = new atlas.Map('myMap', {
5155
center: [-105.076, 40.56],
5256
zoom: 11,
53-
style: 'grayscale_dark',
57+
style: 'grayscale_light',
5458
view: 'Auto',
5559

5660
// Add authentication details for connecting to Azure Maps.
@@ -77,42 +81,45 @@
7781
// Download stop and depot location GeoJSON data.
7882
Promise.all([
7983
fetch('/data/geojson/mio_stops.json').then(response => response.json()),
80-
fetch('/data/geojson/mio_depots.json').then(response => response.json())
84+
fetch('/data/geojson/mio_depots.json').then(response => response.json()),
85+
fetch('/data/geojson/mio_vehicles.json').then(response => response.json())
8186
]).then(results => {
8287
stops = results[0].features;
8388
depots = results[1].features;
89+
vehicles = results[2];
8490

8591
// Create a data source for stops.
86-
stopSource = new atlas.source.DataSource();
87-
map.sources.add(stopSource);
88-
stopSource.add(stops);
92+
stopDataSource = new atlas.source.DataSource();
93+
map.sources.add(stopDataSource);
94+
stopDataSource.add(stops);
8995

9096
// Create a data source for depots.
91-
depotSource = new atlas.source.DataSource();
92-
map.sources.add(depotSource);
93-
depotSource.add(depots);
97+
depotDataSource = new atlas.source.DataSource();
98+
map.sources.add(depotDataSource);
99+
depotDataSource.add(depots);
94100

95101
// Create a data source for routes.
96-
routeSource = new atlas.source.DataSource();
97-
map.sources.add(routeSource);
102+
routeDataSource = new atlas.source.DataSource();
103+
map.sources.add(routeDataSource);
98104

99105
// Add rendering layers below labels.
100106
map.layers.add([
101-
new atlas.layer.LineLayer(routeSource, null, {
102-
strokeColor: '#B97222',
107+
new atlas.layer.LineLayer(routeDataSource, null, {
108+
strokeColor: ['get', 'strokeColor'],
103109
strokeWidth: 5,
110+
strokeOpacity: 0.8,
104111
lineJoin: 'round',
105112
lineCap: 'round'
106113
}),
107-
new atlas.layer.BubbleLayer(stopSource, null, {
114+
new atlas.layer.BubbleLayer(stopDataSource, null, {
108115
createIndicators: true, // to enable bubble layer a11y feature
109116
radius: 4,
110117
strokeWidth: 0
111118
})
112119
], 'labels');
113120

114121
// Render an icon for each depot.
115-
map.layers.add(new atlas.layer.SymbolLayer(depotSource, null, {
122+
map.layers.add(new atlas.layer.SymbolLayer(depotDataSource, null, {
116123
iconOptions: {
117124
image: 'fire-shield-icon',
118125
size: 0.5,
@@ -143,11 +150,11 @@
143150
const allCoordinates = [...depots, ...stops];
144151

145152
// Get the matrix for all coordinates
146-
var matrix = await getMatrixAsync(allCoordinates);
153+
const matrix = await getMatrixAsync(allCoordinates);
147154

148155
// We use the NVIDIA cuOpt optimizer service to calculate the Multi-Itinerary Optimization.
149156
// more details: https://docs.nvidia.com/cuopt/user-guide/supported-features.html
150-
var solver = await getOptimizedRouteAsync(matrix);
157+
const solver = await getOptimizedRouteAsync(matrix);
151158

152159
// Render the route on the map for each vehicle
153160
renderRoute(allCoordinates, solver);
@@ -175,48 +182,60 @@
175182
}
176183
};
177184

178-
// Initiate an async route matrix request.
185+
// Initiate an async route matrix request
179186
var responseRouteMatrix = await processPostRequest(routeMatrixAsyncUrl, JSON.stringify(routeMatrixBody));
180187

181188
return responseRouteMatrix.matrix;
182189
}
183190

184191
// Function to get the optimized routes
185192
async function getOptimizedRouteAsync(matrix) {
186-
// Create the request body for the Multi-Itinerary Optimization.
193+
194+
// Function to convert hours to seconds
195+
const convertHoursToSeconds = (hours) => hours * 60 * 60;
196+
197+
const stopslength = stops.length;
198+
const depotslength = depots.length;
199+
200+
// Create the request body for the Multi-Itinerary Optimization
187201
var optimizerBody = {
188202
requestBody: {
189203
data: {
190204
cost_matrix_data:
191205
{
192206
data:
193207
{
208+
// "1" = is the id for the vehicle_type_id
194209
"1": matrix.map(row => row.map(item => item.response.routeSummary.lengthInMeters))
195210
}
196211
},
197212
travel_time_matrix_data:
198213
{
199214
data:
200215
{
216+
// "1" = is the id for the vehicle_type_id
201217
"1": matrix.map(row => row.map(item => item.response.routeSummary.travelTimeInSeconds))
202218
}
203219
},
204220
fleet_data: {
205221
// in this sample we have 4 vehicles,
206222
// 2 vehicles for each depot (we have 2 depots)
207223
vehicle_locations: [[0, 0], [0, 0], [1, 1], [1, 1]], // [start location in array Depot, stop location in array Depot]
208-
vehicle_ids: ["CarA", "CarB", "CarC", "CarD"], // the id for each vehicle
209-
vehicle_types: [1, 1, 1, 1], // cost_matrix_data & travel_time_matrix_data data id "1"
210-
capacities: [[75, 75, 75, 75]], // the capacity for each vehicle
224+
vehicle_ids: vehicles.map(vehicle => vehicle.name), // ["CarA", "CarB", "CarC", "CarD"], // the id for each vehicle
225+
vehicle_types: vehicles.map(vehicle => vehicle.vehicle_type_id), // cost_matrix_data & travel_time_matrix_data data id "1"
226+
capacities: [vehicles.map(vehicle => vehicle.capacity)], // the capacity for each vehicle
211227
//vehicle_time_windows: [[8, 17], [8, 17], [8, 17], [10, 20], [10, 20]], // the time in seconds
212228
//vehicle_break_time_windows: [[[12, 14], [12, 14], [12, 14], [12, 14], [12, 14]]], // the time in seconds
213-
//vehicle_break_durations: [[0, 0, 0, 0, 0]] // the time in seconds
229+
//vehicle_break_durations: [vehicles.map(vehicle => convertHoursToSeconds(vehicle.break_duration))] // the time in seconds
214230
},
215231
task_data: {
216-
task_locations: [2, 3, 4, 5, 6, 7, 8, 9], // the index in array Stops. the first 2 stops are the depots and does not count as tasks
217-
demand: [[30, 40, 40, 30, 30, 40, 40, 30]], // the demand for each task/stop
218-
//task_time_windows: [[8, 17], [8, 17], [8, 17], [17, 20]], // the time in seconds
219-
//service_times: [0, 0, 0, 0] // the time in seconds
232+
task_locations: Array.from({ length: stopslength }, (_, index) => depotslength + index), // the index in array Stops. the first 2 stops are the depots and does not count as tasks
233+
demand: [stops.map(stop => stop.properties.demand)], // the demand for each task/stop
234+
// task_time_windows: [stops.map(stop => {
235+
// const openingHours = stop.properties.opening_hours;
236+
// return openingHours.map(convertHoursToSeconds);
237+
// })], // the time in seconds
238+
service_times: stops.map(stop => convertHoursToSeconds(stop.properties.service_time)) // the time in seconds
220239
}
221240
},
222241
client_version: "custom"
@@ -279,6 +298,7 @@
279298

280299
// Function to render the route on the map for each vehicle
281300
function renderRoute(allCoordinates, solver) {
301+
var index = 0;
282302
for (var vehicle in solver.vehicle_data) {
283303

284304
// Get the vehicle data.
@@ -307,10 +327,15 @@
307327
const routeCoordinates = route.legs.flatMap(leg => leg.points.map(point => [point.longitude, point.latitude]));
308328

309329
// Create a LineString from the route path points.
310-
const routeLine = new atlas.data.LineString(routeCoordinates);
330+
var routeLine = new atlas.data.LineString(routeCoordinates);
311331

312332
// Add it to the data source.
313-
routeSource.add(routeLine);
333+
routeDataSource.add(new atlas.data.Feature(new atlas.data.LineString(routeCoordinates), {
334+
// Set the stroke color of the route based on the vehicle color.
335+
strokeColor: routeColors[index % routeColors.length]
336+
}));
337+
338+
index++;
314339
});
315340
};
316341
}

‎Static/data/geojson/mio_depots.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"features": [
44
{
55
"type": "Feature",
6-
"id": 1,
6+
"id": 11,
77
"geometry": {
88
"type": "Point",
99
"coordinates": [ -105.072334, 40.581108 ]
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"type": "Feature",
17-
"id": 2,
17+
"id": 22,
1818
"geometry": {
1919
"type": "Point",
2020
"coordinates": [ -105.040072, 40.542962 ]

‎Static/data/geojson/mio_stops.json‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,106 @@
33
"features": [
44
{
55
"type": "Feature",
6-
"id": 1,
6+
"id": 11,
77
"geometry": {
88
"type": "Point",
99
"coordinates": [ -105.0783007, 40.5601487 ]
10+
},
11+
"properties": {
12+
"demand": 30,
13+
"service_time": 0,
14+
"opening_hours": [ 8, 17 ]
1015
}
1116
},
1217
{
1318
"type": "Feature",
14-
"id": 2,
19+
"id": 22,
1520
"geometry": {
1621
"type": "Point",
1722
"coordinates": [ -105.0573299, 40.5628491 ]
23+
},
24+
"properties": {
25+
"demand": 40,
26+
"service_time": 0,
27+
"opening_hours": [ 8, 17 ]
1828
}
1929
},
2030
{
2131
"type": "Feature",
22-
"id": 3,
32+
"id": 33,
2333
"geometry": {
2434
"type": "Point",
2535
"coordinates": [ -105.0351322, 40.5679378 ]
36+
},
37+
"properties": {
38+
"demand": 40,
39+
"service_time": 0,
40+
"opening_hours": [ 8, 17 ]
2641
}
2742
},
2843
{
2944
"type": "Feature",
30-
"id": 4,
45+
"id": 44,
3146
"geometry": {
3247
"type": "Point",
3348
"coordinates": [ -105.0332723, 40.550293 ]
49+
},
50+
"properties": {
51+
"demand": 30,
52+
"service_time": 0,
53+
"opening_hours": [ 8, 17 ]
3454
}
3555
},
3656
{
3757
"type": "Feature",
38-
"id": 5,
58+
"id": 55,
3959
"geometry": {
4060
"type": "Point",
4161
"coordinates": [ -105.0306531, 40.5638493 ]
62+
},
63+
"properties": {
64+
"demand": 30,
65+
"service_time": 0,
66+
"opening_hours": [ 8, 17 ]
4267
}
4368
},
4469
{
4570
"type": "Feature",
46-
"id": 6,
71+
"id": 66,
4772
"geometry": {
4873
"type": "Point",
4974
"coordinates": [ -105.1001173, 40.5472442 ]
75+
},
76+
"properties": {
77+
"demand": 40,
78+
"service_time": 0,
79+
"opening_hours": [ 8, 17 ]
5080
}
5181
},
5282
{
5383
"type": "Feature",
54-
"id": 7,
84+
"id": 77,
5585
"geometry": {
5686
"type": "Point",
5787
"coordinates": [ -105.1212113, 40.5867356 ]
88+
},
89+
"properties": {
90+
"demand": 40,
91+
"service_time": 0,
92+
"opening_hours": [ 8, 17 ]
5893
}
5994
},
6095
{
6196
"type": "Feature",
62-
"id": 8,
97+
"id": 88,
6398
"geometry": {
6499
"type": "Point",
65100
"coordinates": [ -105.1245675, 40.5897754 ]
101+
},
102+
"properties": {
103+
"demand": 30,
104+
"service_time": 0,
105+
"opening_hours": [ 8, 17 ]
66106
}
67107
}
68108
]

0 commit comments

Comments
 (0)