【问题标题】:Google ortools - capacitated vehicle routing pb谷歌 ortools - 有容量的车辆路由 pb
【发布时间】:2016-11-20 04:28:48
【问题描述】:

以下代码的问题是:

即使我只有 10 个要交付的位置,并且在位置 0 设置了一个站点,在此示例中,车辆 1、2、3、4 似乎在位置 10、11、12、13 有它们的站点。这些位置不存在。我拥有的 10 个是从 0 到 9 编号的。

另一方面,业务逻辑似乎还可以:

当我分离出离开站点的成本和返回站点的成本(值 10)时,我得到了预期的结果:104。城市之间只有 4 次不包括站点的行程。

这是 Google or-tools 中的错误吗?

public static void Main(string[] args)
{
    new CVRP().Solve(10);
}

private class RandomManhattan : NodeEvaluator2
{
    public override long Run(int first_index, int second_index)
    {
        if (first_index == 0 || second_index == 0)
            return 10;
        return 1;
    }
};

private class Demand : NodeEvaluator2
{
    public override long Run(int first_index, int second_index)
    {
        return 1;
    }
};

private void Solve(int locations)
{
    var nr_vehicle = 5;
    var routing = new RoutingModel(locations, nr_vehicle, new[] {0, 0, 0, 0, 0}, new[] {0, 0, 0, 0, 0});
    Console.WriteLine("Depot : " + routing.GetDepot());

    NodeEvaluator2 demandCallback = new Demand();
    routing.AddDimension(demandCallback, 0, 3, true, "capacity");

    var distances = new RandomManhattan();
    routing.SetCost(distances);

    var searchParameters =
        RoutingModel.DefaultSearchParameters();
    searchParameters.FirstSolutionStrategy =
        FirstSolutionStrategy.Types.Value.PathCheapestArc;

    var solution = routing.SolveWithParameters(searchParameters);


    if (solution != null)
    {
        var output = "Total cost: " + solution.ObjectiveValue() + "\n";
        // Dropped orders
        var dropped = "";
        for (var order = 0; order < locations; ++order)
        {
            if (solution.Value(routing.NextVar(order)) == order)
            {
                dropped += " " + order;
            }
        }
        if (dropped.Length > 0)
        {
            output += "Dropped orders:" + dropped + "\n";
        }
        // Routes
        for (var vehicle = 0; vehicle < nr_vehicle; ++vehicle)
        {
            var route = "Vehicle " + vehicle + ": ";
            var order = routing.Start(vehicle);
            if (routing.IsEnd(solution.Value(routing.NextVar(order))))
            {
                route += "Empty";
            }
            else
            {
                for (; !routing.IsEnd(order); order = solution.Value(routing.NextVar(order)))
                {
                    var local_load = routing.CumulVar(order, "capacity");
                    route += order + " Load(" + solution.Value(local_load) + ") -> ";
                }
                if (route.Length > 0)
                    route = route + "0";
            }
            output += route + "\n";
        }
        Console.WriteLine(output);
    }
}

【问题讨论】:

    标签: c# or-tools


    【解决方案1】:

    你必须在

    中包装订单
    route += order + " Load(" + solution.Value(local_load) + ") -> ";
    

    在model.IndexToNode(order)里面,像这样

    route += model.IndexToNode(order) + " Load(" + solution.Value(local_load) + ") -> ";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      相关资源
      最近更新 更多