Skip to main content

Update Optimization

|

This example demonstrates how to update an existing optimization by making changes to its configuration, vehicles, constraints, and other fields. The updated optimization can also be reoptimized to generate a new solution. If the optimization is not reoptimized, the changes will not be applied to the optimization's routes.

For updating orders, refer to the following examples:

When you run the example application:

  • The existing optimization is updated with new configurations, vehicles, and constraints.
  • A new solution is generated and returned (if reoptimization is enabled).

Retrieve the Existing Optimization

To update an optimization, you first need to retrieve the existing optimization using its ID.

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

gem::vrp::Optimization optimization;
gem::LargeInteger optimizationId = -1; // Set your optimization ID here
int res = serv.getOptimization(&listener, optimization, optimizationId);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 20000);

Update the Optimization

Once the optimization is retrieved, you can update its fields, such as configuration parameters, vehicles, and constraints.

Update Configuration Parameters

  1. Retrieve the existing configuration parameters from the optimization.
  2. Modify the desired fields, such as the name, route type, and distance unit.
gem::vrp::ConfigurationParameters configParams = optimization.getConfigurationParameters();
configParams.setName(configParams.getName() + " updated");
configParams.setRouteType(gem::vrp::ERouteType::RT_EndAnywhere);
configParams.setIgnoreTimeWindow(true);
configParams.setDistanceUnit(gem::vrp::EDistanceUnit::DU_Miles);

Update Vehicles

  1. Create a vrp::VehicleList and add new or updated vehicles to it.
  2. Call the addVehicle() method from the vrp::Service to add the vehicles to the database.
gem::vrp::VehicleList vehicles;
gem::vrp::Vehicle vehicle1;
vehicle1.setName("Vehicle 1");
vehicle1.setType(gem::vrp::EVehicleType::VT_Car);
vehicle1.setStatus(gem::vrp::EVehicleStatus::VS_Available);
vehicle1.setManufacturer("Kia");
vehicle1.setModel("Ceed");
vehicle1.setFuelType(gem::vrp::EFuelType::FT_GasolinePremium);
vehicle1.setConsumption(6.5);
vehicle1.setLicensePlate("BV01ASD");
vehicle1.setMaxWeight(100);
vehicle1.setMaxCube(2.1);

res = serv.addVehicle(&listener, vehicle1);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 5000);
vehicles.push_back(vehicle1);

// Repeat for other vehicles (vehicle2 and vehicle3)...

Update Vehicle Constraints

  1. Retrieve the existing vehicle constraints from the optimization.
  2. Modify the desired fields, such as the maximum number of packages and maximum distance.
gem::vrp::VehicleConstraintsList vehConstraintsList = optimization.getVehiclesConstraints();
gem::vrp::VehicleConstraints vehConstr = vehConstraintsList.at(0);
vehConstr.setMaxNumberOfPackages(80);
vehConstr.setMinNumberOfOrders(1);
vehConstr.setMaxDistance(932); // 932 miles
auto beginIt = vehConstraintsList.begin();
vehConstraintsList.erase(beginIt, beginIt + 1);
vehConstraintsList.insert(vehConstraintsList.begin(), vehConstr);

Update Departures

  1. Create a vrp::Departure object and set the desired fields.
gem::vrp::Departure departure;       
departure.setAlias("Depot");
departure.setCoordinates(gem::Coordinates(48.234270, -2.133208));

Apply the Updates to the Optimization

  1. Set the updated configuration parameters, vehicles, constraints, and departures to the optimization.
optimization.setConfigurationParameters(configParams);
optimization.setVehiclesConstraints(vehConstraintsList);
optimization.setVehicles(vehicles);
optimization.setDepartures({departure});

Update the Optimization and Retrieve the New Solution

Once the optimization 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 updateOptimization() method from the vrp::Service, passing the updated optimization, the request, and the ProgressListener.
  3. Wait for the update process to complete.
std::shared_ptr<gem::vrp::Request> request = std::make_shared<gem::vrp::Request>();
res = serv.updateOptimization(&listener, optimization, request);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 10000);

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 Solution

After the optimization is updated, you can retrieve the new solution.

  1. Create a vrp::RouteList to hold the updated routes.
  2. Call the getSolution() method from the vrp::Optimization object to populate the vrp::RouteList.
gem::vrp::RouteList routes;
res = optimization.getSolution(&listener, routes);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 10000);

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