以下代码的问题是:
即使我只有 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);
}
}