azal
azal

Reputation: 1260

Google OR Tools to force disjunctions/prevent certain locations from being connected?

I am successfully recreating the pickup and delivery vehicle routing problem but I want to add an extra constraint which I cannot find anywhere how to implement it: I want my vehicles to always return to depot after a pickup-delivery. How can I enforce this constraint? It seems that if I add a disjunction then the solver might end up not delivering to a specific delivery location.

Upvotes: 1

Views: 628

Answers (2)

Martin
Martin

Reputation: 21

You can define vehicle depots (start and/or end) directly when initializing:

# Create the routing index manager.
self.manager = pywrapcp.RoutingIndexManager(len(locations), len(vehicles), start_depots, end_depots)
self.routing = pywrapcp.RoutingModel(self.manager)

start_depots and end_depots are both lists, the size of vehicles. Same depots can be in both lists

Upvotes: 1

azal
azal

Reputation: 1260

Answering my own question:

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
    [4,5,4,6],  # maximum locations per vehicle
    True,  # start cumul to zero
    'Counter')

Upvotes: 2

Related Questions