Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleSolution.cs @ 8139

Last change on this file since 8139 was 8139, checked in by mkommend, 12 years ago

#1722: Renamed indizes to indices in the whole trunk solution.

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