Ejemplo n.º 1
0
        private static void NormalizedEdgeChainTest <TVertex, TEdge>([NotNull] IVertexListGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            if (graph.VertexCount == 0)
            {
                return;
            }

            foreach (TVertex root in graph.Vertices)
            {
                var walker = new RandomWalkAlgorithm <TVertex, TEdge>(graph)
                {
                    EdgeChain = new NormalizedMarkovEdgeChain <TVertex, TEdge>()
                };

                walker.Generate(root);
            }
        }
Ejemplo n.º 2
0
        private static void NormalizedEdgeChainTestWithVisitor <TVertex, TEdge>([NotNull] IVertexListGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            if (graph.VertexCount == 0)
            {
                return;
            }

            foreach (TVertex root in graph.Vertices)
            {
                var walker = new RandomWalkAlgorithm <TVertex, TEdge>(graph)
                {
                    EdgeChain = new NormalizedMarkovEdgeChain <TVertex, TEdge>()
                };

                var vis = new EdgeRecorderObserver <TVertex, TEdge>();
                using (vis.Attach(walker))
                    walker.Generate(root);
            }
        }
Ejemplo n.º 3
0
        public void RandomWalkWithPredicate()
        {
            var edge1 = new Edge <int>(1, 2);
            var edge2 = new Edge <int>(1, 3);
            var edge3 = new Edge <int>(2, 3);
            var edge4 = new Edge <int>(3, 4);
            var edge5 = new Edge <int>(4, 5);
            var edge6 = new Edge <int>(5, 4);
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVerticesAndEdgeRange(new[]
            {
                edge1, edge2, edge3, edge4, edge5, edge6
            });
            var chain = new NormalizedMarkovEdgeChain <int, Edge <int> >
            {
                Rand = new Random(123456)
            };

            var algorithm = new RandomWalkAlgorithm <int, Edge <int> >(graph, chain)
            {
                EndPredicate = edge => edge == edge4
            };

            var encounteredEdges = new List <Edge <int> >();

            algorithm.TreeEdge  += edge => encounteredEdges.Add(edge);
            algorithm.EndVertex += vertex => Assert.AreEqual(3, vertex);

            algorithm.Generate(1, int.MaxValue);

            CollectionAssert.IsNotEmpty(encounteredEdges);
            Assert.AreEqual(3, encounteredEdges.Last().Target);
            Assert.IsTrue(
                edge2 == encounteredEdges.Last()
                ||
                edge3 == encounteredEdges.Last());
        }
Ejemplo n.º 4
0
        private static void RunRandomWalkAndCheck <TVertex, TEdge>(
            [NotNull] IVertexListGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            if (graph.VertexCount == 0)
            {
                return;
            }

            foreach (TVertex root in graph.Vertices)
            {
                RandomWalkAlgorithm <TVertex, TEdge> walker1 = CreateAlgorithm();
                bool calledStart1      = false;
                bool calledEnd1        = false;
                var  encounteredEdges1 = new List <TEdge>();
                walker1.StartVertex += vertex =>
                {
                    Assert.IsFalse(calledStart1);
                    calledStart1 = true;
                    Assert.AreEqual(root, vertex);
                };
                walker1.TreeEdge += edge =>
                {
                    Assert.IsNotNull(edge);
                    encounteredEdges1.Add(edge);
                };
                walker1.EndVertex += vertex =>
                {
                    Assert.IsFalse(calledEnd1);
                    calledEnd1 = true;
                    Assert.IsNotNull(vertex);
                };

                RandomWalkAlgorithm <TVertex, TEdge> walker2 = CreateAlgorithm();
                bool calledStart2      = false;
                bool calledEnd2        = false;
                var  encounteredEdges2 = new List <TEdge>();
                walker2.StartVertex += vertex =>
                {
                    Assert.IsFalse(calledStart2);
                    calledStart2 = true;
                    Assert.AreEqual(root, vertex);
                };
                walker2.TreeEdge += edge =>
                {
                    Assert.IsNotNull(edge);
                    encounteredEdges2.Add(edge);
                };
                walker2.EndVertex += vertex =>
                {
                    Assert.IsFalse(calledEnd2);
                    calledEnd2 = true;
                    Assert.IsNotNull(vertex);
                };

                RandomWalkAlgorithm <TVertex, TEdge> walker3 = CreateAlgorithm();
                bool calledStart3      = false;
                bool calledEnd3        = false;
                var  encounteredEdges3 = new List <TEdge>();
                walker3.StartVertex += vertex =>
                {
                    Assert.IsFalse(calledStart3);
                    calledStart3 = true;
                    Assert.AreEqual(root, vertex);
                };
                walker3.TreeEdge += edge =>
                {
                    Assert.IsNotNull(edge);
                    encounteredEdges3.Add(edge);
                };
                walker3.EndVertex += vertex =>
                {
                    Assert.IsFalse(calledEnd3);
                    calledEnd3 = true;
                    Assert.IsNotNull(vertex);
                };

                var vis1 = new EdgeRecorderObserver <TVertex, TEdge>();
                using (vis1.Attach(walker1))
                    walker1.Generate(root);
                Assert.IsTrue(calledStart1);
                Assert.IsTrue(calledEnd1);

                walker2.SetRootVertex(root);
                var vis2 = new EdgeRecorderObserver <TVertex, TEdge>();
                using (vis2.Attach(walker2))
                    walker2.Compute();
                Assert.IsTrue(calledStart2);
                Assert.IsTrue(calledEnd2);

                var vis3 = new EdgeRecorderObserver <TVertex, TEdge>();
                using (vis3.Attach(walker3))
                    walker3.Generate(root, 100);
                Assert.IsTrue(calledStart3);
                Assert.IsTrue(calledEnd3);

                CollectionAssert.AreEqual(vis1.Edges, encounteredEdges1);
                CollectionAssert.AreEqual(vis1.Edges, encounteredEdges2);
                CollectionAssert.AreEqual(vis1.Edges, encounteredEdges3);
                CollectionAssert.AreEqual(vis1.Edges, vis2.Edges);
                CollectionAssert.AreEqual(vis1.Edges, vis3.Edges);
            }

            #region Local function

            RandomWalkAlgorithm <TVertex, TEdge> CreateAlgorithm()
            {
                var walker = new RandomWalkAlgorithm <TVertex, TEdge>(graph)
                {
                    EdgeChain = new NormalizedMarkovEdgeChain <TVertex, TEdge>
                    {
                        Rand = new Random(123456)
                    }
                };

                return(walker);
            }

            #endregion
        }