public void MergePathsTest() { var actual = LatinComposition.MergePaths(new int[] { 1, 2, 3 }, new int[] { 3, 4, 5 }); var expected = new int[] { 1, 2, 3, 4, 5 }; Assert.True(actual.SequenceEqual(expected)); }
public void Compare_BaB_And_LC(int n, int maxRandomValue, int repeat, double possibilityOfNull) { for (int i = 0; i < repeat; i++) { var weights = GenerateRandomGraph(n, maxRandomValue, possibilityOfNull); var lc = new LatinComposition(weights).GetShortestHamiltonianCycle(); var bb = new BranchAndBound(weights).GetShortestHamiltonianCycle(); if (lc == null && bb == null) { continue; } //var s = ""; //for (int j = 0; j < n; j++) //{ // var v = new string[n]; // for (int k = 0; k < n; k++) // { // v[k] = weights[j, k]?.ToString() ?? "-"; // } // s += string.Join(" ", v) + "\n"; //} //System.IO.File.WriteAllText("D:/1.txt", s); var lcDistance = AdjacencyMatrix.PathDistance(lc, weights); var bbDistance = AdjacencyMatrix.PathDistance(bb, weights); var msg = lcDistance == bbDistance ? "" : $"\nLC distance: {lcDistance};\nB&B distance: {bbDistance}\n" + ToString(weights); Assert.True(lcDistance == bbDistance, msg); } }
public void Compare_BaB_And_LC1(int n, int maxRandomValue, int repeat) { for (int i = 0; i < repeat; i++) { var weights = GenerateRandomFullGraph(n, maxRandomValue); var lc = new LatinComposition(weights).GetShortestHamiltonianCycle(); var bb = new BranchAndBound(weights).GetShortestHamiltonianCycle(); var lcDistance = AdjacencyMatrix.PathDistance(lc, weights); var bbDistance = AdjacencyMatrix.PathDistance(bb, weights); var msg = lcDistance == bbDistance ? "" : $"\nLC distance: {lcDistance};\nB&B distance: {bbDistance}\n" + ToString(weights); Assert.True(lcDistance == bbDistance, msg); } }
public void GetHamiltonianCyclesTest(int testNumber) { var matrix = AdjacencyMatrix.GetGraph(testNumber); var actualPaths = new LatinComposition(matrix.Weights).GetAllHamiltorianCycles(); var expectedPaths = matrix.AllPaths; // due to there can be not only one shortest path, // we compare distance between actual and expected shortest paths int[] actualShortestPath = GraphUtil.ShortestRoute(matrix.Weights, actualPaths); var actualDistance = AdjacencyMatrix.PathDistance(actualShortestPath, matrix.Weights); var expectedDistance = matrix.ShortestPathDistance; Assert.True(expectedPaths.InnerSequencesEqualInSomeOrder(actualPaths)); Assert.Equal(expectedDistance, actualDistance); }
public void LC_Huge() { // string s = //@"5 //- 1 1 1 - //1 - 1 - 1 //1 1 - - 1 //1 - 1 - 1 //1 1 1 - -"; // int?[,] m = Utils.GraphUtil.FromMatrixFormat(s); // new LatinComposition(m).GetAllHamiltorianCycles(); // return; for (int i = 10; i <= 10; i++) { var weights = Compare_LC_And_BaB.GenerateRandomFullGraph(n: i, maxRandomValue: 100); var bb = new LatinComposition(weights).GetAllHamiltorianCycles(); } }
public void SumTest() { const int n = 8; var a = new List <int[]> [n, n]; GeneratePaths(a, 0, 1); GeneratePaths(a, 0, 7); GeneratePaths(a, 1, 0); GeneratePaths(a, 1, 2); GeneratePaths(a, 1, 3); GeneratePaths(a, 2, 3); GeneratePaths(a, 2, 5); GeneratePaths(a, 3, 2); GeneratePaths(a, 3, 4); GeneratePaths(a, 4, 2); GeneratePaths(a, 4, 6); GeneratePaths(a, 5, 0); GeneratePaths(a, 5, 1); GeneratePaths(a, 5, 4); GeneratePaths(a, 6, 5); GeneratePaths(a, 6, 7); GeneratePaths(a, 7, 0); GeneratePaths(a, 7, 1); GeneratePaths(a, 7, 2); GeneratePaths(a, 7, 3); var e = new List <int[]> [n, n]; GeneratePaths(e, 0, 7, 1); GeneratePaths(e, 0, 1, 2); GeneratePaths(e, 0, 7, 2); GeneratePaths(e, 0, 1, 3); GeneratePaths(e, 0, 7, 3); GeneratePaths(e, 1, 3, 2); GeneratePaths(e, 1, 2, 3); GeneratePaths(e, 1, 3, 4); GeneratePaths(e, 1, 2, 5); GeneratePaths(e, 1, 0, 7); GeneratePaths(e, 2, 5, 0); GeneratePaths(e, 2, 5, 1); GeneratePaths(e, 2, 3, 4); GeneratePaths(e, 2, 5, 4); GeneratePaths(e, 3, 4, 2); GeneratePaths(e, 3, 2, 5); GeneratePaths(e, 3, 4, 6); GeneratePaths(e, 4, 2, 3); GeneratePaths(e, 4, 2, 5); GeneratePaths(e, 4, 6, 5); GeneratePaths(e, 4, 6, 7); GeneratePaths(e, 5, 1, 0); GeneratePaths(e, 5, 0, 1); GeneratePaths(e, 5, 1, 2); GeneratePaths(e, 5, 4, 2); GeneratePaths(e, 5, 1, 3); GeneratePaths(e, 5, 4, 6); GeneratePaths(e, 5, 0, 7); GeneratePaths(e, 6, 5, 0); GeneratePaths(e, 6, 7, 0); GeneratePaths(e, 6, 7, 1); GeneratePaths(e, 6, 5, 1); GeneratePaths(e, 6, 7, 2); GeneratePaths(e, 6, 7, 3); GeneratePaths(e, 6, 5, 4); GeneratePaths(e, 7, 1, 0); GeneratePaths(e, 7, 0, 1); GeneratePaths(e, 7, 1, 2); GeneratePaths(e, 7, 3, 2); GeneratePaths(e, 7, 1, 3); GeneratePaths(e, 7, 2, 3); GeneratePaths(e, 7, 3, 4); GeneratePaths(e, 7, 2, 5); var actual = LatinComposition.Sum(a, a, n); var en = e.GetEnumerator(); foreach (var item in actual) { en.MoveNext(); if (item == en.Current) { continue; } var cur = (List <int[]>)en.Current; for (int i = 0; i < cur.Count; i++) { cur[i].SequenceEqual(item[i]); } } }
public void LC_0x0() { var actual = new LatinComposition(new int?[0, 0]).GetShortestHamiltonianCycle(); Assert.Empty(actual); }
public void LC_1x1() { var cycle = new LatinComposition(new int?[1, 1]).GetShortestHamiltonianCycle(); Assert.Single(cycle, 0); }