Ejemplo n.º 1
0
        public override Structuring BuildStructuring()
        {
            try
            {
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ResetProgressBar(1, 1, true);
                    IContainerProgressBar.UpdateProgressBar(1, "Running WSPA algorithm...", true);
                }

                VerifyPartitions(Structurings);
                List <double[, ]> MatrixsOf_dit = new List <double[, ]>();
                double[,] Max_row = new double[Set.ElementsCount, StructuringsCount];
                List <double[, ]> MatrixsObjectRepresentation = new List <double[, ]>();


                for (int i = 0; i < StructuringsCount; i++)
                {
                    double[,] Matrix = new double[Set.ElementsCount, Structurings[i].ClustersCount];
                    MatrixsOf_dit.Add(Matrix);

                    double[,] MatrixRepresentation = new double[Set.ElementsCount, Structurings[i].ClustersCount];
                    MatrixsObjectRepresentation.Add(MatrixRepresentation);

                    for (int j = 0; j < Set.ElementsCount; j++)
                    {
                        int    k    = 0;
                        double _max = double.MinValue;
                        //Aqui se llena la matrix por fila
                        foreach (var cluster in Structurings[i].Clusters.Values)
                        {
                            Matrix[j, k] = Calculate_dit(Set[j], cluster);

                            if (Matrix[j, k] > _max)
                            {
                                _max = Matrix[j, k];
                            }

                            if (j > 0)
                            {
                                int    _q           = Structurings[i].ClustersCount;
                                double _numerator   = Max_row[j - 1, i] - Matrix[j - 1, k] + 1;
                                double _denominator = _q * Max_row[j - 1, i] + _q - SumRow(Matrix, j - 1);
                                MatrixRepresentation[j - 1, k] = _numerator / _denominator;
                            }

                            k++;
                        }

                        Max_row[j, i] = _max;
                    }

                    //Calcula la ultima fila de la representacion, es decir del ultimo objeto
                    for (int k = 0; k < Structurings[i].ClustersCount; k++)
                    {
                        int    j            = Set.ElementsCount;
                        int    _q           = Structurings[i].ClustersCount;
                        double _numerator   = Max_row[j - 1, i] - Matrix[j - 1, k] + 1;
                        double _denominator = _q * Max_row[j - 1, i] + _q - SumRow(Matrix, j - 1);
                        MatrixRepresentation[j - 1, k] = _numerator / _denominator;
                    }
                }


                double[,] _Similarities = new double[Set.ElementsCount, Set.ElementsCount];
                double m = StructuringsCount;
                for (int i = 0; i < Set.ElementsCount; i++)
                {
                    for (int j = 0; j < Set.ElementsCount; j++)
                    {
                        double _Sij = 0;
                        for (int k = 0; k < MatrixsObjectRepresentation.Count; k++)
                        {
                            double[,] A = MatrixsObjectRepresentation[k];

                            if (i != j)
                            {
                                _Sij += EscalarProduct(A, i, j) / (Norm(A, i) * Norm(A, j));
                            }
                        }
                        _Similarities[i, j] = _Sij / m;
                    }
                }

                SimilarityWSPA _sim = new SimilarityWSPA(_Similarities);

                Metis _metis = new Metis(Set, _sim);
                _metis.IContainerProgressBar = IContainerProgressBar;
                _metis.ClustersCount         = ClustersCount;

                Structuring = _metis.BuildStructuring();

                return(Structuring);
            }
            catch (Exception _ex)
            {
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ShowError("Error occurred in WSPA algorithm.\n" + _ex.Message);
                }
                return(null);
            }
        }
