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]);
            }
        }
        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 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
        }
        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 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);
        }
        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);
        }
        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);
        }
Beispiel #8
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);
                }
            }
        }