Add Optimization with Orders in the Same Route
This example demonstrates how to create an optimization where specific orders must be visited by the same vehicle, ensuring they are part of the same route. The example covers the following features:
- Adding an optimization with custom configuration parameters.
- Defining orders with specific sequences that must be visited by the same vehicle.
- Displaying the optimized solution on a map.
When you run the example application:
- An optimization is created and saved.
- The optimized solution is returned and displayed on the map.
Step-by-Step Guide
Step 1: Initialize the API Key and the Environment
To use the Magic Lane SDK, we first need to initialize the API key (project API token) and set up the environment. This ensures that the SDK is properly authenticated and configured for further usage.
Initializing the API Key
There are three ways to set the API key in the code:
-
Manual Assignment
- If neither the macro nor the environment variable is set, the API key must be manually assigned by modifying the source code.
- This is the least secure method and is only recommended for development purposes.
-
Predefined Macro (
API_TOKEN)- If the API key is defined at compile time as
API_TOKEN, it is automatically assigned toprojectApiToken. - This approach is useful when the API key is included in the build configuration.
- If the API key is defined at compile time as
-
Environment Variable (
GEM_TOKEN)- If
API_TOKENis not defined, the program attempts to retrieve the API key from the system environment variableGEM_TOKEN. - This method allows for better security as the API key does not need to be hardcoded.
- If
You must choose one method to initialize the API_KEY. If you are unsure what an API_KEY is, you can visit the Get Started page for more information
Preparing the Environment
Once the API key is set, we need to initialize the environment using the SdkSession class. The Environment class manages the SDK setup and provides functionalities for initializing and releasing the SDK, handling OpenGL rendering, managing event listeners and UI callbacks, and waiting for events like timeouts or window closures.
Step 2: 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. - Create a
gem::vrp::OrdersSequenceMapto specify the association between different orders that should be visited in a certain order. In this example, we define two sequences of orders that must be visited by the same vehicle.
Create Vehicles and 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.
Initializing and Adding Vehicles
- Create a
gem::vrp::VehicleListand add vehicles to it. - Create two
vrp::Vehicleobjects and set the desired fields, and add them to the database. - Call the
addVehicle()method from thevrp::Serviceusing thevrp::VehicleandProgressListenerand wait for the operation to be done.
Define Vehicle Constraints
- Create a
vrp::VehicleConstraintsobject for each vehicle. - Add these constraints to a
vrp::VehicleConstraintsList.
Create the Departure and Destinations
Departures define the starting points for vehicle routes. These locations serve as the origin of a route and can impact optimization by influencing travel distance and time. Destinations define the final stop for a vehicle route. These locations mark the endpoint of a route and play a key role in optimizing route efficiency.
Initializing Departures and Destinations
- Create a
vrp::Departureobject. - Create a
vrp::Destinationobject.
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,Departures, 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,Departures, andDestinations. - Instruct the
MapViewto highlight the landmarks (orders, departures, and destinations). - For better visibility, create a
PolygonGeographicAreafrom theCoordinatesListand 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.