Beispiel #1
0
        public ClusterOutput Run3DJury()
        {
            ClusterOutput output = new ClusterOutput();
            List <KeyValuePair <string, double> > li = new List <KeyValuePair <string, double> >();

            long[] distTab = new long[dMeasure.structNames.Count];

            progressRead = 1;

            dMeasure.CalcDistMatrix(new List <string>(dMeasure.structNames.Keys));

            maxV = dMeasure.structNames.Count + 1;

            for (int i = 0; i < dMeasure.structNames.Count; i++)
            {
                long sum = 0;
                for (int j = 0; j < dMeasure.structNames.Count; j++)
                {
                    sum += dMeasure.GetDistance(i, j);
                }
                distTab[i] = sum;
                currentV++;
            }

            KeyValuePair <string, double> v;

            List <string> structKeys = new List <string>(dMeasure.structNames.Keys);

            for (int m = 0; m < structKeys.Count; m++)
            {
                v = new KeyValuePair <string, double>(structKeys[m], (double)(distTab[m] / (100.0 * dMeasure.structNames.Count)));
                li.Add(v);
            }
            if (dMeasure.order == false)
            {
                li.Sort((firstPair, nextPair) =>
                {
                    return(nextPair.Value.CompareTo(firstPair.Value));
                });
            }
            else
            {
                li.Sort((firstPair, nextPair) =>
                {
                    return(firstPair.Value.CompareTo(nextPair.Value));
                });
            }

            output.juryLike = li;

            currentV             = maxV;
            output.runParameters = "Distance measure: " + this.dMeasure;
            return(output);
        }
        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);
        }
        public ClusterOutput OrgClustering()
        {
            int []                count;
            int []                index;
            bool                  end;
            ClusterOutput         output   = new ClusterOutput();
            List <List <string> > clusters = new List <List <string> >();
            List <string>         items;

            pointMark = new bool[dMeasure.structNames.Count];
            for (int i = 0; i < pointMark.Length; i++)
            {
                pointMark[i] = false;
            }


            progressRead = 1;
            dMeasure.CalcDistMatrix(new List <string>(dMeasure.structNames.Keys));

            maxV  = dMeasure.structNames.Count;
            count = new int[dMeasure.structNames.Count];
            index = new int[dMeasure.structNames.Count];
            end   = false;
            while (!end)
            {
                for (int i = 0; i < pointMark.Length; i++)
                {
                    count[i] = 0;
                    if (pointMark[i])
                    {
                        continue;
                    }
                    index[i] = i;
                    for (int j = 0; j < pointMark.Length; j++)
                    {
                        if (!pointMark[j] && dMeasure.GetDistance(i, j) < threshold)
                        {
                            count[i]++;
                        }
                    }
                }

                Array.Sort <int>(index, (a, b) => count[b].CompareTo(count[a]));
                if (count[index[0]] < minCluster)
                {
                    end = true;
                    break;
                }
                items = CreateCluster(index[0]);
                if (items.Count > minCluster)
                {
                    clusters.Add(items);
                }
                else
                {
                    end = true;
                }

                currentV += items.Count;
            }
            output.clusters = clusters;
            currentV        = maxV;
            return(output);
        }