Skip to content

Commit d7cde19

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

2 files changed

Lines changed: 80 additions & 16 deletions

File tree

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

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,36 @@
133133
});
134134
}
135135

136-
async function itineraryOptimization() {
136+
// Itinerary Optimization function
137+
async function itineraryOptimizationClicked() {
137138

138-
// Show loading icon.
139-
document.getElementById('loadingIcon').style.display = '';
139+
// Show loading icon
140+
showLoadingIcon(true);
140141

141142
// We need all coordinates together depots + stops
142-
var allCoordinates = depots.concat(stops);
143+
const allCoordinates = [...depots, ...stops];
143144

145+
// Get the matrix for all coordinates
146+
var matrix = await getMatrixAsync(allCoordinates);
147+
148+
// We use the NVIDIA cuOpt optimizer service to calculate the Multi-Itinerary Optimization.
149+
// more details: https://docs.nvidia.com/cuopt/user-guide/supported-features.html
150+
var solver = await getOptimizedRouteAsync(matrix);
151+
152+
// Render the route on the map for each vehicle
153+
renderRoute(allCoordinates, solver);
154+
155+
// Hide loading icon
156+
showLoadingIcon(false);
157+
}
158+
159+
// Function to show/hide the loading icon
160+
function showLoadingIcon(visible) {
161+
document.getElementById('loadingIcon').style.display = visible ? '' : 'none';
162+
};
163+
164+
// Function to get the route matrix for all coordinates
165+
async function getMatrixAsync(allCoordinates) {
144166
// Create the request body to get a route matrix
145167
var routeMatrixBody = {
146168
origins: {
@@ -156,11 +178,11 @@
156178
// Initiate an async route matrix request.
157179
var responseRouteMatrix = await processPostRequest(routeMatrixAsyncUrl, JSON.stringify(routeMatrixBody));
158180

159-
var matrix = responseRouteMatrix.matrix;
160-
161-
// We use the NVIDIA optimizer service to calculate the Multi-Itinerary Optimization.
162-
// more details: https://docs.nvidia.com/cuopt/user-guide/supported-features.html
181+
return responseRouteMatrix.matrix;
182+
}
163183

184+
// Function to get the optimized routes
185+
async function getOptimizedRouteAsync(matrix) {
164186
// Create the request body for the Multi-Itinerary Optimization.
165187
var optimizerBody = {
166188
requestBody: {
@@ -205,7 +227,8 @@
205227
var optimizerUrl = 'https://api.nvcf.nvidia.com/v2/nvcf/exec/functions/4bb2668d-c877-4b89-aabf-1630ed9b32c3/versions/471b94dc-931e-4aa1-99f4-935f5f352737';
206228

207229
// Bearer token for the NVIDIA optimizer service.
208-
var token = '';
230+
var tokenResponse = await fetch('https://samples.azuremaps.com/api/GetMIOKey');
231+
var token = await tokenResponse.text();
209232

210233
// Initiate an request to the NVIDIA optimizer service.
211234
var optimizerResponse = await fetch(optimizerUrl, {
@@ -251,11 +274,15 @@
251274
data = statusResponse.json();
252275
}
253276

254-
// Render the route on the map for each vehicle.
255-
for (var vehicle in data.response.response.solver_response.vehicle_data) {
277+
return data.response.response.solver_response;
278+
}
279+
280+
// Function to render the route on the map for each vehicle
281+
function renderRoute(allCoordinates, solver) {
282+
for (var vehicle in solver.vehicle_data) {
256283

257284
// Get the vehicle data.
258-
var car = data.response.response.solver_response.vehicle_data[vehicle];
285+
var car = solver.vehicle_data[vehicle];
259286

260287
// Create a string of coordinates for the route.
261288
var coordinateString = "";
@@ -286,17 +313,14 @@
286313
routeSource.add(routeLine);
287314
});
288315
};
289-
290-
// Hide loading icon.
291-
document.getElementById('loadingIcon').style.display = 'none';
292316
}
293317
</script>
294318
</head>
295319
<body onload="getMap()">
296320
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
297321

298322
<div style="position:absolute;top:10px;left:10px;background-color:#fff;padding:10px;border-radius:5px;">
299-
<input type="button" value="Multi-Itinerary Optimization" onclick="itineraryOptimization()" />
323+
<input type="button" value="Multi-Itinerary Optimization" onclick="itineraryOptimizationClicked()" />
300324
</div>
301325

302326
<img id="loadingIcon" src="/images/loadingIcon.gif" title="Loading" style="position:absolute;left:calc(50% - 25px);top:250px;display:none;" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"vehicles": [
3+
{
4+
"id": 1,
5+
"name": "Car A",
6+
"start_depot_id": 1,
7+
"end_depot_id": 1,
8+
"capacity": 10,
9+
"vehicle_type_id": 1,
10+
"color": "red"
11+
},
12+
{
13+
"id": 2,
14+
"name": "Car B",
15+
"start_depot_id": 1,
16+
"end_depot_id": 1,
17+
"capacity": 10,
18+
"vehicle_type_id": 1,
19+
"color": "blue"
20+
},
21+
{
22+
"id": 3,
23+
"name": "Car C",
24+
"start_depot_id": 2,
25+
"end_depot_id": 2,
26+
"capacity": 10,
27+
"vehicle_type_id": 1,
28+
"color": "green"
29+
},
30+
{
31+
"id": 4,
32+
"name": "Car D",
33+
"start_depot_id": 2,
34+
"end_depot_id": 2,
35+
"capacity": 10,
36+
"vehicle_type_id": 1,
37+
"color": "yellow"
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)