Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6239 was 6239, checked in by gkronber, 12 years ago

#1450: implemented support for ensemble solutions for classification.

File size: 7.7 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  /// <summary>
32  /// Represents regression solutions that contain an ensemble of multiple regression models
33  /// </summary>
34  [StorableClass]
35  [Item("Regression Ensemble Solution", "A regression solution that contains an ensemble of multiple regression models")]
36  // [Creatable("Data Analysis")]
37  public class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution {
38    public new IRegressionEnsembleModel Model {
39      get { return (IRegressionEnsembleModel)base.Model; }
40    }
41
42    [Storable]
43    private Dictionary<IRegressionModel, IntRange> trainingPartitions;
44    [Storable]
45    private Dictionary<IRegressionModel, IntRange> testPartitions;
46
47    [StorableConstructor]
48    protected RegressionEnsembleSolution(bool deserializing) : base(deserializing) { }
49    protected RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
50      : base(original, cloner) {
51      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
52      testPartitions = new Dictionary<IRegressionModel, IntRange>();
53      foreach (var model in Model.Models) {
54        trainingPartitions[model] = (IntRange)ProblemData.TrainingPartition.Clone();
55        testPartitions[model] = (IntRange)ProblemData.TestPartition.Clone();
56      }
57    }
58
59    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
60      : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
61      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
62      testPartitions = new Dictionary<IRegressionModel, IntRange>();
63      foreach (var model in models) {
64        trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
65        testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
66      }
67      RecalculateResults();
68    }
69
70    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
71      : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
72      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
73      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
74      var modelEnumerator = models.GetEnumerator();
75      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
76      var testPartitionEnumerator = testPartitions.GetEnumerator();
77      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
78        this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
79        this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
80      }
81      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
82        throw new ArgumentException();
83      }
84      RecalculateResults();
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new RegressionEnsembleSolution(this, cloner);
89    }
90
91    public override IEnumerable<double> EstimatedTrainingValues {
92      get {
93        var rows = ProblemData.TrainingIndizes;
94        var estimatedValuesEnumerators = (from model in Model.Models
95                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
96                                         .ToList();
97        var rowsEnumerator = rows.GetEnumerator();
98        // aggregate to make sure that MoveNext is called for all enumerators
99        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
100          int currentRow = rowsEnumerator.Current;
101
102          var selectedEnumerators = from pair in estimatedValuesEnumerators
103                                    where trainingPartitions == null || !trainingPartitions.ContainsKey(pair.Model) ||
104                                         (trainingPartitions[pair.Model].Start <= currentRow && currentRow < trainingPartitions[pair.Model].End)
105                                    select pair.EstimatedValuesEnumerator;
106          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
107        }
108      }
109    }
110
111    public override IEnumerable<double> EstimatedTestValues {
112      get {
113        var rows = ProblemData.TestIndizes;
114        var estimatedValuesEnumerators = (from model in Model.Models
115                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
116                                         .ToList();
117        var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
118        // aggregate to make sure that MoveNext is called for all enumerators
119        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
120          int currentRow = rowsEnumerator.Current;
121
122          var selectedEnumerators = from pair in estimatedValuesEnumerators
123                                    where testPartitions == null || !testPartitions.ContainsKey(pair.Model) ||
124                                      (testPartitions[pair.Model].Start <= currentRow && currentRow < testPartitions[pair.Model].End)
125                                    select pair.EstimatedValuesEnumerator;
126
127          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
128        }
129      }
130    }
131
132    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
133      return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
134             select AggregateEstimatedValues(xs);
135    }
136
137    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
138      var estimatedValuesEnumerators = (from model in Model.Models
139                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
140                                       .ToList();
141
142      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
143        yield return from enumerator in estimatedValuesEnumerators
144                     select enumerator.Current;
145      }
146    }
147
148    private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
149      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
150    }   
151  }
152}
Note: See TracBrowser for help on using the repository browser.