Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMClustering.cs @ 16899

Last change on this file since 16899 was 16899, checked in by msemenki, 3 years ago

#2988: New version of class structure.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Core;
23using System.Collections.Generic;
24
25namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
26  public class EMModelsClusterizationAlgorithm {
27    public int K { get; private set; }
28    public EMModelsClusterizationAlgorithm() {
29    }
30    public EMModelsClusterizationAlgorithm(int k) {
31      K = k;
32    }
33    public EMModelsClusterizationAlgorithm(EMModelsClusterizationAlgorithm old) {
34      this.K = old.K;
35    }
36    public int Apply(IRandom random, double[,] distances, List<int> numberCluster) {
37
38      K = ApplyClusteringAlgorithm(random, distances, numberCluster, K);
39      return K;
40    }
41
42    public static int ApplyClusteringAlgorithm(IRandom random, double[,] distances, List<int> numberCluster, int k) {
43      int mapSize = distances.GetLength(0);
44      List<int> centroids = new List<int>(); // capacity is equal K
45      List<double> averageClusterDistance = new List<double>();
46      List<List<int>> clusters = new List<List<int>>(); // capacity is equal K
47      CentroidsRandomSetUp(random, centroids, mapSize, k);
48      if (numberCluster.Count == 0) {
49        for (int i = 0; i < mapSize; i++) {
50          numberCluster.Add(0);
51        }
52      }
53      bool flag = true;
54
55      while (flag) {
56        clusters.Clear();
57        for (int i = 0; i < k; i++) {
58          clusters.Add(new List<int>());
59
60        }
61        for (int i = 0; i < mapSize; i++) {
62          numberCluster[i] = LookCloseCentroid(centroids, distances, i, k);
63          clusters[numberCluster[i]].Add(numberCluster[i]);
64        }
65        k = NullSizeClusterDelete(centroids, clusters, mapSize, numberCluster, k);
66        flag = false;
67        for (int i = 0; i < k; i++) {
68          AverageClusterDistanceCalculation(averageClusterDistance, distances, numberCluster, mapSize, i);
69          var newCentroid = clusters[i][HelpFunctions.ChooseMinElementIndex(averageClusterDistance)];
70          if (newCentroid != centroids[i]) {
71            flag = true;
72            centroids[i] = newCentroid;
73          }
74          averageClusterDistance.Clear();
75        }
76      }
77      return k;
78    }
79    private static void CentroidsRandomSetUp(IRandom random, List<int> centroids, int size, int k) {
80      for (int i = 0; i < k; i++) {
81        centroids.Add(random.Next(size));
82      }
83    }
84    private static int LookCloseCentroid(List<int> centroids, double[,] distances, int currentNumber, int k) {
85      double minDistanse = distances[currentNumber, centroids[0]];
86      int clusterNum = 0;
87      for (int i = 1; i < k; i++) {
88        if (minDistanse > distances[currentNumber, centroids[i]]) {
89          minDistanse = distances[currentNumber, centroids[i]];
90          clusterNum = i;
91        }
92      }
93      return clusterNum;
94    }
95    private static int NullSizeClusterDelete(List<int> centroids, List<List<int>> clusters, int mapSize, List<int> numberCluster, int k) {
96      int iter = 0;
97      for (int i = 0; i < k; i++) {
98        if (clusters[i - iter].Count == 0) {
99          for (int j = 0; j < mapSize; j++) {
100            if (numberCluster[j] > (i - iter))
101              numberCluster[j]--;
102          }
103
104          for (int j = 0; j < k - iter; j++) {
105            if (j != i - iter) {
106              for (int m = 0; m < clusters[j].Count; m++)
107                if (clusters[j][m] > (i - iter))
108                  (clusters[j][m])--;
109            }
110          }
111          clusters.Remove(clusters[i - iter]);
112          centroids.Remove(i - iter);
113          iter++;
114        }
115      }
116      k -= iter;
117      return k;
118    }
119    private static void AverageClusterDistanceCalculation(List<double> averageClusterDistance, double[,] distances, List<int> numberCluster, int MapSize, int currentClusterNumber) {
120      int m = 0;
121      for (int i = 0; i < MapSize; i++) {
122        if (numberCluster[i] == currentClusterNumber) {
123          averageClusterDistance.Add(0);
124          for (int j = 0; j < MapSize; j++) {
125            if (numberCluster[j] == currentClusterNumber)
126              averageClusterDistance[m] += distances[i, j];
127          }
128          m++;
129        }
130      }
131    }
132
133
134  }
135}
Note: See TracBrowser for help on using the repository browser.