Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1450 adapted views for regression solution to work for ensembles of regression solutions as well.

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