Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs @ 12843

Last change on this file since 12843 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
RevLine 
[5816]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5816]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
[6588]22using System;
[5816]23using System.Collections.Generic;
24using System.Linq;
[6612]25using HeuristicLab.Collections;
[5816]26using HeuristicLab.Common;
27using HeuristicLab.Core;
[6588]28using HeuristicLab.Data;
[5816]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")]
[12504]37  [Creatable(CreatableAttribute.Categories.DataAnalysisEnsembles, Priority = 100)]
[8724]38  public sealed class RegressionEnsembleSolution : RegressionSolutionBase, IRegressionEnsembleSolution {
[8167]39    private readonly Dictionary<int, double> trainingEvaluationCache = new Dictionary<int, double>();
40    private readonly Dictionary<int, double> testEvaluationCache = new Dictionary<int, double>();
[8724]41    private readonly Dictionary<int, double> evaluationCache = new Dictionary<int, double>();
[8151]42
[5816]43    public new IRegressionEnsembleModel Model {
44      get { return (IRegressionEnsembleModel)base.Model; }
45    }
46
[6666]47    public new RegressionEnsembleProblemData ProblemData {
48      get { return (RegressionEnsembleProblemData)base.ProblemData; }
49      set { base.ProblemData = value; }
50    }
51
[12816]52    [Storable]
[6612]53    private readonly ItemCollection<IRegressionSolution> regressionSolutions;
54    public IItemCollection<IRegressionSolution> RegressionSolutions {
55      get { return regressionSolutions; }
56    }
57
[5816]58    [Storable]
[8152]59    private readonly Dictionary<IRegressionModel, IntRange> trainingPartitions;
[5816]60    [Storable]
[8152]61    private readonly Dictionary<IRegressionModel, IntRange> testPartitions;
[5816]62
63    [StorableConstructor]
[6612]64    private RegressionEnsembleSolution(bool deserializing)
65      : base(deserializing) {
66      regressionSolutions = new ItemCollection<IRegressionSolution>();
67    }
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() {
[12816]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;
[6612]77
[12816]78          regressionSolutions.Add(model.CreateRegressionSolution(problemData));
79        }
[6612]80      }
81      RegisterRegressionSolutionsEventHandler();
82    }
83
[6592]84    private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
[5816]85      : base(original, cloner) {
[6239]86      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
87      testPartitions = new Dictionary<IRegressionModel, IntRange>();
[6302]88      foreach (var pair in original.trainingPartitions) {
89        trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
[6239]90      }
[6302]91      foreach (var pair in original.testPartitions) {
92        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
93      }
[6612]94
[8174]95      trainingEvaluationCache = new Dictionary<int, double>(original.ProblemData.TrainingIndices.Count());
96      testEvaluationCache = new Dictionary<int, double>(original.ProblemData.TestIndices.Count());
97
[6612]98      regressionSolutions = cloner.Clone(original.regressionSolutions);
99      RegisterRegressionSolutionsEventHandler();
[5816]100    }
[6239]101
[6666]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
[7738]111    public RegressionEnsembleSolution(IRegressionProblemData problemData)
112      : this(Enumerable.Empty<IRegressionModel>(), problemData) {
113    }
114
[5816]115    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
[6612]116      : this(models, problemData,
117             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
118             models.Select(m => (IntRange)problemData.TestPartition.Clone())
119      ) { }
[5816]120
121    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
[6612]122      : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
[5816]123      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
124      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
[6612]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
[8174]145      trainingEvaluationCache = new Dictionary<int, double>(problemData.TrainingIndices.Count());
146      testEvaluationCache = new Dictionary<int, double>(problemData.TestIndices.Count());
147
[6612]148      RegisterRegressionSolutionsEventHandler();
149      regressionSolutions.AddRange(solutions);
[5816]150    }
151
152    public override IDeepCloneable Clone(Cloner cloner) {
153      return new RegressionEnsembleSolution(this, cloner);
154    }
[6612]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    }
[5816]160
[6612]161    #region Evaluation
[8724]162    public override IEnumerable<double> EstimatedValues {
163      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
164    }
165
[5816]166    public override IEnumerable<double> EstimatedTrainingValues {
[8152]167      get {
168        var rows = ProblemData.TrainingIndices;
[8167]169        var rowsToEvaluate = rows.Except(trainingEvaluationCache.Keys);
[8152]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()) {
[8167]174          trainingEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
[8152]175        }
176
[8167]177        return rows.Select(row => trainingEvaluationCache[row]);
[8152]178      }
[5816]179    }
180
181    public override IEnumerable<double> EstimatedTestValues {
[8152]182      get {
183        var rows = ProblemData.TestIndices;
[8167]184        var rowsToEvaluate = rows.Except(testEvaluationCache.Keys);
[8152]185        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
186        var valuesEnumerator = GetEstimatedValues(rowsToEvaluate, RowIsTestForModel).GetEnumerator();
187
188        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
[8167]189          testEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
[8152]190        }
191
[8167]192        return rows.Select(row => testEvaluationCache[row]);
[8152]193      }
[8151]194    }
[5816]195
[8151]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;
[5816]204
[8151]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));
[5816]210      }
211    }
212
[6254]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
[5816]223    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
[8167]224      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
[8152]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()) {
[8167]231        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
[8152]232      }
233
[8167]234      return rows.Select(row => evaluationCache[row]);
[5816]235    }
236
[12509]237    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(IDataset dataset, IEnumerable<int> rows) {
[6982]238      if (!Model.Models.Any()) yield break;
[5816]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) {
[6238]250      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
[6254]251    }
[6612]252    #endregion
[6520]253
[6666]254    protected override void OnProblemDataChanged() {
[8167]255      trainingEvaluationCache.Clear();
256      testEvaluationCache.Clear();
257      evaluationCache.Clear();
[6666]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
[6612]284    public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
285      regressionSolutions.AddRange(solutions);
[8152]286
[8167]287      trainingEvaluationCache.Clear();
288      testEvaluationCache.Clear();
289      evaluationCache.Clear();
[6612]290    }
291    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
292      regressionSolutions.RemoveRange(solutions);
[8152]293
[8167]294      trainingEvaluationCache.Clear();
295      testEvaluationCache.Clear();
296      evaluationCache.Clear();
[6612]297    }
[6520]298
[6612]299    private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
300      foreach (var solution in e.Items) AddRegressionSolution(solution);
[6520]301      RecalculateResults();
302    }
[6612]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    }
[6520]312
[6612]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;
[8152]318
[8167]319      trainingEvaluationCache.Clear();
320      testEvaluationCache.Clear();
321      evaluationCache.Clear();
[6612]322    }
[6520]323
[6612]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);
[8152]329
[8167]330      trainingEvaluationCache.Clear();
331      testEvaluationCache.Clear();
332      evaluationCache.Clear();
[6520]333    }
[5816]334  }
335}
Note: See TracBrowser for help on using the repository browser.