// -------------------------------------------------------------
 private void FindShortestPaths(
     PathFinderFactory pathFinderFactory,
     Vertex startVertex,
     Vertex endVertex
     )
 {
     PathFinder   pathFinder    = pathFinderFactory.CreatePathFinder(edges_A_B_and_B_C, GraphEdgesValidationDesired.YES);
     IList <Path> shortestPaths = pathFinder.FindShortestPaths(startVertex, endVertex, maxNumberOfPaths);
 }
Ejemplo n.º 2
0
        //[Test]
        //public void TestFindShortestPaths_Parrisha() {
        // TestFindShortestPaths(
        //  new PathFinderFactoryParrisha()
        // );
        //}

        public void TestFindShortestPaths(
            PathFinderFactory pathFinderFactory
            )
        {
            Edge edgeAB3  = CreateEdge(CreateVertex("A"), CreateVertex("B"), CreateWeight(3));
            Edge edgeBC5  = CreateEdge(CreateVertex("B"), CreateVertex("C"), CreateWeight(5));
            Edge edgeCD7  = CreateEdge(CreateVertex("C"), CreateVertex("D"), CreateWeight(7));
            Edge edgeBD13 = CreateEdge(CreateVertex("B"), CreateVertex("D"), CreateWeight(13));

            IList <Edge> edges = new List <Edge>();

            edges.Add(edgeAB3);
            edges.Add(edgeBC5);
            edges.Add(edgeCD7);
            edges.Add(edgeBD13);
            // There are two ways from A to C in a Graph with the above edges:
            // A - B - C- D     , with weight 15 ( 3 + 5 + 7 )
            // A - B - D        , with weight 16 ( 3 + 13 )

            PathFinder pathFinder = pathFinderFactory.CreatePathFinder(
                edges,
                GraphEdgesValidationDesired.YES                                                                 // TODO: refactor the construction of edges to able to do the validation only once instead of doing it for each factory
                );
            IList <Path> shortestPaths = pathFinder.FindShortestPaths(CreateVertex("A"), CreateVertex("D"), 5); // max 5 but actually we should only find 2

            Assert.AreEqual(2, shortestPaths.Count);

            Path path = shortestPaths[0];         // the shortest mentioned above with total weight 15

            Assert.AreEqual(15, path.TotalWeightForPath.WeightValue, SMALL_DELTA_VALUE_FOR_WEIGHT_COMPARISONS);
            IList <Edge> edgesForPath = path.EdgesForPath;

            Assert.AreEqual(3, edgesForPath.Count);
            assertEqualsAndTheSameInstance(edgeAB3, edgesForPath[0]);
            assertEqualsAndTheSameInstance(edgeBC5, edgesForPath[1]);
            assertEqualsAndTheSameInstance(edgeCD7, edgesForPath[2]);
            //
            //
            Path path2 = shortestPaths[1];

            Assert.AreEqual(16, path2.TotalWeightForPath.WeightValue, SMALL_DELTA_VALUE_FOR_WEIGHT_COMPARISONS);
            IList <Edge> edgesForPath2 = path2.EdgesForPath;

            Assert.AreEqual(2, edgesForPath2.Count);
            assertEqualsAndTheSameInstance(edgeAB3, edgesForPath2[0]);
            assertEqualsAndTheSameInstance(edgeBD13, edgesForPath2[1]);
        }
Ejemplo n.º 3
0
        private void PathFinderTest(PathFinderFactory pathFinderFactory)
        {
            pathFinder = pathFinderFactory.CreatePathFinder(graph);

            IList <Path> shortestPaths = pathFinder.FindShortestPaths(a, d, 10);

            AreEqual(3, shortestPaths.Count);

            // path1 : A -> B -> D (with total weight 13)
            assertPath(shortestPaths[0], 13, "A", "B", "D");

            // path2 : A -> C -> D (with total weight 15)
            assertPath(shortestPaths[1], 15, "A", "C", "D");

            // path3 : A -> B -> C -> D (with total weight 21)
            assertPath(shortestPaths[2], 21, "A", "B", "C", "D");
        }
