Class representing graph of connected nodes.
コード例 #1
0
        public void ThreePoints3DTest()
        {
            var node1 = new GraphElement(new[] { 15.0, 1.0, -20.0 }, "node1");
            var node2 = new GraphElement(new[] { 0.0, -3.0, -4.0 }, "node2");
            var node3 = new GraphElement(new[] { 15.0, 1.0, -25.0 }, "node3");

            var el = new List<GraphElement> { node1, node2, node3 };

            var conn1 = new Connection(0, 1);
            var conn2 = new Connection(0, 2);
            var conn3 = new Connection(1, 2);

            var graph = new List<Connection> { conn1, conn2, conn3 };

            var gm = new GraphManager(graph, el);

            ICalculator calculator = new LinearCalculator();
            calculator.Calculate(gm);
            calculator = new NormalizedLinearCalculator();
            calculator.Calculate(gm);
            calculator = new TauStarCalculator();
            calculator.Calculate(gm);
            calculator = new TauCalculator();
            calculator.Calculate(gm);
            var lambdaCalculator = new LambdaCalculator();
            lambdaCalculator.Calculate(gm, 2, 1);
            Assert.AreEqual(1625, Math.Round(gm.Connections[0].Lambda * 100));
            Assert.AreEqual(9, Math.Round(gm.Connections[1].Lambda * 1000));
            Assert.AreEqual(2612, Math.Round(gm.Connections[2].Lambda * 100));
        }
コード例 #2
0
        public void ThreePointsTest()
        {
            var node1 = new GraphElement(new[] { 15.0 }, "node1");
            var node2 = new GraphElement(new[] { 10.0 }, "node2");
            var node3 = new GraphElement(new[] { -3.0 }, "node3");

            var el = new List<GraphElement> { node1, node2, node3 };

            var conn1 = new Connection(0, 1);
            var conn2 = new Connection(0, 2);
            var conn3 = new Connection(1, 2);

            var graph = new List<Connection> { conn1, conn2, conn3 };

            var gm = new GraphManager(graph, el);

            ICalculator calculator = new LinearCalculator();
            calculator.Calculate(gm);
            calculator = new NormalizedLinearCalculator();
            calculator.Calculate(gm);
            calculator = new TauStarCalculator();
            calculator.Calculate(gm);
            calculator = new TauCalculator();
            calculator.Calculate(gm);
            Assert.AreEqual(107, Math.Round(gm.Connections[0].Tau * 1000));
            Assert.AreEqual(722, Math.Round(gm.Connections[2].Tau * 1000));
            Assert.AreEqual(1, gm.Connections[1].Tau);
        }
コード例 #3
0
 public void CutConnectionTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     connector.Cut(connector.Connections[0]);
     Assert.IsFalse(connectionsList[0].Connected);
     Assert.AreEqual(3, elementsList[0].TaxonNumber);
     Assert.AreEqual(1, elementsList[1].TaxonNumber);
     Assert.AreEqual(2, elementsList[4].TaxonNumber);
     Assert.AreEqual(2, elementsList[5].TaxonNumber);
 }
コード例 #4
0
 public void BothConnectionGraphTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     connector.Connect(1, 5);
     Assert.IsTrue(connectionsList[8].Connected);
     Assert.AreEqual(1, elementsList[5].TaxonNumber);
     Assert.AreEqual(1, elementsList[1].TaxonNumber);
     Assert.AreEqual(1, elementsList[4].TaxonNumber);
     Assert.AreEqual(1, elementsList[5].TaxonNumber);
 }
コード例 #5
0
 public void CloneTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     var connector2 = connector.Clone();
     Assert.IsInstanceOf(typeof(GraphManager), connector2);
     Assert.IsInstanceOf(typeof(Connection), connector2.Connections[0]);
     Assert.IsInstanceOf(typeof(GraphElement), connector2.Elements[1]);
     Assert.AreNotSame(connector, connector2);
     Assert.AreNotSame(connector.Elements[0], connector2.Elements[0]);
     Assert.AreNotSame(connector.Connections[1], connector2.Connections[1]);
 }
