Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10479 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 15.5 KB
RevLine 
[5816]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5816]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
[6588]22using System;
[5816]23using System.Collections.Generic;
24using System.Linq;
[6612]25using HeuristicLab.Collections;
[5816]26using HeuristicLab.Common;
27using HeuristicLab.Core;
[6588]28using HeuristicLab.Data;
[5816]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")]
[6666]37  [Creatable("Data Analysis - Ensembles")]
[8724]38  public sealed class RegressionEnsembleSolution : RegressionSolutionBase, IRegressionEnsembleSolution {
[8167]39    private readonly Dictionary<int, double> trainingEvaluationCache = new Dictionary<int, double>();
40    private readonly Dictionary<int, double> testEvaluationCache = new Dictionary<int, double>();
[8724]41    private readonly Dictionary<int, double> evaluationCache = new Dictionary<int, double>();
[8151]42
[5816]43    public new IRegressionEnsembleModel Model {
44      get { return (IRegressionEnsembleModel)base.Model; }
45    }
46
[6666]47    public new RegressionEnsembleProblemData ProblemData {
48      get { return (RegressionEnsembleProblemData)base.ProblemData; }
49      set { base.ProblemData = value; }
50    }
51
[6612]52    private readonly ItemCollection<IRegressionSolution> regressionSolutions;
53    public IItemCollection<IRegressionSolution> RegressionSolutions {
54      get { return regressionSolutions; }
55    }
56
[5816]57    [Storable]
[8152]58    private readonly Dictionary<IRegressionModel, IntRange> trainingPartitions;
[5816]59    [Storable]
[8152]60    private readonly Dictionary<IRegressionModel, IntRange> testPartitions;
[5816]61
62    [StorableConstructor]
[6612]63    private RegressionEnsembleSolution(bool deserializing)
64      : base(deserializing) {
65      regressionSolutions = new ItemCollection<IRegressionSolution>();
66    }
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      foreach (var model in Model.Models) {
[6982]70        IRegressionProblemData problemData = (IRegressionProblemData)ProblemData.Clone();
[6612]71        problemData.TrainingPartition.Start = trainingPartitions[model].Start;
72        problemData.TrainingPartition.End = trainingPartitions[model].End;
73        problemData.TestPartition.Start = testPartitions[model].Start;
74        problemData.TestPartition.End = testPartitions[model].End;
75
76        regressionSolutions.Add(model.CreateRegressionSolution(problemData));
77      }
78      RegisterRegressionSolutionsEventHandler();
79    }
80
[6592]81    private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
[5816]82      : base(original, cloner) {
[6239]83      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
84      testPartitions = new Dictionary<IRegressionModel, IntRange>();
[6302]85      foreach (var pair in original.trainingPartitions) {
86        trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
[6239]87      }
[6302]88      foreach (var pair in original.testPartitions) {
89        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
90      }
[6612]91
[8174]92      trainingEvaluationCache = new Dictionary<int, double>(original.ProblemData.TrainingIndices.Count());
93      testEvaluationCache = new Dictionary<int, double>(original.ProblemData.TestIndices.Count());
94
[6612]95      regressionSolutions = cloner.Clone(original.regressionSolutions);
96      RegisterRegressionSolutionsEventHandler();
[5816]97    }
[6239]98
[6666]99    public RegressionEnsembleSolution()
100      : base(new RegressionEnsembleModel(), RegressionEnsembleProblemData.EmptyProblemData) {
101      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
102      testPartitions = new Dictionary<IRegressionModel, IntRange>();
103      regressionSolutions = new ItemCollection<IRegressionSolution>();
104
105      RegisterRegressionSolutionsEventHandler();
106    }
107
[7738]108    public RegressionEnsembleSolution(IRegressionProblemData problemData)
109      : this(Enumerable.Empty<IRegressionModel>(), problemData) {
110    }
111
[5816]112    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
[6612]113      : this(models, problemData,
114             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
115             models.Select(m => (IntRange)problemData.TestPartition.Clone())
116      ) { }
[5816]117
118    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
[6612]119      : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
[5816]120      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
121      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
[6612]122      this.regressionSolutions = new ItemCollection<IRegressionSolution>();
123
124      List<IRegressionSolution> solutions = new List<IRegressionSolution>();
125      var modelEnumerator = models.GetEnumerator();
126      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
127      var testPartitionEnumerator = testPartitions.GetEnumerator();
128
129      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
130        var p = (IRegressionProblemData)problemData.Clone();
131        p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
132        p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
133        p.TestPartition.Start = testPartitionEnumerator.Current.Start;
134        p.TestPartition.End = testPartitionEnumerator.Current.End;
135
136        solutions.Add(modelEnumerator.Current.CreateRegressionSolution(p));
137      }
138      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
139        throw new ArgumentException();
140      }
141
[8174]142      trainingEvaluationCache = new Dictionary<int, double>(problemData.TrainingIndices.Count());
143      testEvaluationCache = new Dictionary<int, double>(problemData.TestIndices.Count());
144
[6612]145      RegisterRegressionSolutionsEventHandler();
146      regressionSolutions.AddRange(solutions);
[5816]147    }
148
149    public override IDeepCloneable Clone(Cloner cloner) {
150      return new RegressionEnsembleSolution(this, cloner);
151    }
[6612]152    private void RegisterRegressionSolutionsEventHandler() {
153      regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
154      regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
155      regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
156    }
[5816]157
[6612]158    #region Evaluation
[8724]159    public override IEnumerable<double> EstimatedValues {
160      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
161    }
162
[5816]163    public override IEnumerable<double> EstimatedTrainingValues {
[8152]164      get {
165        var rows = ProblemData.TrainingIndices;
[8167]166        var rowsToEvaluate = rows.Except(trainingEvaluationCache.Keys);
[8152]167        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
168        var valuesEnumerator = GetEstimatedValues(rowsToEvaluate, (r, m) => RowIsTrainingForModel(r, m) && !RowIsTestForModel(r, m)).GetEnumerator();
169
170        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
[8167]171          trainingEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
[8152]172        }
173
[8167]174        return rows.Select(row => trainingEvaluationCache[row]);
[8152]175      }
[5816]176    }
177
178    public override IEnumerable<double> EstimatedTestValues {
[8152]179      get {
180        var rows = ProblemData.TestIndices;
[8167]181        var rowsToEvaluate = rows.Except(testEvaluationCache.Keys);
[8152]182        var rowsEnumerator = rowsToEvaluate.GetEnumerator();
183        var valuesEnumerator = GetEstimatedValues(rowsToEvaluate, RowIsTestForModel).GetEnumerator();
184
185        while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
[8167]186          testEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
[8152]187        }
188
[8167]189        return rows.Select(row => testEvaluationCache[row]);
[8152]190      }
[8151]191    }
[5816]192
[8151]193    private IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows, Func<int, IRegressionModel, bool> modelSelectionPredicate) {
194      var estimatedValuesEnumerators = (from model in Model.Models
195                                        select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
196                                       .ToList();
197      var rowsEnumerator = rows.GetEnumerator();
198      // aggregate to make sure that MoveNext is called for all enumerators
199      while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
200        int currentRow = rowsEnumerator.Current;
[5816]201
[8151]202        var selectedEnumerators = from pair in estimatedValuesEnumerators
203                                  where modelSelectionPredicate(currentRow, pair.Model)
204                                  select pair.EstimatedValuesEnumerator;
205
206        yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
[5816]207      }
208    }
209
[6254]210    private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
211      return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
212              (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
213    }
214
215    private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
216      return testPartitions == null || !testPartitions.ContainsKey(model) ||
217              (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
218    }
219
[5816]220    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
[8167]221      var rowsToEvaluate = rows.Except(evaluationCache.Keys);
[8152]222      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
223      var valuesEnumerator = (from xs in GetEstimatedValueVectors(ProblemData.Dataset, rowsToEvaluate)
224                              select AggregateEstimatedValues(xs))
225                             .GetEnumerator();
226
227      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
[8167]228        evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
[8152]229      }
230
[8167]231      return rows.Select(row => evaluationCache[row]);
[5816]232    }
233
234    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
[6982]235      if (!Model.Models.Any()) yield break;
[5816]236      var estimatedValuesEnumerators = (from model in Model.Models
237                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
238                                       .ToList();
239
240      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
241        yield return from enumerator in estimatedValuesEnumerators
242                     select enumerator.Current;
243      }
244    }
245
246    private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
[6238]247      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
[6254]248    }
[6612]249    #endregion
[6520]250
[6666]251    protected override void OnProblemDataChanged() {
[8167]252      trainingEvaluationCache.Clear();
253      testEvaluationCache.Clear();
254      evaluationCache.Clear();
[6666]255      IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
256                                                                     ProblemData.AllowedInputVariables,
257                                                                     ProblemData.TargetVariable);
258      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
259      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
260      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
261      problemData.TestPartition.End = ProblemData.TestPartition.End;
262
263      foreach (var solution in RegressionSolutions) {
264        if (solution is RegressionEnsembleSolution)
265          solution.ProblemData = ProblemData;
266        else
267          solution.ProblemData = problemData;
268      }
269      foreach (var trainingPartition in trainingPartitions.Values) {
270        trainingPartition.Start = ProblemData.TrainingPartition.Start;
271        trainingPartition.End = ProblemData.TrainingPartition.End;
272      }
273      foreach (var testPartition in testPartitions.Values) {
274        testPartition.Start = ProblemData.TestPartition.Start;
275        testPartition.End = ProblemData.TestPartition.End;
276      }
277
278      base.OnProblemDataChanged();
279    }
280
[6612]281    public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
282      regressionSolutions.AddRange(solutions);
[8152]283
[8167]284      trainingEvaluationCache.Clear();
285      testEvaluationCache.Clear();
286      evaluationCache.Clear();
[6612]287    }
288    public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
289      regressionSolutions.RemoveRange(solutions);
[8152]290
[8167]291      trainingEvaluationCache.Clear();
292      testEvaluationCache.Clear();
293      evaluationCache.Clear();
[6612]294    }
[6520]295
[6612]296    private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
297      foreach (var solution in e.Items) AddRegressionSolution(solution);
[6520]298      RecalculateResults();
299    }
[6612]300    private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
301      foreach (var solution in e.Items) RemoveRegressionSolution(solution);
302      RecalculateResults();
303    }
304    private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
305      foreach (var solution in e.OldItems) RemoveRegressionSolution(solution);
306      foreach (var solution in e.Items) AddRegressionSolution(solution);
307      RecalculateResults();
308    }
[6520]309
[6612]310    private void AddRegressionSolution(IRegressionSolution solution) {
311      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
312      Model.Add(solution.Model);
313      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
314      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
[8152]315
[8167]316      trainingEvaluationCache.Clear();
317      testEvaluationCache.Clear();
318      evaluationCache.Clear();
[6612]319    }
[6520]320
[6612]321    private void RemoveRegressionSolution(IRegressionSolution solution) {
322      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
323      Model.Remove(solution.Model);
324      trainingPartitions.Remove(solution.Model);
325      testPartitions.Remove(solution.Model);
[8152]326
[8167]327      trainingEvaluationCache.Clear();
328      testEvaluationCache.Clear();
329      evaluationCache.Clear();
[6520]330    }
[5816]331  }
332}
Note: See TracBrowser for help on using the repository browser.