Ejemplo n.º 2
0
        public override Structuring BuildStructuring()
        {
            try
            {
                if (Set == null || Structurings == null)
                {
                    throw new NullReferenceException();
                }

                int _current = 1, _maxpb = Set.ElementsCount * Structurings.Count;
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ResetProgressBar(1, _maxpb, true);
                    IContainerProgressBar.UpdateProgressBar(1, "Running HGPA algorithm...", true);
                }

                if (ClustersCount <= 0)
                {
                    throw new Exception("La cantidad de clusters debe ser mayor que cero");
                }
                else if (ClustersCount == 1)
                {
                    Dictionary <string, Cluster> dic_clus = new Dictionary <string, Cluster>();
                    string         name = "C-0";
                    List <Element> temp = new List <Element>();

                    for (int i = 0; i < Set.ElementsCount; i++)
                    {
                        temp.Add(Set[i]);
                    }

                    dic_clus.Add(name, new Cluster(name)
                    {
                        Elements = temp
                    });

                    Structuring = new Partition()
                    {
                        Clusters = dic_clus
                    };

                    if (IContainerProgressBar != null)
                    {
                        IContainerProgressBar.FinishProgressBar();
                    }

                    return(Structuring);
                }
                else
                {
                    #region Algorithm
                    //Construir un conjunto de elementos donde ahora cada elemento es un label,
                    //o lo que es lo mismo una hyperedge. Este seria el meta-grafo
                    Set hyperedgeSet = new Set("HyperEdge Set");

                    int dimH = 0;
                    foreach (Structuring s in Structurings)
                    {
                        dimH += s.ClustersCount;
                    }

                    byte[,] metagraph = new byte[Set.ElementsCount, dimH];

                    //Ahora por cada label annadir un nuevo elemento al conjunto, recordar que cada label
                    //seria cada hyperedges, lo que ahora cada hyperedges estaria representada por la clase Element
                    //pero la lista de Values seria el vector binario o mejor dicho los elementos que pertenecen
                    //a la hyperedges.
                    //La matriz metagraph es la representacion en vectores binarios del conjunto hyperedges.

                    // Ojo esto ya  ///////Es mejor representar cada hyperedges de esta forma es decir con los elementos que pertenecen
                    // no se hace   ///////a dicha hyperedges ya que si se representa por el vector binario entonces necesitariamos mucha memoria.
                    int index = 0;
                    foreach (Structuring structuring in Structurings)
                    {
                        foreach (Cluster cluster in structuring.Clusters.Values)
                        {
                            Element element = new Element();
                            element.Index = index;
                            element.Name  = "hyperedge-" + index;
                            foreach (Element temp in cluster.Elements)
                            {
                                metagraph[temp.Index, index] = 1;

                                if (IContainerProgressBar != null)
                                {
                                    IContainerProgressBar.UpdateProgressBar(_current++, "Running HGPA algorithm...", false);
                                }
                            }

                            hyperedgeSet.AddElement(element);
                            index++;
                        }
                    }

                    if (IContainerProgressBar != null)
                    {
                        IContainerProgressBar.UpdateProgressBar(_maxpb, "Running HGPA algorithm...", true);
                    }

                    Similarity _sim = new BinaryJaccardMeasure(metagraph);

                    ClusterAlgorithm ca = new Metis(hyperedgeSet, _sim);

                    ca.ClustersCount = ClustersCount;
                    ca.BuildStructuring();

                    Partition metaClusters = (Partition)ca.Structuring;

                    //Asignar cada elemento original a su metacluster correspondiente

                    //double[] va a tener dimension igual a la cantidad de elementos iniciales y en cada posicion va a estar un numero entre 0 y 1
                    List <double[]> _meta_hyperedges = new List <double[]>();
                    foreach (Cluster _c in metaClusters.Clusters.Values)
                    {
                        double[] _meta_hyperedge = new double[metagraph.GetLength(0)];
                        foreach (Element _e in _c.Elements)
                        {
                            //_e.Index es la columna de la matrix metgraph, que dicha columna representa a ese elemento
                            for (int row = 0; row < metagraph.GetLength(0); row++)
                            {
                                _meta_hyperedge[row] += metagraph[row, _e.Index];
                            }
                        }

                        //LLevar cada posicion a un valor enre 0 y 1
                        for (int i = 0; i < _meta_hyperedge.Length; i++)
                        {
                            _meta_hyperedge[i] = _meta_hyperedge[i] / _c.ElementsCount;
                        }

                        _meta_hyperedges.Add(_meta_hyperedge);
                    }

                    //Construir la particion final
                    Dictionary <string, Cluster> _dic_clusters = new Dictionary <string, Cluster>();

                    for (int i = 0; i < Set.ElementsCount; i++)
                    {
                        Element _e = Set[i];

                        double _max            = -1;
                        int    _meta_hyperedge = -1;
                        for (int j = 0; j < _meta_hyperedges.Count; j++)
                        {
                            if (_meta_hyperedges[j][_e.Index] > _max)
                            {
                                _max            = _meta_hyperedges[j][_e.Index];
                                _meta_hyperedge = j;
                            }
                        }

                        string _nameOfCluster = "C-" + _meta_hyperedge;
                        if (_dic_clusters.ContainsKey(_nameOfCluster))
                        {
                            _dic_clusters[_nameOfCluster].AddElement(_e);
                        }
                        else
                        {
                            Cluster c = new Cluster(_nameOfCluster);
                            c.AddElement(_e);
                            _dic_clusters.Add(_nameOfCluster, c);
                        }
                    }


                    Structuring = new Partition()
                    {
                        Clusters = _dic_clusters
                    };

                    if (IContainerProgressBar != null)
                    {
                        IContainerProgressBar.FinishProgressBar();
                    }

                    return(Structuring);

                    #endregion
                }
            }
            catch
            {
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ShowError("Error occurred in MCLA algorithm.");
                }
                return(null);
            }
        }
