or-tools - 如何限制 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)
然后你得到这个节点的车辆变量
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 表示节点未执行。
推荐阅读
- elasticsearch - Elasticsearch:“一个索引有很多分片”与“很多索引每个索引一个分片”
- spring-boot - 如何使用 Spring Boot 和 Kotlin 启动 Web 应用程序
- r - 如何在图形上用直线分割多边形?
- javascript - 如何将行号存储在数组中?
- apache-spark - 用 spark Scala 替换campaign_id 列的缺失值
- tableau-api - 想要使用 excel vba 从画面仪表板单击下载按钮
- react-native - 是否可以在类中使用反应钩子组件?
- android - 如何在 AR 应用程序中共享 GPS 数据
- python - 如何以特定格式将文件作为参数传递给 Python 中的参数?
- react-native - react native导入时如何更改默认组件名称?