Ejemplo n.º 4
0
        private void VerifyExpectedPaths(
            Vertex startVertex,
            Vertex endVertex,
            IList <Edge> edges,
            PathFinderFactory pathFinderFactory,
            ExpectedPath[] expectedShortestPaths
            )
        {
            PathFinder pathFinder = pathFinderFactory.CreatePathFinder(
                edges,
                GraphEdgesValidationDesired.NO             // do the validation one time instead of doing it for each pathFinderFactory
                );
            IList <Path> actualShortestPaths = pathFinder.FindShortestPaths(startVertex, endVertex, 10);

            Assert.AreEqual(expectedShortestPaths.Length, actualShortestPaths.Count);

            string errorContext = pathFinderFactory.GetType().Name;

            for (int i = 0; i < expectedShortestPaths.Length; i++)
            {
                errorContext += " , i: " + i;
                ExpectedPath expectedPath = expectedShortestPaths[i];
                Path         actualPath   = actualShortestPaths[i];
                Assert.AreEqual(expectedPath.totalWeight, actualPath.TotalWeightForPath.WeightValue, SMALL_DELTA_VALUE_FOR_WEIGHT_COMPARISONS, errorContext);
                ExpectedEdge[] expectedEdgesForPath = expectedPath.expectedEdges;
                IList <Edge>   actualEdgesForPath   = actualPath.EdgesForPath;
                Assert.AreEqual(expectedEdgesForPath.Length, actualEdgesForPath.Count, errorContext);
                for (int j = 0; j < expectedEdgesForPath.Length; j++)
                {
                    errorContext += " , j=" + j;
                    ExpectedEdge expectedEdge = expectedEdgesForPath[j];
                    Edge         actualEdge   = actualEdgesForPath[j];
                    Assert.NotNull(actualEdge, errorContext);
                    Assert.NotNull(actualEdge.EdgeWeight, errorContext);
                    Assert.NotNull(actualEdge.StartVertex, errorContext);
                    Assert.NotNull(actualEdge.EndVertex, errorContext);
                    Assert.AreEqual(expectedEdge.weight, actualEdge.EdgeWeight.WeightValue, SMALL_DELTA_VALUE_FOR_WEIGHT_COMPARISONS, errorContext);
                    Assert.AreEqual(expectedEdge.startVertexId, actualEdge.StartVertex.VertexId, errorContext);
                    Assert.AreEqual(expectedEdge.endVertexId, actualEdge.EndVertex.VertexId, errorContext);
                    Assert.AreEqual(expectedEdge.edgeId, actualEdge.EdgeId, errorContext);
                }
            }
        }
 private void shouldThrowExceptionIfAnyOfTheVerticesIsNotPartOfTheGraph(
     PathFinderFactory pathFinderFactory,
     Vertex startVertex,
     Vertex endVertex,
     bool isExceptionExpected
     )
 {
     if (isExceptionExpected)
     {
         Assert.Throws <GraphValidationException>(() => {
             FindShortestPaths(pathFinderFactory, startVertex, endVertex);
         });
         //ActualValueDelegate<object> testDelegate = () => FindShortestPaths(pathFinderFactory, startVertex, endVertex);
         //Assert.That( () => {
         //    FindShortestPaths(pathFinderFactory, startVertex, endVertex);
         //},
         //Throws.TypeOf<GraphValidationException>
         //);
     }
     else
     {
         FindShortestPaths(pathFinderFactory, startVertex, endVertex);
     }
 }
