Skip to main content

Reoptimize Optimization

|

This example demonstrates how to reoptimize an existing optimization to generate a new and potentially better solution. The reoptimization process uses the latest fuel prices to calculate the new solution (refer to the Get Fuel Prices example).

When you run the example application:

  • The existing optimization is reoptimized.
  • A new solution is generated and returned.

Retrieve the Existing Optimization

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

Reoptimize the Optimization

Once the optimization is retrieved, you can reoptimize it to generate a new solution.

  1. Create a std::shared_ptr<gem::vrp::Request> to hold the reoptimization request.
  2. Call the reoptimize() method from the vrp::Optimization 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 = optimization.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 Solution

After the reoptimization process is complete, you can retrieve the new solution.

  1. Create a vrp::RouteList to hold the reoptimized 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 reoptimized successfully" << std::endl;
else
std::cout << "Optimization couldn't be reoptimized" << std::endl;