我有一个应该为装运设置多个时间窗口的实现:
def _set_allowed_time_window(time_dimension, index, time_windows: list):
""" Sets the appropriate time windows for a node. """
# ortools lacks a function to set a list of time windows
# workaround is to set the min and max of a list of sorted time windows as the allowed range
# and then to restrict the times in between the allowed time windows
# see https://github.com/google/or-tools/issues/456 and
# https://groups.google.com/forum/#!topic/or-tools-discuss/MBq1TcqSQTI
earliest_start = int(time_windows[0][0])
latest_end = int(time_windows[len(time_windows)-1][1])
time_dimension.CumulVar(index).SetRange(earliest_start, latest_end)
for tw_index, time_window in enumerate(time_windows):
if tw_index == len(time_windows)-1:
break
time_window_end = int(time_window[1])
next_time_window_start = int(time_windows[tw_index+1][0])
time_dimension.CumulVar(index).RemoveInterval(time_window_end, next_time_window_start)
逻辑上似乎没有错,但 or-tools 无法返回解决方案,除非我删除该行time_dimension.CumulVar(index).RemoveInterval(time_window_end, next_time_window_start)
。任何想法我在这里做错了什么?
这time_windows
是一个 lis,例如: [[100, 200], [300, 400]] 并且index
是从 检索到的索引NodeToIndex
。