Google/OR-Tools 获取持续时间和距离

Google/OR-Tools Get Duration And Distance

我正在尝试了解 MVRP 示例中的解决方案调用

我有两个矩阵,持续时间和距离,它们已通过调用 google

返回

我的解决方案是基于距离,但考虑到我已经返回了数据,我想找到与持续时间关联的索引。 不幸的是,我不完全确定路由调用的幕后发生了什么,所以希望有一个简单的快速答案来查找和使用什么索引

为简单起见,我将展示 google 示例而不是我的代码,并突出显示我要查找的内容:

    public string PrintSolution()
        {
            // Inspect solution.
            string ret = "";
            long maxRouteDistance = 0;
            for (int i = 0; i < _data.Drivers; ++i)
            {
                ret += $"Route for Vehicle {i}:";
                ret += Environment.NewLine;
                long routeDistance = 0;
                var index = _routing.Start(i);
                while (_routing.IsEnd(index) == false)
                {
                    ret += $"{_manager.IndexToNode((int) index)} -> ";

                    var previousIndex = index;
                    index = _solution.Value(_routing.NextVar(index));
                    long legDistance = _routing.GetArcCostForVehicle(previousIndex, index, i);
                    //LOOKING FOR
                    //long legDuration = ??? what index am is using here to find in my duration matrix which is built the same as indexes as distance
                    ret += " leg distance: " + legDistance;
                    routeDistance += legDistance;
                }

                ret += $"{_manager.IndexToNode((int) index)}";
                ret += Environment.NewLine;
                ret += $"Distance of the route: {routeDistance}m";
                ret += Environment.NewLine;
                ret += Environment.NewLine;
                maxRouteDistance = Math.Max(routeDistance, maxRouteDistance);
            }

            ret += $"Maximum distance of the routes: {maxRouteDistance}m";
            ret += Environment.NewLine;
            return ret;
        }

@Mizux

免责声明:这是一种简化,但应该有助于您理解。

在 OR-Tools Routing 中有一个没有名称的原始“隐藏”维度,但您可以使用 RoutingModel::GetArcCostForVehicle()

检索成本

对于任何“常规”维度,您可以在每个节点检查 CumulVar。 例如假设您使用名称为“距离”和“持续时间”的 RoutingModel::AddDimension() 创建了两个维度。 注意:CumulVar 是一个累加器,所以如果你想要“电弧成本”,你需要这样的东西 dim.CumulVar(next_index) - dim.CumulVar(index)

然后在你的 PrintFunction 中你可以使用:

public string PrintSolution()
{
  ...
  RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");
  RoutingDimension durationDimension = routing.GetMutableDimension("Duration");
  for (int i = 0; i < _manager.getNumberOfVehicles(); ++i)
  {
    while (_routing.IsEnd(index) == false)
    {
      ...
      IntVar distanceVar = distanceDimension.CumulVar(index);
      IntVar durationVar = durationDimension.CumulVar(index);
      long distance = _solution.Value(distanceVar);
      long duration = _solution.Value(durationVar);
      ...
    }
  }
}