Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClustering.cs @ 16565

Last change on this file since 16565 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HEAL.Attic;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace 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("84FAF4ED-0679-40BA-B3DD-83809095F8CF")]
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 _) : base(_) { }
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.ContainsNanOrInfinity())
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}
Note: See TracBrowser for help on using the repository browser.