Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1776_ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleSolution.cs

Last change on this file was 8811, checked in by sforsten, 12 years ago

#1776:

File size: 13.9 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;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  /// <summary>
33  /// Represents classification solutions that contain an ensemble of multiple classification models
34  /// </summary>
35  [StorableClass]
36  [Item("Classification Ensemble Solution", "A classification solution that contains an ensemble of multiple classification models")]
37  [Creatable("Data Analysis - Ensembles")]
38  public sealed class ClassificationEnsembleSolution : ClassificationSolutionBase, IClassificationEnsembleSolution {
39    private readonly Dictionary<int, double> trainingEvaluationCache = new Dictionary<int, double>();
40    private readonly Dictionary<int, double> testEvaluationCache = new Dictionary<int, double>();
41    private readonly Dictionary<int, double> evaluationCache = new Dictionary<int, double>();
42
43    public new IClassificationEnsembleModel Model {
44      get { return (IClassificationEnsembleModel)base.Model; }
45    }
46    public new ClassificationEnsembleProblemData ProblemData {
47      get { return (ClassificationEnsembleProblemData)base.ProblemData; }
48      set { base.ProblemData = value; }
49    }
50
51    private readonly CheckedItemCollection<IClassificationSolution> classificationSolutions;
52    public ICheckedItemCollection<IClassificationSolution> ClassificationSolutions {
53      get { return classificationSolutions; }
54    }
55
56    private IClassificationEnsembleSolutionWeightCalculator weightCalculator;
57    public IClassificationEnsembleSolutionWeightCalculator WeightCalculator {
58      set {
59        if (value != null) {
60          weightCalculator = value;
61          if (!ProblemData.IsEmpty) {
62            RecalculateResults();
63          }
64        }
65      }
66      get { return weightCalculator; }
67    }
68
69    [Storable]
70    private Dictionary<IClassificationModel, IntRange> trainingPartitions;
71    [Storable]
72    private Dictionary<IClassificationModel, IntRange> testPartitions;
73
74    [StorableConstructor]
75    private ClassificationEnsembleSolution(bool deserializing)
76      : base(deserializing) {
77      classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
78    }
79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
81      foreach (var model in Model.Models) {
82        IClassificationProblemData problemData = (IClassificationProblemData)ProblemData.Clone();
83        problemData.TrainingPartition.Start = trainingPartitions[model].Start;
84        problemData.TrainingPartition.End = trainingPartitions[model].End;
85        problemData.TestPartition.Start = testPartitions[model].Start;
86        problemData.TestPartition.End = testPartitions[model].End;
87
88        classificationSolutions.Add(model.CreateClassificationSolution(problemData));
89      }
90      RegisterClassificationSolutionsEventHandler();
91    }
92
93    private ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
94      : base(original, cloner) {
95      trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
96      testPartitions = new Dictionary<IClassificationModel, IntRange>();
97      foreach (var pair in original.trainingPartitions) {
98        trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
99      }
100      foreach (var pair in original.testPartitions) {
101        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
102      }
103
104      weightCalculator = cloner.Clone(original.weightCalculator);
105      classificationSolutions = cloner.Clone(original.classificationSolutions);
106      RegisterClassificationSolutionsEventHandler();
107    }
108
109    public ClassificationEnsembleSolution()
110      : base(new ClassificationEnsembleModel(), ClassificationEnsembleProblemData.EmptyProblemData) {
111      trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
112      testPartitions = new Dictionary<IClassificationModel, IntRange>();
113      classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
114      weightCalculator = new MajorityVoteWeightCalculator();
115
116      RegisterClassificationSolutionsEventHandler();
117    }
118
119    public ClassificationEnsembleSolution(IClassificationProblemData problemData) :
120      this(Enumerable.Empty<IClassificationModel>(), problemData) { }
121
122    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
123      : this(models, problemData,
124             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
125             models.Select(m => (IntRange)problemData.TestPartition.Clone())
126      ) { }
127
128    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
129      : base(new ClassificationEnsembleModel(Enumerable.Empty<IClassificationModel>()), new ClassificationEnsembleProblemData(problemData)) {
130      this.trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
131      this.testPartitions = new Dictionary<IClassificationModel, IntRange>();
132      this.classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
133
134      List<IClassificationSolution> solutions = new List<IClassificationSolution>();
135      var modelEnumerator = models.GetEnumerator();
136      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
137      var testPartitionEnumerator = testPartitions.GetEnumerator();
138
139      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
140        IClassificationProblemData p = (IClassificationProblemData)ProblemData.Clone();
141        p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
142        p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
143        p.TestPartition.Start = testPartitionEnumerator.Current.Start;
144        p.TestPartition.End = testPartitionEnumerator.Current.End;
145
146        solutions.Add(modelEnumerator.Current.CreateClassificationSolution(p));
147      }
148      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
149        throw new ArgumentException();
150      }
151
152      RegisterClassificationSolutionsEventHandler();
153      weightCalculator = new MajorityVoteWeightCalculator();
154      classificationSolutions.AddRange(solutions);
155    }
156
157    public override IDeepCloneable Clone(Cloner cloner) {
158      return new ClassificationEnsembleSolution(this, cloner);
159    }
160    private void RegisterClassificationSolutionsEventHandler() {
161      classificationSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsAdded);
162      classificationSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsRemoved);
163      classificationSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CollectionReset);
164      classificationSolutions.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CheckedItemsChanged);
165    }
166
167
168    #region Evaluation
169    public override IEnumerable<double> EstimatedClassValues {
170      get { return GetEstimatedClassValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
171    }
172
173    public override IEnumerable<double> EstimatedTrainingClassValues {
174      get {
175        return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
176                                                              ProblemData.Dataset,
177                                                              ProblemData.TrainingIndices,
178                                                              weightCalculator.GetTrainingClassDelegate());
179      }
180    }
181
182    public override IEnumerable<double> EstimatedTestClassValues {
183      get {
184        return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
185                                                              ProblemData.Dataset,
186                                                              ProblemData.TestIndices,
187                                                              weightCalculator.GetTestClassDelegate());
188      }
189    }
190
191    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
192      return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
193                                                            ProblemData.Dataset,
194                                                            rows,
195                                                            weightCalculator.GetAllClassDelegate());
196    }
197
198    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
199      IEnumerable<IClassificationModel> models = classificationSolutions.CheckedItems.Select(sol => sol.Model);
200      if (!models.Any()) yield break;
201      var estimatedValuesEnumerators = (from model in models
202                                        select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
203                                       .ToList();
204
205      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
206        yield return from enumerator in estimatedValuesEnumerators
207                     select enumerator.Current;
208      }
209    }
210    #endregion
211
212    protected override void OnProblemDataChanged() {
213      IClassificationProblemData problemData = new ClassificationProblemData(ProblemData.Dataset,
214                                                                     ProblemData.AllowedInputVariables,
215                                                                     ProblemData.TargetVariable);
216      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
217      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
218      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
219      problemData.TestPartition.End = ProblemData.TestPartition.End;
220
221      foreach (var solution in ClassificationSolutions) {
222        if (solution is ClassificationEnsembleSolution)
223          solution.ProblemData = ProblemData;
224        else
225          solution.ProblemData = problemData;
226      }
227      foreach (var trainingPartition in trainingPartitions.Values) {
228        trainingPartition.Start = ProblemData.TrainingPartition.Start;
229        trainingPartition.End = ProblemData.TrainingPartition.End;
230      }
231      foreach (var testPartition in testPartitions.Values) {
232        testPartition.Start = ProblemData.TestPartition.Start;
233        testPartition.End = ProblemData.TestPartition.End;
234      }
235
236      base.OnProblemDataChanged();
237    }
238
239    public void AddClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
240      classificationSolutions.AddRange(solutions);
241    }
242    public void RemoveClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
243      classificationSolutions.RemoveRange(solutions);
244    }
245
246    private void classificationSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
247      foreach (var solution in e.Items) AddClassificationSolution(solution);
248      RecalculateResults();
249    }
250    private void classificationSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
251      foreach (var solution in e.Items) RemoveClassificationSolution(solution);
252      RecalculateResults();
253    }
254    private void classificationSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
255      foreach (var solution in e.OldItems) RemoveClassificationSolution(solution);
256      foreach (var solution in e.Items) AddClassificationSolution(solution);
257      RecalculateResults();
258    }
259    private void classificationSolutions_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
260      RecalculateResults();
261    }
262
263    protected override void RecalculateResults() {
264      weightCalculator.CalculateNormalizedWeights(classificationSolutions.CheckedItems);
265      base.RecalculateResults();
266    }
267
268    private void AddClassificationSolution(IClassificationSolution solution) {
269      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
270      Model.Add(solution.Model);
271      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
272      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
273    }
274
275    private void RemoveClassificationSolution(IClassificationSolution solution) {
276      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
277      Model.Remove(solution.Model);
278      trainingPartitions.Remove(solution.Model);
279      testPartitions.Remove(solution.Model);
280    }
281  }
282}
Note: See TracBrowser for help on using the repository browser.