我成功地重新创建了取车和送货车辆的路线问题,但我想添加一个我在任何地方都找不到的额外约束:我希望我的车辆总是在取车后返回仓库。如何强制执行此约束?似乎如果我添加了一个析取,那么求解器可能最终无法传递到特定的传递位置。
回答我自己的问题:
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')
您可以在初始化时直接定义车辆段(起始和/或结束(:
# 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和end_depots都是车辆大小的列表。相同的仓库可以在两个列表中