[14559] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17011] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14559] | 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 System;
|
---|
[14424] | 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
[14559] | 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[14424] | 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
| 28 |
|
---|
[14559] | 29 | public class KMeansAlgorithm<TData> where TData : class {
|
---|
[14424] | 30 |
|
---|
[14559] | 31 | private class ClusterInfo {
|
---|
| 32 | public TData Mean;
|
---|
| 33 | public double Variance;
|
---|
| 34 | public int Size;
|
---|
| 35 | }
|
---|
[14424] | 36 |
|
---|
[14559] | 37 | private readonly Func<List<TData>, TData> meanCalculator;
|
---|
| 38 | private readonly Func<TData, TData, double> distanceCalculator;
|
---|
| 39 |
|
---|
| 40 | public KMeansAlgorithm(Func<List<TData>, TData> meanCalculator, Func<TData, TData, double> distanceCalculator) {
|
---|
| 41 | this.meanCalculator = meanCalculator;
|
---|
| 42 | this.distanceCalculator = distanceCalculator;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public List<List<int>> Run(List<TData> data, int k, double changeThreshold, IRandom random) {
|
---|
| 46 | int numClusters = Math.Min(k, data.Count);
|
---|
| 47 |
|
---|
| 48 | var assignments = new int[data.Count];
|
---|
| 49 | for (int i = 0; i < assignments.Length; i++)
|
---|
| 50 | assignments[i] = -1; // unassigned
|
---|
| 51 |
|
---|
[14431] | 52 | // (1) initialize each cluster with a random element as mean
|
---|
[14559] | 53 | var clusters = new ClusterInfo[numClusters];
|
---|
| 54 | var initIndices = new Permutation(PermutationTypes.Absolute, data.Count, random);
|
---|
| 55 | for (int c = 0; c < numClusters; c++) {
|
---|
| 56 | assignments[initIndices[c]] = c;
|
---|
| 57 | clusters[c] = new ClusterInfo {
|
---|
| 58 | Mean = data[initIndices[c]],
|
---|
| 59 | Size = 1
|
---|
| 60 | };
|
---|
[14431] | 61 | }
|
---|
[14424] | 62 |
|
---|
[14559] | 63 | // (2) repeat clustering until change rate is below the threshold
|
---|
| 64 | double changeRate;
|
---|
[14431] | 65 | do {
|
---|
[14559] | 66 | int changes = Iterate(data, assignments, clusters);
|
---|
| 67 | changeRate = (double)changes / data.Count;
|
---|
[14431] | 68 | } while (changeRate > changeThreshold);
|
---|
[14424] | 69 |
|
---|
[14559] | 70 | // (3) return non-empty clusters
|
---|
| 71 | var clustersData = new List<List<int>>(numClusters);
|
---|
| 72 | for (int c = 0; c < numClusters; c++)
|
---|
| 73 | clustersData.Add(new List<int>());
|
---|
| 74 | for (int i = 0; i < assignments.Length; i++)
|
---|
| 75 | clustersData[assignments[i]].Add(i);
|
---|
| 76 | clustersData.RemoveAll(c => c.Count == 0);
|
---|
| 77 | return clustersData;
|
---|
[14431] | 78 | }
|
---|
| 79 |
|
---|
[14559] | 80 | private int Iterate(List<TData> data, int[] assignments, ClusterInfo[] clusters) {
|
---|
[14431] | 81 | int changes = 0;
|
---|
| 82 |
|
---|
[14559] | 83 | var newAssignments = new int[data.Count];
|
---|
| 84 | assignments.CopyTo(newAssignments, 0);
|
---|
[14431] | 85 |
|
---|
[14559] | 86 | // assign elements to currently most suited cluster
|
---|
| 87 | for (int i = 0; i < data.Count; i++) {
|
---|
| 88 | int bestCluster = 0;
|
---|
| 89 | double bestImpact = CalculateImpact(data[i], clusters[0]);
|
---|
| 90 | for (int c = 1; c < clusters.Length; c++) {
|
---|
| 91 | double impact = CalculateImpact(data[i], clusters[c]);
|
---|
| 92 | if (impact < bestImpact) {
|
---|
| 93 | bestImpact = impact;
|
---|
| 94 | bestCluster = c;
|
---|
[14431] | 95 | }
|
---|
| 96 | }
|
---|
[14559] | 97 | newAssignments[i] = bestCluster;
|
---|
| 98 | if (newAssignments[i] != assignments[i])
|
---|
[14431] | 99 | changes++;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[14559] | 102 | // update clusters
|
---|
| 103 | var clustersData = new List<List<TData>>(clusters.Length);
|
---|
| 104 | for (int c = 0; c < clusters.Length; c++)
|
---|
| 105 | clustersData.Add(new List<TData>());
|
---|
| 106 | for (int i = 0; i < data.Count; i++) {
|
---|
| 107 | assignments[i] = newAssignments[i];
|
---|
| 108 | clustersData[assignments[i]].Add(data[i]);
|
---|
[14431] | 109 | }
|
---|
[14559] | 110 | for (int c = 0; c < clusters.Length; c++) {
|
---|
| 111 | var clusterData = clustersData[c];
|
---|
| 112 | if (clusterData.Count == 0)
|
---|
| 113 | continue;
|
---|
[14431] | 114 |
|
---|
[14559] | 115 | clusters[c].Mean = meanCalculator(clusterData);
|
---|
[14431] | 116 |
|
---|
[14559] | 117 | clusters[c].Variance = 0;
|
---|
| 118 | foreach (var e in clusterData)
|
---|
| 119 | clusters[c].Variance += Math.Pow(distanceCalculator(e, clusters[c].Mean), 2);
|
---|
| 120 | clusters[c].Variance /= clusterData.Count;
|
---|
[14424] | 121 |
|
---|
[14559] | 122 | clusters[c].Size = clusterData.Count;
|
---|
[14431] | 123 | }
|
---|
| 124 |
|
---|
[14559] | 125 | return changes;
|
---|
[14431] | 126 | }
|
---|
[14424] | 127 |
|
---|
[14559] | 128 | private double CalculateImpact(TData datum, ClusterInfo cluster) {
|
---|
| 129 | double newVariance = (cluster.Variance * cluster.Size + Math.Pow(distanceCalculator(datum, cluster.Mean), 2)) / (cluster.Size + 1);
|
---|
| 130 | return newVariance - cluster.Variance;
|
---|
[14431] | 131 | }
|
---|
[14424] | 132 | }
|
---|
| 133 | }
|
---|