IBM ILOG Dispatcher User's Manual > The Basics > Minimizing the Number of Vehicles > Solve > Define the closeEmptyVehicles function

Next, you define the closeEmptyVehicles function. You use an instance of IloIterator and the member function IloRoutingSolution::getRouteSize to locate any vehicles that have an empty route. Vehicles that are not used in the solution have a route size of 0 (zero). You constrain these vehicles to be closed by adding the constraint that the next-variable of their first visit is set equal to their last visit. In other words, they never leave the depot.

Step 9   -  

Close empty vehicles

Add the following code after the comment //Close empty vehicles

void RoutingSolver::closeEmptyVehicles() {
  for (IloIterator<IloVehicle> iter(_env); iter.ok(); ++iter) {
    IloVehicle vehicle = *iter;
    IloInt size = _solution.getRouteSize(vehicle);
    if (size == 0) {
      _mdl.add(vehicle.getFirstVisit().getNextVar() == vehicle.getLastVisit());
    }
  }
}