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

Last change on this file since 7549 was 7549, checked in by sforsten, 8 years ago

#1776:

  • models can be selected with a check box
  • all strategies are now finished
  • major changes have been made to provide the same behaviour when getting the estimated training or test values of an ensemble
File size: 13.2 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 CheckedItemCollection<IClassificationSolution> classificationSolutions;
49    public ICheckedItemCollection<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    public IClassificationEnsembleSolutionWeightCalculator WeightCalculator {
61      set {
62        if (value != null) {
63          weightCalculator = value;
64          if (!ProblemData.IsEmpty)
65            RecalculateResults();
66        }
67      }
68    }
69
70    [StorableConstructor]
71    private ClassificationEnsembleSolution(bool deserializing)
72      : base(deserializing) {
73      classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
74    }
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      foreach (var model in Model.Models) {
78        IClassificationProblemData problemData = (IClassificationProblemData)ProblemData.Clone();
79        //problemData.TrainingPartition.Start = trainingPartitions[model].Start;
80        //problemData.TrainingPartition.End = trainingPartitions[model].End;
81        //problemData.TestPartition.Start = testPartitions[model].Start;
82        //problemData.TestPartition.End = testPartitions[model].End;
83
84        classificationSolutions.Add(model.CreateClassificationSolution(problemData));
85      }
86      RegisterClassificationSolutionsEventHandler();
87    }
88
89    private ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
90      : base(original, cloner) {
91      //trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
92      //testPartitions = new Dictionary<IClassificationModel, IntRange>();
93      //foreach (var pair in original.trainingPartitions) {
94      //  trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
95      //}
96      //foreach (var pair in original.testPartitions) {
97      //  testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
98      //}
99
100      classificationSolutions = cloner.Clone(original.classificationSolutions);
101      RegisterClassificationSolutionsEventHandler();
102    }
103
104    public ClassificationEnsembleSolution()
105      : base(new ClassificationEnsembleModel(), ClassificationEnsembleProblemData.EmptyProblemData) {
106      //trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
107      //testPartitions = new Dictionary<IClassificationModel, IntRange>();
108      classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
109
110      weightCalculator = new MajorityVoteWeightCalculator();
111
112      RegisterClassificationSolutionsEventHandler();
113    }
114
115    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
116      : this(models, problemData,
117             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
118             models.Select(m => (IntRange)problemData.TestPartition.Clone())
119      ) { }
120
121    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
122      : base(new ClassificationEnsembleModel(Enumerable.Empty<IClassificationModel>()), new ClassificationEnsembleProblemData(problemData)) {
123      //this.trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
124      //this.testPartitions = new Dictionary<IClassificationModel, IntRange>();
125      this.classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
126
127      List<IClassificationSolution> solutions = new List<IClassificationSolution>();
128      var modelEnumerator = models.GetEnumerator();
129      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
130      var testPartitionEnumerator = testPartitions.GetEnumerator();
131
132      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
133        var p = (IClassificationProblemData)problemData.Clone();
134        p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
135        p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
136        p.TestPartition.Start = testPartitionEnumerator.Current.Start;
137        p.TestPartition.End = testPartitionEnumerator.Current.End;
138
139        solutions.Add(modelEnumerator.Current.CreateClassificationSolution(p));
140      }
141      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
142        throw new ArgumentException();
143      }
144
145      RegisterClassificationSolutionsEventHandler();
146      classificationSolutions.AddRange(solutions);
147    }
148
149    public override IDeepCloneable Clone(Cloner cloner) {
150      return new ClassificationEnsembleSolution(this, cloner);
151    }
152    private void RegisterClassificationSolutionsEventHandler() {
153      classificationSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsAdded);
154      classificationSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsRemoved);
155      classificationSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CollectionReset);
156      classificationSolutions.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CheckedItemsChanged);
157    }
158
159    protected override void RecalculateResults() {
160      weightCalculator.CalculateNormalizedWeights(classificationSolutions.CheckedItems);
161      CalculateResults();
162    }
163
164    #region Evaluation
165    public override IEnumerable<double> EstimatedTrainingClassValues {
166      get {
167        return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
168                                                              ProblemData.Dataset,
169                                                              ProblemData.TrainingIndizes,
170                                                              weightCalculator.GetTrainingClassDelegate());
171      }
172    }
173
174    public override IEnumerable<double> EstimatedTestClassValues {
175      get {
176        return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
177                                                              ProblemData.Dataset,
178                                                              ProblemData.TestIndizes,
179                                                              weightCalculator.GetTestClassDelegate());
180      }
181    }
182
183    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
184      return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
185                                                            ProblemData.Dataset,
186                                                            rows,
187                                                            weightCalculator.GetAllClassDelegate());
188    }
189
190    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
191      IEnumerable<IClassificationModel> models = classificationSolutions.CheckedItems.Select(sol => sol.Model);
192      if (!models.Any()) yield break;
193      var estimatedValuesEnumerators = (from model in models
194                                        select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
195                                       .ToList();
196
197      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
198        yield return from enumerator in estimatedValuesEnumerators
199                     select enumerator.Current;
200      }
201    }
202    #endregion
203
204    protected override void OnProblemDataChanged() {
205      IClassificationProblemData problemData = new ClassificationProblemData(ProblemData.Dataset,
206                                                                     ProblemData.AllowedInputVariables,
207                                                                     ProblemData.TargetVariable);
208      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
209      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
210      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
211      problemData.TestPartition.End = ProblemData.TestPartition.End;
212
213      foreach (var solution in ClassificationSolutions) {
214        if (solution is ClassificationEnsembleSolution)
215          solution.ProblemData = ProblemData;
216        else
217          solution.ProblemData = problemData;
218      }
219      //foreach (var trainingPartition in trainingPartitions.Values) {
220      //  trainingPartition.Start = ProblemData.TrainingPartition.Start;
221      //  trainingPartition.End = ProblemData.TrainingPartition.End;
222      //}
223      //foreach (var testPartition in testPartitions.Values) {
224      //  testPartition.Start = ProblemData.TestPartition.Start;
225      //  testPartition.End = ProblemData.TestPartition.End;
226      //}
227
228      base.OnProblemDataChanged();
229    }
230
231    public void AddClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
232      classificationSolutions.AddRange(solutions);
233    }
234    public void RemoveClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
235      classificationSolutions.RemoveRange(solutions);
236    }
237
238    private void classificationSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
239      foreach (var solution in e.Items) AddClassificationSolution(solution);
240      RecalculateResults();
241    }
242    private void classificationSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
243      foreach (var solution in e.Items) RemoveClassificationSolution(solution);
244      RecalculateResults();
245    }
246    private void classificationSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
247      foreach (var solution in e.OldItems) RemoveClassificationSolution(solution);
248      foreach (var solution in e.Items) AddClassificationSolution(solution);
249      RecalculateResults();
250    }
251    private void classificationSolutions_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
252      RecalculateResults();
253    }
254
255    private void AddClassificationSolution(IClassificationSolution solution) {
256      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
257      Model.Add(solution.Model);
258      //trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
259      //testPartitions[solution.Model] = solution.ProblemData.TestPartition;
260    }
261
262    private void RemoveClassificationSolution(IClassificationSolution solution) {
263      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
264      Model.Remove(solution.Model);
265      //trainingPartitions.Remove(solution.Model);
266      //testPartitions.Remove(solution.Model);
267    }
268  }
269}
Note: See TracBrowser for help on using the repository browser.