Integrate with Route Optimization API

After retrieving the lists of vehicles and delivery locations from Geotab, the next step is to map this data to the required format for the Nextbillion.ai’s Route Optimization API.

By using the advanced parameters of the API, you can further refine and customize the optimization process, ensuring efficient and cost-effective last-mile delivery operations.

Step 1: Initialize Variables

Start by initializing the necessary variables to store vehicle and job data, shift times, and location indices.

1
let vehicles = [];
2
let shifts = [];
3
let starts = [];
4
let ends = [];
5
let veh_attr = [];
6
let stop_attr = [];
7
let vehicle_index = 1;
8
let job_index = 1;
9
let shiftStart = (new Date(dtShiftStart2.value).getTime()/1000);
10
let shiftEnd = (new Date(dtShiftEnd2.value).getTime()/1000);
  • vehicles, shifts, starts, ends: Arrays to store vehicle and shift information.
  • veh_attr, stop_attr: Arrays to store vehicle and stop attributes.
  • vehicle_index, job_index: Counters for indexing vehicles and jobs.
  • shiftStart, shiftEnd: Convert shift start and end times to UNIX timestamps.

Step 2: Reset and Initialize Job and Location Arrays

Initialize arrays to hold locations, jobs, and shipments data. Reset the previously defined arrays to ensure clean data processing.

1
let locations = [];
2
let jobs = [];
3
let shipments = [];
4
vehicles = [];
5
veh_attr = [];
6
stop_attr = [];

Step 3: Create Job Array and Indexed List of Locations

Iterate through each stop and create job objects while maintaining an indexed list of locations.

1
let location_index = 0;
2
let location = [];
3
address.data.forEach(planstop => {
4
let job = {
5
id: job_index++,
6
description: planstop.id,
7
location_index: location_index++,
8
service: numberInput1.value,
9
priority: 0
10
//time_windows: [[from,to]]
11
}
12
location.push(`${planstop.points[0].y},${planstop.points[0].x}`);
13
14
jobs.push(job);
15
16
});
  • address.data.forEach: Loop through each stop in the address data.
  • job: Create a job object with unique ID, description, location index, service time, and priority.
  • location.push: Add the coordinates of each stop to the location array.
  • jobs.push: Add the job object to the jobs array.

Step 4: Add Shift Start and End Locations

Add the start and end locations to the location index and store their indices.

1
location.push(`${end_location.value}`);
2
let endLocationIndex = location_index; // Store the index for the end location
3
location_index += 1;
4
5
location.push(`${start_location.value}`);
6
let startLocationIndex = location_index; // Store the index for the start location
7
location_index += 1;
  • location.push: Add the end and start locations to the location array.
  • endLocationIndex, startLocationIndex: Store indices for end and start locations.

Step 5: Create Vehicle Objects

Loop through each selected vehicle and create vehicle objects with start and end location indices, shift times, and costs.

1
let veh_fixed_cost = 0;
2
trucks.selectedRows.forEach( (v) => {
3
let vehicle = {
4
id: vehicle_index++,
5
description:v.name,
6
start_index: startLocationIndex,
7
end_index: endLocationIndex,
8
time_window: [shiftStart, shiftEnd],
9
max_tasks: 25,
10
costs: {
11
fixed: veh_fixed_cost
12
}
13
};
14
vehicles.push(vehicle);
15
veh_fixed_cost += 2;
16
});
  • trucks.selectedRows.forEach: Loop through each selected vehicle.
  • vehicle: Create a vehicle object with unique ID, description, start and end location indices, shift times, max tasks, and fixed costs.
  • vehicles.push: Add the vehicle object to the vehicles array.
  • veh_fixed_cost: Increment the fixed cost for each vehicle.

Step 6: Create Locations Object

Create an object to store location data.

1
locations = {
2
id: 12,
3
description: 'GeoTab Test',
4
location: location
5
};

Step 7: Define Optimization Options

Set up the optimization options, including the objective and routing mode.

1
let options = {
2
objective: {
3
travel_cost: "duration",
4
custom: {
5
type: "min",
6
value: "vehicles"
7
}
8
},
9
routing: {
10
mode: "car"
11
}
12
};

Step 8: Store Data in Local Storage

Save the vehicles, shipments, jobs, locations, and options data in local storage for use by 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
9
`localStorage.setValue` stores the data in local storage for use by the Nextbillion Route Optimization API.

Step 9: Trigger Optimization Process

Finally, trigger the optimization process using the saved data.

1
nbaiOptimizationRun.trigger();

By following these detailed steps, you can effectively map and integrate the fetched data with the Nextbillion Route Optimization API, enabling efficient and optimized delivery route planning.

© 2024 NextBillion.ai all rights reserved.