Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClustering.cs @ 5815

Last change on this file since 5815 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 4.2 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.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.")]
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 linear regression solution.", solution));
82    }
83
84    public static KMeansClusteringSolution CreateKMeansSolution(IClusteringProblemData problemData, int k, int restarts) {
85      Dataset dataset = problemData.Dataset;
86      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
87      int start = problemData.TrainingPartition.Start;
88      int end = problemData.TrainingPartition.End;
89      IEnumerable<int> rows = Enumerable.Range(start, end - start);
90      int info;
91      double[,] centers;
92      int[] xyc;
93      double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
94      alglib.kmeansgenerate(inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), k, restarts + 1, out info, out centers, out xyc);
95      if (info != 1) throw new ArgumentException("Error in calculation of k-Means clustering solution");
96
97      KMeansClusteringSolution solution = new KMeansClusteringSolution(new KMeansClusteringModel(centers, allowedInputVariables), problemData);
98      return solution;
99    }
100    #endregion
101  }
102}
Note: See TracBrowser for help on using the repository browser.