Ejemplo n.º 6
0
        public static void RunAntQuest(JsonTypes jsonWorker, PathAlgorithms pathAlgorithm, bool DEBUG, string url = "")
        {
            try
            {
                var sw = new Stopwatch();
                sw.Start();

                Console.WriteLine("Running ANT QUEST for {0} and {1}", jsonWorker, pathAlgorithm);

                AntModel antModel = JsonSimpleFactory.GetJsonData(jsonWorker, url);
                if (DEBUG)
                {
                    Console.WriteLine("Obtaining data took: {0} ms", sw.Elapsed.Milliseconds);
                }

                sw.Restart();

                var antGraph = new Graph(antModel);
                if (DEBUG)
                {
                    Console.WriteLine("Graph preparation took: {0} ms", sw.Elapsed.Milliseconds);
                }

                sw.Restart();

                var pathFinder = PathFinderFactory.PathFactory(antGraph, pathAlgorithm);
                var path       = pathFinder.FindPath();
                var pathString = pathFinder.GetPathSimpleString(path);

                if (DEBUG)
                {
                    Console.WriteLine("Path finding took: {0} ms", sw.Elapsed.Milliseconds);
                }
                if (DEBUG)
                {
                    Console.WriteLine("Final path is: {0}", pathString);
                }

                sw.Restart();

                if (jsonWorker == JsonTypes.TEST || jsonWorker == JsonTypes.URL || jsonWorker == JsonTypes.API_QUADIENT)
                {
                    var serverResponse =
                        JsonSimpleFactory.SendJsonResponse(jsonWorker, antGraph.ID, pathString, url);
                    Console.WriteLine(serverResponse.Message);

                    if (serverResponse.Valid && serverResponse.InTime && DEBUG)
                    {
                        Console.WriteLine("Test passed");
                    }
                    else if (DEBUG)
                    {
                        Console.WriteLine("Test failed");
                    }
                }

                if (DEBUG)
                {
                    Console.WriteLine("Sending data took: {0} ms", sw.Elapsed.Milliseconds);
                }

                sw.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 7
0
        /**
         * Overloaded method. Note that the last parameter can be null if we only want to compare
         * results from different implementations.
         * The second last parameter is used when we also have an expected path retrieved for example from an xml file.
         * The last parameter can be used temporarirly for "debugging" purposes when we want to display the results to the console
         * See comment at class level.
         */
        public void AssertExpectedResultsOrAssertImplementationsWithEachOther(
            IList <Edge> edgesForBigGraph,
            Vertex startVertex,
            Vertex endVertex,
            int numberOfPathsToFind,
            IList <PathFinderFactory> pathFinderFactoriesForImplementationsToTest,
            IList <Path> expectedListOfPaths,
            string optionalPathToResourceXmlFile,
            bool shouldTestResultsWithImplementationsAgainstEachOther = false
            )
        {
            // TODO: clean up this method e.g. regarding "shouldAlsoTestResultsWithImplementationsAgainstEachOther"
            //this.SetConsoleOutputDesired(ConsoleOutputDesired.TIME_MEASURE);
            string messagePrefixWithInformationAboutXmlSourcefileWithTestData = optionalPathToResourceXmlFile == null ? "" : "Xml file with test data: " + optionalPathToResourceXmlFile + " . ";

            output("Number of edges in the graph to be tested : " + edgesForBigGraph.Count);
            IDictionary <string, IList <Path> > shortestPathsPerImplementation = new Dictionary <string, IList <Path> >();

            // the parameter GraphEdgesValidationDesired.NO will be used so therefore do the validation once externally here first
            GraphEdgesValidator <Path, Edge, Vertex, Weight> .ValidateEdgesForGraphCreation <Path, Edge, Vertex, Weight>(edgesForBigGraph);

            PathParser <Path, Edge, Vertex, Weight> pathParser = PathParser <Path, Edge, Vertex, Weight> .CreatePathParserDefault(edgesForBigGraph);

            //assertThat("At least some implementation should be used", pathFinderFactoriesForImplementationsToTest.size(), greaterThanOrEqualTo(1));
            // TODO: "hamcrest" syntax similar to above java code
            Assert.That(pathFinderFactoriesForImplementationsToTest.Count >= 1, "At least some implementation should be used");
            for (int i = 0; i < pathFinderFactoriesForImplementationsToTest.Count; i++)
            {
                PathFinderFactory pathFinderFactory = pathFinderFactoriesForImplementationsToTest[i];
                output("Will now test file " + optionalPathToResourceXmlFile + " with impl " + pathFinderFactory.GetType().Name);
                TimeMeasurer tm         = TimeMeasurer.Start();
                PathFinder   pathFinder = pathFinderFactory.CreatePathFinder(
                    edgesForBigGraph,
                    GraphEdgesValidationDesired.NO                 // do the validation one time instead of doing it for each pathFinderFactory
                    );
                IList <Path> shortestPaths = pathFinder.FindShortestPaths(startVertex, endVertex, numberOfPathsToFind);
                Assert.IsNotNull(shortestPaths);
                //assertThat("At least some path should be found", shortestPaths.size(), greaterThanOrEqualTo(1));
                Assert.That(shortestPaths.Count >= 1, "At least some path should be found"); // TODO "hamcrest" syntax as java above
                output(
                    messagePrefixWithInformationAboutXmlSourcefileWithTestData
                    + "Seconds: " + tm.GetSeconds()
                    + ". Implementation: " + pathFinder.GetType().Name,
                    ConsoleOutputDesired.TIME_MEASURE
                    );
                if (isAllConsoleOutputDesired())
                {
                    DisplayListOfShortestPath(shortestPaths);
                    output("Implementation class for above and below output: " + pathFinderFactory.GetType().Name);
                    DisplayAsPathStringsWhichCanBeUsedInXml(shortestPaths, pathParser);
                }
                shortestPathsPerImplementation.Add(pathFinder.GetType().Name, shortestPaths);
            }
            IList <string> nameOfImplementations = new List <string>(shortestPathsPerImplementation.Keys);

            if (expectedListOfPaths != null)
            {
                AssertResultsWithExpectedPaths(
                    expectedListOfPaths,
                    optionalPathToResourceXmlFile,
                    shortestPathsPerImplementation
                    );
            }
            else // if(expectedListOfPaths != null) {
            if (shouldTestResultsWithImplementationsAgainstEachOther)
            {
                AssertResultsWithImplementationsAgainstEachOther(
                    optionalPathToResourceXmlFile,
                    shortestPathsPerImplementation
                    );
            }
        }