Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs @ 12417

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

#1964: Corrected creation of ensemble solutions.

File size: 15.5 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 regression solutions that contain an ensemble of multiple regression models
34  /// </summary>
35  [StorableClass]
36  [Item("Regression Ensemble Solution", "A regression solution that contains an ensemble of multiple regression models")]
37  [Creatable("Data Analysis - Ensembles")]
38  public sealed class RegressionEnsembleSolution : RegressionSolutionBase, IRegressionEnsembleSolution {
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 IRegressionEnsembleModel Model {
44      get { return (IRegressionEnsembleModel)base.Model; }
45    }
46
47    public new RegressionEnsembleProblemData ProblemData {
48      get { return (RegressionEnsembleProblemData)base.ProblemData; }
49      set { base.ProblemData = value; }
50    }
51
52    private readonly ItemCollection<IRegressionSolution> regressionSolutions;
53    public IItemCollection<IRegressionSolution> RegressionSolutions {
54      get { return regressionSolutions; }
55    }
56
57    [Storable]
58    private readonly Dictionary<IRegressionModel, IntRange> trainingPartitions;
59    [Storable]
60    private readonly Dictionary<IRegressionModel, IntRange> testPartitions;
61
62    [StorableConstructor]
63    private RegressionEnsembleSolution(bool deserializing)
64      : base(deserializing) {
65      regressionSolutions = new ItemCollection<IRegressionSolution>();
66    }
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      foreach (var model in Model.Models) {
70        IRegressionProblemData problemData = (IRegressionProblemData)ProblemData.Clone();
71        problemData.TrainingPartition.Start = trainingPartitions[model].Start;
72        problemData.TrainingPartition.End = trainingPartitions[model].End;
73        problemData.TestPartition.Start = testPartitions[model].Start;
74        problemData.TestPartition.End = testPartitions[model].End;
75
76        regressionSolutions.Add(model.CreateRegressionSolution(problemData));
77      }
78      RegisterRegressionSolutionsEventHandler();
79    }
80
81    private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
82      : base(original, cloner) {
83      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
84      testPartitions = new Dictionary<IRegressionModel, IntRange>();
85      foreach (var pair in original.trainingPartitions) {
86        trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
87      }
88      foreach (var pair in original.testPartitions) {
89        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
90      }
91
92      trainingEvaluationCache = new Dictionary<int, double>(original.ProblemData.TrainingIndices.Count());
93      testEvaluationCache = new Dictionary<int, double>(original.ProblemData.TestIndices.Count());
94
95      regressionSolutions = cloner.Clone(original.regressionSolutions);
96      RegisterRegressionSolutionsEventHandler();
97    }
98
99    public RegressionEnsembleSolution()
100      : base(new RegressionEnsembleModel(), RegressionEnsembleProblemData.EmptyProblemData) {
101      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
102      testPartitions = new Dictionary<IRegressionModel, IntRange>();
103      regressionSolutions = new ItemCollection<IRegressionSolution>();
104
105      RegisterRegressionSolutionsEventHandler();
106    }
107
108    public RegressionEnsembleSolution(IRegressionProblemData problemData)
109      : this(Enumerable.Empty<IRegressionModel>(), problemData) {
110    }
111
112    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
113      : this(models, problemData,
114             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
115             models.Select(m => (IntRange)problemData.TestPartition.Clone())
116      ) { }
117
118    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
119      : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
120      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
121      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
122      this.regressionSolutions = new ItemCollection<IRegressionSolution>();
123
124      List<IRegressionSolution> solutions = new List<IRegressionSolution>();
125      var modelEnumerator = models.GetEnumerator();
126      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
127      var testPartitionEnumerator = testPartitions.GetEnumerator();
128
129      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
130        var p = (IRegressionProblemData)problemData.Clone();
131        p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
132        p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
133        p.TestPartition.Start = testPartitionEnumerator.Current.Start;
134        p.TestPartition.End = testPartitionEnumerator.Current.End;
135
136        solutions.Add(modelEnumerator.Current.CreateRegressionSolution(p));
137      }
138      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
139        throw new ArgumentException();
140      }
141
142      trainingEvaluationCache = new Dictionary<int, double>(problemData.TrainingIndices.Count());
143      testEvaluationCache = new Dictionary<int, double>(problemData.TestIndices.Count());
144
145      RegisterRegressionSolutionsEventHandler();
146      regressionSolutions.AddRange(solutions);
147    }
148
149    public override IDeepCloneable Clone(Cloner cloner) {
150      return new RegressionEnsembleSolution(this, cloner);
151    }
152    private void RegisterRegressionSolutionsEventHandler() {
153      regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
154      regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
155      regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
156    }
157
158    #region Evaluation
159    public override IEnumerable<double> EstimatedValues {
160      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
161    }
162
163    public override IEnumerable<double> EstimatedTrainingValues {
164      get {
165        var rows = ProblemData.TrainingIndices;
166        var rowsToEvaluate = rows.Except(trainingEvaluationCache.Keys);
167        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
168        var valuesEnumerator = GetEstimatedValues(rowsToEvaluate, (r, m) => RowIsTrainingForModel(r, m) && !RowIsTestForModel(r, m)).GetEnumerator();
169
170        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
171          trainingEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
172        }
173
174        return rows.Select(row => trainingEvaluationCache[row]);
175      }
176    }
177
178    public override IEnumerable<double> EstimatedTestValues {
179      get {
180        var rows = ProblemData.TestIndices;
181        var rowsToEvaluate = rows.Except(testEvaluationCache.Keys);
182        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
183        var valuesEnumerator = GetEstimatedValues(rowsToEvaluate, RowIsTestForModel).GetEnumerator();
184
185        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
186          testEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
187        }
188
189        return rows.Select(row => testEvaluationCache[row]);
190      }
191    }
192
193    private IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows, Func<int, IRegressionModel, bool> modelSelectionPredicate) {
194      var estimatedValuesEnumerators = (from model in Model.Models
195                                        select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
196                                       .ToList();
197      var rowsEnumerator = rows.GetEnumerator();
198      // aggregate to make sure that MoveNext is called for all enumerators
199      while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
200        int currentRow = rowsEnumerator.Current;
201
202        var selectedEnumerators = from pair in estimatedValuesEnumerators
203                                  where modelSelectionPredicate(currentRow, pair.Model)
204                                  select pair.EstimatedValuesEnumerator;
205
206        yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
207      }
208    }
209
210    private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
211      return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
212              (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
213    }
214
215    private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
216      return testPartitions == null || !testPartitions.ContainsKey(model) ||
217              (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
218    }
219
220    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
221      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
222      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
223      var valuesEnumerator = (from xs in GetEstimatedValueVectors(ProblemData.Dataset, rowsToEvaluate)
224                              select AggregateEstimatedValues(xs))
225                             .GetEnumerator();
226
227      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
228        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
229      }
230
231      return rows.Select(row => evaluationCache[row]);
232    }
233
234    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
235      if (!Model.Models.Any()) yield break;
236      var estimatedValuesEnumerators = (from model in Model.Models
237                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
238                                       .ToList();
239
240      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
241        yield return from enumerator in estimatedValuesEnumerators
242                     select enumerator.Current;
243      }
244    }
245
246    private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
247      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
248    }
249    #endregion
250
251    protected override void OnProblemDataChanged() {
252      trainingEvaluationCache.Clear();
253      testEvaluationCache.Clear();
254      evaluationCache.Clear();
255      IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
256                                                                     ProblemData.AllowedInputVariables,
257                                                                     ProblemData.TargetVariable);
258      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
259      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
260      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
261      problemData.TestPartition.End = ProblemData.TestPartition.End;
262
263      foreach (var solution in RegressionSolutions) {
264        if (solution is RegressionEnsembleSolution)
265          solution.ProblemData = ProblemData;
266        else
267          solution.ProblemData = problemData;
268      }
269      foreach (var trainingPartition in trainingPartitions.Values) {
270        trainingPartition.Start = ProblemData.TrainingPartition.Start;
271        trainingPartition.End = ProblemData.TrainingPartition.End;
272      }
273      foreach (var testPartition in testPartitions.Values) {
274        testPartition.Start = ProblemData.TestPartition.Start;
275        testPartition.End = ProblemData.TestPartition.End;
276      }
277
278      base.OnProblemDataChanged();
279    }
280
281    public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
282      regressionSolutions.AddRange(solutions);
283
284      trainingEvaluationCache.Clear();
285      testEvaluationCache.Clear();
286      evaluationCache.Clear();
287    }
288    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
289      regressionSolutions.RemoveRange(solutions);
290
291      trainingEvaluationCache.Clear();
292      testEvaluationCache.Clear();
293      evaluationCache.Clear();
294    }
295
296    private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
297      foreach (var solution in e.Items) AddRegressionSolution(solution);
298      RecalculateResults();
299    }
300    private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
301      foreach (var solution in e.Items) RemoveRegressionSolution(solution);
302      RecalculateResults();
303    }
304    private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
305      foreach (var solution in e.OldItems) RemoveRegressionSolution(solution);
306      foreach (var solution in e.Items) AddRegressionSolution(solution);
307      RecalculateResults();
308    }
309
310    private void AddRegressionSolution(IRegressionSolution solution) {
311      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
312      Model.Add(solution.Model);
313      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
314      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
315
316      trainingEvaluationCache.Clear();
317      testEvaluationCache.Clear();
318      evaluationCache.Clear();
319    }
320
321    private void RemoveRegressionSolution(IRegressionSolution solution) {
322      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
323      Model.Remove(solution.Model);
324      trainingPartitions.Remove(solution.Model);
325      testPartitions.Remove(solution.Model);
326
327      trainingEvaluationCache.Clear();
328      testEvaluationCache.Clear();
329      evaluationCache.Clear();
330    }
331  }
332}
Note: See TracBrowser for help on using the repository browser.