python - OR-tools:根据python中另一个维度的累积添加一个维度(具体是每24小时使用的车辆成本)
问题描述
我想为车辆使用的每 24 小时添加一个固定成本(使用 Python 版本的 OR-tools)。我不知道如何解决这个问题。
解决方案
我从 James E. Marca 那里收到了一个解决方案的想法(链接:https ://groups.google.com/g/or-tools-discuss/c/zEZ8PaMsV6g/m/W5KwdQN3AAAJ )
“正如你所发现的,你不能在 python 中使用依赖维度。
我建议创建一个明确的每日成本维度。常规节点向成本维度添加零,但创建一个虚拟的“日终”节点,每辆车每天一个,这会增加您的每日成本。所以在回调函数中处理这个逻辑(检查节点类型,如果不是一天结束的节点,则返回零,否则返回每日成本)。
然后通过调用 routing.AddVariableMinimizedByFinalizer(daily_cost_dimension.CumulVar(day_i_vehicle_j_index)) 将所有日终节点添加到求解器的最小化
也许这为你指明了正确的方向?”
[稍后留言]
我只是在考虑第 1 点。我认为您不需要使用额外的成本维度,而是可以与您已经使模型最小化的任何内容(距离或时间)合并。
我的意思是按照我的描述创建每日节点(每天每辆车一个),允许它们被丢弃(零分离),但如果车辆的时间维度大于 24 小时的倍数,则需要访问它们. 然后使到每日节点的距离等于您想要产生的每天的额外成本。
例如,在标准示例中,您有以下内容:
def create_data_model():
"""Stores the data for the problem."""
data = {}
data['distance_matrix'] = [
...
# set distance from all nodes to dummy end-of-day nodes as daily cost
]
...
# Instantiate the data problem.
data = create_data_model()
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
#
# this next line will correctly add "cost" of visiting end-of-day nodes
# if the data was set up correctly earlier
#
return data['distance_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
推荐阅读
- xml - XAMARIN VS2019 中的 RecyclerView 不在设计器中显示数据
- c - 无法链接到自建 LLVM API
- javascript - Javascript - 使用下拉菜单动态更改标题的 h 标签
- json - PostgreSql 交叉表
- google-sheets - 用Google表格上的公式将数字替换为从一张到另一张的字符?
- tableau-api - 如何在其他过滤值中添加过滤值
- java - 握手没有发生,但是邮递员/卷曲命令与 pem 文件一起工作正常
- reactjs - 使用输入数字滚动但在主容器上停止滚动
- overlay - SpatRaster 和使用 terra 包的向量之间的数学运算
- django - Djngo Signals 如何在查询集中找到重复项并将它们指定为发送方和接收方?