Ejemplo n.º 1
0
        public void DisposeTest()
        {
            // creates network nodes
            const int num     = 10;
            var       network = new Network();

            for (var i = 0u; i < num; i++)
            {
                network.AddVertex(i);
            }

            // creates edges
            for (var i = 0u; i < num; i++)
            {
                for (var j = i + 1; j < num; j++)
                {
                    network.AddEdge(new Connection(i, j));
                }
            }

            // creates and updates community algorithm
            var commAlgorithm = new CommunityAlgorithm(network);

            commAlgorithm.Update();

            // disposes algorithm
            commAlgorithm.Dispose();

            Assert.IsNull(commAlgorithm.Communities, "Community algorithm should not have any communities.");
            Assert.IsNull(commAlgorithm.NodesCommunities, "Community algorithm should not have any communities.");

            commAlgorithm.Dispose();
        }
Ejemplo n.º 2
0
        public void ZeroWeightsTest()
        {
            // creates network nodes
            const int num     = 10;
            var       network = new Network();

            for (var i = 0u; i < num; i++)
            {
                network.AddVertex(i);
            }

            // creates edges (full with 0 weight)
            for (var i = 0u; i < num; i++)
            {
                for (var j = i + 1; j < num; j++)
                {
                    network.AddEdge(new Connection(i, j, 0));
                }
            }
            Console.WriteLine(network);

            // creates and updates community algorithm
            var commAlgorithm = new CommunityAlgorithm(network);

            commAlgorithm.Update();
            commAlgorithm.DisplayNodesCommunities();
            commAlgorithm.DisplayCommunities();

            for (var i = 0u; i < num; i++)
            {
                Assert.AreEqual(i, commAlgorithm.NodesCommunities[i], $"Node {i} should be in community {i}.");
            }
            Assert.AreEqual(num, commAlgorithm.GetNumberCommunities(), $"Num. communities should be {num}.");
            Assert.AreEqual(0, commAlgorithm.GetModularity(), double.Epsilon, "Community modularity should be 0.");
        }
Ejemplo n.º 3
0
        public void CommunityGraphTest()
        {
            // creates network nodes
            const int num     = 10;
            var       network = new Network();

            for (var i = 0u; i < num; i++)
            {
                network.AddVertex(i);
            }
            Console.WriteLine(network);

            // creates and updates community algorithm
            var commAlgorithm = new CommunityAlgorithm(network);

            commAlgorithm.Update();
            commAlgorithm.DisplayNodesCommunities();
            commAlgorithm.DisplayCommunities();

            // gets community graph
            var communityGraph = commAlgorithm.GetCommunityNetwork();

            Console.WriteLine(communityGraph);

            Assert.AreEqual(0, communityGraph.EdgeCount, "Community graph should not contain edges.");
            Assert.AreEqual(num, communityGraph.VertexCount, $"Num. communities should be {num}.");
            Assert.AreEqual(0, communityGraph.TotalWeight, double.Epsilon, "Community graph total weight should be 0.");
        }
Ejemplo n.º 4
0
        public void NoConnectionsTest()
        {
            // creates network nodes
            const int num     = 10;
            var       network = new Network();

            for (var i = 0u; i < num; i++)
            {
                network.AddVertex(i);
            }
            Console.WriteLine(network);

            // creates and updates community algorithm
            var commAlgorithm = new CommunityAlgorithm(network);

            commAlgorithm.Update();
            commAlgorithm.DisplayNodesCommunities();
            commAlgorithm.DisplayCommunities();

            Assert.AreEqual(0, network.EdgeCount, "Network should not contain edges.");
            for (var i = 0u; i < num; i++)
            {
                Assert.AreEqual(i, commAlgorithm.NodesCommunities[i], $"Node {i} should be in community {i}.");
            }
            Assert.AreEqual(num, commAlgorithm.GetNumberCommunities(), $"Num. communities should be {num}.");
            Assert.AreEqual(0, commAlgorithm.GetModularity(), double.Epsilon, "Community modularity should be 0.");
        }
Ejemplo n.º 5
0
        private static void Update(CommunityAlgorithm communityAlg, CommunityTracker tracker)
        {
            communityAlg.Update();
            communityAlg.DisplayNodesCommunities();
            Console.WriteLine("Modularity: {0}", communityAlg.GetModularity());

            tracker.Update();
        }
