谷歌或工具:AddVariableMinimizedByFinalizer TypeError

Posted

技术标签:

【中文标题】谷歌或工具:AddVariableMinimizedByFinalizer TypeError【英文标题】:Google OR-Tools: AddVariableMinimizedByFinalizer TypeError 【发布时间】:2020-08-19 15:17:19 【问题描述】:

在我的车辆路径问题中,我试图最小化成本目标函数,同时还添加额外的变量以使用routing.AddVariableMinimizedByFinalizer 最小化。

但是,当我执行以下操作时,我得到了 TypeError。从两个CumulVars 创建IntVar 的正确方法是什么,然后您可以将其传递给AddVariableMinimizedByFinalizer

for i in range(data['num_vehicles']):
    duration = time_dimension.CumulVar(routing.End(vehicle_id)) - time_dimension.CumulVar(routing.Start(vehicle_id))
    routing.AddVariableMinimizedByFinalizer(duration)

错误是:

TypeError                                 Traceback (most recent call last)
<ipython-input-15-d27f070bd2ea> in <module>
      6 #         time_dimension.CumulVar(routing.End(i)))
      7     duration = time_dimension.CumulVar(routing.End(vehicle_id)) - time_dimension.CumulVar(routing.Start(vehicle_id))
----> 8     routing.AddVariableMinimizedByFinalizer(duration)

~/.local/lib/python3.8/site-packages/ortools/constraint_solver/pywrapcp.py in AddVariableMinimizedByFinalizer(self, var)
   3925     def AddVariableMinimizedByFinalizer(self, var: "IntVar") -> "void":
   3926         r""" Adds a variable to minimize in the solution finalizer. The solution finalizer is called each time a solution is found during the search and allows to instantiate secondary variables (such as dimension cumul variables)."""
-> 3927         return _pywrapcp.RoutingModel_AddVariableMinimizedByFinalizer(self, var)
   3928 
   3929     def AddVariableMaximizedByFinalizer(self, var: "IntVar") -> "void":

TypeError: in method 'RoutingModel_AddVariableMinimizedByFinalizer', argument 2 of type 'operations_research::IntVar *'

对于其他上下文,这是我在代码中设置时间窗口的方式:

# Create and register a time callback.
def time_callback(from_index, to_index):
    """Returns the travel time between two nodes."""
    ...
    return transit_time

time_callback_index = routing.RegisterTransitCallback(time_callback)

# Add time windows constraint.
routing.AddDimension(
    time_callback_index,
    10 * MIN_PER_HR * SEC_PER_MIN,  # allow waiting time
    24 * MIN_PER_HR * SEC_PER_MIN,  # maximum time per vehicle
    False,  # Don't force start cumul to zero.
    'Time')
time_dimension = routing.GetDimensionOrDie('Time')

# Add time window constraints for each location.
for location_idx, time_window in enumerate(data['time_windows']):
    index = manager.NodeToIndex(location_idx)
    time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
    
for vehicle_id in range(data['num_vehicles']):
    # Limit max on-duty time for each vehicle
    routing.solver().Add(
        time_dimension.CumulVar(routing.End(vehicle_id)) - time_dimension.CumulVar(routing.Start(vehicle_id)) <= MAX_TIME_ON_DUTY)

【问题讨论】:

【参考方案1】:

为什么不直接使用下面的代码呢?

routing.AddVariableMaximizedByFinalizer(
  time_dimension.CumulVar(routing.Start(i)))
routing.AddVariableMinimizedByFinalizer(
  time_dimension.CumulVar(routing.End(i)))

这里你的duration 对象是一个表达式...

【讨论】:

感谢@Mizux!简单的解决方案。然而,这暴露了一个更大的问题,似乎没有考虑运输时间......***.com/questions/63492421/…

以上是关于谷歌或工具:AddVariableMinimizedByFinalizer TypeError的主要内容,如果未能解决你的问题,请参考以下文章

谷歌或工具可用于 jdk 8 吗?

在谷歌或求解器中没有给出距离矩阵的最近距离

谷歌或推出4K Android TV电视棒

loadrunner11怎样实现谷歌或火狐浏览器录制?

从谷歌或微软引用 jQuery可以提高加载速度。

使用cmd提示符搜索谷歌或其他搜索引擎上的单词