17 using System.Collections.Generic;
28 public long[,] TimeMatrix = {
29 {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7},
30 {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14},
31 {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9},
32 {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16},
33 {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14},
34 {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8},
35 {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5},
36 {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10},
37 {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6},
38 {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5},
39 {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4},
40 {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10},
41 {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8},
42 {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6},
43 {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2},
44 {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9},
45 {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0},
47 public long[,] TimeWindows = {
66 public int VehicleNumber = 4;
75 static void PrintSolution(
83 for (
int i = 0; i < data.VehicleNumber; ++i) {
84 Console.WriteLine(
"Route for Vehicle {0}:", i);
85 var index = routing.Start(i);
86 while (routing.IsEnd(index) ==
false) {
87 var timeVar = timeDimension.
CumulVar(index);
88 Console.Write(
"{0} Time({1},{2}) -> ",
89 manager.IndexToNode(index),
90 solution.Min(timeVar),
91 solution.Max(timeVar));
92 index = solution.
Value(routing.NextVar(index));
94 var endTimeVar = timeDimension.
CumulVar(index);
95 Console.WriteLine(
"{0} Time({1},{2})",
96 manager.IndexToNode(index),
97 solution.Min(endTimeVar),
98 solution.Max(endTimeVar));
99 Console.WriteLine(
"Time of the route: {0}min", solution.Min(endTimeVar));
100 totalTime += solution.
Min(endTimeVar);
102 Console.WriteLine(
"Total time of all routes: {0}min", totalTime);
106 public static void Main(String[] args) {
109 DataModel data =
new DataModel();
115 data.TimeMatrix.GetLength(0),
127 int transitCallbackIndex = routing.RegisterTransitCallback(
128 (
long fromIndex,
long toIndex) => {
132 return data.TimeMatrix[fromNode, toNode]; }
138 routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
143 routing.AddDimension(
144 transitCallbackIndex,
151 for (
int i = 1; i < data.TimeWindows.GetLength(0); ++i) {
154 data.TimeWindows[i, 0],
155 data.TimeWindows[i, 1]);
158 for (
int i = 0; i < data.VehicleNumber; ++i) {
159 long index = routing.Start(i);
161 data.TimeWindows[0, 0],
162 data.TimeWindows[0, 1]);
168 for (
int i = 0; i < data.VehicleNumber; ++i) {
169 routing.AddVariableMinimizedByFinalizer(
170 timeDimension.
CumulVar(routing.Start(i)));
171 routing.AddVariableMinimizedByFinalizer(
172 timeDimension.
CumulVar(routing.End(i)));
180 searchParameters.FirstSolutionStrategy =
186 Assignment solution = routing.SolveWithParameters(searchParameters);
191 PrintSolution(data, routing, manager, solution);