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