public void RunSearch <TVertex, TEdge>(
            [PexAssumeNotNull] IBidirectionalGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            if (g.VertexCount == 0)
            {
                return;
            }

            Func <TEdge, double> edgeWeights = e => 1;
            var distanceRelaxer = DistanceRelaxers.ShortestDistance;

            var search = new BestFirstFrontierSearchAlgorithm <TVertex, TEdge>(
                null,
                g,
                edgeWeights,
                distanceRelaxer);
            var root     = Enumerable.First(g.Vertices);
            var target   = Enumerable.Last(g.Vertices);
            var recorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (recorder.Attach(search))
                search.Compute(root, target);

            if (recorder.VertexPredecessors.ContainsKey(target))
            {
                TestConsole.WriteLine("cost: {0}", recorder.VertexPredecessors[target]);
                IEnumerable <TEdge> path;
                Assert.IsTrue(recorder.TryGetPath(target, out path));
            }
#if DEBUG
            TestConsole.WriteLine("operator max count: {0}", search.OperatorMaxCount);
#endif
        }
        public void CompareSearch <TVertex, TEdge>(
            [PexAssumeNotNull] IBidirectionalGraph <TVertex, TEdge> g,
            TVertex root, TVertex target)
            where TEdge : IEdge <TVertex>
        {
            Func <TEdge, double> edgeWeights = e => 1;
            var distanceRelaxer = DistanceRelaxers.ShortestDistance;

            var search = new BestFirstFrontierSearchAlgorithm <TVertex, TEdge>(
                null,
                g,
                edgeWeights,
                distanceRelaxer);
            var recorder = new VertexDistanceRecorderObserver <TVertex, TEdge>(edgeWeights);

            using (recorder.Attach(search))
                search.Compute(root, target);

            var dijkstra    = new DijkstraShortestPathAlgorithm <TVertex, TEdge>(g, edgeWeights, distanceRelaxer);
            var dijRecorder = new VertexDistanceRecorderObserver <TVertex, TEdge>(edgeWeights);

            using (dijRecorder.Attach(dijkstra))
                dijkstra.Compute(root);

            var    fvp = recorder.Distances;
            var    dvp = dijRecorder.Distances;
            double cost;

            if (dvp.TryGetValue(target, out cost))
            {
                Assert.IsTrue(fvp.ContainsKey(target), "target {0} not found, should be {1}", target, cost);
                Assert.AreEqual(dvp[target], fvp[target]);
            }
        }
        public void SetTargetVertex_Throws()
        {
            var graph     = new BidirectionalGraph <TestVertex, Edge <TestVertex> >();
            var algorithm = new BestFirstFrontierSearchAlgorithm <TestVertex, Edge <TestVertex> >(graph, _ => 1.0, DistanceRelaxers.EdgeShortestDistance);

            SetTargetVertex_Throws_Test(algorithm);
        }
        private static void CompareSearches <TVertex, TEdge>(
            [NotNull] IBidirectionalGraph <TVertex, TEdge> graph,
            [NotNull] TVertex root,
            [NotNull] TVertex target)
            where TEdge : IEdge <TVertex>
        {
            double EdgeWeights(TEdge edge) => 1.0;

            IDistanceRelaxer distanceRelaxer = DistanceRelaxers.ShortestDistance;

            var search = new BestFirstFrontierSearchAlgorithm <TVertex, TEdge>(
                graph,
                EdgeWeights,
                distanceRelaxer);
            var recorder = new VertexDistanceRecorderObserver <TVertex, TEdge>(EdgeWeights);

            using (recorder.Attach(search))
                search.Compute(root, target);

            var dijkstra         = new DijkstraShortestPathAlgorithm <TVertex, TEdge>(graph, EdgeWeights, distanceRelaxer);
            var dijkstraRecorder = new VertexDistanceRecorderObserver <TVertex, TEdge>(EdgeWeights);

            using (dijkstraRecorder.Attach(dijkstra))
                dijkstra.Compute(root);

            IDictionary <TVertex, double> bffsVerticesDistances     = recorder.Distances;
            IDictionary <TVertex, double> dijkstraVerticesDistances = dijkstraRecorder.Distances;

            if (dijkstraVerticesDistances.TryGetValue(target, out double cost))
            {
                Assert.IsTrue(bffsVerticesDistances.ContainsKey(target), $"Target {target} not found, should be {cost}.");
                Assert.AreEqual(dijkstraVerticesDistances[target], bffsVerticesDistances[target]);
            }
        }
        public void ClearTargetVertex()
        {
            var graph     = new BidirectionalGraph <int, Edge <int> >();
            var algorithm = new BestFirstFrontierSearchAlgorithm <int, Edge <int> >(graph, _ => 1.0, DistanceRelaxers.EdgeShortestDistance);

            ClearTargetVertex_Test(algorithm);
        }
        private static void RunSearch <TVertex, TEdge>(
            [NotNull] IBidirectionalGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            if (graph.VertexCount == 0)
            {
                return;
            }

            IDistanceRelaxer distanceRelaxer = DistanceRelaxers.ShortestDistance;

            var search = new BestFirstFrontierSearchAlgorithm <TVertex, TEdge>(
                graph,
                e => 1,
                distanceRelaxer);

            TVertex root   = graph.Vertices.First();
            TVertex target = graph.Vertices.Last();

            var recorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (recorder.Attach(search))
                search.Compute(root, target);

            if (recorder.VertexPredecessors.ContainsKey(target))
            {
                Assert.IsTrue(recorder.TryGetPath(target, out _));
            }
        }
        public void ComputeWithRootAndTarget()
        {
            var graph = new BidirectionalGraph <int, Edge <int> >();

            graph.AddVertexRange(new[] { 0, 1 });
            var algorithm = new BestFirstFrontierSearchAlgorithm <int, Edge <int> >(graph, _ => 1.0, DistanceRelaxers.EdgeShortestDistance);

            ComputeWithRootAndTarget_Test(algorithm);
        }
        public void Constructor()
        {
            var graph     = new BidirectionalGraph <int, Edge <int> >();
            var algorithm = new BestFirstFrontierSearchAlgorithm <int, Edge <int> >(graph, _ => 1.0, DistanceRelaxers.ShortestDistance);

            AssertAlgorithmState(algorithm, graph);

            algorithm = new BestFirstFrontierSearchAlgorithm <int, Edge <int> >(null, graph, _ => 1.0, DistanceRelaxers.ShortestDistance);
            AssertAlgorithmState(algorithm, graph);
        }
        public void ComputeWithRootAndTarget_Throws()
        {
            var graph1     = new BidirectionalGraph <int, Edge <int> >();
            var algorithm1 = new BestFirstFrontierSearchAlgorithm <int, Edge <int> >(graph1, _ => 1.0, DistanceRelaxers.EdgeShortestDistance);

            ComputeWithRootAndTarget_Throws_Test(graph1, algorithm1);

            var graph2     = new BidirectionalGraph <TestVertex, Edge <TestVertex> >();
            var algorithm2 = new BestFirstFrontierSearchAlgorithm <TestVertex, Edge <TestVertex> >(graph2, _ => 1.0, DistanceRelaxers.EdgeShortestDistance);

            ComputeWithRootAndTarget_Throws_Test(algorithm2);
        }
