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