17 using System.Collections.Generic;
29 public int[,] Locations = {
30 {288, 149}, {288, 129}, {270, 133}, {256, 141}, {256, 157}, {246, 157},
31 {236, 169}, {228, 169}, {228, 161}, {220, 169}, {212, 169}, {204, 169},
32 {196, 169}, {188, 169}, {196, 161}, {188, 145}, {172, 145}, {164, 145},
33 {156, 145}, {148, 145}, {140, 145}, {148, 169}, {164, 169}, {172, 169},
34 {156, 169}, {140, 169}, {132, 169}, {124, 169}, {116, 161}, {104, 153},
35 {104, 161}, {104, 169}, {90, 165}, {80, 157}, {64, 157}, {64, 165},
36 {56, 169}, {56, 161}, {56, 153}, {56, 145}, {56, 137}, {56, 129},
37 {56, 121}, {40, 121}, {40, 129}, {40, 137}, {40, 145}, {40, 153},
38 {40, 161}, {40, 169}, {32, 169}, {32, 161}, {32, 153}, {32, 145},
39 {32, 137}, {32, 129}, {32, 121}, {32, 113}, {40, 113}, {56, 113},
40 {56, 105}, {48, 99}, {40, 99}, {32, 97}, {32, 89}, {24, 89}, {16, 97},
41 {16, 109}, {8, 109}, {8, 97}, {8, 89}, {8, 81}, {8, 73}, {8, 65},
42 {8, 57}, {16, 57}, {8, 49}, {8, 41}, {24, 45}, {32, 41}, {32, 49},
43 {32, 57}, {32, 65}, {32, 73}, {32, 81}, {40, 83}, {40, 73}, {40, 63},
44 {40, 51}, {44, 43}, {44, 35}, {44, 27}, {32, 25}, {24, 25}, {16, 25},
45 {16, 17}, {24, 17}, {32, 17}, {44, 11}, {56, 9}, {56, 17}, {56, 25},
46 {56, 33}, {56, 41}, {64, 41}, {72, 41}, {72, 49}, {56, 49}, {48, 51},
47 {56, 57}, {56, 65}, {48, 63}, {48, 73}, {56, 73}, {56, 81}, {48, 83},
48 {56, 89}, {56, 97}, {104, 97}, {104, 105}, {104, 113}, {104, 121},
49 {104, 129}, {104, 137}, {104, 145}, {116, 145}, {124, 145}, {132, 145},
50 {132, 137}, {140, 137}, {148, 137}, {156, 137}, {164, 137}, {172, 125},
51 {172, 117}, {172, 109}, {172, 101}, {172, 93}, {172, 85}, {180, 85},
52 {180, 77}, {180, 69}, {180, 61}, {180, 53}, {172, 53}, {172, 61},
53 {172, 69}, {172, 77}, {164, 81}, {148, 85}, {124, 85}, {124, 93},
54 {124, 109}, {124, 125}, {124, 117}, {124, 101}, {104, 89}, {104, 81},
55 {104, 73}, {104, 65}, {104, 49}, {104, 41}, {104, 33}, {104, 25},
56 {104, 17}, {92, 9}, {80, 9}, {72, 9}, {64, 21}, {72, 25}, {80, 25},
57 {80, 25}, {80, 41}, {88, 49}, {104, 57}, {124, 69}, {124, 77}, {132, 81},
58 {140, 65}, {132, 61}, {124, 61}, {124, 53}, {124, 45}, {124, 37},
59 {124, 29}, {132, 21}, {124, 21}, {120, 9}, {128, 9}, {136, 9}, {148, 9},
60 {162, 9}, {156, 25}, {172, 21}, {180, 21}, {180, 29}, {172, 29},
61 {172, 37}, {172, 45}, {180, 45}, {180, 37}, {188, 41}, {196, 49},
62 {204, 57}, {212, 65}, {220, 73}, {228, 69}, {228, 77}, {236, 77},
63 {236, 69}, {236, 61}, {228, 61}, {228, 53}, {236, 53}, {236, 45},
64 {228, 45}, {228, 37}, {236, 37}, {236, 29}, {228, 29}, {228, 21},
65 {236, 21}, {252, 21}, {260, 29}, {260, 37}, {260, 45}, {260, 53},
66 {260, 61}, {260, 69}, {260, 77}, {276, 77}, {276, 69}, {276, 61},
67 {276, 53}, {284, 53}, {284, 61}, {284, 69}, {284, 77}, {284, 85},
68 {284, 93}, {284, 101}, {288, 109}, {280, 109}, {276, 101}, {276, 93},
69 {276, 85}, {268, 97}, {260, 109}, {252, 101}, {260, 93}, {260, 85},
70 {236, 85}, {228, 85}, {228, 93}, {236, 93}, {236, 101}, {228, 101},
71 {228, 109}, {228, 117}, {228, 125}, {220, 125}, {212, 117}, {204, 109},
72 {196, 101}, {188, 93}, {180, 93}, {180, 101}, {180, 109}, {180, 117},
73 {180, 125}, {196, 145}, {204, 145}, {212, 145}, {220, 145}, {228, 145},
74 {236, 145}, {246, 141}, {252, 125}, {260, 129}, {280, 133},
76 public int VehicleNumber = 1;
87 static long[,] ComputeEuclideanDistanceMatrix(in
int[,] locations) {
89 int locationNumber = locations.GetLength(0);
90 long[,] distanceMatrix =
new long[locationNumber, locationNumber];
91 for (
int fromNode = 0; fromNode < locationNumber; fromNode++) {
92 for (
int toNode = 0; toNode < locationNumber; toNode++) {
93 if (fromNode == toNode)
94 distanceMatrix[fromNode, toNode] = 0;
96 distanceMatrix[fromNode, toNode] = (long)
98 Math.Pow(locations[toNode, 0] - locations[fromNode, 0], 2) +
99 Math.Pow(locations[toNode, 1] - locations[fromNode, 1], 2));
102 return distanceMatrix;
110 static void PrintSolution(
114 Console.WriteLine(
"Objective: {0}", solution.ObjectiveValue());
116 Console.WriteLine(
"Route:");
117 long routeDistance = 0;
118 var index = routing.Start(0);
119 while (routing.IsEnd(index) ==
false) {
120 Console.Write(
"{0} -> ", manager.IndexToNode((
int)index));
121 var previousIndex = index;
122 index = solution.Value(routing.NextVar(index));
123 routeDistance += routing.GetArcCostForVehicle(previousIndex, index, 0);
125 Console.WriteLine(
"{0}", manager.IndexToNode((
int)index));
126 Console.WriteLine(
"Route distance: {0}m", routeDistance);
130 public static void Main(String[] args) {
133 DataModel data =
new DataModel();
139 data.Locations.GetLength(0),
151 long[,] distanceMatrix = ComputeEuclideanDistanceMatrix(data.Locations);
153 (
long fromIndex,
long toIndex) => {
157 return distanceMatrix[fromNode, toNode]; }
180 PrintSolution(routing, manager, solution);