public void Constructor()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();
            IMarkovEdgeChain <int, Edge <int> > markovChain1 = new NormalizedMarkovEdgeChain <int, Edge <int> >();
            IMarkovEdgeChain <int, Edge <int> > markovChain2 = new WeightedMarkovEdgeChain <int, Edge <int> >(new Dictionary <Edge <int>, double>());

            var algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

            algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, markovChain1);
            AssertAlgorithmProperties(algorithm, graph, markovChain1);

            algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(graph, markovChain2);
            AssertAlgorithmProperties(algorithm, graph, markovChain2);

            algorithm = new CyclePoppingRandomTreeAlgorithm <int, Edge <int> >(null, graph, markovChain1);
            AssertAlgorithmProperties(algorithm, graph, markovChain1);

            var random = new Random(123456);

            algorithm.Rand = random;
            AssertAlgorithmProperties(algorithm, graph, markovChain1, random);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                CyclePoppingRandomTreeAlgorithm <TVertex, TEdge> algo,
                IVertexListGraph <TVertex, TEdge> g,
                IMarkovEdgeChain <TVertex, TEdge> chain = null,
                Random rand = null
                )
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                if (chain is null)
                {
                    Assert.IsNotNull(algo.EdgeChain);
                }
                else
                {
                    Assert.AreSame(chain, algo.EdgeChain);
                }
                if (rand is null)
                {
                    Assert.IsNotNull(algo.Rand);
                }
                else
                {
                    Assert.AreSame(rand, algo.Rand);
                }
                CollectionAssert.IsEmpty(algo.Successors);
                CollectionAssert.IsEmpty(algo.VerticesColors);
            }

            #endregion
        }
コード例 #2
0
        public void Constructor_WeightedMarkovEdgeChains()
        {
            var weights = new Dictionary <Edge <int>, double>();
            var chain1  = new WeightedMarkovEdgeChain <int, Edge <int> >(weights);

            Assert.AreSame(weights, chain1.Weights);

            var chain2 = new VanishingWeightedMarkovEdgeChain <int, Edge <int> >(weights);

            Assert.AreSame(weights, chain2.Weights);

            chain2 = new VanishingWeightedMarkovEdgeChain <int, Edge <int> >(weights, 2.0);
            Assert.AreSame(weights, chain2.Weights);
        }
コード例 #3
0
        public void Constructor()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();
            var chain = new WeightedMarkovEdgeChain <int, Edge <int> >(new Dictionary <Edge <int>, double>());
            EdgePredicate <int, Edge <int> > predicate = edge => true;
            var algorithm = new RandomWalkAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

            algorithm = new RandomWalkAlgorithm <int, Edge <int> >(graph, chain);
            AssertAlgorithmProperties(algorithm, graph, chain);

            algorithm = new RandomWalkAlgorithm <int, Edge <int> >(graph)
            {
                EndPredicate = predicate
            };
            AssertAlgorithmProperties(algorithm, graph, p: predicate);

            algorithm = new RandomWalkAlgorithm <int, Edge <int> >(graph)
            {
                EdgeChain = chain
            };
            AssertAlgorithmProperties(algorithm, graph, chain);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                RandomWalkAlgorithm <TVertex, TEdge> algo,
                IVertexListGraph <TVertex, TEdge> g,
                IEdgeChain <TVertex, TEdge> c    = null,
                EdgePredicate <TVertex, TEdge> p = null)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                if (c is null)
                {
                    Assert.IsNotNull(algo.EdgeChain);
                }
                else
                {
                    Assert.AreSame(c, algo.EdgeChain);
                }
                Assert.AreEqual(p, algo.EndPredicate);
            }

            #endregion
        }
コード例 #4
0
ファイル: MainForm.cs プロジェクト: tmauldin/mb-unit
        private void menuItem8_Click(object sender, System.EventArgs e)
        {
            if (this.netronPanel.Graph == null)
            {
                throw new Exception("Generate a graph first");
            }
            if (this.netronPanel.Populator == null)
            {
                throw new Exception("Populator should not be null.");
            }

            ResetVertexAndEdgeColors();

            // create algorithm
            this.vertexCounts = new VertexIntDictionary();
            this.edgeCounts   = new EdgeIntDictionary();
            foreach (IVertex vertex in this.netronPanel.Graph.Vertices)
            {
                this.vertexCounts[vertex] = 0;
            }
            foreach (IEdge edge in this.netronPanel.Graph.Edges)
            {
                this.edgeCounts[edge] = 0;
            }

            this.edgeWeights = new EdgeDoubleDictionary();
            foreach (IEdge edge in this.netronPanel.Graph.Edges)
            {
                edgeWeights[edge] = 1;
            }
            WeightedMarkovEdgeChain chain  = new WeightedMarkovEdgeChain(edgeWeights);
            RandomWalkAlgorithm     walker = new RandomWalkAlgorithm(
                this.netronPanel.Graph
                );

            walker.TreeEdge += new EdgeEventHandler(walker_WeightedTreeEdge);

            LayoutAlgorithmTraverVisitor tracer = new LayoutAlgorithmTraverVisitor(this.netronPanel.Populator);

            walker.TreeEdge += new EdgeEventHandler(tracer.TreeEdge);


            Thread thread = new Thread(new ThreadStart(walker.Generate));

            thread.Start();
        }
