Skip to main content

Update Route

|

This example demonstrates how to update an existing route by making changes to its configuration, vehicle constraints, and other fields. The updated route can also be reoptimized to generate a new order of visits. If the route is not reoptimized, the changes will not affect the route's optimization. However, certain configuration changes (e.g., ignoring time windows, optimization criteria, etc.) automatically trigger reoptimization.

For updating orders, refer to the following examples:

When you run the example application:

  • The existing route is updated with new configurations, vehicle constraints, and other fields.
  • The route is reoptimized (if specified), and a new order of visits is returned.

Retrieve the Existing Route

To update a route, you first need to retrieve the existing route using its ID.

  1. Create a ProgressListener and vrp::Service.
  2. Retrieve the route using the getRoute() method from the vrp::Service.
ProgressListener listener;
gem::vrp::Service serv;

gem::vrp::Route route;
gem::LargeInteger routeId = 0; // Set your route ID here
int res = serv.getRoute(&listener, route, routeId);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 20000);

Update the Route

Once the route is retrieved, you can update its fields, such as configuration parameters, vehicle constraints, and other properties.

Update Configuration Parameters

  1. Retrieve the existing configuration parameters from the route.
  2. Modify the desired fields, such as the name and route type.
gem::vrp::ConfigurationParameters configParams = route.getConfigurationParameters();
configParams.setName(configParams.getName() + " updated");
configParams.setRouteType(gem::vrp::ERouteType::RT_RoundRoute);

Update Vehicle Constraints

  1. Retrieve the existing vehicle constraints from the route.
  2. Modify the desired fields, such as the maximum number of packages and maximum distance.
gem::vrp::VehicleConstraints vehConstr = route.getVehicleConstraints();
vehConstr.setMaxNumberOfPackages(80);
vehConstr.setMaxDistance(700); // 700 miles

Apply the Updates to the Route

  1. Set the updated configuration parameters and vehicle constraints to the route.
route.setConfigurationParameters(configParams);
route.setVehicleConstraints(vehConstr);

Update the Route and Retrieve the New Solution

Once the route is updated, you can apply the changes and retrieve the new solution.

  1. Create a std::shared_ptr<gem::vrp::Request> to hold the update request.
  2. Call the updateRoute() method from the vrp::Service, passing the updated route, the request, and the ProgressListener.
  3. Wait for the update process to complete.
bool reoptimize = true; // Set to true to reoptimize the route
std::shared_ptr<gem::vrp::Request> request = std::make_shared<gem::vrp::Request>();
res = serv.updateRoute(&listener, route, request);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 20000);

WAIT_UNTIL([&]() {
serv.getRequest(&listener, request, request->id);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 7000);
return request->status == gem::vrp::ERequestStatus::eFinished;
}, 40000);

Retrieve the Updated Route

After the update process is complete, you can retrieve the updated route.

  1. Create a new vrp::Route object to hold the updated route.
  2. Call the getRoute() method from the vrp::Service to populate the vrp::Route object with the updated route.
gem::vrp::Route getRoute;
res = serv.getRoute(&listener, getRoute, route.getId());
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 10000);
route = getRoute;

if (listener.IsFinished() && listener.GetError() == gem::KNoError && res == gem::KNoError)
std::cout << "Route updated successfully" << std::endl;
else
std::cout << "Route couldn't be updated or reoptimized" << std::endl;