[5651] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5651] | 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;
|
---|
[5777] | 23 | using System.Collections.Generic;
|
---|
[5651] | 24 | using System.Linq;
|
---|
[14523] | 25 | using System.Threading;
|
---|
[5651] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 35 | /// <summary>
|
---|
| 36 | /// k-Means clustering algorithm data analysis algorithm.
|
---|
| 37 | /// </summary>
|
---|
[6240] | 38 | [Item("k-Means", "The k-Means clustering algorithm (wrapper for ALGLIB).")]
|
---|
[12504] | 39 | [Creatable(CreatableAttribute.Categories.DataAnalysis, Priority = 10)]
|
---|
[5651] | 40 | [StorableClass]
|
---|
| 41 | public sealed class KMeansClustering : FixedDataAnalysisAlgorithm<IClusteringProblem> {
|
---|
| 42 | private const string KParameterName = "k";
|
---|
| 43 | private const string RestartsParameterName = "Restarts";
|
---|
| 44 | private const string KMeansSolutionResultName = "k-Means clustering solution";
|
---|
| 45 | #region parameter properties
|
---|
| 46 | public IValueParameter<IntValue> KParameter {
|
---|
| 47 | get { return (IValueParameter<IntValue>)Parameters[KParameterName]; }
|
---|
| 48 | }
|
---|
| 49 | public IValueParameter<IntValue> RestartsParameter {
|
---|
| 50 | get { return (IValueParameter<IntValue>)Parameters[RestartsParameterName]; }
|
---|
| 51 | }
|
---|
| 52 | #endregion
|
---|
| 53 | #region properties
|
---|
| 54 | public IntValue K {
|
---|
| 55 | get { return KParameter.Value; }
|
---|
| 56 | }
|
---|
| 57 | public IntValue Restarts {
|
---|
| 58 | get { return RestartsParameter.Value; }
|
---|
| 59 | }
|
---|
| 60 | #endregion
|
---|
| 61 | [StorableConstructor]
|
---|
| 62 | private KMeansClustering(bool deserializing) : base(deserializing) { }
|
---|
| 63 | private KMeansClustering(KMeansClustering original, Cloner cloner)
|
---|
| 64 | : base(original, cloner) {
|
---|
| 65 | }
|
---|
| 66 | public KMeansClustering()
|
---|
| 67 | : base() {
|
---|
| 68 | Parameters.Add(new ValueParameter<IntValue>(KParameterName, "The number of clusters.", new IntValue(3)));
|
---|
| 69 | Parameters.Add(new ValueParameter<IntValue>(RestartsParameterName, "The number of restarts.", new IntValue(0)));
|
---|
| 70 | Problem = new ClusteringProblem();
|
---|
| 71 | }
|
---|
| 72 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 73 | private void AfterDeserialization() { }
|
---|
| 74 |
|
---|
| 75 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 76 | return new KMeansClustering(this, cloner);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | #region k-Means clustering
|
---|
[14523] | 80 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[5651] | 81 | var solution = CreateKMeansSolution(Problem.ProblemData, K.Value, Restarts.Value);
|
---|
[8080] | 82 | Results.Add(new Result(KMeansSolutionResultName, "The k-Means clustering solution.", solution));
|
---|
[5651] | 83 | }
|
---|
| 84 |
|
---|
| 85 | public static KMeansClusteringSolution CreateKMeansSolution(IClusteringProblemData problemData, int k, int restarts) {
|
---|
[12509] | 86 | var dataset = problemData.Dataset;
|
---|
[5651] | 87 | IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
|
---|
[8139] | 88 | IEnumerable<int> rows = problemData.TrainingIndices;
|
---|
[5651] | 89 | int info;
|
---|
| 90 | double[,] centers;
|
---|
| 91 | int[] xyc;
|
---|
[14400] | 92 | double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
|
---|
[6002] | 93 | if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
|
---|
| 94 | throw new NotSupportedException("k-Means clustering does not support NaN or infinity values in the input dataset.");
|
---|
| 95 |
|
---|
[5651] | 96 | alglib.kmeansgenerate(inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), k, restarts + 1, out info, out centers, out xyc);
|
---|
| 97 | if (info != 1) throw new ArgumentException("Error in calculation of k-Means clustering solution");
|
---|
| 98 |
|
---|
[5914] | 99 | KMeansClusteringSolution solution = new KMeansClusteringSolution(new KMeansClusteringModel(centers, allowedInputVariables), (IClusteringProblemData)problemData.Clone());
|
---|
[5651] | 100 | return solution;
|
---|
| 101 | }
|
---|
| 102 | #endregion
|
---|
| 103 | }
|
---|
| 104 | }
|
---|