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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 |
|
---|
30 | namespace 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 pair in original.trainingPartitions) {
|
---|
54 | trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
55 | }
|
---|
56 | foreach (var pair in original.testPartitions) {
|
---|
57 | testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
58 | }
|
---|
59 | RecalculateResults();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
|
---|
63 | : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
|
---|
64 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
65 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
66 | foreach (var model in models) {
|
---|
67 | trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
|
---|
68 | testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
|
---|
69 | }
|
---|
70 | RecalculateResults();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
74 | : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
|
---|
75 | this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
76 | this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
77 | var modelEnumerator = models.GetEnumerator();
|
---|
78 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
79 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
80 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
81 | this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
|
---|
82 | this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
|
---|
83 | }
|
---|
84 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
85 | throw new ArgumentException();
|
---|
86 | }
|
---|
87 | RecalculateResults();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
91 | return new RegressionEnsembleSolution(this, cloner);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
95 | get {
|
---|
96 | var rows = ProblemData.TrainingIndizes;
|
---|
97 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
98 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
99 | .ToList();
|
---|
100 | var rowsEnumerator = rows.GetEnumerator();
|
---|
101 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
102 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
103 | int currentRow = rowsEnumerator.Current;
|
---|
104 |
|
---|
105 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
106 | where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
|
---|
107 | select pair.EstimatedValuesEnumerator;
|
---|
108 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override IEnumerable<double> EstimatedTestValues {
|
---|
114 | get {
|
---|
115 | var rows = ProblemData.TestIndizes;
|
---|
116 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
117 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
118 | .ToList();
|
---|
119 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
120 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
121 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
122 | int currentRow = rowsEnumerator.Current;
|
---|
123 |
|
---|
124 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
125 | where RowIsTestForModel(currentRow, pair.Model)
|
---|
126 | select pair.EstimatedValuesEnumerator;
|
---|
127 |
|
---|
128 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
|
---|
134 | return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
|
---|
135 | (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
|
---|
136 | }
|
---|
137 |
|
---|
138 | private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
|
---|
139 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
140 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
144 | return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
|
---|
145 | select AggregateEstimatedValues(xs);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
149 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
150 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
151 | .ToList();
|
---|
152 |
|
---|
153 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
154 | yield return from enumerator in estimatedValuesEnumerators
|
---|
155 | select enumerator.Current;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
|
---|
160 | return estimatedValues.DefaultIfEmpty(double.NaN).Average();
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|