コード例 #1
0
        public void TestDoubleTreeFailOnIncompleteGraph()
        {
            Graph <int, double> graph = new GraphBuilder <int, double>()
                                        .AddVerteces(5, v => v)
                                        .AddEdge(0, 2)
                                        .AddEdge(2, 1)
                                        .AddEdge(2, 3)
                                        .AddEdge(2, 4)
                                        .Build();

            NearestNeighbor.FindTour(graph);
        }
コード例 #2
0
        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);
            }
        }