private TestResult RunTest(string file, IWeightedGraph <int, double> graph, TspAlgorithm algorithm) { IWeightedGraph <int, double> route = null; Stopwatch sw = new Stopwatch(); switch (algorithm) { case TspAlgorithm.NearestNeighbor: sw.Start(); route = Algorithms.TravelingSalesmanProblem.NearestNeighbor.FindTour(graph); sw.Stop(); break; case TspAlgorithm.DoubleTree: sw.Start(); route = Algorithms.TravelingSalesmanProblem.DoubleTree.FindTour(graph); sw.Stop(); break; case TspAlgorithm.BruteForce: sw.Start(); route = Algorithms.TravelingSalesmanProblem.BruteForce.FindOptimalTour(graph, 0.0, double.MaxValue, (w1, w2) => w1 + w2); sw.Stop(); break; default: throw new NotSupportedException($"Testing the TSP algorithm {algorithm} is currently not supported!"); } Logger.LogDebug($" - Computed route for '{file}' using {algorithm} algorithm in {sw.Elapsed}."); return(new TestResult(route.GetAllEdges().Sum(e => e.Weight), sw.Elapsed)); }
private void TestTspAlgorithm(IWeightedGraph <int, double> graph, TspAlgorithm algorithm, int expectedVerteces, int expectedEdges, double optimalTourCosts = 0.0, double precision = 0.01) { IWeightedGraph <int, double> tour = null; // Compute tour with the chosen algorithm switch (algorithm) { case TspAlgorithm.NearestNeighbor: tour = NearestNeighbor.FindTour(graph); break; case TspAlgorithm.DoubleTree: tour = DoubleTree.FindTour(graph); break; case TspAlgorithm.BruteForce: tour = BruteForce.FindOptimalTour(graph, 0.0, double.MaxValue, (w1, w2) => w1 + w2); break; default: throw new NotSupportedException($"Testing TSP with the {algorithm} algorithm is currently not supported."); } // Check route for component count Assert.AreEqual(expectedVerteces, tour.VertexCount); Assert.AreEqual(expectedEdges, tour.GetAllEdges().Count()); // For algorithms that find the optimal tour, check the optmial tour costs if (algorithm == TspAlgorithm.BruteForce) { AssertDoublesNearlyEqual(optimalTourCosts, tour.GetAllEdges().Sum(e => e.Weight), precision); } }