Routes
The Route class provides functionalities for managing and optimizing routes within a Vehicle Routing Problem (VRP) solution. A route represents the trip that a vehicle takes to visit a set of orders, and it is part of an optimization solution. The API allows users to:
- Retrieve existing routes by ID or list all routes.
- Update route parameters such as vehicle, constraints, or orders.
- Delete routes when no longer needed.
- Reoptimize existing routes to find better solutions.
- Add or remove orders from an existing route.
- Merge multiple routes into a single route.
- Export route data in various formats.
Route Structure
Each route object consists of the following attributes:
| Name | Type | Description | Setter | Getter |
|---|---|---|---|---|
| Id | LargeInteger | Unique identifier for the route, used for updates. | ❌ | ✅ |
| OptimizationId | LargeInteger | Identifier of the optimization to which the route belongs. | ❌ | ✅ |
| ConfigurationParameters | ConfigurationParameters | Parameters used to configure the route. | ✅ | ✅ |
| Orders | RouteOrderList | List of orders in the route, ordered by the visit order. | ❌ | ✅ |
| Vehicle | Vehicle | The vehicle assigned to the route. | ✅ | ✅ |
| VehicleConstraints | IVehicleConstraints | Constraints that apply to the vehicle for the route. | ✅ | ✅ |
| Departure | Departure | Departure details of the route. | ✅ | ✅ |
| Destination | Destination | Destination details of the route. | ✅ | ✅ |
| MatrixBuildType | EMatrixBuildType | Defines the method used to construct the distance and time matrices. | ✅ | ✅ |
| DistanceMatrices | std::map<EVehicleType, FloatListList> | Distance matrices for the route (used when matrixBuildType is MBT_Set). | ❌ | ✅ |
| TimeMatrices | std::map<EVehicleType, IntListList> | Time matrices for the route (used when matrixBuildType is MBT_Set). | ❌ | ✅ |
| RideStatus | ERideStatus | The current status of the ride. | ❌ | ✅ |
| TotalDistance | float | Total distance traveled by the vehicle (in the set distance unit). | ❌ | ✅ |
| TotalTime | int | Total duration of the route in seconds. | ❌ | ✅ |
| TotalServiceTime | int | Total service time of all the orders in the route (in seconds). | ❌ | ✅ |
| TotalWaitTime | int | Total wait time during the route (in seconds). | ❌ | ✅ |
| NeededFuel | float | Total fuel used during the route. | ❌ | ✅ |
| Cost | float | Total cost of the route. Calculated based on fuel consumption and distance for driving, or 0 for walking. | ❌ | ✅ |
| Shape | CoordinatesList | Shape of the route as a list of coordinates. | ❌ | ✅ |
| CreationTime | Time | Time when the route was created. | ❌ | ✅ |
Managing Routes
Retrieving a Route
There are two ways to retrieve Routes:
a) Get a Route by ID
Get a certain route.
How it works
- Create a
ProgressListener, avrp::Serviceand avrp::Route. - Call the
getRoute()method from thevrp::Serviceusing thevrp::Routefrom 1.), the ID of the route that you want to retrieve and theProgressListener. - Once the operation completes, the
vrp::Routefrom 1.) will be populated.
b) Get All Routes
Returns all routes of the API user (which contain the search term).
How it works
- Create a
ProgressListener, avrp::Serviceand avrp::RouteList. - Call the
getRoutes()method from thevrp::Serviceusing the list from 1.) and theProgressListener. - Once the operation completes, the list from 1.) will be populated.
Updating a Route
Routes can be updated with new parameters, such as vehicle or constraints. Make changes on Route and return the new solution after reptimizing the changed route.
Orders within a route cannot be updated directly using UpdateRoute().
However, all other fields in the route can be modified using this method.
To modify orders, refer to the following examples:
How it works
- Create a
ProgressListenerand avrp::Service. - Retrieve the route you want to update (see Get Route) in a
vrp::Route. - Change the desired fields of the
vrp::Route. - Call the
updateRoute()method from thevrp::Serviceusing thevrp::Routefrom 2.), the route will be reoptimized. - Check if the associated request has reached the finished status. Once completed, you can retrieve the updated route by calling the
getRoute()method, which returns avrp::Routecontaining the updated route.
Example
Deleting a Route
Delete the routes from the list. Routes can be deleted individually or in bulk.
The orders of a deleted route will also be deleted from the optimization to which the route belongs. If the route is the only route of an optimization, then it cannot be deleted, instead delete the optimization (see Delete Optimization).
How it works
- Create a
ProgressListenerandvrp::Service. - Call the
deleteRoute()method from thevrp::Serviceusing the routes' IDs andProgressListenerand wait for the operation to be done.
Example
Reoptimizing a Route
Reoptimization allows you to find a better solution for an existing route.
Rearranges the orders in a better order of visit, if exists. The latest fuel prices are used to calculate the route's cost (see Get Fuel PricesGet Fuel Prices example).
How it works
- Create a
ProgressListener, avrp::Serviceand avrp::Route. - Call the
route.reoptimize()using thevrp::Routefrom 1.) and theProgressListener. - Check if the associated request has reached the finished status. Once completed, you can retrieve the updated route by calling the
getRoute()method, which returns avrp::Routecontaining the reoptimized route.
Example
Adding Orders to a Route
Add a list of orders into an existing route's orders list. The orders will also be added in the optimization's orders list.
There are three options to add orders to a route:
- at the end of the route's orders list.
- at the optimal positions, which are determined by the algorithm.The orders are inserted at the best positions between the route's orders, without rearranging the route's orders.
- at specified positions between the route's orders.
The route can be reoptimized, which means that after the addition, the route orders will be rearranged in the best order of visit, so it doesn’t matter which option was chosen to add the orders. The orders will be also added in the list of orders of the route's optimization.
How it works
- Create a
vrp::RouteOrderfor each order that will be added. Set the specified position at which it will be added using the methodsetIndexInRoute()and set other desired fields. - Insert all the
vrp::RouteOrdercreated at 1.) in avrp::RouteOrderList. - Create a
ProgressListener,vrp::Serviceand avrp::Requestthat will be used for traking the request status. - Retrieve the route like in the example
GetRoute()in avrp::Route. 5 Call theroute.addOrders()method fromvrp::Route, using the list from 2.), a boolean to specify if the route should be reoptimized, a boolean to specify if the orders should be added at the optimal position (in this examples it has to befalse) and theProgressListener. - Check if the associated request has reached the finished status. Once completed, you can retrieve the updated route by calling the
getRoute()method, which returns avrp::Routecontaining the reoptimized route.
Example
Add orders at specified postions. You can also see more examples here: Examples
Deleting an Order from a Route
Delete an order from an existing route. The order will also be deleted from the optimization.
A route must contain at least two orders for one to be deleted. If the order you want to delete is the only one in the route, it cannot be deleted.
How it works
- Create a
ProgressListenerand avrp::Service. - Retrieve the route like in the example
GetRoute()in avrp::Route. - Create a
vrp::RouteOrderand initialize it with the order that you want to delete. - Call the
deleteOrder()method fromvrp::Routefrom 2.) using thevrp::RouteOrderfrom 3.) and theProgressListener. - Once the operation completes, the
vrp::Routefrom 2.) will be updated.
Example
Unlink Routes
Unlinks a route from its optimization. The route will no longer be a part of the optimization's solution. The orders used in this route will be deleted from the optimization. A new optimization will be created for the unlinked route. The new optimization will have same configuration parameters, vehicle constraints and the rest of the fields as the unlinked route.
The route cannot be unlinked if it is the only route in the optimization's solution.
How it works
- Create a
ProgressListenerand avrp::Service. - Call the
unlinkRoute()method from thevrp::Serviceusing the route's id which will be unlinked and theProgressListenerand wait for the operation to be done.
Example
Merging Routes
Merge multiple routes into a new one. A new optimization will be created for the merged route. The optimization will have same configuration parameters, vehicle constraints and the rest of the fields as the first route from the list. The merged route will not be optimized.
Routes with matrices build type set to EMatrixBuildType::MBT_Set, cannot be merged.
How it works
- Create a
ProgressListener, avrp::Service, aLargeIntListwith the route ids to be merged and avrp::Routein which the merged route will be returned. - Call the
mergeRoutes()method from thevrp::Serviceusing thevrp::Routeand list from 1.) and theProgressListener. - Once the operation completes, the merged route will be returned in the
vrp::Routefrom 1.)
Example
Error Handling
The API provides specific error codes to identify potential issues. Below is a summary of common errors and their solutions:
| Error Code | Description | Solution |
||-|-|
| KInvalidInput | Missing required fields or invalid data. | Ensure all mandatory fields are filled. |
| KNotFound | The specified route ID does not exist. | Verify that the correct route ID is used. |
| KInternalAbort | Server-side issue or unexpected error. | Retry the request or check API status. |
| KNoRoute | Route could not be reoptimized. | Check constraints and parameters. |
Request Handling
Route operations such as reoptimize, addOrders, and mergeRoutes return a Request object. This object allows you to track the status of the operation.
Request Structure
- Request ID (LargeInteger): Unique identifier for the request.
- Creation Time (LargeInteger): The time when the request was created.
- Entity ID (LargeInteger): The ID of the entity (e.g., route) associated with the request.
- Status (ERequestStatus): The current status of the request (e.g., created, in progress, finished).
- ErrorMessage (std::string): A message describing any errors that occurred.