Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6612 was 6612, checked in by mkommend, 13 years ago

#1592: Implemented view for regression solutions contained in a RegressionEnsembleSolution.

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