Add Optimization with Multiple Destinations
Overview
The example covers the following features:
- Adding an optimization with multiple vehicles and different destination locations.
- Defining orders with various fields (e.g., time windows, packages, weights).
- Setting up multiple vehicles with specific constraints.
- Displaying the optimized solution on a map.
In this optimization, multiple vehicles start their routes from a single departure point but end at different destinations, allowing for more flexible and efficient route planning.
When you run the example application:
- An optimization is created and saved.
- The optimized solution is returned and displayed on the map.
Create Customers and Orders
Each order must have a customer associated with it. You can either:
- Create a new customer and assign it to the order.
- Use an existing customer (refer to the Get Customer example).
Initializing and Adding Customers
- Initialize a
ProgressListenerandvrp::Service. - Create twelve
vrp::Customerobjects and set the desired fields, and add them to the database. - Call the
addCustomer()method from thevrp::Serviceusing thevrp::CustomerandProgressListenerand wait for the operation to be done.
Initializing and Adding Orders
- Create a
vrp::OrderListand add orders to it. Each order must have a customer associated with it. - Create twelve
vrp::Orderobjects and associate one customer for each, set the desired fields, and add them to the database. - Call the
addOrder()method from thevrp::Serviceusing thevrp::OrderandProgressListenerand wait for the operation to be done.
Configure Optimization Parameters
Configuration Parameters define key settings that influence the behavior of the route optimization process. These settings determine aspects such as optimization goals, search time limits, and flexibility in handling orders.
- Create a
vrp::ConfigurationParametersobject and set the desired parameters. - Set the route type to
RT_CustomEndto allow vehicles to end their routes at different destinations.
Create the Vehicle and Define Vehicle Constraints
Initializing and Adding the Vehicle
Vehicles are the resources that will be used to fulfill the orders. Each vehicle can have specific constraints and capabilities.
- Create a
vrp::VehicleListand add vehicles to it. - Create two
vrp::Vehicleobjects and set the desired fields, such as name, type, status, and capacity. - Call the
addVehicle()method from thevrp::Serviceusing thevrp::VehicleandProgressListenerand wait for the operation to be done.
Define Vehicle Constraints
Vehicle constraints define the limitations and requirements applied to a vehicle during the route optimization process. Ensure that the vehicle operates within its capabilities, such as time windows, capacity, distance, and revenue.
- Create a
vrp::VehicleConstraintsobject and set the desired constraints. - Add these constraints to a
vrp::VehicleConstraintsList.
Create Departure and Destinations
Departures define the starting points for vehicle routes, while destinations define the final stops. These locations play a key role in optimizing route efficiency.
- Create a
vrp::Departureobject for the vehicles' starting point. - Create two
vrp::Destinationobjects for the vehicles' ending points.
Create the Optimization
An optimization represents a set of orders, vehicles, constraints, and other parameters that define a routing problem.
- Create a
vrp::Optimizationobject. - Assign the
OrderList,ConfigurationParameters,VehicleList,VehicleConstraintsList,Departure, andDestinationsto the optimization.
Displaying Orders on the Map
Once the orders have been added, we can display them on the map.

Initialize Map Components
- Create a
MapServiceListener,OpenGLContext, andMapView.
Highlight Orders and Departures
- Create a
LandmarkListandCoordinatesListusing theOrderList,Departure, andDestinations. - Instruct the
MapViewto highlight the landmarks (orders, departure, and destinations). - For better visibility, create a
PolygonGeographicAreafrom theCoordinatesList, and center theMapViewon this area.
Run the Optimization
- Call the
addOptimization()method fromvrp::Service, passing theOptimizationobject and theProgressListener. - After the operation is finished, a solution for optimization will be generated. To view the solution, you need to call the
getSolutionmethod from the optimization, which will return avrp::RouteListcontaining the optimization results.
Display Routes on the Map
Once the optimization is complete and a solution has been found, we can display the solution on the map.

- Ensure that the operation was done, and a solution was found.
- Create a
MarkerCollectionof typePolylinefor each route. - Add the route shapes to the
MarkerCollection. - Set the
MarkerCollectionin the map view preferences. - After highlighting on the map, center the screen over the routes.