1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
35 | /// <summary>
|
---|
36 | /// k-Means clustering algorithm data analysis algorithm.
|
---|
37 | /// </summary>
|
---|
38 | [Item("k-Means", "The k-Means clustering algorithm (wrapper for ALGLIB).")]
|
---|
39 | [Creatable(CreatableAttribute.Categories.DataAnalysis, Priority = 10)]
|
---|
40 | [StorableType("b85d0012-07d4-4601-87a4-2a6d19648b22")]
|
---|
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(StorableConstructorFlag 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
|
---|
80 | protected override void Run(CancellationToken cancellationToken) {
|
---|
81 | var solution = CreateKMeansSolution(Problem.ProblemData, K.Value, Restarts.Value);
|
---|
82 | Results.Add(new Result(KMeansSolutionResultName, "The k-Means clustering solution.", solution));
|
---|
83 | }
|
---|
84 |
|
---|
85 | public static KMeansClusteringSolution CreateKMeansSolution(IClusteringProblemData problemData, int k, int restarts) {
|
---|
86 | var dataset = problemData.Dataset;
|
---|
87 | IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
|
---|
88 | IEnumerable<int> rows = problemData.TrainingIndices;
|
---|
89 | int info;
|
---|
90 | double[,] centers;
|
---|
91 | int[] xyc;
|
---|
92 | double[,] inputMatrix = dataset.ToArray(allowedInputVariables, rows);
|
---|
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 |
|
---|
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 |
|
---|
99 | KMeansClusteringSolution solution = new KMeansClusteringSolution(new KMeansClusteringModel(centers, allowedInputVariables), (IClusteringProblemData)problemData.Clone());
|
---|
100 | return solution;
|
---|
101 | }
|
---|
102 | #endregion
|
---|
103 | }
|
---|
104 | }
|
---|