Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16811 was 16760, checked in by msemenki, 6 years ago

#2988: ADD:

  1. new type of mutation that respect node type and do not change trigonometric node to "add" node;
  2. variation of previous mutation type that do not change type of terminal node;
  3. some interface adjusting for comfort work with algorithm;
  4. second variant of map, that is not "island" map but provide "net" map.
File size: 6.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;
24using System.Linq;
25
26namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
27  public class EMModelsClusterizationAlgorithm {
28    public int K { get; private set; }
29    public EMModelsClusterizationAlgorithm() {
30    }
31    public EMModelsClusterizationAlgorithm(int k) {
32      K = k;
33    }
34    public EMModelsClusterizationAlgorithm(EMModelsClusterizationAlgorithm old) {
35      this.K = old.K;
36    }
37    public int Apply(IRandom random, double[,] distances, List<int> numberCluster) {
38
39      K = ApplyClusteringAlgorithm(random, distances, numberCluster, K);
40      return K;
41    }
42    public static void ApplyFullConectedMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, int k, int neghboorNumber = 10) {
43      int mapSize = distances.GetLength(0);
44      List<double> currentList = new List<double>();
45      for (int i = 0; i < mapSize; i++) {
46        map.Add(new List<int>());
47        for (int j = 0; j < mapSize; j++) {
48          currentList.Add(distances[i, j]);
49        }
50        map[i].Add(ChooseMinElement(currentList));
51        while (map[i].Count < neghboorNumber) {
52          map[i].Add(ChooseMinElement(currentList, i, map[i]));
53        }
54        currentList.Clear();
55      }
56    }
57    private static bool CheckNumberIsInList(int number, List<int> priviousNumber) {
58      foreach (var pNum in priviousNumber) {
59        if (number == pNum)
60          return true;
61      }
62      return false;
63    }
64    public static int ApplyClusteringAlgorithm(IRandom random, double[,] distances, List<int> numberCluster, int k) {
65      int mapSize = distances.GetLength(0);
66      List<int> centroids = new List<int>(); // capacity is equal K
67      List<double> averageClusterDistance = new List<double>();
68      List<List<int>> clusters = new List<List<int>>(); // capacity is equal K
69      CentroidsRandomSetUp(random, centroids, mapSize, k);
70      if (numberCluster.Count == 0) {
71        for (int i = 0; i < mapSize; i++) {
72          numberCluster.Add(0);
73        }
74      }
75      bool flag = true;
76
77      while (flag) {
78        clusters.Clear();
79        for (int i = 0; i < k; i++) {
80          clusters.Add(new List<int>());
81
82        }
83        for (int i = 0; i < mapSize; i++) {
84          numberCluster[i] = LookCloseCentroid(centroids, mapSize, distances, i, k);
85          clusters[numberCluster[i]].Add(numberCluster[i]);
86        }
87        k = NullSizeClusterDelete(centroids, clusters, mapSize, numberCluster, k);
88        flag = false;
89        for (int i = 0; i < k; i++) {
90          AverageClusterDistanceCalculation(averageClusterDistance, distances, numberCluster, mapSize, i);
91          var newCentroid = clusters[i][ChooseMinElement(averageClusterDistance)];
92          if (newCentroid != centroids[i]) {
93            flag = true;
94            centroids[i] = newCentroid;
95          }
96          averageClusterDistance.Clear();
97        }
98      }
99      return k;
100    }
101    private static void CentroidsRandomSetUp(IRandom random, List<int> centroids, int size, int k) {
102      for (int i = 0; i < k; i++) {
103        centroids.Add(random.Next(size));
104      }
105    }
106    private static int LookCloseCentroid(List<int> centroids, int MapSize, double[,] distances, int currentNumber, int k) {
107      double minDistanse = distances[currentNumber, centroids[0]];
108      int clusterNum = 0;
109      for (int i = 1; i < k; i++) {
110        if (minDistanse > distances[currentNumber, centroids[i]]) {
111          minDistanse = distances[currentNumber, centroids[i]];
112          clusterNum = i;
113        }
114      }
115      return clusterNum;
116    }
117    private static int NullSizeClusterDelete(List<int> centroids, List<List<int>> clusters, int mapSize, List<int> numberCluster, int k) {
118      int iter = 0;
119      for (int i = 0; i < k; i++) {
120        if (clusters[i - iter].Count == 0) {
121          for (int j = 0; j < mapSize; j++) {
122            if (numberCluster[j] > (i - iter))
123              numberCluster[j]--;
124          }
125
126          for (int j = 0; j < k - iter; j++) {
127            if (j != i - iter) {
128              for (int m = 0; m < clusters[j].Count; m++)
129                if (clusters[j][m] > (i - iter))
130                  (clusters[j][m])--;
131            }
132          }
133          clusters.Remove(clusters[i - iter]);
134          centroids.Remove(i - iter);
135          iter++;
136        }
137      }
138      k -= iter;
139      return k;
140    }
141    private static void AverageClusterDistanceCalculation(List<double> averageClusterDistance, double[,] distances, List<int> numberCluster, int MapSize, int currentClusterNumber) {
142      int m = 0;
143      for (int i = 0; i < MapSize; i++) {
144        if (numberCluster[i] == currentClusterNumber) {
145          averageClusterDistance.Add(0);
146          for (int j = 0; j < MapSize; j++) {
147            if (numberCluster[j] == currentClusterNumber)
148              averageClusterDistance[m] += distances[i, j];
149          }
150          m++;
151        }
152      }
153    }
154    private static int ChooseMinElement(List<double> averageClusterDistance) {
155      double minValue = averageClusterDistance[0];
156      int minElementNumber = 0;
157      for (int i = 1; i < averageClusterDistance.Count(); i++) {
158        if (averageClusterDistance[i] < minValue) {
159          minValue = averageClusterDistance[i];
160          minElementNumber = i;
161        }
162      }
163      return minElementNumber;
164    }
165    private static int ChooseMinElement(List<double> distances, int currentElement, List<int> previousNumbers) {
166      double minValue = 100;
167      int minElementNumber = 0;
168      int temp = 0, i = 0;
169      while (temp == 0) {
170        if ((currentElement != i) && (!CheckNumberIsInList(i, previousNumbers))) {
171          minValue = distances[i];
172          minElementNumber = i;
173          temp = i;
174        }
175        i++;
176      }
177      for (i = 0; i < distances.Count(); i++) {
178        if ((distances[i] < minValue) && (currentElement != i) && (!CheckNumberIsInList(i, previousNumbers))) {
179          minValue = distances[i];
180          minElementNumber = i;
181        }
182      }
183      return minElementNumber;
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.