Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClustering.cs @ 5658

Last change on this file since 5658 was 5658, checked in by gkronber, 14 years ago

#1418 implemented wrapper for LDA (linear discriminant analysis) implemented in alglib.

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using System.Collections.Generic;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
35
36namespace HeuristicLab.Algorithms.DataAnalysis {
37  /// <summary>
38  /// k-Means clustering algorithm data analysis algorithm.
39  /// </summary>
40  [Item("k-Means", "The k-Means clustering algorithm.")]
41  [Creatable("Data Analysis")]
42  [StorableClass]
43  public sealed class KMeansClustering : FixedDataAnalysisAlgorithm<IClusteringProblem> {
44    private const string KParameterName = "k";
45    private const string RestartsParameterName = "Restarts";
46    private const string KMeansSolutionResultName = "k-Means clustering solution";
47    #region parameter properties
48    public IValueParameter<IntValue> KParameter {
49      get { return (IValueParameter<IntValue>)Parameters[KParameterName]; }
50    }
51    public IValueParameter<IntValue> RestartsParameter {
52      get { return (IValueParameter<IntValue>)Parameters[RestartsParameterName]; }
53    }
54    #endregion
55    #region properties
56    public IntValue K {
57      get { return KParameter.Value; }
58    }
59    public IntValue Restarts {
60      get { return RestartsParameter.Value; }
61    }
62    #endregion
63    [StorableConstructor]
64    private KMeansClustering(bool deserializing) : base(deserializing) { }
65    private KMeansClustering(KMeansClustering original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public KMeansClustering()
69      : base() {
70      Parameters.Add(new ValueParameter<IntValue>(KParameterName, "The number of clusters.", new IntValue(3)));
71      Parameters.Add(new ValueParameter<IntValue>(RestartsParameterName, "The number of restarts.", new IntValue(0)));
72      Problem = new ClusteringProblem();
73    }
74    [StorableHook(HookType.AfterDeserialization)]
75    private void AfterDeserialization() { }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new KMeansClustering(this, cloner);
79    }
80
81    #region k-Means clustering
82    protected override void Run() {
83      var solution = CreateKMeansSolution(Problem.ProblemData, K.Value, Restarts.Value);
84      Results.Add(new Result(KMeansSolutionResultName, "The linear regression solution.", solution));
85    }
86
87    public static KMeansClusteringSolution CreateKMeansSolution(IClusteringProblemData problemData, int k, int restarts) {
88      Dataset dataset = problemData.Dataset;
89      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
90      int start = problemData.TrainingPartitionStart.Value;
91      int end = problemData.TrainingPartitionEnd.Value;
92      IEnumerable<int> rows = Enumerable.Range(start, end - start);
93      int info;
94      double[,] centers;
95      int[] xyc;
96      double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
97      alglib.kmeansgenerate(inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), k, restarts + 1, out info, out centers, out xyc);
98      if (info != 1) throw new ArgumentException("Error in calculation of k-Means clustering solution");
99
100      KMeansClusteringSolution solution = new KMeansClusteringSolution(new KMeansClusteringModel(centers, allowedInputVariables), problemData);
101      return solution;
102    }
103    #endregion
104  }
105}
Note: See TracBrowser for help on using the repository browser.