如何限制 VehicleVar 的域?

How to restrict the domain of a VehicleVar?

我想限制车辆变量的域。例如,停靠点 1 应由车辆 0 或 1 提供服务。

Python:

n_stops = 11
n_vehicles = 3
ix_depot = 0

index_manager = pywrapcp.RoutingIndexManager(n_stops, n_vehicles, ix_depot)
routing_model = pywrapcp.RoutingModel(index_manager)
cpsolver = routing_model.solver()

stop = 1
admissible_vehicles = [0, 1]

# missing code here

assignment = routing_model.Solve()
print(assignment.Value(routing_model.VehicleVar(stop)))

首先需要获取停止节点的内部索引

stop_index = index_manager.NodeToIndex(stop)

然后就得到了这个节点的车辆var

stop_vehicle_var = routing_model.VehicleVar(stop_index)

最后,你可以限制这个变量的域

routing.solver().Add(routing.solver().MemberCt(stop_vehicle_var, [0, 1]))

请注意,如果使用[0, 1],必须执行节点。 如果该节点是可选的,则使用[-1,0,1],因为-1表示该节点未执行。