Ejemplo n.º 6
0
        private static void SaveFileTest(PaletteGenerator paletteGenerator, string name)
        {
            // creates graph and adds nodes
            var network = new Network();

            for (var i = 0u; i < NUM_NODES; i++)
            {
                network.AddVertex(i);
            }

            // adds connections
            network.AddEdge(new Connection(0, 1));
            network.AddEdge(new Connection(0, 2));
            network.AddEdge(new Connection(0, 9));
            network.AddEdge(new Connection(2, 4));
            network.AddEdge(new Connection(2, 9));
            network.AddEdge(new Connection(4, 7));
            network.AddEdge(new Connection(7, 9));
            network.AddEdge(new Connection(8, 9));

            // creates algorithm and updates communities
            var communityAlg = new CommunityAlgorithm(network, -1, 0.000001);

            communityAlg.Update();
            communityAlg.DisplayCommunities();

            var fullPath = Path.GetFullPath(".");

            foreach (var imageType in ImageTypes)
            {
                var fileName = $"{FILE_NAME}-{name}-{imageType}";
                var dotPath  = Path.Combine(fullPath, $"{fileName}.dot");
                var imgPath  = $"{dotPath}.{imageType.ToString().ToLower()}";
                File.Delete(dotPath);
                File.Delete(imgPath);

                var filePath = communityAlg.ToGraphvizFile(
                    fullPath, fileName, true, imageType, paletteGenerator, WAIT_TIMEOUT);

                Console.WriteLine(dotPath);
                Assert.IsTrue(File.Exists(dotPath), $"Dot file should exist in {dotPath}.");
                Assert.AreEqual(filePath, dotPath, $"Dot file should be exist in {imgPath}");

                Console.WriteLine(imgPath);
                Assert.IsTrue(File.Exists(imgPath), $"Image file should exist in {imgPath}.");
                Assert.IsTrue(new FileInfo(imgPath).Length > 0, "Image size should be > 0 bytes.");

#if !DEBUG
                File.Delete(dotPath);
                File.Delete(imgPath);
#endif
            }
        }
Ejemplo n.º 7
0
        public void SaveFileTest()
        {
            // creates graph and adds nodes
            var network = new Network();

            for (var i = 0u; i < NUM_NODES; i++)
            {
                network.AddVertex(i);
            }

            // adds connections
            network.AddEdge(new Connection(0, 1));
            network.AddEdge(new Connection(0, 2));
            network.AddEdge(new Connection(0, 9));
            network.AddEdge(new Connection(2, 4));
            network.AddEdge(new Connection(2, 9));
            network.AddEdge(new Connection(4, 7));
            network.AddEdge(new Connection(7, 9));
            network.AddEdge(new Connection(8, 9));

            // creates algorithm and updates communities
            var communityAlg = new CommunityAlgorithm(network, -1, 0.000001);

            communityAlg.Update();
            communityAlg.DisplayCommunities();

            var fullPath = Path.Combine(Path.GetFullPath("."), FILE_NAME);

            File.Delete(fullPath);

            communityAlg.ToD3GraphFile(FILE_NAME, Formatting.Indented);

            Console.WriteLine(fullPath);
            Assert.IsTrue(File.Exists(fullPath), $"D3 json file should exist in {fullPath}.");
            Assert.IsTrue(new FileInfo(fullPath).Length > 0, "Json file size should be > 0 bytes.");
#if !DEBUG
            File.Delete(fullPath);
#endif
        }
Ejemplo n.º 8
0
        public void RenumberTest()
        {
            // creates network nodes
            const int num     = 10;
            var       network = new Network();

            for (var i = 0u; i < num; i++)
            {
                network.AddVertex(i);
            }

            // creates edges (random)
            var random = new Random();

            for (var i = 0u; i < num; i++)
            {
                for (var j = i + 1; j < num; j++)
                {
                    if (random.NextDouble() > 0.6)
                    {
                        network.AddEdge(new Connection(i, j));
                    }
                }
            }
            Console.WriteLine(network);

            // creates and updates community algorithm
            var commAlgorithm = new CommunityAlgorithm(network);

            commAlgorithm.Update(false);

            // copies communities
            Console.WriteLine("Before renumbering:");
            commAlgorithm.DisplayCommunities();
            var beforeComms = new HashSet <uint> [num];

            for (var i = 0; i < num; i++)
            {
                beforeComms[i] = new HashSet <uint>(commAlgorithm.Communities[i]);
            }
            var beforeNumComms = commAlgorithm.GetNumberCommunities();

            // renumbers communities
            Console.WriteLine("After renumbering:");
            commAlgorithm.RenumberCommunities();
            commAlgorithm.DisplayCommunities();

            Assert.AreEqual(beforeNumComms, commAlgorithm.GetNumberCommunities(),
                            $"Number of communities after renumbering should be the same: {beforeNumComms}");
            for (var i = 0u; i < num - 1; i++)
            {
                if (commAlgorithm.Communities[i].Count == 0)
                {
                    Assert.IsTrue(commAlgorithm.Communities[i + 1].Count == 0,
                                  $"Community {i + 1} should have 0 nodes since community {i} has 0.");
                }
                if (i < beforeNumComms && beforeComms[i].Count > 0)
                {
                    Assert.IsTrue(beforeComms[i].SetEquals(commAlgorithm.Communities[i]),
                                  $"Renumbering should have maintained community {i}.");
                }
            }
        }
