Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7908 was 7738, checked in by gkronber, 13 years ago

#1722 added an additional ctor for RegerssionEnsembleSolution to simplify code.

File size: 13.7 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 : 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(IRegressionProblemData problemData)
102      : this(Enumerable.Empty<IRegressionModel>(), problemData) {
103    }
104
105    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
106      : this(models, problemData,
107             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
108             models.Select(m => (IntRange)problemData.TestPartition.Clone())
109      ) { }
110
111    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
112      : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
113      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
114      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
115      this.regressionSolutions = new ItemCollection<IRegressionSolution>();
116
117      List<IRegressionSolution> solutions = new List<IRegressionSolution>();
118      var modelEnumerator = models.GetEnumerator();
119      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
120      var testPartitionEnumerator = testPartitions.GetEnumerator();
121
122      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
123        var p = (IRegressionProblemData)problemData.Clone();
124        p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
125        p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
126        p.TestPartition.Start = testPartitionEnumerator.Current.Start;
127        p.TestPartition.End = testPartitionEnumerator.Current.End;
128
129        solutions.Add(modelEnumerator.Current.CreateRegressionSolution(p));
130      }
131      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
132        throw new ArgumentException();
133      }
134
135      RegisterRegressionSolutionsEventHandler();
136      regressionSolutions.AddRange(solutions);
137    }
138
139    public override IDeepCloneable Clone(Cloner cloner) {
140      return new RegressionEnsembleSolution(this, cloner);
141    }
142    private void RegisterRegressionSolutionsEventHandler() {
143      regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
144      regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
145      regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
146    }
147
148    protected override void RecalculateResults() {
149      CalculateResults();
150    }
151
152    #region Evaluation
153    public override IEnumerable<double> EstimatedTrainingValues {
154      get {
155        var rows = ProblemData.TrainingIndizes;
156        var estimatedValuesEnumerators = (from model in Model.Models
157                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
158                                         .ToList();
159        var rowsEnumerator = rows.GetEnumerator();
160        // aggregate to make sure that MoveNext is called for all enumerators
161        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
162          int currentRow = rowsEnumerator.Current;
163
164          var selectedEnumerators = from pair in estimatedValuesEnumerators
165                                    where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
166                                    select pair.EstimatedValuesEnumerator;
167          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
168        }
169      }
170    }
171
172    public override IEnumerable<double> EstimatedTestValues {
173      get {
174        var rows = ProblemData.TestIndizes;
175        var estimatedValuesEnumerators = (from model in Model.Models
176                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
177                                         .ToList();
178        var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
179        // aggregate to make sure that MoveNext is called for all enumerators
180        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
181          int currentRow = rowsEnumerator.Current;
182
183          var selectedEnumerators = from pair in estimatedValuesEnumerators
184                                    where RowIsTestForModel(currentRow, pair.Model)
185                                    select pair.EstimatedValuesEnumerator;
186
187          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
188        }
189      }
190    }
191
192    private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
193      return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
194              (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
195    }
196
197    private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
198      return testPartitions == null || !testPartitions.ContainsKey(model) ||
199              (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
200    }
201
202    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
203      return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
204             select AggregateEstimatedValues(xs);
205    }
206
207    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
208      if (!Model.Models.Any()) yield break;
209      var estimatedValuesEnumerators = (from model in Model.Models
210                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
211                                       .ToList();
212
213      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
214        yield return from enumerator in estimatedValuesEnumerators
215                     select enumerator.Current;
216      }
217    }
218
219    private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
220      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
221    }
222    #endregion
223
224    protected override void OnProblemDataChanged() {
225      IRegressionProblemData problemData = new RegressionProblemData(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 RegressionSolutions) {
234        if (solution is RegressionEnsembleSolution)
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 AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
252      regressionSolutions.AddRange(solutions);
253    }
254    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
255      regressionSolutions.RemoveRange(solutions);
256    }
257
258    private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
259      foreach (var solution in e.Items) AddRegressionSolution(solution);
260      RecalculateResults();
261    }
262    private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
263      foreach (var solution in e.Items) RemoveRegressionSolution(solution);
264      RecalculateResults();
265    }
266    private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
267      foreach (var solution in e.OldItems) RemoveRegressionSolution(solution);
268      foreach (var solution in e.Items) AddRegressionSolution(solution);
269      RecalculateResults();
270    }
271
272    private void AddRegressionSolution(IRegressionSolution 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 RemoveRegressionSolution(IRegressionSolution 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.