public double GetDistance(List <ClusterEntityDTO> first, List <ClusterEntityDTO> second, IDistance distance) { double dist = 0.0; foreach (var frs in first) { foreach (var sc in second) { dist = distance.GetDinstance(frs, sc); } } return(dist / (first.Count * second.Count)); }
public double GetDistance(List <ClusterEntityDTO> first, List <ClusterEntityDTO> second, IDistance distance) { double dist = double.MinValue; foreach (var frs in first) { foreach (var sc in second) { dist = Math.Max(dist, distance.GetDinstance(frs, sc)); } } return(dist); }
public double GetDistance(List <ClusterEntityDTO> first, List <ClusterEntityDTO> second, IDistance distance) { ClusterEntityDTO firstDTO = new ClusterEntityDTO(); ClusterEntityDTO secondDTO = new ClusterEntityDTO(); double dist = 0.0; for (int j = 0; j < first[0].Interests.Count; j++) { double weight = 0.0; for (int i = 0; i < first.Count; i++) { weight += first[i].Interests[j].Weight; } firstDTO.Interests.Add(new Interest { Name = first[0].Interests[j].Name, Weight = weight / (double)first.Count }); } for (int j = 0; j < second[0].Interests.Count; j++) { double weight = 0.0; for (int i = 0; i < second.Count; i++) { weight += second[i].Interests[j].Weight; } secondDTO.Interests.Add(new Interest { Name = first[0].Interests[j].Name, Weight = weight / second.Count }); } dist = distance.GetDinstance(firstDTO, secondDTO); return(dist); }
private bool UpdateClusters(ref List <ClusterEntityDTO> clusters, ref List <ClusterEntityDTO> centroids) { double[] distance = new double[centroids.Count]; bool changes = false; for (int i = 0; i < centroids.Count; i++) { centroids[i].ClustersCount = 0; } for (int i = 0; i < clusters.Count; i++) { for (int j = 0; j < centroids.Count; j++) { distance[j] = _distance.GetDinstance(clusters[i], centroids[j]); } int clId = GetIndexOfMin(distance); if (clId != clusters[i].CentroidId) { changes = true; clusters[i].CentroidId = clId; } centroids[clId].ClustersCount++; } if (!changes) { return(false); } if (centroids.Where(x => x.ClustersCount == 0).Count() > 0) { return(true); } return(changes); }
private List <ClusterEntityDTO> Union(List <ClusterEntityDTO> data, int maxUnion, int step) { int unionCount = maxUnion; List <ClusterEntityDTO> result = new List <ClusterEntityDTO>(); List <HierarchicalUnionObjectsDTO> distances = new List <HierarchicalUnionObjectsDTO>(); for (int i = 0; i < data.Count; i++) { for (int j = i + 1; j < data.Count; j++) { distances.Add(new HierarchicalUnionObjectsDTO { FirstId = i, SecondId = j, Distance = _distance.GetDinstance(data[i], data[j]) }); } } distances.Sort((x, y) => x.Distance.CompareTo(y.Distance)); bool[] used = new bool[data.Count]; int counter = 0; for (int i = 0; i < distances.Count; i++) { if (unionCount == 0) { break; } if (!used[distances[i].FirstId] && !used[distances[i].SecondId]) { ClusterEntityDTO dTO = new ClusterEntityDTO(); dTO.Children = new List <ClusterEntityDTO> { data[distances[i].FirstId], data[distances[i].SecondId] }; if (step == 1) { dTO.Name = GetName(counter++); } else { dTO.Name = dTO.Children[0].Name + dTO.Children[1].Name; } used[distances[i].FirstId] = true; used[distances[i].SecondId] = true; result.Add(dTO); --unionCount; } } for (int i = 0; i < data.Count; i++) { if (!used[i]) { ClusterEntityDTO temp = new ClusterEntityDTO(data[i]); temp.Children = new List <ClusterEntityDTO>(new List <ClusterEntityDTO> { data[i] }); temp.Name = data[i].Children == null?GetName(counter ++) : data[i].Name; result.Add(temp); //result.Add(data[i]); } } for (int k = 0; k < result.Count; k++) { result[k].Interests = new List <Interest>(data[0].Interests); if (result[k].Children != null) { for (int j = 0; j < result[k].Children[0].Interests.Count; j++) { double weight = 0.0; for (int i = 0; i < result[k].Children.Count; i++) { weight += result[k].Children[i].Interests[j].Weight; } result[k].Interests[j].Weight = weight / result[k].Children.Count; } } } return(result); }