# Add Capacity constraint.
def demand_callback(from_index):
"""Returns the demand of the node."""
# Convert from routing variable Index to demands NodeIndex.
from_node = manager.IndexToNode(from_index)
location = locations[from_node]
number_of_packages = location.number_of_packages
if location.is_drop:
number_of_packages *= -1
return number_of_packages
demand_callback_index = routing.RegisterUnaryTransitCallback(demand_callback)
routing.AddDimensionWithVehicleCapacity(
demand_callback_index,
0, # null capacity slack
[50], # vehicle maximum capacities
True, # start cumul to zero
'Capacity',
)
--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/93636ef5-6fd5-4387-b6fa-ca3c9d8be25d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
def demand_callback(self, from_index):
"""Returns the demand of the node."""
# Convert from routing variable Index to demands NodeIndex.
from_node = self.manager.IndexToNode(from_index)
if from_node == 0: # Start location
return 0
else:
location = locations[from_node]
number_of_packages = location.number_of_packages
# If it is a Drop, the demand is negative.
if not location.is_pickup:
number_of_packages *= -1
return number_of_packages
demand_callback_index = routing.RegisterUnaryTransitCallback(demand_callback)
routing.AddDimensionWithVehicleCapacity(
demand_callback_index,
0, # null capacity slack
[20 for _ in range(number_of_vehicles)], # vehicle maximum capacities
True, # start cumul to zero
'Capacity',
)
horizon = 20 * 60 * 60 # Extreme time duration, to make sure that isn't bottleneck. With capacity it should be done in 1,5 hour.
routing.AddDimension(
transit_callback_index,
horizon, # allow waiting time
horizon, # maximum time per vehicle,
False, # Don't force start cumul to zero.
'Time',
)
time_dimension = routing.GetDimensionOrDie('Time')
demand_dimension = routing.GetDimensionOrDie('Capacity')
# Make sure pickup and delivery happens on same vehicle.
node = 1
while node <= len(self.orders) * 2:
pickup_index = self.manager.NodeToIndex(node)
delivery_index = self.manager.NodeToIndex(node + 1)
# routing.AddPickupAndDelivery(pickup_index, delivery_index)
routing.solver().Add(routing.VehicleVar(pickup_index) == routing.VehicleVar(delivery_index))
routing.solver().Add(time_dimension.CumulVar(pickup_index) <= time_dimension.CumulVar(delivery_index))
routing.solver().Add(routing.ActiveVar(delivery_index) * demand_dimension.CumulVar(delivery_index) >= routing.ActiveVar(delivery_index)) # Added by your suggestion.
node += 2
# Add time window constraints for each vehicle start node.
for vehicle_id in range(number_of_vehicles):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(0, horizon)
routing.AddVariableMinimizedByFinalizer(
time_dimension.CumulVar(routing.Start(vehicle_id)))
routing.AddVariableMinimizedByFinalizer(
time_dimension.CumulVar(routing.End(vehicle_id)))
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools...@googlegroups.com.
location = locations[from_node]
number_of_packages = location.number_of_packages
# If it is a Drop, the demand is negative.
if not location.is_pickup:
number_of_packages *= -1
But then when you set the constraints for pickup and dropoff in the while loop, you just count through integers. Why aren’t you again iterating over the locations object? Without seeing all your code I can only guess, but that looks suspect. In my code, typically I store the link from pickup to delivery. Then I use the built in routing.AddPickupAndDelivery(pickup_index, delivery_index) call (see the guide https://developers.google.com/optimization/routing/pickup_delivery)
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/4136593a-6de2-4406-9923-7f42a65e3d26%40googlegroups.com.