source: branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleSolution.cs @ 7459

Last change on this file since 7459 was 7459, checked in by sforsten, 10 years ago

#1776: first implementation of different voting strategies (currently no gui elements are available to choose between the strategies)

File size: 15.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Interfaces.Classification;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  /// <summary>
34  /// Represents classification solutions that contain an ensemble of multiple classification models
35  /// </summary>
36  [StorableClass]
37  [Item("Classification Ensemble Solution", "A classification solution that contains an ensemble of multiple classification models")]
38  [Creatable("Data Analysis - Ensembles")]
39  public sealed class ClassificationEnsembleSolution : ClassificationSolution, IClassificationEnsembleSolution {
40    public new IClassificationEnsembleModel Model {
41      get { return (IClassificationEnsembleModel)base.Model; }
42    }
43    public new ClassificationEnsembleProblemData ProblemData {
44      get { return (ClassificationEnsembleProblemData)base.ProblemData; }
45      set { base.ProblemData = value; }
46    }
47
48    private readonly ItemCollection<IClassificationSolution> classificationSolutions;
49    public IItemCollection<IClassificationSolution> ClassificationSolutions {
50      get { return classificationSolutions; }
51    }
52
53    [Storable]
54    private Dictionary<IClassificationModel, IntRange> trainingPartitions;
55    [Storable]
56    private Dictionary<IClassificationModel, IntRange> testPartitions;
57
58    private IClassificationEnsembleSolutionWeightCalculator weightCalculator;
59
60    [StorableConstructor]
61    private ClassificationEnsembleSolution(bool deserializing)
62      : base(deserializing) {
63      classificationSolutions = new ItemCollection<IClassificationSolution>();
64    }
65    [StorableHook(HookType.AfterDeserialization)]
66    private void AfterDeserialization() {
67      foreach (var model in Model.Models) {
68        IClassificationProblemData problemData = (IClassificationProblemData)ProblemData.Clone();
69        problemData.TrainingPartition.Start = trainingPartitions[model].Start;
70        problemData.TrainingPartition.End = trainingPartitions[model].End;
71        problemData.TestPartition.Start = testPartitions[model].Start;
72        problemData.TestPartition.End = testPartitions[model].End;
73
74        classificationSolutions.Add(model.CreateClassificationSolution(problemData));
75      }
76      RegisterClassificationSolutionsEventHandler();
77    }
78
79    private ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
80      : base(original, cloner) {
81      trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
82      testPartitions = new Dictionary<IClassificationModel, IntRange>();
83      foreach (var pair in original.trainingPartitions) {
84        trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
85      }
86      foreach (var pair in original.testPartitions) {
87        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
88      }
89
90      classificationSolutions = cloner.Clone(original.classificationSolutions);
91      RegisterClassificationSolutionsEventHandler();
92    }
93
94    public ClassificationEnsembleSolution()
95      : base(new ClassificationEnsembleModel(), ClassificationEnsembleProblemData.EmptyProblemData) {
96      trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
97      testPartitions = new Dictionary<IClassificationModel, IntRange>();
98      classificationSolutions = new ItemCollection<IClassificationSolution>();
99
100      weightCalculator = new AccuracyWeightCalculator();
101
102      RegisterClassificationSolutionsEventHandler();
103    }
104
105    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
106      : this(models, problemData,
107             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
108             models.Select(m => (IntRange)problemData.TestPartition.Clone())
109      ) { }
110
111    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
112      : base(new ClassificationEnsembleModel(Enumerable.Empty<IClassificationModel>()), new ClassificationEnsembleProblemData(problemData)) {
113      this.trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
114      this.testPartitions = new Dictionary<IClassificationModel, IntRange>();
115      this.classificationSolutions = new ItemCollection<IClassificationSolution>();
116
117      List<IClassificationSolution> solutions = new List<IClassificationSolution>();
118      var modelEnumerator = models.GetEnumerator();
119      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
120      var testPartitionEnumerator = testPartitions.GetEnumerator();
121
122      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
123        var p = (IClassificationProblemData)problemData.Clone();
124        p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
125        p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
126        p.TestPartition.Start = testPartitionEnumerator.Current.Start;
127        p.TestPartition.End = testPartitionEnumerator.Current.End;
128
129        solutions.Add(modelEnumerator.Current.CreateClassificationSolution(p));
130      }
131      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
132        throw new ArgumentException();
133      }
134
135      RegisterClassificationSolutionsEventHandler();
136      classificationSolutions.AddRange(solutions);
137    }
138
139    public override IDeepCloneable Clone(Cloner cloner) {
140      return new ClassificationEnsembleSolution(this, cloner);
141    }
142    private void RegisterClassificationSolutionsEventHandler() {
143      classificationSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsAdded);
144      classificationSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsRemoved);
145      classificationSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CollectionReset);
146    }
147
148    protected override void RecalculateResults() {
149      CalculateResults();
150    }
151
152    #region Evaluation
153    public override IEnumerable<double> EstimatedTrainingClassValues {
154      get {
155        var rows = ProblemData.TrainingIndizes;
156        var estimatedValuesEnumerators = (from model in Model.Models
157                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
158                                         .ToList();
159        var rowsEnumerator = rows.GetEnumerator();
160        IEnumerable<double> weights = weightCalculator.CalculateWeights(classificationSolutions);
161        // aggregate to make sure that MoveNext is called for all enumerators
162        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
163          int currentRow = rowsEnumerator.Current;
164
165          var selectedEnumerators = from pair in estimatedValuesEnumerators
166                                    where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
167                                    select pair.EstimatedValuesEnumerator;
168          yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current), weights);
169        }
170      }
171    }
172
173    public override IEnumerable<double> EstimatedTestClassValues {
174      get {
175        var rows = ProblemData.TestIndizes;
176        var estimatedValuesEnumerators = (from model in Model.Models
177                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
178                                         .ToList();
179        var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
180        IEnumerable<double> weights = weightCalculator.CalculateWeights(classificationSolutions);
181        // aggregate to make sure that MoveNext is called for all enumerators
182        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
183          int currentRow = rowsEnumerator.Current;
184
185          var selectedEnumerators = from pair in estimatedValuesEnumerators
186                                    where RowIsTestForModel(currentRow, pair.Model)
187                                    select pair.EstimatedValuesEnumerator;
188
189          yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current), weights);
190        }
191      }
192    }
193
194    private bool RowIsTrainingForModel(int currentRow, IClassificationModel model) {
195      return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
196              (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
197    }
198
199    private bool RowIsTestForModel(int currentRow, IClassificationModel model) {
200      return testPartitions == null || !testPartitions.ContainsKey(model) ||
201              (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
202    }
203
204    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
205      IEnumerable<double> weights = weightCalculator.CalculateWeights(classificationSolutions);
206      return from xs in GetEstimatedClassValueVectors(ProblemData.Dataset, rows)
207             select AggregateEstimatedClassValues(xs, weights);
208    }
209
210    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
211      if (!Model.Models.Any()) yield break;
212      var estimatedValuesEnumerators = (from model in Model.Models
213                                        select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
214                                       .ToList();
215
216      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
217        yield return from enumerator in estimatedValuesEnumerators
218                     select enumerator.Current;
219      }
220    }
221
222    private double AggregateEstimatedClassValues(IEnumerable<double> estimatedClassValues, IEnumerable<double> weights) {
223      IDictionary<double, double> weightSum = new Dictionary<double, double>();
224      for (int i = 0; i < estimatedClassValues.Count(); i++) {
225        if (!weightSum.ContainsKey(estimatedClassValues.ElementAt(i)))
226          weightSum[estimatedClassValues.ElementAt(i)] = 0.0;
227        weightSum[estimatedClassValues.ElementAt(i)] += weights.ElementAt(i);
228      }
229      if (weightSum.Count <= 0)
230        return double.NaN;
231      var max = weightSum.Max(x => x.Value);
232      max = weightSum
233        .Where(x => x.Value.Equals(max))
234        .Select(x => x.Key)
235        .First();
236      return max;
237      //old code
238      //return weightSum
239      //  .Where(x => x.Value.Equals(max))
240      //  .Select(x => x.Key)
241      //  .First();
242      //return estimatedClassValues
243      //.GroupBy(x => x)
244      //.OrderBy(g => -g.Count())
245      //.Select(g => g.Key)
246      //.DefaultIfEmpty(double.NaN)
247      //.First();
248    }
249    #endregion
250
251    protected override void OnProblemDataChanged() {
252      IClassificationProblemData problemData = new ClassificationProblemData(ProblemData.Dataset,
253                                                                     ProblemData.AllowedInputVariables,
254                                                                     ProblemData.TargetVariable);
255      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
256      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
257      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
258      problemData.TestPartition.End = ProblemData.TestPartition.End;
259
260      foreach (var solution in ClassificationSolutions) {
261        if (solution is ClassificationEnsembleSolution)
262          solution.ProblemData = ProblemData;
263        else
264          solution.ProblemData = problemData;
265      }
266      foreach (var trainingPartition in trainingPartitions.Values) {
267        trainingPartition.Start = ProblemData.TrainingPartition.Start;
268        trainingPartition.End = ProblemData.TrainingPartition.End;
269      }
270      foreach (var testPartition in testPartitions.Values) {
271        testPartition.Start = ProblemData.TestPartition.Start;
272        testPartition.End = ProblemData.TestPartition.End;
273      }
274
275      base.OnProblemDataChanged();
276    }
277
278    public void AddClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
279      classificationSolutions.AddRange(solutions);
280    }
281    public void RemoveClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
282      classificationSolutions.RemoveRange(solutions);
283    }
284
285    private void classificationSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
286      foreach (var solution in e.Items) AddClassificationSolution(solution);
287      RecalculateResults();
288    }
289    private void classificationSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
290      foreach (var solution in e.Items) RemoveClassificationSolution(solution);
291      RecalculateResults();
292    }
293    private void classificationSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
294      foreach (var solution in e.OldItems) RemoveClassificationSolution(solution);
295      foreach (var solution in e.Items) AddClassificationSolution(solution);
296      RecalculateResults();
297    }
298
299    private void AddClassificationSolution(IClassificationSolution solution) {
300      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
301      Model.Add(solution.Model);
302      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
303      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
304    }
305
306    private void RemoveClassificationSolution(IClassificationSolution solution) {
307      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
308      Model.Remove(solution.Model);
309      trainingPartitions.Remove(solution.Model);
310      testPartitions.Remove(solution.Model);
311    }
312  }
313}
Note: See TracBrowser for help on using the repository browser.