コード例 #5
0
        public void Constructor_Throws()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();
            var chain = new WeightedMarkovEdgeChain <int, Edge <int> >(new Dictionary <Edge <int>, double>());

            // ReSharper disable ObjectCreationAsStatement
            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(
                () => new RandomWalkAlgorithm <int, Edge <int> >(null));
            Assert.Throws <ArgumentNullException>(
                () => new RandomWalkAlgorithm <int, Edge <int> >(graph, null));
            Assert.Throws <ArgumentNullException>(
                () => new RandomWalkAlgorithm <int, Edge <int> >(null, chain));
            Assert.Throws <ArgumentNullException>(
                () => new RandomWalkAlgorithm <int, Edge <int> >(null, null));

            var algorithm = new RandomWalkAlgorithm <int, Edge <int> >(graph, chain);

            Assert.Throws <ArgumentNullException>(() => algorithm.EdgeChain = null);
            // ReSharper restore AssignNullToNotNullAttribute
            // ReSharper restore ObjectCreationAsStatement
        }
コード例 #6
0
        public void WeightedMarkovEdgeChain()
        {
            var weights = new Dictionary <EquatableEdge <int>, double>
            {
                [new EquatableEdge <int>(1, 2)] = 1.0,
                [new EquatableEdge <int>(1, 3)] = 2.0,
                [new EquatableEdge <int>(2, 3)] = -1.0,
                [new EquatableEdge <int>(2, 5)] = 10.0,
                [new EquatableEdge <int>(3, 4)] = 5.0,
                [new EquatableEdge <int>(4, 3)] = 25.0,
                [new EquatableEdge <int>(4, 5)] = 2.0,
                [new EquatableEdge <int>(4, 7)] = 1.0,
                [new EquatableEdge <int>(5, 2)] = 3.0,
                [new EquatableEdge <int>(5, 6)] = 1.0,
                [new EquatableEdge <int>(6, 7)] = 1.5,
                [new EquatableEdge <int>(7, 4)] = 0.0,
                [new EquatableEdge <int>(8, 3)] = 1.0
            };
            IVertexAndEdgeListGraph <int, EquatableEdge <int> > graph1 = CreateGraph1();

            var chain = new WeightedMarkovEdgeChain <int, EquatableEdge <int> >(weights)
            {
                Rand = new Random(123456)
            };

            Assert.IsTrue(chain.TryGetSuccessor(graph1, 1, out EquatableEdge <int> edge));
            Assert.AreEqual(1, edge.Source);
            Assert.AreEqual(2, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(graph1, 2, out edge));
            Assert.AreEqual(2, edge.Source);
            Assert.AreEqual(5, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(graph1, 5, out edge));
            Assert.AreEqual(5, edge.Source);
            Assert.AreEqual(6, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(graph1, 6, out edge));
            Assert.AreEqual(6, edge.Source);
            Assert.AreEqual(7, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(graph1, 7, out edge));
            Assert.AreEqual(7, edge.Source);
            Assert.AreEqual(4, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(graph1, 4, out edge));
            Assert.AreEqual(4, edge.Source);
            Assert.AreEqual(3, edge.Target);
            Assert.IsFalse(chain.TryGetSuccessor(graph1, 3, out edge));

            chain = new WeightedMarkovEdgeChain <int, EquatableEdge <int> >(weights)
            {
                Rand = new Random(123456)
            };
            EquatableEdge <int>[] edges = graph1.Edges.ToArray();
            Assert.IsTrue(chain.TryGetSuccessor(edges, 1, out edge));
            Assert.AreEqual(2, edge.Source);
            Assert.AreEqual(5, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(edges, 5, out edge));
            Assert.AreEqual(2, edge.Source);
            Assert.AreEqual(5, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(edges, 5, out edge));
            Assert.AreEqual(1, edge.Source);
            Assert.AreEqual(3, edge.Target);
            // etc.
            Assert.IsFalse(chain.TryGetSuccessor(Enumerable.Empty <EquatableEdge <int> >(), 1, out _));


            IVertexAndEdgeListGraph <int, EquatableEdge <int> > graph2 = CreateGraph2();

            chain = new WeightedMarkovEdgeChain <int, EquatableEdge <int> >(weights)
            {
                Rand = new Random(123456)
            };
            Assert.IsTrue(chain.TryGetSuccessor(graph2, 1, out edge));
            Assert.AreEqual(1, edge.Source);
            Assert.AreEqual(2, edge.Target);
            Assert.IsFalse(chain.TryGetSuccessor(graph2, 2, out edge));

            chain = new WeightedMarkovEdgeChain <int, EquatableEdge <int> >(weights)
            {
                Rand = new Random(123456)
            };
            edges = graph2.Edges.ToArray();
            Assert.IsTrue(chain.TryGetSuccessor(edges, 1, out edge));
            Assert.AreEqual(3, edge.Source);
            Assert.AreEqual(4, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(edges, 4, out edge));
            Assert.AreEqual(1, edge.Source);
            Assert.AreEqual(3, edge.Target);
            Assert.IsTrue(chain.TryGetSuccessor(edges, 1, out edge));
            Assert.AreEqual(1, edge.Source);
            Assert.AreEqual(2, edge.Target);
            // Etc.
        }