Integrate Fetched Data with Route Optimization API

After successfully retrieving the necessary data from Motive, the next step is to integrate this data with the NextBillion.ai Route Optimization API.

Follow the steps below to map the fetched Motive data to the API parameters and configure the optimization settings.

Step 1: Map and Configure Parameters

First, prepare the data and configuration parameters. Here's an explanation of the key components:

1
// Initialize arrays for storing data
2
let vehicles = [];
3
let shifts = [];
4
let starts = [];
5
let ends = [];
6
let veh_attr = [];
7
let stop_attr = [];
8
let vehicle_index = 1;
9
10
// Convert shift start and end times to UNIX timestamp
11
let shiftStart = (new Date(dtShiftStart2.value).getTime()/1000);
12
let shiftEnd = (new Date(dtShiftEnd2.value).getTime()/1000);
13
14
// Initialize arrays for jobs, locations, and shipments
15
let locations = [];
16
let jobs = [];
17
let shipments = [];
18
vehicles = [];
19
veh_attr = [];
20
stop_attr = [];
  • vehicles: Array to store vehicle data.
  • shifts, starts, ends: Arrays for storing shift-related data.
  • veh_attr, stop_attr: Arrays for vehicle and stop attributes.
  • vehicle_index: Index to track vehicles.
  • shiftStart, shiftEnd: Convert shift times to UNIX timestamp format.

Step 2: Build the Optimization Problem

Create job arrays and indexed lists of locations by iterating through all stops.

1
let location_index = 0;
2
let location = [];
3
4
tblAddresses.selectedRows.forEach(planstop => {
5
let job = {
6
id: parseInt(planstop.dispatch_location.id),
7
description: planstop.dispatch_location.vendor_id,
8
location_index: location_index++,
9
service: numberInput1.value,
10
priority: 0
11
// Optional: time_windows: [[from, to]]
12
}
13
location.push(`${planstop.dispatch_location.lat},${planstop.dispatch_location.lon}`);
14
jobs.push(job);
15
});
  • location_index: Index to track locations.
  • location: Array to store location data.
  • jobs: Array to store job data including job ID, description, location index, and service time.

Step 3: Add Shift Start/End Locations

Include shift start and end locations in the index.

1
location.push(`${txtVehicleStartPosition2.value}`);
2
location.push(`${txtVehicleEndPosition2.value}`);
3
location_index += 1;

Step 4: Define Vehicles and Their Attributes

Configure vehicle-related data including start index, time window, maximum tasks, and fixed costs.

1
let veh_fixed_cost = 0;
2
tblDrivers.selectedRows.forEach(v => {
3
let vehicle = {
4
id: parseInt(v.Driver_Id),
5
start_index: location_index,
6
time_window: [shiftStart, shiftEnd],
7
max_tasks: 25,
8
costs: {
9
fixed: veh_fixed_cost
10
}
11
};
12
vehicles.push(vehicle);
13
veh_fixed_cost += 2;
14
});
15
locations = {
16
id: 12,
17
description: 'Motive Test',
18
location: location
19
};
20
let options = {
21
routing: {
22
mode: "car"
23
}
24
};
  • veh_fixed_cost: Incremental cost associated with each vehicle.
  • vehicles: Array to store vehicle details including ID, start index, time window, max tasks, and costs.
  • locations: Object to store all locations.
  • options: Configuration for routing mode.

Step 5: Store Data and Trigger Optimization

Store the configured data in local storage and trigger the optimization process.

1
localStorage.setValue('vehiclesNBAI', vehicles);
2
localStorage.setValue('shipmentsNBAI', shipments);
3
localStorage.setValue('jobsNBAI', jobs);
4
localStorage.setValue('locationsNBAI', locations);
5
localStorage.setValue('nbaiOptions', options);
6
localStorage.setValue('nbaiVRPResult', null);
7
8
nbaiOptimizationRun.trigger();

© 2024 NextBillion.ai all rights reserved.