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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
27 | public class KMeansClusterizationAlgorithm {
|
---|
28 | public int K { get; private set; }
|
---|
29 | public KMeansClusterizationAlgorithm() {
|
---|
30 | }
|
---|
31 | public KMeansClusterizationAlgorithm(int k) {
|
---|
32 | K = k;
|
---|
33 | }
|
---|
34 | public KMeansClusterizationAlgorithm(KMeansClusterizationAlgorithm 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 |
|
---|
43 | public static int ApplyClusteringAlgorithm(IRandom random, double[,] distances, List<int> numberCluster, int k) {
|
---|
44 | int mapSize = distances.GetLength(0);
|
---|
45 | List<int> centroids = new List<int>(); // capacity is equal K
|
---|
46 | List<double> averageClusterDistance = new List<double>();
|
---|
47 | List<List<int>> clusters = new List<List<int>>(); // capacity is equal K
|
---|
48 | CentroidsRandomSetUp(random, centroids, mapSize, k);
|
---|
49 | if (numberCluster.Count == 0) {
|
---|
50 | for (int i = 0; i < mapSize; i++) {
|
---|
51 | numberCluster.Add(0);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | bool flag = true;
|
---|
55 | int count = 0;
|
---|
56 | while (flag&&(count<1000)) {
|
---|
57 | clusters.Clear();
|
---|
58 | for (int i = 0; i < k; i++) {
|
---|
59 | clusters.Add(new List<int>());
|
---|
60 |
|
---|
61 | }
|
---|
62 | for (int i = 0; i < mapSize; i++) {
|
---|
63 | numberCluster[i] = LookCloseCentroid(centroids, distances, i, k);
|
---|
64 | clusters[numberCluster[i]].Add(i);
|
---|
65 | }
|
---|
66 | k = NullSizeClusterDelete(centroids, clusters, mapSize, numberCluster, k);
|
---|
67 | flag = false;
|
---|
68 | for (int i = 0; i < k; i++) {
|
---|
69 | AverageClusterDistanceCalculation(averageClusterDistance, distances, numberCluster, mapSize, i);
|
---|
70 | var newCentroid = clusters[i][HelpFunctions.ChooseMinElementIndex(averageClusterDistance)];
|
---|
71 | if (newCentroid != centroids[i]) {
|
---|
72 | flag = true;
|
---|
73 | centroids[i] = newCentroid;
|
---|
74 | }
|
---|
75 | averageClusterDistance.Clear();
|
---|
76 | }
|
---|
77 | count++;
|
---|
78 | }
|
---|
79 | return k;
|
---|
80 | }
|
---|
81 | private static void CentroidsRandomSetUp(IRandom random, List<int> centroids, int size, int k) {
|
---|
82 | for (int i = 0; i < k; i++) {
|
---|
83 | centroids.Add(random.Next(size));
|
---|
84 | }
|
---|
85 | }
|
---|
86 | private static int LookCloseCentroid(List<int> centroids, double[,] distances, int currentNumber, int k) {
|
---|
87 | double minDistanse = distances[currentNumber, centroids[0]];
|
---|
88 | int clusterNum = 0;
|
---|
89 | for (int i = 1; i < k; i++) {
|
---|
90 | if (minDistanse > distances[currentNumber, centroids[i]]) {
|
---|
91 | minDistanse = distances[currentNumber, centroids[i]];
|
---|
92 | clusterNum = i;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | return clusterNum;
|
---|
96 | }
|
---|
97 | private static int NullSizeClusterDelete(List<int> centroids, List<List<int>> clusters, int mapSize, List<int> numberCluster, int k) {
|
---|
98 | int iter = 0;
|
---|
99 | for (int i = 0; i < k; i++) {
|
---|
100 | if (clusters[i - iter].Count == 0) {
|
---|
101 | for (int j = 0; j < mapSize; j++) {
|
---|
102 | if (numberCluster[j] > (i - iter))
|
---|
103 | numberCluster[j]--;
|
---|
104 | }
|
---|
105 |
|
---|
106 | for (int j = 0; j < k - iter; j++) {
|
---|
107 | if (j != i - iter) {
|
---|
108 | for (int m = 0; m < clusters[j].Count; m++)
|
---|
109 | if (clusters[j][m] > (i - iter))
|
---|
110 | (clusters[j][m])--;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | clusters.Remove(clusters[i - iter]);
|
---|
114 | centroids.Remove(centroids[i - iter]);
|
---|
115 | iter++;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | k -= iter;
|
---|
119 | return k;
|
---|
120 | }
|
---|
121 | public static void AverageClusterDistanceCalculation(List<double> averageClusterDistance, double[,] distances, List<int> numberCluster, int MapSize, int currentClusterNumber) {
|
---|
122 | int m = 0;
|
---|
123 | for (int i = 0; i < MapSize; i++) {
|
---|
124 | if (numberCluster[i] == currentClusterNumber) {
|
---|
125 | averageClusterDistance.Add(0);
|
---|
126 | for (int j = 0; j < MapSize; j++) {
|
---|
127 | if (numberCluster[j] == currentClusterNumber)
|
---|
128 | averageClusterDistance[m] += Math.Abs(distances[i, j]);
|
---|
129 | }
|
---|
130 | m++;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | }
|
---|
137 | }
|
---|