Ejemplo n.º 3
0
        public override Structuring BuildStructuring()
        {
            try
            {
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ResetProgressBar(1, 1, true);
                    IContainerProgressBar.UpdateProgressBar(1, "Running WBPA algorithm...", true);
                }

                WSPA.VerifyPartitions(Structurings);
                List <double[, ]> MatrixsOf_dit = new List <double[, ]>();
                double[,] Max_row = new double[Set.ElementsCount, StructuringsCount];
                List <double[, ]> MatrixsObjectRepresentation = new List <double[, ]>();

                int q_m = StructuringsCount * Structurings[0].ClustersCount;
                double[,] A = new double[Set.ElementsCount, q_m];

                for (int i = 0; i < StructuringsCount; i++)
                {
                    double[,] Matrix = new double[Set.ElementsCount, Structurings[i].ClustersCount];
                    MatrixsOf_dit.Add(Matrix);

                    double[,] MatrixRepresentation = new double[Set.ElementsCount, Structurings[i].ClustersCount];
                    MatrixsObjectRepresentation.Add(MatrixRepresentation);

                    for (int j = 0; j < Set.ElementsCount; j++)
                    {
                        int    k    = 0;
                        double _max = double.MinValue;
                        //Aqui se llena la matrix por fila
                        foreach (var cluster in Structurings[i].Clusters.Values)
                        {
                            Matrix[j, k] = WSPA.Calculate_dit(Set[j], cluster);

                            if (Matrix[j, k] > _max)
                            {
                                _max = Matrix[j, k];
                            }

                            if (j > 0)
                            {
                                int    _q           = Structurings[i].ClustersCount;
                                double _numerator   = Max_row[j - 1, i] - Matrix[j - 1, k] + 1;
                                double _denominator = _q * Max_row[j - 1, i] + _q - WSPA.SumRow(Matrix, j - 1);
                                MatrixRepresentation[j - 1, k] = _numerator / _denominator;
                                A[j - 1, i *_q + k]            = MatrixRepresentation[j - 1, k];
                            }

                            k++;
                        }

                        Max_row[j, i] = _max;
                    }

                    //Calcula la ultima fila de la representacion, es decir del ultimo objeto
                    for (int k = 0; k < Structurings[i].ClustersCount; k++)
                    {
                        int    j            = Set.ElementsCount;
                        int    _q           = Structurings[i].ClustersCount;
                        double _numerator   = Max_row[j - 1, i] - Matrix[j - 1, k] + 1;
                        double _denominator = _q * Max_row[j - 1, i] + _q - WSPA.SumRow(Matrix, j - 1);
                        MatrixRepresentation[j - 1, k] = _numerator / _denominator;
                        A[j - 1, i *_q + k]            = MatrixRepresentation[j - 1, k];
                    }
                }

                int _ElementsCount = Set.ElementsCount;

                for (int i = 0; i < q_m; i++)
                {
                    Element _element = new Element();
                    _element.Name       = "Element cluster - " + i;
                    _element.Index      = i;
                    _element.Attributes = Set.Attributes;

                    Set.Elements.Insert(0, _element);
                }
                //Actualizar los indices de los elementos originales
                for (int i = q_m; i < Set.ElementsCount; i++)
                {
                    Set[i].Index = i;
                }

                SimilarityWBPA _sim = new SimilarityWBPA(A, _ElementsCount, Structurings[0].ClustersCount, StructuringsCount);

                Metis _metis = new Metis(Set, _sim);
                _metis.IContainerProgressBar = IContainerProgressBar;
                _metis.ClustersCount         = ClustersCount;

                Structuring _StructTemp = _metis.BuildStructuring();



                Dictionary <string, Cluster> dic_clusters = new Dictionary <string, Cluster>();
                foreach (var cluster in _StructTemp.Clusters.Values)
                {
                    for (int i = 0; i < cluster.ElementsCount; i++)
                    {
                        if (cluster.Elements[i].Index < q_m)
                        {
                            cluster.Elements.RemoveAt(i);
                            i--;
                        }
                    }
                    if (cluster.ElementsCount > 0)
                    {
                        dic_clusters.Add(cluster.Name, cluster);
                    }
                }

                Structuring = new Partition()
                {
                    Clusters = dic_clusters
                };

                //Borrar los elementos annadidos en el conjunto
                for (int i = 0; i < q_m; i++)
                {
                    Set.Elements.RemoveAt(0);
                }

                //Actualizar los indices de los elementos que estan en el SET, que eran los originales
                for (int i = 0; i < Set.ElementsCount; i++)
                {
                    Set[i].Index = i;
                }

                return(Structuring);
            }
            catch (Exception _ex)
            {
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ShowError("Error occurred in WBPA algorithm.\n" + _ex.Message);
                }
                return(null);
            }
        }
