17 using System.Collections.Generic;
27 public long[,] TimeMatrix = {
28 {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7},
29 {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14},
30 {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9},
31 {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16},
32 {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14},
33 {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8},
34 {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5},
35 {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10},
36 {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6},
37 {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5},
38 {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4},
39 {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10},
40 {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8},
41 {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6},
42 {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2},
43 {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9},
44 {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0},
46 public long[,] TimeWindows = {
65 public int VehicleNumber = 4;
74 static void PrintSolution(
82 for (
int i = 0; i < data.VehicleNumber; ++i) {
83 Console.WriteLine(
"Route for Vehicle {0}:", i);
84 var index = routing.Start(i);
85 while (routing.IsEnd(index) ==
false) {
86 var timeVar = timeDimension.
CumulVar(index);
87 Console.Write(
"{0} Time({1},{2}) -> ",
88 manager.IndexToNode(index),
89 solution.Min(timeVar),
90 solution.Max(timeVar));
91 index = solution.
Value(routing.NextVar(index));
93 var endTimeVar = timeDimension.
CumulVar(index);
94 Console.WriteLine(
"{0} Time({1},{2})",
95 manager.IndexToNode(index),
96 solution.Min(endTimeVar),
97 solution.Max(endTimeVar));
98 Console.WriteLine(
"Time of the route: {0}min", solution.Min(endTimeVar));
99 totalTime += solution.
Min(endTimeVar);
101 Console.WriteLine(
"Total time of all routes: {0}min", totalTime);
105 public static void Main(String[] args) {
108 DataModel data =
new DataModel();
114 data.TimeMatrix.GetLength(0),
126 int transitCallbackIndex = routing.RegisterTransitCallback(
127 (
long fromIndex,
long toIndex) => {
131 return data.TimeMatrix[fromNode, toNode]; }
137 routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
142 routing.AddDimension(
143 transitCallbackIndex,
150 for (
int i = 1; i < data.TimeWindows.GetLength(0); ++i) {
153 data.TimeWindows[i, 0],
154 data.TimeWindows[i, 1]);
157 for (
int i = 0; i < data.VehicleNumber; ++i) {
158 long index = routing.Start(i);
160 data.TimeWindows[0, 0],
161 data.TimeWindows[0, 1]);
167 for (
int i = 0; i < data.VehicleNumber; ++i) {
168 routing.AddVariableMinimizedByFinalizer(
169 timeDimension.
CumulVar(routing.Start(i)));
170 routing.AddVariableMinimizedByFinalizer(
171 timeDimension.
CumulVar(routing.End(i)));
185 Assignment solution = routing.SolveWithParameters(searchParameters);
190 PrintSolution(data, routing, manager, solution);
Vehicles Routing Problem (VRP) with Time Windows.
static void Main(String[] args)