Exemplo n.º 10
0
        public static IDictionary <string, TaggedEdge <string, string> > GetVertexPredecessor(IBidirectionalGraph <string, TaggedEdge <string, string> > g, string root, string target)
        {
            IDistanceRelaxer distanceRelaxer = DistanceRelaxers.EdgeShortestDistance;
            var algorithm = new BestFirstFrontierSearchAlgorithm <string, TaggedEdge <string, string> >(g, edgeWeights => double.Parse(edgeWeights.Tag), distanceRelaxer);

            var recorder = new VertexPredecessorRecorderObserver <string, TaggedEdge <string, string> >();

            using (recorder.Attach(algorithm))
                algorithm.Compute(root, target);

            return(recorder.VerticesPredecessors);
        }
Exemplo n.º 11
0
        public static IDictionary <int, Edge <int> > GetVertexPredecessor(IBidirectionalGraph <int, Edge <int> > g, int root, int target)
        {
            IDistanceRelaxer distanceRelaxer = DistanceRelaxers.EdgeShortestDistance;
            var algorithm = new BestFirstFrontierSearchAlgorithm <int, Edge <int> >(g, edgeWeights => 1.0, distanceRelaxer);

            var recorder = new VertexPredecessorRecorderObserver <int, Edge <int> >();

            using (recorder.Attach(algorithm))
                algorithm.Compute(root, target);

            return(recorder.VerticesPredecessors);
        }
