Google 或强制 disjunctions/prevent 连接某些位置的工具?

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

我成功地重新创建了 pickup and delivery vehicle routing problem,但我想添加一个额外的约束,我无法在任何地方找到如何实现它:我希望我的车辆总是 return 在取货后停放 -送货。我如何强制执行此约束?似乎如果我添加一个析取,那么求解器可能最终不会交付到特定的交付位置。

回答我自己的问题:

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')

可以在初始化的时候直接定义车场(开始and/or结束):

# 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都是列表,车辆的大小。两个列表中可以有相同的 depot