Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs @ 5816

Last change on this file since 5816 was 5816, checked in by gkronber, 13 years ago

#1450 Added preliminary implementation for solution ensemble support.

File size: 8.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.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    }
52    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
53      : base(new RegressionEnsembleModel(models), problemData) {
54      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
55      testPartitions = new Dictionary<IRegressionModel, IntRange>();
56      foreach (var model in models) {
57        trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
58        testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
59      }
60      RecalculateResults();
61    }
62
63    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
64      : base(new RegressionEnsembleModel(models), problemData) {
65      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
66      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
67      var modelEnumerator = models.GetEnumerator();
68      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
69      var testPartitionEnumerator = testPartitions.GetEnumerator();
70      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
71        this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
72        this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
73      }
74      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
75        throw new ArgumentException();
76      }
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new RegressionEnsembleSolution(this, cloner);
81    }
82
83    public override IEnumerable<double> EstimatedTrainingValues {
84      get {
85        var estimatedValuesEnumerators = (from model in Model.Models
86                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndizes).GetEnumerator() })
87                                         .ToList();
88        var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
89        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.All(en => en.EstimatedValuesEnumerator.MoveNext())) {
90          int currentRow = rowsEnumerator.Current;
91
92          var selectedEnumerators = from pair in estimatedValuesEnumerators
93                                    where trainingPartitions == null || !trainingPartitions.ContainsKey(pair.Model) ||
94                                         (trainingPartitions[pair.Model].Start >= currentRow && trainingPartitions[pair.Model].End < currentRow)
95                                    select pair.EstimatedValuesEnumerator;
96          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
97        }
98      }
99    }
100
101    public override IEnumerable<double> EstimatedTestValues {
102      get {
103        var estimatedValuesEnumerators = (from model in Model.Models
104                                          select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndizes).GetEnumerator() })
105                                         .ToList();
106        var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
107        while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.All(en => en.EstimatedValuesEnumerator.MoveNext())) {
108          int currentRow = rowsEnumerator.Current;
109
110          var selectedEnumerators = from pair in estimatedValuesEnumerators
111                                    where testPartitions == null || !testPartitions.ContainsKey(pair.Model) ||
112                                      (testPartitions[pair.Model].Start >= currentRow && testPartitions[pair.Model].End < currentRow)
113                                    select pair.EstimatedValuesEnumerator;
114
115          yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
116        }
117      }
118    }
119
120    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
121      return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
122             select AggregateEstimatedValues(xs);
123    }
124
125    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
126      var estimatedValuesEnumerators = (from model in Model.Models
127                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
128                                       .ToList();
129
130      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
131        yield return from enumerator in estimatedValuesEnumerators
132                     select enumerator.Current;
133      }
134    }
135
136    private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
137      return estimatedValues.Average();
138    }
139
140    //[Storable]
141    //private string name;
142    //public string Name {
143    //  get {
144    //    return name;
145    //  }
146    //  set {
147    //    if (value != null && value != name) {
148    //      var cancelEventArgs = new CancelEventArgs<string>(value);
149    //      OnNameChanging(cancelEventArgs);
150    //      if (cancelEventArgs.Cancel == false) {
151    //        name = value;
152    //        OnNamedChanged(EventArgs.Empty);
153    //      }
154    //    }
155    //  }
156    //}
157
158    //public bool CanChangeName {
159    //  get { return true; }
160    //}
161
162    //[Storable]
163    //private string description;
164    //public string Description {
165    //  get {
166    //    return description;
167    //  }
168    //  set {
169    //    if (value != null && value != description) {
170    //      description = value;
171    //      OnDescriptionChanged(EventArgs.Empty);
172    //    }
173    //  }
174    //}
175
176    //public bool CanChangeDescription {
177    //  get { return true; }
178    //}
179
180    //#region events
181    //public event EventHandler<CancelEventArgs<string>> NameChanging;
182    //private void OnNameChanging(CancelEventArgs<string> cancelEventArgs) {
183    //  var listener = NameChanging;
184    //  if (listener != null) listener(this, cancelEventArgs);
185    //}
186
187    //public event EventHandler NameChanged;
188    //private void OnNamedChanged(EventArgs e) {
189    //  var listener = NameChanged;
190    //  if (listener != null) listener(this, e);
191    //}
192
193    //public event EventHandler DescriptionChanged;
194    //private void OnDescriptionChanged(EventArgs e) {
195    //  var listener = DescriptionChanged;
196    //  if (listener != null) listener(this, e);
197    //}
198    // #endregion
199  }
200}
Note: See TracBrowser for help on using the repository browser.