Exemplo n.º 12
0
        public static bool FoundTarget <TVertex, TEdge>(IBidirectionalGraph <TVertex, TEdge> g, TVertex root, TVertex target) where TEdge : IEdge <TVertex>
        {
            IDistanceRelaxer distanceRelaxer = DistanceRelaxers.EdgeShortestDistance;
            var  algorithm     = new BestFirstFrontierSearchAlgorithm <TVertex, TEdge>(g, edgeWeights => 1.0, distanceRelaxer);
            bool targetReached = false;

            algorithm.TargetReached += (sender, args) => targetReached = true;

            var recorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (recorder.Attach(algorithm))
                algorithm.Compute(root, target);

            return(targetReached);
        }
Exemplo n.º 13
0
        static void FrontierDijkstra <TVertex, TEdge>(
            IBidirectionalGraph <TVertex, TEdge> g,
            Dictionary <TEdge, double> distances,
            TVertex root,
            TVertex target)
            where TEdge : IEdge <TVertex>
        {
            var algo = new BestFirstFrontierSearchAlgorithm <TVertex, TEdge>(
                null,
                g,
                AlgorithmExtensions.GetIndexer(distances),
                DistanceRelaxers.ShortestDistance
                );
            var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (predecessors.Attach(algo))
                algo.Compute(root, target);
        }
        public void SameStartAndEnd()
        {
            var graph = new BidirectionalGraph <int, Edge <int> >();

            graph.AddVerticesAndEdge(new Edge <int>(1, 3));
            graph.AddVerticesAndEdge(new Edge <int>(1, 2));
            graph.AddVerticesAndEdge(new Edge <int>(2, 5));
            graph.AddVerticesAndEdge(new Edge <int>(2, 4));
            graph.AddVerticesAndEdge(new Edge <int>(5, 6));
            graph.AddVerticesAndEdge(new Edge <int>(5, 7));

            var algorithm = new BestFirstFrontierSearchAlgorithm <int, Edge <int> >(
                graph, _ => 1.0, DistanceRelaxers.ShortestDistance);
            bool targetReached = false;

            algorithm.TargetReached += (_, _) => targetReached = true;

            algorithm.Compute(1, 1);
            Assert.IsTrue(targetReached);
        }
        private static void RunAndCheckSearch <TVertex, TEdge>(
            [NotNull] IBidirectionalGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            if (graph.VertexCount == 0)
            {
                return;
            }

            IDistanceRelaxer distanceRelaxer = DistanceRelaxers.ShortestDistance;

            var search = new BestFirstFrontierSearchAlgorithm <TVertex, TEdge>(
                graph,
                _ => 1.0,
                distanceRelaxer);
            bool targetReached = false;

            search.TargetReached += (_, _) => targetReached = true;

            TVertex root   = graph.Vertices.First();
            TVertex target = graph.Vertices.Last();

            var recorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (recorder.Attach(search))
                search.Compute(root, target);

            if (recorder.VerticesPredecessors.ContainsKey(target))
            {
                Assert.IsTrue(recorder.TryGetPath(target, out IEnumerable <TEdge> path));

                if (Equals(root, path.First().Source))
                {
                    Assert.IsTrue(targetReached);
                }
                else
                {
                    Assert.IsFalse(targetReached);
                }
            }
        }