Skip to main content

Reoptimize Route

|

This example demonstrates how to reoptimize an existing route to rearrange the orders in a better sequence of visits, if such a sequence exists. The reoptimization process uses the latest fuel prices to calculate the route's cost. For more details on retrieving fuel prices, refer to the Get Fuel Prices.

When you run the example application:

  • The existing route is reoptimized to improve the order of visits.
  • The updated route is returned, reflecting the new sequence and cost.

Retrieve the Existing Route

To reoptimize 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), 10000);

Reoptimize the Route

Once the route is retrieved, you can reoptimize it to improve the order of visits.

  1. Create a std::shared_ptr<gem::vrp::Request> to hold the reoptimization request.
  2. Call the reoptimize() method from the vrp::Route object, passing the ProgressListener and the request.
  3. Wait for the reoptimization process to complete.
std::shared_ptr<gem::vrp::Request> request = std::make_shared<gem::vrp::Request>();
res = route.reoptimize(&listener, 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 Reoptimized Route

After the reoptimization 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 reoptimized successfully" << std::endl;
else
std::cout << "Route couldn't be reoptimized" << std::endl;