コード例 #6
0
        public void TwoIntPointsTest()
        {
            var node1 = new GraphElement(new[] { 15.0 }, "node1");
            var node2 = new GraphElement(new[] { 15.0 }, "node2");
            var el = new List<GraphElement> { node1, node2 };

            var conn1 = new Connection(0, 1);
            var con = new List<Connection> { conn1 };

            var gm = new GraphManager(con, el);

            var calculator = new LinearCalculator();
            calculator.Calculate(gm);
            Assert.AreEqual(0, gm.Connections[0].Distance);
        }
コード例 #7
0
        public void ThreePoints3DTest()
        {
            var node1 = new GraphElement(new[] { 15.0, 1.0, -20.0 }, "node1");
            var node2 = new GraphElement(new[] { 0.0, -3.0, -4.0 }, "node2");
            var node3 = new GraphElement(new[] { 15.0, 1.0, -20.0 }, "node3");

            var el = new List<GraphElement> { node1, node2, node3 };

            var conn1 = new Connection(0, 1);
            var conn2 = new Connection(0, 2);
            var conn3 = new Connection(1, 2);
            var con = new List<Connection> { conn1, conn2, conn3 };

            var gm = new GraphManager(con, el);

            var calculator = new LinearCalculator();
            calculator.Calculate(gm);
            Assert.AreEqual(22293, Math.Round(gm.Connections[0].Distance * 1000));
            Assert.AreEqual(0, Math.Round(gm.Connections[1].Distance * 1000));
            Assert.AreEqual(22293, Math.Round(gm.Connections[2].Distance * 1000));
        }
コード例 #8
0
        public void ThreePointsTest()
        {
            var node1 = new GraphElement(new[] { 15.0 }, "node1");
            var node2 = new GraphElement(new[] { 10.0 }, "node2");
            var node3 = new GraphElement(new[] { -3.0 }, "node3");

            var el = new List<GraphElement> { node1, node2, node3 };

            var conn1 = new Connection(0, 1);
            var conn2 = new Connection(1, 2);
            var conn3 = new Connection(0, 2);

            var con = new List<Connection> { conn1, conn2, conn3 };

            var gm = new GraphManager(con, el);

            var calculator = new LinearCalculator();
            calculator.Calculate(gm);
            Assert.AreEqual(5, gm.Connections[0].Distance);
            Assert.AreEqual(13, gm.Connections[1].Distance);
            Assert.AreEqual(18, gm.Connections[2].Distance);
        }
        public void ThreePoints3DTest()
        {
            var node1 = new GraphElement(new[] { 15.0, 1.0, -20.0 }, "node1");
            var node2 = new GraphElement(new[] { 0.0, -3.0, -4.0 }, "node2");
            var node3 = new GraphElement(new[] { 15.0, 1.0, -20.0 }, "node3");

            var el = new List<GraphElement> { node1, node2, node3 };

            var conn1 = new Connection(0, 1);
            var conn2 = new Connection(0, 2);
            var conn3 = new Connection(1, 2);

            var graph = new List<Connection> { conn1, conn2, conn3 };

            var gm = new GraphManager(graph, el);

            ICalculator calculator = new LinearCalculator();
            calculator.Calculate(gm);
            calculator = new NormalizedLinearCalculator();
            calculator.Calculate(gm);
            Assert.AreEqual(1, gm.Connections[0].NormalizedDistance);
            Assert.AreEqual(0, gm.Connections[1].NormalizedDistance);
            Assert.AreEqual(1, gm.Connections[2].NormalizedDistance);
        }
コード例 #10
0
 public void OneConnectedGraphTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     connector.Connect(0, 2);
     Assert.IsTrue(connectionsList[1].Connected);
     Assert.AreEqual(1, elementsList[0].TaxonNumber);
     Assert.AreEqual(1, elementsList[1].TaxonNumber);
     Assert.AreEqual(1, elementsList[2].TaxonNumber);
 }
