Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12702 was 12702, checked in by mkommend, 9 years ago

#2276: Merged changes to stable.

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  /// <summary>
35  /// k-Means clustering algorithm data analysis algorithm.
36  /// </summary>
37  [Item("k-Means", "The k-Means clustering algorithm (wrapper for ALGLIB).")]
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);
81      Results.Add(new Result(KMeansSolutionResultName, "The k-Means clustering solution.", solution));
82    }
83
84    public static KMeansClusteringSolution CreateKMeansSolution(IClusteringProblemData problemData, int k, int restarts) {
85      var dataset = problemData.Dataset;
86      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
87      IEnumerable<int> rows = problemData.TrainingIndices;
88      int info;
89      double[,] centers;
90      int[] xyc;
91      double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
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
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
98      KMeansClusteringSolution solution = new KMeansClusteringSolution(new KMeansClusteringModel(centers, allowedInputVariables), (IClusteringProblemData)problemData.Clone());
99      return solution;
100    }
101    #endregion
102  }
103}
Note: See TracBrowser for help on using the repository browser.