Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13704 was 13704, checked in by mkommend, 9 years ago

#2590: Added model weights for ensembles.

File size: 13.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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(CreatableAttribute.Categories.DataAnalysisEnsembles, Priority = 100)]
38  public sealed class RegressionEnsembleSolution : RegressionSolutionBase, IRegressionEnsembleSolution {
39    private readonly Dictionary<int, double> trainingEvaluationCache = new Dictionary<int, double>();
40    private readonly Dictionary<int, double> testEvaluationCache = new Dictionary<int, double>();
41    private readonly Dictionary<int, double> evaluationCache = new Dictionary<int, double>();
42
43    public new IRegressionEnsembleModel Model {
44      get { return (IRegressionEnsembleModel)base.Model; }
45    }
46
47    public new RegressionEnsembleProblemData ProblemData {
48      get { return (RegressionEnsembleProblemData)base.ProblemData; }
49      set { base.ProblemData = value; }
50    }
51
52    [Storable]
53    private readonly ItemCollection<IRegressionSolution> regressionSolutions;
54    public IItemCollection<IRegressionSolution> RegressionSolutions {
55      get { return regressionSolutions; }
56    }
57
58    [Storable]
59    private readonly Dictionary<IRegressionModel, IntRange> trainingPartitions;
60    [Storable]
61    private readonly Dictionary<IRegressionModel, IntRange> testPartitions;
62
63    [StorableConstructor]
64    private RegressionEnsembleSolution(bool deserializing)
65      : base(deserializing) {
66      regressionSolutions = new ItemCollection<IRegressionSolution>();
67    }
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() {
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;
77
78          regressionSolutions.Add(model.CreateRegressionSolution(problemData));
79        }
80      }
81
82      RegisterModelEvents();
83      RegisterRegressionSolutionsEventHandler();
84    }
85
86    private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
87      : base(original, cloner) {
88      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
89      testPartitions = new Dictionary<IRegressionModel, IntRange>();
90      foreach (var pair in original.trainingPartitions) {
91        trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
92      }
93      foreach (var pair in original.testPartitions) {
94        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
95      }
96
97      evaluationCache = new Dictionary<int, double>(original.ProblemData.Dataset.Rows);
98      trainingEvaluationCache = new Dictionary<int, double>(original.ProblemData.TrainingIndices.Count());
99      testEvaluationCache = new Dictionary<int, double>(original.ProblemData.TestIndices.Count());
100
101      regressionSolutions = cloner.Clone(original.regressionSolutions);
102      RegisterModelEvents();
103      RegisterRegressionSolutionsEventHandler();
104    }
105
106    public RegressionEnsembleSolution()
107      : base(new RegressionEnsembleModel(), RegressionEnsembleProblemData.EmptyProblemData) {
108      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
109      testPartitions = new Dictionary<IRegressionModel, IntRange>();
110      regressionSolutions = new ItemCollection<IRegressionSolution>();
111
112      RegisterModelEvents();
113      RegisterRegressionSolutionsEventHandler();
114    }
115
116    public RegressionEnsembleSolution(IRegressionProblemData problemData)
117      : this(new RegressionEnsembleModel(), problemData) {
118    }
119
120    public RegressionEnsembleSolution(IRegressionEnsembleModel model, IRegressionProblemData problemData)
121      : base(model, new RegressionEnsembleProblemData(problemData)) {
122      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
123      testPartitions = new Dictionary<IRegressionModel, IntRange>();
124      regressionSolutions = new ItemCollection<IRegressionSolution>();
125
126      evaluationCache = new Dictionary<int, double>(problemData.Dataset.Rows);
127      trainingEvaluationCache = new Dictionary<int, double>(problemData.TrainingIndices.Count());
128      testEvaluationCache = new Dictionary<int, double>(problemData.TestIndices.Count());
129
130
131      var solutions = model.Models.Select(m => m.CreateRegressionSolution((IRegressionProblemData)problemData.Clone()));
132      foreach (var solution in solutions) {
133        regressionSolutions.Add(solution);
134        trainingPartitions.Add(solution.Model, solution.ProblemData.TrainingPartition);
135        testPartitions.Add(solution.Model, solution.ProblemData.TestPartition);
136      }
137
138      RecalculateResults();
139      RegisterModelEvents();
140      RegisterRegressionSolutionsEventHandler();
141    }
142
143
144    public override IDeepCloneable Clone(Cloner cloner) {
145      return new RegressionEnsembleSolution(this, cloner);
146    }
147
148    private void RegisterModelEvents() {
149      Model.Changed += Model_Changed;
150    }
151    private void RegisterRegressionSolutionsEventHandler() {
152      regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
153      regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
154      regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
155    }
156
157    #region Evaluation
158    public override IEnumerable<double> EstimatedValues {
159      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
160    }
161
162    public override IEnumerable<double> EstimatedTrainingValues {
163      get {
164        var rows = ProblemData.TrainingIndices;
165        var rowsToEvaluate = rows.Except(trainingEvaluationCache.Keys);
166
167        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
168        var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate, (r, m) => RowIsTrainingForModel(r, m) && !RowIsTestForModel(r, m)).GetEnumerator();
169
170        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
171          trainingEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
172        }
173
174        return rows.Select(row => trainingEvaluationCache[row]);
175      }
176    }
177
178    public override IEnumerable<double> EstimatedTestValues {
179      get {
180        var rows = ProblemData.TestIndices;
181        var rowsToEvaluate = rows.Except(testEvaluationCache.Keys);
182        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
183        var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate, RowIsTestForModel).GetEnumerator();
184
185        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
186          testEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
187        }
188
189        return rows.Select(row => testEvaluationCache[row]);
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    private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
197      return testPartitions == null || !testPartitions.ContainsKey(model) ||
198              (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
199    }
200
201    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
202      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
203      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
204      var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
205
206      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
207        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
208      }
209
210      return rows.Select(row => evaluationCache[row]);
211    }
212
213    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(IEnumerable<int> rows) {
214      return Model.GetEstimatedValueVectors(ProblemData.Dataset, rows);
215    }
216    #endregion
217
218    protected override void OnProblemDataChanged() {
219      trainingEvaluationCache.Clear();
220      testEvaluationCache.Clear();
221      evaluationCache.Clear();
222      IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
223                                                                     ProblemData.AllowedInputVariables,
224                                                                     ProblemData.TargetVariable);
225      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
226      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
227      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
228      problemData.TestPartition.End = ProblemData.TestPartition.End;
229
230      foreach (var solution in RegressionSolutions) {
231        if (solution is RegressionEnsembleSolution)
232          solution.ProblemData = ProblemData;
233        else
234          solution.ProblemData = problemData;
235      }
236      foreach (var trainingPartition in trainingPartitions.Values) {
237        trainingPartition.Start = ProblemData.TrainingPartition.Start;
238        trainingPartition.End = ProblemData.TrainingPartition.End;
239      }
240      foreach (var testPartition in testPartitions.Values) {
241        testPartition.Start = ProblemData.TestPartition.Start;
242        testPartition.End = ProblemData.TestPartition.End;
243      }
244
245      base.OnProblemDataChanged();
246    }
247
248    private void Model_Changed(object sender, EventArgs e) {
249      var modelSet = new HashSet<IRegressionModel>(Model.Models);
250      foreach (var model in Model.Models) {
251        if (!trainingPartitions.ContainsKey(model)) trainingPartitions.Add(model, ProblemData.TrainingPartition);
252        if (!testPartitions.ContainsKey(model)) testPartitions.Add(model, ProblemData.TrainingPartition);
253      }
254      foreach (var model in trainingPartitions.Keys) {
255        if (modelSet.Contains(model)) continue;
256        trainingPartitions.Remove(model);
257        testPartitions.Remove(model);
258      }
259
260      trainingEvaluationCache.Clear();
261      testEvaluationCache.Clear();
262      evaluationCache.Clear();
263
264      OnModelChanged();
265    }
266
267    public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
268      regressionSolutions.AddRange(solutions);
269    }
270    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
271      regressionSolutions.RemoveRange(solutions);
272    }
273
274    private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
275      foreach (var solution in e.Items) {
276        trainingPartitions.Add(solution.Model, solution.ProblemData.TrainingPartition);
277        testPartitions.Add(solution.Model, solution.ProblemData.TestPartition);
278      }
279      Model.AddRange(e.Items.Select(s => s.Model));
280    }
281    private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
282      foreach (var solution in e.Items) {
283        trainingPartitions.Remove(solution.Model);
284        testPartitions.Remove(solution.Model);
285      }
286      Model.RemoveRange(e.Items.Select(s => s.Model));
287    }
288    private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
289      foreach (var solution in e.OldItems) {
290        trainingPartitions.Remove(solution.Model);
291        testPartitions.Remove(solution.Model);
292      }
293      Model.RemoveRange(e.OldItems.Select(s => s.Model));
294
295      foreach (var solution in e.Items) {
296        trainingPartitions.Add(solution.Model, solution.ProblemData.TrainingPartition);
297        testPartitions.Add(solution.Model, solution.ProblemData.TestPartition);
298      }
299      Model.AddRange(e.Items.Select(s => s.Model));
300    }
301  }
302}
Note: See TracBrowser for help on using the repository browser.