Ejemplo n.º 9
0
        public void NetworkPassesTest()
        {
            // creates network nodes
            const int numNodes = 16;
            var       network  = new Network();

            for (var i = 0u; i < numNodes; i++)
            {
                network.AddVertex(i);
            }

            // creates edges (according to paper)
            network.AddEdge(new Connection(0, 2));
            network.AddEdge(new Connection(0, 4));
            network.AddEdge(new Connection(0, 3));
            network.AddEdge(new Connection(0, 5));
            network.AddEdge(new Connection(1, 2));
            network.AddEdge(new Connection(1, 4));
            network.AddEdge(new Connection(1, 7));
            network.AddEdge(new Connection(2, 4));
            network.AddEdge(new Connection(2, 5));
            network.AddEdge(new Connection(2, 6));
            network.AddEdge(new Connection(3, 7));
            network.AddEdge(new Connection(4, 10));
            network.AddEdge(new Connection(5, 7));
            network.AddEdge(new Connection(5, 11));
            network.AddEdge(new Connection(6, 7));
            network.AddEdge(new Connection(6, 11));
            network.AddEdge(new Connection(8, 9));
            network.AddEdge(new Connection(8, 10));
            network.AddEdge(new Connection(8, 11));
            network.AddEdge(new Connection(8, 14));
            network.AddEdge(new Connection(8, 15));
            network.AddEdge(new Connection(9, 12));
            network.AddEdge(new Connection(9, 14));
            network.AddEdge(new Connection(10, 11));
            network.AddEdge(new Connection(10, 12));
            network.AddEdge(new Connection(10, 13));
            network.AddEdge(new Connection(10, 14));
            network.AddEdge(new Connection(11, 13));

            // creates and updates community algorithm
            var communityAlg = new CommunityAlgorithm(network, -1, 0);

            communityAlg.Update();
            communityAlg.DisplayCommunities();

            //checks communities
            Assert.AreEqual(4, communityAlg.GetNumberCommunities(), "Network should have 4 communities.");
            var communities = new[]
            {
                new HashSet <uint> {
                    0, 1, 2, 4, 5
                },
                new HashSet <uint> {
                    3, 6, 7
                },
                new HashSet <uint> {
                    11, 13
                },
                new HashSet <uint> {
                    8, 9, 10, 12, 14, 15
                }
            };

            for (var i = 0; i < 4; i++)
            {
                var community = communityAlg.Communities[i];
                Console.WriteLine(ArrayUtil.ToString(community.ToArray()));
                Assert.IsTrue(communities.Any(comm => comm.SetEquals(community)), $"Community {i} is unexpected.");
            }

            // gets and checks community graph (1st pass)
            communityAlg.DisplayCommunityGraph();
            communityAlg.DisplayPartition();
            network = communityAlg.GetCommunityNetwork();
            Assert.AreEqual(4, network.VertexCount, "Community network should have 4 nodes.");

            // checks connections
            var exptConns = new HashSet <Connection>
            {
                new Connection(0, 0, 16),
                new Connection(0, 1, 1),
                new Connection(0, 2, 3),
                new Connection(1, 1, 14),
                new Connection(1, 2, 1),
                new Connection(1, 3, 4),
                new Connection(2, 2, 2),
                new Connection(2, 3, 1),
                new Connection(3, 3, 4)
            };
            var netConns = new HashSet <Connection>(network.Edges);

            foreach (var exptConn in exptConns)
            {
                Console.WriteLine(exptConn);
                Assert.IsTrue(netConns.Contains(exptConn), $"Community network should contain connection: {exptConn}.");
            }

            //// creates and updates community algorithm
            //communityAlg = new CommunityAlgorithm(network, -1, 0);
            //communityAlg.Update();
            //communityAlg.DisplayCommunities();
        }