Search code examples
pythonconstraint-programmingor-toolsvehicle-routing

How to set minimum locations per route in Google OR-Tools?


I am trying to limit the minimum locations visit per vehicle, I have implemented the maximum location constraint successfully but having issues in figuring out minimum locations. My code for maximum location:

def counter_callback(from_index):
    """Returns 1 for any locations except depot."""
    # Convert from routing variable Index to user NodeIndex.
    from_node = manager.IndexToNode(from_index)
    return 1 if (from_node != 0) else 0;

counter_callback_index = routing.RegisterUnaryTransitCallback(counter_callback)

routing.AddDimensionWithVehicleCapacity(
    counter_callback_index,
    0,  # null slack
    [16,16,16],  # maximum locations per vehicle
    True,  # start cumul to zero
    'Counter')

Solution

  • You should not put a hard limit on the number of nodes as it easily makes the model unfeasible.

    The recommended way is to create a new dimension which just counts the number of visits (the evaluator always returns 1), then push a soft lower bound on the cumulvar of this dimension at the end of each vehicle.