Пример #1
0
        private void MakeDendrogs(AglomerativeType linkage)
        {
            ClusterOutput       outCl;
            hierarchicalCluster dendrog = new hierarchicalCluster(dMeasure, input, dirName);

            currentV = 0;
            maxV     = leaves.Count + 1;
            double remProgress = currentProgress;

            for (int i = 0; i < leaves.Count; i++)
            {
                HClusterNode c = leaves[i];
                dendrog.mustRefStructure = c.setStruct[0];
                outCl = dendrog.HierarchicalClustering(c.setStruct);
                dendrogList.Add(c);
                c.levelDist    = outCl.hNode.levelDist;
                c.realDist     = dMeasure.GetRealValue(c.levelDist);
                c.refStructure = outCl.hNode.refStructure;
                if (outCl.hNode.joined != null)
                {
                    c.joined = new List <HClusterNode>();
                    foreach (var item in outCl.hNode.joined)
                    {
                        c.joined.Add(item);
                    }
                }
                currentV++;
                currentProgress = remProgress + 1.0 / maxProgress * (double)currentV / maxV;
            }
            maxV            = currentV;
            currentProgress = remProgress;
        }
Пример #2
0
        public ClusterOutput HierarchicalKMeans()
        {
            HClusterNode node;

            maxDist  = 0;
            currentV = 0;
            List <string> availStruct = new List <string>(dMeasure.structNames.Keys);

            hmaxV          = availStruct.Count;
            node           = MakeNodes(availStruct, 0);
            hcurrentV      = hmaxV;
            node.levelDist = maxDist;
            node.realDist  = dMeasure.GetRealValue(maxDist);
            AddDistance(node);

            ClusterOutput outClust = new ClusterOutput();

            outClust.hNode = node;

            clusterName = "H-Kmeans";

            return(outClust);
        }
Пример #3
0
        public ClusterOutput HierarchicalClustering(List <string> structures)
        {
            List <List <HClusterNode> > level      = new List <List <HClusterNode> >();
            List <HClusterNode>         levelNodes = new List <HClusterNode>();
            List <HClusterNode>         rowNodes   = new List <HClusterNode>();
            ClusterOutput outCl      = new ClusterOutput();
            int           levelCount = 0;
            bool          end        = false;
            HClusterNode  node;

            if (structures.Count <= 1)
            {
                outCl.hNode              = new HClusterNode();
                outCl.hNode.setStruct    = structures;
                outCl.hNode.refStructure = structures[0];
                outCl.hNode.levelDist    = 0;
                outCl.hNode.joined       = null;
                return(outCl);
            }

            progressRead = 1;
            dMeasure.CalcDistMatrix(structures);


            for (int i = 0; i < structures.Count; i++)
            {
                node = new HClusterNode();
                node.refStructure = structures[i];
                node.joined       = null;
                node.setStruct.Add(structures[i]);
                node.levelNum = levelCount;

                node.levelDist = dMeasure.maxSimilarity;
                node.realDist  = dMeasure.GetRealValue(node.levelDist);
                levelNodes.Add(node);
            }
            maxV = levelNodes.Count + 1;
            level.Add(levelNodes);

            while (!end)
            {
                levelNodes = new List <HClusterNode>();
                List <List <HClusterNode> > rowList = LevelMinimalDist(level[level.Count - 1]);
                if (rowList.Count > 0)
                {
                    foreach (var item in rowList)
                    {
                        node           = new HClusterNode();
                        node.joined    = item;
                        node.levelDist = min;
                        node.realDist  = dMeasure.GetRealValue(min);
                        node.levelNum  = level.Count;
                        for (int m = 0; m < item.Count; m++)
                        {
                            node.setStruct.AddRange(item[m].setStruct);
                            item[m].fNode = true;
                        }
                        node.refStructure = dMeasure.GetReferenceStructure(node.setStruct);

                        List <string> refList = new List <string>();
                        foreach (var itemJoined in node.joined)
                        {
                            refList.Add(itemJoined.refStructure);
                        }

                        node.refStructure = null;
                        if (mustRefStructure != null)
                        {
                            foreach (var itemRef in refList)
                            {
                                if (itemRef == mustRefStructure)
                                {
                                    node.refStructure = mustRefStructure;
                                }
                            }
                        }

                        if (node.refStructure == null)
                        {
                            node.refStructure = dMeasure.GetReferenceStructure(node.setStruct, refList);
                        }


                        levelNodes.Add(node);
                    }
                }
                if (levelNodes.Count > 0)
                {
                    level.Add(levelNodes);
                    for (int i = 0; i < level[level.Count - 2].Count; i++)
                    {
                        if (!level[level.Count - 2][i].fNode)
                        {
                            level[level.Count - 1].Add(level[level.Count - 2][i]);
                        }
                    }
                    currentV = maxV - levelNodes.Count;
                }
                if (level[level.Count - 1].Count == 1)
                {
                    end = true;
                }
            }


            outCl.hNode          = level[level.Count - 1][0];
            outCl.hNode.levelNum = 0;



            //At the end level num must be set properly
            Queue <HClusterNode> qq = new Queue <HClusterNode>();
            HClusterNode         h;

            for (int i = 0; i < level.Count; i++)
            {
                for (int j = 0; j < level[i].Count; j++)
                {
                    level[i][j].fNode = true;
                }
            }

            for (int i = 0; i < level.Count; i++)
            {
                for (int j = 0; j < level[i].Count; j++)
                {
                    if (level[i][j].fNode)
                    {
                        level[i][j].levelDist = Math.Abs(level[i][j].levelDist - dMeasure.maxSimilarity);
                        level[i][j].realDist  = dMeasure.GetRealValue(level[i][j].levelDist);
                        level[i][j].fNode     = false;
                    }
                }
            }



            qq.Enqueue(level[level.Count - 1][0]);
            while (qq.Count != 0)
            {
                h = qq.Dequeue();

                if (h.joined != null)
                {
                    foreach (var item in h.joined)
                    {
                        item.levelNum = h.levelNum + 1;
                        qq.Enqueue(item);
                    }
                }
            }

            outCl.hNode.dirName = dirName;
            outCl.clusters      = null;
            outCl.juryLike      = null;
            currentV            = maxV;
            outCl.runParameters = hierOpt.GetVitalParameters();
            return(outCl);
        }