public void TestMethodAddElement() { Set<int> s = new Set<int>(); s.AddElement(0); s.AddElement(1); Assert.AreEqual(s.FindElement(1), true); }
private void AddElements() { set.AddElement(1); set.AddElement(2); set.AddElement(3); set.AddElement(4); }
public void TestMethodRemoveElement() { Set<int> s = new Set<int>(); s.AddElement(0); s.AddElement(1); s.AddElement(1); s.RemoveElement(1); Assert.AreEqual(s.FindElement(1), false); }
public void IntersectionTest() { Set<int> newSet = new Set<int>(); set.AddElement(1); set.AddElement(2); newSet.AddElement(0); newSet.AddElement(1); newSet = set.SetIntersection(newSet); List<int> expectedList = new List<int> { 1 }; CollectionAssert.AreEqual(newSet.elementSet, expectedList); }
public void TestMethodDifferenceWithSet() { Set<int> s1 = new Set<int>(); s1.AddElement(0); s1.AddElement(1); s1.AddElement(2); Set<int> s2 = new Set<int>(); s2.AddElement(1); s2.AddElement(2); s2.AddElement(3); Set<int> s3 = new Set<int>(); s3.AddElement(0); Assert.IsTrue(s3 == s1.DifferenceWithSet(s2)); }
public void IntersectionTest() { Set <int> newSet = new Set <int>(); set.AddElement(1); set.AddElement(2); newSet.AddElement(0); newSet.AddElement(1); newSet = set.SetIntersection(newSet); List <int> expectedList = new List <int> { 1 }; CollectionAssert.AreEqual(newSet.elementSet, expectedList); }
public void TestMethodIntersectionWithSet() { Set<int> s1 = new Set<int>(); s1.AddElement(0); s1.AddElement(1); s1.AddElement(2); Set<int> s2 = new Set<int>(); s2.AddElement(1); s2.AddElement(2); s2.AddElement(3); Set<int> s3 = new Set<int>(); s3.AddElement(1); s3.AddElement(2); Assert.IsTrue(s3 == s1.IntersectionWithSet(s2)); }
public void TestMethodUnionWithSet() { Set<int> s1 = new Set<int>(); s1.AddElement(0); s1.AddElement(1); Set<int> s2 = new Set<int>(); s2.AddElement(1); s2.AddElement(2); s2.AddElement(3); Set<int> s3 = new Set<int>(); for (int i = 0; i < 4; i++) { s3.AddElement(i); } Assert.IsTrue(s3 == s1.UnionWithSet(s2)); }
public static Set ReversivelProdutoDasPartes(Set a) { string name = a.Name.Substring(a.Name.IndexOf('('), (a.Name.IndexOf(')'))); name = Regex.Replace(name, @"\(|\)", ""); Set newSet = new Set { Name = name }; string allElements = a.ElementsSetToString(); string pattern = @"\(|\)|{|}|\s"; string replacement = ""; string body; body = Regex.Replace(allElements, pattern, replacement); string[] elements = body.Split(','); for (int i = 0; i < elements.Length; i++) { if (!elements[i].Equals("")) { newSet.AddElement(new Element { Value = elements[i] }); } } return(newSet); }
public void UnionTest3() { set1.AddElement(1); set1.AddElement(2); set2.AddElement(3); set2.AddElement(2); Set <int> result = Set <int> .Union(set1, set2); Assert.IsTrue(result.IsContaining(1)); Assert.IsTrue(result.IsContaining(2)); Assert.IsTrue(result.IsContaining(3)); Assert.AreEqual(result.Length, 3); }
public Element TableauToElement(char[] row, Set c) { Set setAux = new Set(); for (int i = 0; i < row.Length; i++) { if (row[i].Equals('1')) { setAux.AddElement(new Element { Value = c.ListElements[i].Value }); } } return(new Element { Value = setAux.ElementsSetToString() }); }
public static Tuple <Set, Set> ReversivelProdutoCartesiano(Set a) { string[] names = a.Name.Split('x'); Set conjuntoA = new Set { Name = names[0].Trim() }; Set conjuntoB = new Set { Name = names[1].Trim() }; for (int i = 0; i < a.ListElements.Count; i++) { string element = a.ListElements[i].Value; string pattern = @"\(|\)"; string replacement = ""; string body; body = Regex.Replace(element, pattern, replacement); string[] elements = body.Split(','); conjuntoA.AddElement(new Element { Value = elements[0].Trim() }); conjuntoB.AddElement(new Element { Value = elements[1].Trim() }); } return(new Tuple <Set, Set>(conjuntoA, conjuntoB)); }
public override Structuring BuildStructuring() { if (Structurings == null || Set == null) { throw new NullReferenceException(); } if (IContainerProgressBar != null) { IContainerProgressBar.ResetProgressBar(1, 1, true); IContainerProgressBar.UpdateProgressBar(0, "Running QMI algorithm...", true); } List <Attribute> list_att = new List <Attribute>(); int cont = 0; foreach (Structuring s in Structurings) { foreach (Cluster c in s.Clusters.Values) { Attribute att = new Attribute("x" + cont, null); cont++; att.AttributeType = AttributeType.Numeric; list_att.Add(att); } } Set newset = new Set("Artificial"); newset.Attributes = new Attributes(list_att); newset.ElementType = ElementType.Numeric; foreach (Element e in Set.Elements) { List <object> values = new List <object>(); foreach (Structuring s in Structurings) { foreach (Cluster c in s.Clusters.Values) { double temp = c.HaveElement(e) ? 1 : 0; temp = temp - ((double)c.ElementsCount / (double)Set.ElementsCount); values.Add(temp); } } Element newelement = new Element(newset, values); newelement.Name = e.Name; newelement.Index = e.Index; newset.AddElement(newelement); } KMeans kms = new KMeans(newset, new EuclideanDistance() { AttributesToCalculateProximity = newset.Attributes.Values }); kms.ClustersCount = ClusterCount; kms.IterationsCount = IterationsCount; kms.Seed = Environment.TickCount; kms.IContainerProgressBar = IContainerProgressBar; Structuring art_struct = kms.BuildStructuring(); List <Cluster> clusters = new List <Cluster>(); cont = 0; foreach (Cluster c in art_struct.Clusters.Values) { Cluster temp = new Cluster("C-" + cont); cont++; foreach (Element item in c.Elements) { temp.AddElement(Set[item.Index]); } clusters.Add(temp); } Dictionary <string, Cluster> temp_dic = new Dictionary <string, Cluster>(); foreach (Cluster item in clusters) { temp_dic.Add(item.Name, item); } Structuring real_struct = new Partition() { Clusters = temp_dic }; return(real_struct); }
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); } }
public void EmptinessTest() { Assert.IsTrue(set.IsEmpty()); set.AddElement(1); Assert.IsFalse(set.IsEmpty()); }
public override Structuring BuildStructuring() { if (Structurings == null || Set == null) throw new NullReferenceException(); if (IContainerProgressBar != null) { IContainerProgressBar.ResetProgressBar(1, 1, true); IContainerProgressBar.UpdateProgressBar(0, "Running QMI algorithm...", true); } List<Attribute> list_att = new List<Attribute>(); int cont = 0; foreach (Structuring s in Structurings) { foreach (Cluster c in s.Clusters.Values) { Attribute att = new Attribute("x" + cont, null); cont++; att.AttributeType = AttributeType.Numeric; list_att.Add(att); } } Set newset = new Set("Artificial"); newset.Attributes = new Attributes(list_att); newset.ElementType = ElementType.Numeric; foreach (Element e in Set.Elements) { List<object> values = new List<object>(); foreach (Structuring s in Structurings) { foreach (Cluster c in s.Clusters.Values) { double temp = c.HaveElement(e) ? 1 : 0; temp = temp - ((double)c.ElementsCount / (double)Set.ElementsCount); values.Add(temp); } } Element newelement = new Element(newset, values); newelement.Name = e.Name; newelement.Index = e.Index; newset.AddElement(newelement); } KMeans kms = new KMeans(newset, new EuclideanDistance() { AttributesToCalculateProximity = newset.Attributes.Values }); kms.ClustersCount = ClusterCount; kms.IterationsCount = IterationsCount; kms.Seed = Environment.TickCount; kms.IContainerProgressBar = IContainerProgressBar; Structuring art_struct = kms.BuildStructuring(); List<Cluster> clusters = new List<Cluster>(); cont = 0; foreach (Cluster c in art_struct.Clusters.Values) { Cluster temp = new Cluster("C-" + cont); cont++; foreach (Element item in c.Elements) { temp.AddElement(Set[item.Index]); } clusters.Add(temp); } Dictionary<string, Cluster> temp_dic=new Dictionary<string,Cluster>(); foreach (Cluster item in clusters) { temp_dic.Add(item.Name, item); } Structuring real_struct = new Partition() { Clusters = temp_dic }; return real_struct; }