17 using System.Collections.Generic;
32 for (
int i=0; i < Locations.GetLength(0); i++) {
33 Locations[i, 0] *= 114;
34 Locations[i, 1] *= 80;
37 public int[,] Locations = {
48 public int VehicleNumber = 1;
59 class ManhattanDistance {
60 public ManhattanDistance(
64 int locationNumber = data.Locations.GetLength(0);
65 distancesMatrix_ =
new long[locationNumber, locationNumber];
66 indexManager_ = manager;
67 for (
int fromNode = 0; fromNode < locationNumber; fromNode++) {
68 for (
int toNode = 0; toNode < locationNumber; toNode++) {
69 if (fromNode == toNode)
70 distancesMatrix_[fromNode, toNode] = 0;
72 distancesMatrix_[fromNode, toNode] =
73 Math.Abs(data.Locations[toNode, 0] - data.Locations[fromNode, 0]) +
74 Math.Abs(data.Locations[toNode, 1] - data.Locations[fromNode, 1]);
82 public long Call(
long fromIndex,
long toIndex) {
84 int fromNode = indexManager_.IndexToNode(fromIndex);
85 int toNode = indexManager_.IndexToNode(toIndex);
86 return distancesMatrix_[fromNode, toNode];
88 private long[,] distancesMatrix_;
97 static void PrintSolution(
101 Console.WriteLine(
"Objective: {0}", solution.ObjectiveValue());
103 Console.WriteLine(
"Route for Vehicle 0:");
104 long routeDistance = 0;
105 var index = routing.Start(0);
106 while (routing.IsEnd(index) ==
false) {
107 Console.Write(
"{0} -> ", manager.IndexToNode((
int)index));
108 var previousIndex = index;
109 index = solution.Value(routing.NextVar(index));
110 routeDistance += routing.GetArcCostForVehicle(previousIndex, index, 0);
112 Console.WriteLine(
"{0}", manager.IndexToNode((
int)index));
113 Console.WriteLine(
"Distance of the route: {0}m", routeDistance);
117 public static void Main(String[] args) {
120 DataModel data =
new DataModel();
126 data.Locations.GetLength(0),
138 var distanceCallback =
new ManhattanDistance(data, manager);
139 int transitCallbackIndex = routing.RegisterTransitCallback(distanceCallback.Call);
144 routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
151 searchParameters.FirstSolutionStrategy =
157 Assignment solution = routing.SolveWithParameters(searchParameters);
162 PrintSolution(routing, manager, solution);