首页 > 解决方案 > 如何限制 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)))

标签: or-tools

解决方案


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

stop_index = index_manager.NodeToIndex(stop)

然后你得到这个节点的车辆变量

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 表示节点未执行。


推荐阅读