首页 > 解决方案 > OR-tools:根据python中另一个维度的累积添加一个维度(具体是每24小时使用的车辆成本)

问题描述

我想为车辆使用的每 24 小时添加一个固定成本(使用 Python 版本的 OR-tools)。我不知道如何解决这个问题。

标签: pythonroutesor-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) 

推荐阅读