コード例 #11
0
        /// <summary>
        /// Clusterization method for given groups number.
        /// </summary>
        /// <param name="clustersCount">
        /// Groups count.
        /// </param>
        /// <param name="data">
        /// Characteristics values for every object.
        /// First dimension represents objects.
        /// Second Dimension represents characteristics.
        /// </param>
        /// <returns>
        /// Optimal clusterization as <see cref="ClusterizationVariants"/>.
        /// </returns>
        public int[] Cluster(int clustersCount, double[][] data)
        {
            // all connections (pairs of elements)
            var connections = new List<Connection>();

            // all elements
            var elements = new List<GraphElement>();

            for (int i = 0; i < data.Length; i++)
            {
                elements.Add(new GraphElement(data[i], i));
            }

            for (int j = 0; j < elements.Count - 1; j++)
            {
                for (int k = j + 1; k < elements.Count; k++)
                {
                    connections.Add(new Connection(j, k));
                }
            }

            manager = new GraphManager(connections, elements);

            // calculating distances
            CommonCalculator.CalculateCharacteristic(manager, normalizedDistanceWeight, distanceWeight);
            manager.ConnectGraph();
            GraphManager tempManager = manager.Clone();
            ChooseDivision(clustersCount, 0, manager);
            var result = new int[data.Length];

            // extracting clusters from the graph
            for (int j = 0; j < optimalDivide.Elements.Count; j++)
            {
                result[j] = optimalDivide.Elements[j].TaxonNumber;
            }

            manager = tempManager;
            return result;
        }
コード例 #12
0
 /// <summary>
 /// Recursive method for finding optimal division of graph into clusters (groups).
 /// </summary>
 /// <param name="clusters">
 /// Clusters left to separate.
 /// </param>
 /// <param name="position">
 /// Starting position for iteration.
 /// </param>
 /// <param name="currentManager">
 /// Graph and its manager.
 /// </param>
 private void ChooseDivision(int clusters, int position, GraphManager currentManager)
 {
     // if recursive calls are required
     if (clusters > 1)
     {
         for (int i = position; i < (manager.Connections.Count - clusters); i++)
         {
             // creatingcopy of graph to be able to disconnect pair of nodes (cut the link)
             GraphManager tempManager = currentManager.Clone();
             if (tempManager.Connections[i].Connected)
             {
                 tempManager.Cut(tempManager.Connections[i]);
                 ChooseDivision(clusters - 1, i + 1, tempManager);
             }
         }
     }
     else
     {
         // calculating clusterization quality
         double f = QualityCalculator.Calculate(currentManager, manager, powerWeight);
         if (f > optimalF)
         {
             // saving optimal clusterization
             optimalDivide = currentManager;
             optimalF = f;
         }
     }
 }
コード例 #13
0
        public void GraphConnectionTest()
        {
            connectionsList[0].Connected = false;
            connectionsList[14].Connected = false;

            for (int i = 0; i < connectionsList.Count; i++)
            {
                elementsList[connectionsList[i].FirstElementIndex].TaxonNumber = 0;
                elementsList[connectionsList[i].SecondElementIndex].TaxonNumber = 0;
            }

            var lambdas = new[] { 10, 20, 15, 21, 6, 11, 12, 27, 16, 9, 25, 26, 13, 21, 22 };

            for (int i = 0; i < lambdas.Length; i++)
            {
                connectionsList[i].Lambda = lambdas[i];
            }

            var connector = new GraphManager(connectionsList, elementsList);
            connector.ConnectGraph();
            Assert.IsTrue(connectionsList[0].Connected);
            Assert.IsTrue(connectionsList[4].Connected);
            Assert.IsTrue(connectionsList[5].Connected);
            Assert.IsTrue(connectionsList[9].Connected);
            Assert.IsTrue(connectionsList[12].Connected);
            Assert.IsFalse(connectionsList[1].Connected);
            Assert.IsFalse(connectionsList[2].Connected);
            Assert.IsFalse(connectionsList[3].Connected);
            Assert.IsFalse(connectionsList[6].Connected);
            Assert.IsFalse(connectionsList[7].Connected);
            Assert.IsFalse(connectionsList[8].Connected);
            Assert.IsFalse(connectionsList[10].Connected);
            Assert.IsFalse(connectionsList[11].Connected);
            Assert.IsFalse(connectionsList[13].Connected);
            Assert.IsFalse(connectionsList[14].Connected);
        }
