[5651] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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 |
|
---|
[17054] | 22 | using System;
|
---|
[5651] | 23 | using System.Collections.Generic;
|
---|
[7201] | 24 | using System.Drawing;
|
---|
[5651] | 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[17097] | 28 | using HEAL.Attic;
|
---|
[5651] | 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Represents a k-Means clustering model.
|
---|
| 34 | /// </summary>
|
---|
[17097] | 35 | [StorableType("61D987AC-A142-433B-901C-B124E12A1C55")]
|
---|
[5651] | 36 | [Item("KMeansClusteringModel", "Represents a k-Means clustering model.")]
|
---|
[17054] | 37 | public sealed class KMeansClusteringModel : DataAnalysisModel, IClusteringModel {
|
---|
[7201] | 38 | public static new Image StaticItemImage {
|
---|
[5651] | 39 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[17054] | 42 | public override IEnumerable<string> VariablesUsedForPrediction {
|
---|
[14027] | 43 | get { return allowedInputVariables; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[5651] | 46 | [Storable]
|
---|
| 47 | private string[] allowedInputVariables;
|
---|
| 48 | public IEnumerable<string> AllowedInputVariables {
|
---|
| 49 | get { return allowedInputVariables; }
|
---|
| 50 | }
|
---|
| 51 | [Storable]
|
---|
| 52 | private List<double[]> centers;
|
---|
| 53 | public IEnumerable<double[]> Centers {
|
---|
| 54 | get {
|
---|
| 55 | return centers.Select(x => (double[])x.Clone());
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | [StorableConstructor]
|
---|
[17097] | 59 | private KMeansClusteringModel(StorableConstructorFlag _) : base(_) { }
|
---|
[5651] | 60 | private KMeansClusteringModel(KMeansClusteringModel original, Cloner cloner)
|
---|
| 61 | : base(original, cloner) {
|
---|
| 62 | this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
| 63 | this.centers = new List<double[]>(original.Centers);
|
---|
| 64 | }
|
---|
| 65 | public KMeansClusteringModel(double[,] centers, IEnumerable<string> allowedInputVariables)
|
---|
| 66 | : base() {
|
---|
[5658] | 67 | this.name = ItemName;
|
---|
| 68 | this.description = ItemDescription;
|
---|
[5651] | 69 | // disect center matrix into list of double[]
|
---|
| 70 | // centers are given as double matrix where number of rows = dimensions and number of columns = clusters
|
---|
| 71 | // each column is a cluster center
|
---|
| 72 | this.centers = new List<double[]>();
|
---|
| 73 | for (int i = 0; i < centers.GetLength(1); i++) {
|
---|
| 74 | double[] c = new double[centers.GetLength(0)];
|
---|
| 75 | for (int j = 0; j < c.Length; j++) {
|
---|
| 76 | c[j] = centers[j, i];
|
---|
| 77 | }
|
---|
| 78 | this.centers.Add(c);
|
---|
| 79 | }
|
---|
| 80 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new KMeansClusteringModel(this, cloner);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[17054] | 87 | public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
|
---|
| 88 | if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
|
---|
| 89 | return IsDatasetCompatible(problemData.Dataset, out errorMessage);
|
---|
| 90 | }
|
---|
[5651] | 91 |
|
---|
[17054] | 92 |
|
---|
[12702] | 93 | public IEnumerable<int> GetClusterValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[5651] | 94 | return KMeansClusteringUtil.FindClosestCenters(centers, dataset, allowedInputVariables, rows);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|