Ejemplo n.º 4
0
        public override Structuring BuildStructuring()
        {
            try
            {
                if (Set == null || Structurings == null)
                {
                    throw new NullReferenceException();
                }

                int _current = 1, _max = Set.ElementsCount * Structurings.Count;
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ResetProgressBar(1, _max, true);
                    IContainerProgressBar.UpdateProgressBar(1, "Running CSPA algorithm...", true);
                }

                if (ClustersCount <= 0)
                {
                    throw new Exception("La cantidad de clusters debe ser mayor que cero");
                }
                else if (ClustersCount == 1)
                {
                    Dictionary <string, Cluster> dic_clus = new Dictionary <string, Cluster>();
                    string         name = "C-0";
                    List <Element> temp = new List <Element>();

                    for (int i = 0; i < Set.ElementsCount; i++)
                    {
                        temp.Add(Set[i]);
                    }

                    dic_clus.Add(name, new Cluster(name)
                    {
                        Elements = temp
                    });

                    Structuring = new Partition()
                    {
                        Clusters = dic_clus
                    };

                    if (IContainerProgressBar != null)
                    {
                        IContainerProgressBar.FinishProgressBar();
                    }

                    return(Structuring);
                }
                else
                {
                    #region Algorithm
                    //Calcular la matrix V x H
                    int dimH = 0;
                    foreach (Structuring s in Structurings)
                    {
                        dimH += s.ClustersCount;
                    }

                    byte[,] VxH = new byte[Set.ElementsCount, dimH];

                    //rellenar VxH
                    int currentColumn = 0;//cada columna representa un cluster
                    //Recordar tbn que los elementos tienen un indice que los representa
                    foreach (Structuring s in Structurings)
                    {
                        foreach (Cluster cluster in s.Clusters.Values)
                        {
                            foreach (Element e in cluster.Elements)
                            {
                                VxH[e.Index, currentColumn] = 1;

                                if (IContainerProgressBar != null)
                                {
                                    IContainerProgressBar.UpdateProgressBar(_current++, "Running CSPA algorithm...", false);
                                }
                            }
                            //Debo actualizar currentColumn ya que cada cluser representa una columna
                            currentColumn++;
                        }
                    }

                    //Construir CoAsociationMatrixDissToMetis para pasarselo como similitud entre elementos
                    //al algoritmo Metis
                    Similarity _sim = new CoAsociationMatrixDissToMetis(Set, VxH, Structurings.Count);

                    ClusterAlgorithm ca = new Metis(Set, _sim);

                    //Llamar al algoritmo Metis
                    ca.ClustersCount = ClustersCount;
                    ca.BuildStructuring();

                    Structuring = ca.Structuring;

                    if (IContainerProgressBar != null)
                    {
                        IContainerProgressBar.FinishProgressBar();
                    }

                    return(Structuring);

                    #endregion
                }
            }
            catch
            {
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ShowError("Error occurred in CSPA algorithm.");
                }
                return(null);
            }
        }