Skip to main content

Merge Routes

|

This example demonstrates how to merge multiple routes into a single route. A new optimization will be created for the merged route, inheriting the configuration parameters, vehicle constraints, and other settings from the first route in the list. The merged route will not be optimized automatically.

Info

After merging routes, reoptimize the new route using Reoptimize Optimization to improve efficiency. Verify that vehicle constraints (capacity, distance limits) remain suitable for the combined load, and check that order time windows are preserved in the merged schedule.

When you run the example application:

  • The selected routes are merged into a single route.
  • A new optimization is created for the merged route.
  • The merged route is returned without reoptimization.
  • The route and its orders are displayed on an interactive map

Retrieve the Routes to Be Merged

  1. Create a ProgressListener, a vrp::Service, a LargeIntList with the route ids to be merged and a vrp::Route in which the merged route will be returned.
  2. Call the mergeRoutes() method from the vrp::Service using the vrp::Route and list from 1.) and the ProgressListener.
  3. Once the operation completes, the merged route will be returned in the vrp::Route from 1.)
    ProgressListener listener;
gem::vrp::Service serv;

gem::LargeIntList routeIds;
routeIds.push_back(0); // Set the ID of the first route
routeIds.push_back(0); // Set the ID of the second route
routeIds.push_back(0); // Set the ID of the third route

gem::vrp::Route mergedRoute;
int res = serv.mergeRoutes(&listener, mergedRoute, routeIds);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 20000);

Display the Merged Route on the Map

After the routes are merged successfully, we can display them on a map by following these steps:

  1. Create a MapServiceListener, OpenGLContext, and MapView.
  2. Extract landmarks and coordinates from the merged route.
  3. Highlight orders using LandmarkList.
  4. Center the map on the route.
  5. Draw the merged route using a MarkerCollection.
    MapServiceListener mapListener;
OpenGLContext glContext;
MapView mapView;

LandmarkList landmarks;
CoordinatesList routeCoordinates;

for (const auto& order : mergedRoute.getOrders()) {
landmarks.push_back(Landmark(order.getCoordinates()));
routeCoordinates.push_back(order.getCoordinates());
}

PolygonGeographicArea routeArea(routeCoordinates);
mapView.highlight(landmarks);
mapView.centerOn(routeArea);

MarkerCollection polylineMarkers;
polylineMarkers.setPolyline(mergedRoute.getShape());
mapView.setMarkerCollection(polylineMarkers);