Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs @ 17317

Last change on this file since 17317 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

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