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

Last change on this file since 7259 was 7259, checked in by swagner, 10 years ago

Updated year of copyrights to 2012 (#1716)

File size: 13.6 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(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      if (!Model.Models.Any()) yield break;
205      var estimatedValuesEnumerators = (from model in Model.Models
206                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
207                                       .ToList();
208
209      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
210        yield return from enumerator in estimatedValuesEnumerators
211                     select enumerator.Current;
212      }
213    }
214
215    private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
216      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
217    }
218    #endregion
219
220    protected override void OnProblemDataChanged() {
221      IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
222                                                                     ProblemData.AllowedInputVariables,
223                                                                     ProblemData.TargetVariable);
224      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
225      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
226      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
227      problemData.TestPartition.End = ProblemData.TestPartition.End;
228
229      foreach (var solution in RegressionSolutions) {
230        if (solution is RegressionEnsembleSolution)
231          solution.ProblemData = ProblemData;
232        else
233          solution.ProblemData = problemData;
234      }
235      foreach (var trainingPartition in trainingPartitions.Values) {
236        trainingPartition.Start = ProblemData.TrainingPartition.Start;
237        trainingPartition.End = ProblemData.TrainingPartition.End;
238      }
239      foreach (var testPartition in testPartitions.Values) {
240        testPartition.Start = ProblemData.TestPartition.Start;
241        testPartition.End = ProblemData.TestPartition.End;
242      }
243
244      base.OnProblemDataChanged();
245    }
246
247    public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
248      regressionSolutions.AddRange(solutions);
249    }
250    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
251      regressionSolutions.RemoveRange(solutions);
252    }
253
254    private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
255      foreach (var solution in e.Items) AddRegressionSolution(solution);
256      RecalculateResults();
257    }
258    private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
259      foreach (var solution in e.Items) RemoveRegressionSolution(solution);
260      RecalculateResults();
261    }
262    private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
263      foreach (var solution in e.OldItems) RemoveRegressionSolution(solution);
264      foreach (var solution in e.Items) AddRegressionSolution(solution);
265      RecalculateResults();
266    }
267
268    private void AddRegressionSolution(IRegressionSolution solution) {
269      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
270      Model.Add(solution.Model);
271      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
272      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
273    }
274
275    private void RemoveRegressionSolution(IRegressionSolution solution) {
276      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
277      Model.Remove(solution.Model);
278      trainingPartitions.Remove(solution.Model);
279      testPartitions.Remove(solution.Model);
280    }
281  }
282}
Note: See TracBrowser for help on using the repository browser.