Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13702 was 13702, checked in by mkommend, 8 years ago

#2590: Fixed bugs in RegressionEnsembleSolution when adding new solutions.

File size: 13.0 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      RegisterRegressionSolutionsEventHandler();
82    }
83
84    private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
85      : base(original, cloner) {
86      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
87      testPartitions = new Dictionary<IRegressionModel, IntRange>();
88      foreach (var pair in original.trainingPartitions) {
89        trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
90      }
91      foreach (var pair in original.testPartitions) {
92        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
93      }
94
95      evaluationCache = new Dictionary<int, double>(original.ProblemData.Dataset.Rows);
96      trainingEvaluationCache = new Dictionary<int, double>(original.ProblemData.TrainingIndices.Count());
97      testEvaluationCache = new Dictionary<int, double>(original.ProblemData.TestIndices.Count());
98
99      regressionSolutions = cloner.Clone(original.regressionSolutions);
100      RegisterRegressionSolutionsEventHandler();
101    }
102
103    public RegressionEnsembleSolution()
104      : base(new RegressionEnsembleModel(), RegressionEnsembleProblemData.EmptyProblemData) {
105      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
106      testPartitions = new Dictionary<IRegressionModel, IntRange>();
107      regressionSolutions = new ItemCollection<IRegressionSolution>();
108
109      RegisterRegressionSolutionsEventHandler();
110    }
111
112    public RegressionEnsembleSolution(IRegressionProblemData problemData)
113      : this(new RegressionEnsembleModel(), problemData) {
114    }
115
116    public RegressionEnsembleSolution(IRegressionEnsembleModel model, IRegressionProblemData problemData)
117      : base(model, new RegressionEnsembleProblemData(problemData)) {
118      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
119      testPartitions = new Dictionary<IRegressionModel, IntRange>();
120      regressionSolutions = new ItemCollection<IRegressionSolution>();
121
122      evaluationCache = new Dictionary<int, double>(problemData.Dataset.Rows);
123      trainingEvaluationCache = new Dictionary<int, double>(problemData.TrainingIndices.Count());
124      testEvaluationCache = new Dictionary<int, double>(problemData.TestIndices.Count());
125
126
127      var solutions = model.Models.Select(m => m.CreateRegressionSolution((IRegressionProblemData)problemData.Clone()));
128      foreach (var solution in solutions) {
129        regressionSolutions.Add(solution);
130        trainingPartitions.Add(solution.Model, solution.ProblemData.TrainingPartition);
131        testPartitions.Add(solution.Model, solution.ProblemData.TestPartition);
132      }
133
134      RecalculateResults();
135      RegisterRegressionSolutionsEventHandler();
136    }
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    #region Evaluation
149    public override IEnumerable<double> EstimatedValues {
150      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
151    }
152
153    public override IEnumerable<double> EstimatedTrainingValues {
154      get {
155        var rows = ProblemData.TrainingIndices;
156        var rowsToEvaluate = rows.Except(trainingEvaluationCache.Keys);
157        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
158        var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate, (r, m) => RowIsTrainingForModel(r, m) && !RowIsTestForModel(r, m)).GetEnumerator();
159
160        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
161          trainingEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
162        }
163
164        return rows.Select(row => trainingEvaluationCache[row]);
165      }
166    }
167
168    public override IEnumerable<double> EstimatedTestValues {
169      get {
170        var rows = ProblemData.TestIndices;
171        var rowsToEvaluate = rows.Except(testEvaluationCache.Keys);
172        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
173        var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate, RowIsTestForModel).GetEnumerator();
174
175        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
176          testEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
177        }
178
179        return rows.Select(row => testEvaluationCache[row]);
180      }
181    }
182    private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
183      return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
184              (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
185    }
186    private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
187      return testPartitions == null || !testPartitions.ContainsKey(model) ||
188              (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
189    }
190
191    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
192      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
193      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
194      var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
195
196      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
197        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
198      }
199
200      return rows.Select(row => evaluationCache[row]);
201    }
202
203    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(IEnumerable<int> rows) {
204      return Model.GetEstimatedValueVectors(ProblemData.Dataset, rows);
205    }
206    #endregion
207
208    protected override void OnProblemDataChanged() {
209      trainingEvaluationCache.Clear();
210      testEvaluationCache.Clear();
211      evaluationCache.Clear();
212      IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
213                                                                     ProblemData.AllowedInputVariables,
214                                                                     ProblemData.TargetVariable);
215      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
216      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
217      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
218      problemData.TestPartition.End = ProblemData.TestPartition.End;
219
220      foreach (var solution in RegressionSolutions) {
221        if (solution is RegressionEnsembleSolution)
222          solution.ProblemData = ProblemData;
223        else
224          solution.ProblemData = problemData;
225      }
226      foreach (var trainingPartition in trainingPartitions.Values) {
227        trainingPartition.Start = ProblemData.TrainingPartition.Start;
228        trainingPartition.End = ProblemData.TrainingPartition.End;
229      }
230      foreach (var testPartition in testPartitions.Values) {
231        testPartition.Start = ProblemData.TestPartition.Start;
232        testPartition.End = ProblemData.TestPartition.End;
233      }
234
235      base.OnProblemDataChanged();
236    }
237
238    public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
239      regressionSolutions.AddRange(solutions);
240
241      trainingEvaluationCache.Clear();
242      testEvaluationCache.Clear();
243      evaluationCache.Clear();
244    }
245    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
246      regressionSolutions.RemoveRange(solutions);
247
248      trainingEvaluationCache.Clear();
249      testEvaluationCache.Clear();
250      evaluationCache.Clear();
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
271      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
272      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
273
274      trainingEvaluationCache.Clear();
275      testEvaluationCache.Clear();
276      evaluationCache.Clear();
277    }
278
279    private void RemoveRegressionSolution(IRegressionSolution solution) {
280      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
281      Model.Remove(solution.Model);
282
283      trainingPartitions.Remove(solution.Model);
284      testPartitions.Remove(solution.Model);
285
286      trainingEvaluationCache.Clear();
287      testEvaluationCache.Clear();
288      evaluationCache.Clear();
289    }
290  }
291}
Note: See TracBrowser for help on using the repository browser.