Optimizations
The Optimization class is a core component of the Fleet Management SDK. An optimization represents a set of orders, vehicles, constraints, and other parameters that define a routing problem. Whenever an optimization operation - such as creating, updating, or reoptimizing - is performed, it returns a Request object. This object allows you to track the status and progress of the operation asynchronously. It includes details such as the request ID, creation time, associated entity, status, and any error messages. Once the request is completed, the resulting solution can be retrieved. For more information about request handling, see the end of this page.
The API allows users to:
- Create new optimizations with orders, vehicles, and constraints.
- Retrieve existing optimizations by ID or list all optimizations.
- Update optimization parameters such as constraints or vehicles.
- Delete optimizations when no longer needed.
- Reoptimize existing optimizations to find better solutions.
- Add or remove orders from an existing optimization.
- Retrieve solutions for an optimization.
Optimization Structure
Each optimization object consists of the following attributes:
| Name | Type | Description | Setter | Getter |
|---|---|---|---|---|
| Id | LargeInteger | Unique identifier for the optimization, used for updates. | ❌ | ✅ |
| ConfigurationParameters | ConfigurationParameters | Configuration parameters for the optimization. | ✅ | ✅ |
| Orders | OrderList | List of orders to be delivered or picked up during the optimization. | ✅ | ✅ |
| Vehicles | VehicleList | List of vehicles available for optimization. | ✅ | ✅ |
| VehiclesConstraints | VehicleConstraintsList | Constraints that apply to the vehicles in the optimization. If there are multiple vehicles, you can either set a single set of constraints that applies to all vehicles or apply different constraints for each vehicle. | ✅ | ✅ |
| Departures | DepartureList | List of departure points where the vehicles start their routes. | ✅ | ✅ |
| Destinations | DestinationList | List of destinations where the vehicles finish their routes. | ✅ | ✅ |
| MatrixBuildType | EMatrixBuildType | Defines the method used to construct the distance and time matrices for the optimization. | ✅ | ✅ |
| DistanceMatrices | std::map<EVehicleType, FloatListList> | Distance matrices for the optimization (used when matrixBuildType is MBT_Set). A different distance matrix is used for each vehicle type from optimization. | ✅ | ✅ |
| TimeMatrices | std::map<EVehicleType, IntListList> | Time matrices for the optimization (used when matrixBuildType is MBT_Set). A different time matrix is used for each vehicle type from optimization. | ✅ | ✅ |
| CreationTime | Time | Time when the optimization was created (in Epoch format). | ❌ | ✅ |
Managing Optimizations
Creating an Optimization
An optimization must be created before it can be solved. It defines the routing problem by including orders, vehicles, and constraints. To solve the optimization, the system initiates the process and returns a Request object. This object allows you to track the status of the optimization until the solution is ready.
Adding an optimization returns a Request object, which allows you to track the status of the optimization process. Once the request status is finished, the solution can be retrieved using getSolution(). A route is the trip that a vehicle travels to visit the orders, so the number of routes returned is maximum the number of vehicles set in the optimization(see Routes). If in the optimization only one vehicle is used, then the solution will contain only one route.
How it works
- Create a
vrp::OrderListand add the orders to it. Each order needs to have a customer set; you can either add a new customer and then set it to the order, or you can use a previously created customer (see Get Customer) - Create a
vrp::ConfigurationParametersand set the desired parameters. - Create one
vrp::VehicleConstraintsfor each vehicle and set them to avrp::VehicleConstraintsList. - Create a
vrp::Optimization, set the desired fields and the objects created at 1.), 2.) and 3.) to it. - Create a
ProgressListener,vrp::Service, and avrp::Requestthat will be used for traking the request status. - Call the
addOptimization()method fromvrp::Serviceusing the list from 5.), thevrp::Optimizationfrom 4.) and the progress listener. - After adding an optimization, check whether the associated request has reached the finished status. Once completed, you can retrieve the optimization results by calling the
getSolution()method, which returns avrp::RouteListcontaining the generated routes.
Example
Retrieving the Solution for an Optimization
Returns the solution for an optimization. The result is a list of routes.
How it works
- Create a
ProgressListener, avrp::Serviceand avrp::RouteList. - Call the
getSolutionForOptimization()method from thevrp::Serviceusing the list from 1.), the ID of the optimization and theProgressListener. - Once the operation completes, the list from 1.) will be populated.
Example
Retrieving Optimizations
There are two ways to retrieve optimization data:
a) Get an Optimization by ID
Get a certain optimization
How it works
- Create a
ProgressListener, avrp::Serviceand avrp::Optimization. - Call the
getOptimization()method from thevrp::Serviceusing thevrp::Optimizationfrom 1.), the ID of the optimization that you want to retrieve and theProgressListener. - Once the operation completes, the
vrp::Optimizationfrom 1.) will be populated.
b) Get All Optimizations (with optional filtering)
Returns all optimizations of the API user (which contain the search term).
How it works
- Create a
ProgressListener, avrp::Serviceand avrp::OptimizationList. - Call the
getOptimizations()method from thevrp::Serviceusing the list from 1.) and theProgressListener. - Once the operation completes, the list from 1.) will be populated.
Updating an Optimization
Makes changes to an optimization and triggers a reoptimization. The updated solution can be retrieved using getSolution() once the request status is Finished.
Orders within an optimization cannot be updated directly using UpdateOptimization().
However, all other fields in the optimization can be modified using this method.
To modify orders, refer to the following examples:
- Adding Orders to an Optimization
- Updating an Order from an Optimization
- Deleting an Order from an Optimization
Once the optimization is updated, it will be reoptimized.
The reoptimized solution can be retrieved using getSolution() once the request status is Finished.
How it works
- Create a
ProgressListenerand avrp::Service. - Retrieve the optimization you want to update (see Get Optimization) in a
vrp::Optimization. - Change the desired fields of the
vrp::Optimization. - Create a
vrp::RouteList, and avrp::Requestthat will be use for traking the status of the operation. - Call the
updateOptimization()method from thevrp::Serviceusing the list from 4.), thevrp::Optimizationfrom 2.), the optimization will be reoptimized. - Check if the associated request has reached the finished status. Once completed, you can retrieve the optimization results by calling the
getSolution()method, which returns avrp::RouteListcontaining the reoptimized routes.
Example
Deleting an Optimization
Delete the optimizations for the user. Optimizations can be deleted individually or in bulk.
When an optimization is deleted, the routes assigned to that optimization are also deleted.
How it works
- Create a
ProgressListenerandvrp::Service. - Call the
deleteOptimization()method from thevrp::Serviceusing the optimization's id andProgressListenerand wait for the operation to be done.
Example
Reoptimizing an Optimization
Creates a new and better solution (if exists) for the optimization. The latest fuel prices are used for the new solution (see Get Fuel Prices).
How it works
- Create a
ProgressListener, avrp::Serviceavrp::RouteListand avrp::Request. - Call the
reoptimizeOptimization()method from thevrp::Serviceusing thevrp::RouteListfrom 1.), the ID of the optimization that you will be reoptimized and theProgressListener. - Check if the associated request has reached the finished status. Once completed, you can retrieve the optimization results by calling the
getSolution()method, which returns avrp::RouteListcontaining the reoptimized routes.
Example
Adding Orders to an Optimization
Add orders to the optimization. The orders will be added at the end of the existing optimization's orders list. If the optimization is also reoptimized, the added orders will be assigned to the optimization's routes.
How it works
- Create a
vrp::Orderwith the desired fields for each order that will be added and insert them in avrp::OrderList. - Create a
ProgressListenerandvrp::Service. - Retrieve the optimization like in the example
GetOptimization()in avrp::Optimization. - Call the
optimization.addOrders()method fromvrp::Optimizationusing the list from 1.), a boolean to specify if the optimization should be reoptimized and theProgressListener. - Check if the associated request has reached the finished status. Once completed, you can retrieve the optimization results by calling the
getSolution()method, which returns avrp::RouteListcontaining the generated routes.
Example
Updating an Order from an Optimization
Update an order from an existing optimization.
These changes will also be seen in the route orders for the routes involved in the optimization. So when you want to update a route order you can use this method.
How it works
- Create a
ProgressListenerand avrp::Service. - Retrieve the optimization like in the example
GetOptimization()in avrp::Optimization. - Create a
vrp::Orderand initialize it with the optimization order that you want to update. - Make the desired changes on the
vrp::Orderfrom 3.). - Call the
optimization.updateOrder()method from thevrp::Optimizationfrom 2.) using thevrp::Orderfrom 3.) and theProgressListener. - Once the operation completes, the
vrp::Optimizationfrom 2.) will contain the new optimization with the order updated.
Example
Deleting an Order from an Optimization
Delete an order from an existing optimization. It will not appear in the Route as well.
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 optimization like in the example
GetOptimization()in avrp::Optimization. - Create a
vrp::Orderand initialize it with the order that you want to delete. - Call the
deleteOrder()method fromvrp::Optimizationfrom 2.) using thevrp::Orderfrom 3.) and theProgressListener. - Once the operation completes, the new optimization will be returned in the
vrp::Optimizationfrom 2.).
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. You can check the error message in the request to see more details. |
| KNotFound | The specified optimization ID does not exist.| Verify that the correct optimization ID is used. |
| KInternalAbort | Server-side issue or unexpected error. | Retry the request or check API status. |
| KNoRoute | Optimization could not be solved. | Check constraints and parameters. |
Request Handling
Optimization operations such as addOptimization, updateOptimization, and reoptimize return a Request object. This object allows you to track the status of the operation.
Request Structure
| Name | Type | Description | Setter | Getter |
|---|---|---|---|---|
| RequestId | LargeInteger | Unique identifier for the request. | ❌ | ✅ |
| CreationTime | LargeInteger | The time when the request was created. | ❌ | ✅ |
| EntityId | LargeInteger | The ID of the entity (e.g., optimization) 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 during processing. | ❌ | ✅ |