Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClustering.cs @ 16350

Last change on this file since 16350 was 15788, checked in by gkronber, 7 years ago

#2902 merged r15783 and r15786 from trunk to stable

File size: 4.4 KB
RevLine 
[5651]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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
22using System;
[5777]23using System.Collections.Generic;
[5651]24using System.Linq;
[15061]25using System.Threading;
[5651]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace 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).")]
[12708]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
[15061]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) {
[12702]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;
[15142]92      double[,] inputMatrix = dataset.ToArray(allowedInputVariables, rows);
[15788]93      if (inputMatrix.ContainsNanOrInfinity())
[6002]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}
Note: See TracBrowser for help on using the repository browser.