2016-11-20 92 views
1

用下面的代碼的問題是:谷歌ortools - 車輛路徑PB

即使我只有10個地點交付和一個倉庫設置在位置0,在這個例子中,車輛1, 2,3,4似乎有他們在地點10,11,12,13的倉庫。這些位置不存在。我擁有的10個數字從0到9。

在另一方面業務邏輯似乎是OK:

我孤立離開車廠我得到預期的結果的成本和要回它(價值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); 
    } 
} 

enter image description here

回答

1

你有

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

包內秩序model.IndexToNode(訂單),這樣

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