コード例 #14
0
        public void Initialization()
        {
            var elements = new List<GraphElement>
                               {
                                   new GraphElement(new[] { 0.0 }, "1"),
                                   new GraphElement(new[] { 2.0 }, "2"),
                                   new GraphElement(new[] { 5.0 }, "3"),
                                   new GraphElement(new[] { 6.0 }, "4")
                               };

            var connections = new List<Connection>
                                  {
                                      new Connection(0, 1),
                                      new Connection(0, 2),
                                      new Connection(0, 3),
                                      new Connection(1, 2),
                                      new Connection(1, 3),
                                      new Connection(2, 3)
                                  };

            manager = new GraphManager(connections, elements);
        }
コード例 #15
0
 public void UnConnectedGraphsTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     connector.Connect(2, 3);
     Assert.IsTrue(connectionsList[9].Connected);
     Assert.AreEqual(3, elementsList[2].TaxonNumber);
     Assert.AreEqual(3, elementsList[3].TaxonNumber);
 }
コード例 #16
0
 public void UnBothConnectionGraphTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     connectionsList[1].Connected = true;
     elementsList[2].TaxonNumber = 1;
     connector.Connect(1, 2);
     Assert.IsFalse(connectionsList[5].Connected);
     Assert.AreEqual(1, elementsList[0].TaxonNumber);
     Assert.AreEqual(1, elementsList[1].TaxonNumber);
     Assert.AreEqual(1, elementsList[2].TaxonNumber);
 }
コード例 #17
0
 public void SearchConnectionTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     Assert.AreEqual(-1, connector.SearchConnection(elementsList[0], elementsList[0]), "Search fault failure");
     Assert.AreEqual(-1, connector.SearchConnection(elementsList[4], elementsList[4]), "Search fault failure");
     Assert.AreEqual(0, connector.SearchConnection(elementsList[0], elementsList[1]), "Search fault failure");
     Assert.AreEqual(0, connector.SearchConnection(elementsList[1], elementsList[0]), "Search fault failure");
     Assert.AreEqual(14, connector.SearchConnection(elementsList[4], elementsList[5]), "Search fault failure");
     Assert.AreEqual(14, connector.SearchConnection(elementsList[5], elementsList[4]), "Search fault failure");
     Assert.AreEqual(0, connector.SearchConnection(elementsList[1], elementsList[0]), "Search fault failure");
 }
        public void TwoPointsTest()
        {
            var node1 = new GraphElement(new[] { 15.0 }, "node1");
            var node2 = new GraphElement(new[] { 20.0 }, "node2");

            var el = new List<GraphElement> { node1, node2 };

            var conn1 = new Connection(0, 1);
            var graph = new List<Connection> { conn1 };

            var gm = new GraphManager(graph, el);

            ICalculator calculator = new LinearCalculator();

            calculator.Calculate(gm);
            calculator = new NormalizedLinearCalculator();
            calculator.Calculate(gm);
            Assert.AreEqual(1, gm.Connections[0].NormalizedDistance);
        }
コード例 #19
0
 public void CutGraphTrioTest()
 {
     var connector = new GraphManager(connectionsList, elementsList);
     connectionsList[1].Connected = true;
     elementsList[2].TaxonNumber = 1;
     connector.Cut(elementsList[0], elementsList[2]);
     Assert.IsFalse(connectionsList[1].Connected);
     Assert.AreEqual(3, elementsList[0].TaxonNumber);
     Assert.AreEqual(3, elementsList[1].TaxonNumber);
     Assert.AreEqual(1, elementsList[2].TaxonNumber);
 }