Skip to main content

Add Orders to an Optimization

|

This example demonstrates how to add a list of orders to an existing optimization. The newly added orders will be assigned to the optimization’s routes if the optimization is reoptimized. If reoptimization is not enabled, the added orders will be stored but not assigned to routes.

When you run the example application:

  • The specified orders are added to the existing optimization.
  • If reoptimization is enabled, the optimization will be updated, and a new solution will be generated.

Retrieve the existing Optimization

To add orders to an optimization, you have to retrieve that optimization by 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 = 0; // Set your optimization ID here
int ret = serv.getOptimization(&listener, optimization, optimizationId);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 20000);

Create Customers and Orders

Prepare customers and their associated orders, which will then be added in optimization.

Creating Customers

  1. Create vrp::Customer objects and set the necessary fields such as coordinates, alias, phone number, and email.
  2. Use the addCustomer() method from vrp::Service to add customers.
gem::vrp::Customer c1;
c1.setCoordinates(gem::Coordinates(47.016075, -0.849623));
c1.setAlias("c1");
c1.setPhoneNumber("+12312312");
c1.setEmail("c1@yahoo.com");
ret = serv.addCustomer(&listener, c1);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 5000);

// Create another customer
gem::vrp::Customer c2(gem::Coordinates(45.212821, 3.166858));
c2.setAlias("c2");
c2.setPhoneNumber("+12312312");
c2.setEmail("c2@yahoo.com");
ret = serv.addCustomer(&listener, c2);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 5000);

Creating and Adding Orders

  1. Create vrp::Order objects associated with the customers.
  2. Set necessary attributes such as the number of packages, service time, and order type.
  3. Use the addOrder() method from vrp::Service to add the orders.
gem::vrp::OrderList ordersToAdd;
gem::vrp::Order orderToAdd1(c1);
orderToAdd1.setNumberOfPackages(5);
orderToAdd1.setServiceTime(600);
orderToAdd1.setType(gem::vrp::EOrderType::OT_PickUp);
ret = serv.addOrder(&listener, orderToAdd1, false);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 5000);
ordersToAdd.push_back(orderToAdd1);

// Create another order
gem::vrp::Order orderToAdd2(c2);
orderToAdd2.setNumberOfPackages(4);
orderToAdd2.setType(gem::vrp::EOrderType::OT_Delivery);
ret = serv.addOrder(&listener, orderToAdd2, false);
WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &listener), 5000);
ordersToAdd.push_back(orderToAdd2);

Add Orders to Optimization and Reoptimize

Once the orders are created, they need to be added to the optimization.

  1. Create a std::shared_ptr<gem::vrp::Request> to hold the request.
  2. Call the addOrders() method from vrp::Optimization.
  3. Wait for the operation to complete.
bool reoptimize = true;
std::shared_ptr<gem::vrp::Request> request = std::make_shared<gem::vrp::Request>();
ret = optimization.addOrders(&listener, ordersToAdd, request, reoptimize);
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);

//After adding the orders and reoptimizing, check if the operation was successful.
if (listener.IsFinished() && listener.GetError() == gem::KNoError && ret == gem::KNoError)
std::cout << "Orders added successfully" << std::endl;
else
std::cout << "Failed to add orders" << std::endl;