谷歌ortools - 有能力的车辆路由pb

时间:2016-11-20 04:28:49

标签: c# or-tools

  

以下代码的问题是:

即使我只有10个地点可以交付,而且一个仓库设置在0号位置,在这个例子中,车辆1,2,3,4似乎在10,11,12,13处设置了仓库。那些位置不存在。我所拥有的10个编号为0-9。

  

另一方面,业务逻辑似乎没问题:

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

  

这是Google或工具中的错误吗?

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);
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

你必须在

中换行
route += order + " Load(" + solution.Value(local_load) + ") -> ";

在model.IndexToNode(order)中,像这样

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