Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs @ 14615

Last change on this file since 14615 was 12816, checked in by gkronber, 9 years ago

#2448: added storable attribute to collection of individual solutions and recreate solutions in after-deserialization hook only when the collection is empty.

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