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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
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 sealed 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 | private RegressionEnsembleSolution(bool deserializing) : base(deserializing) { }
|
---|
49 | private 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 | AddModelsAndPartitions(models,
|
---|
67 | from m in models select (IntRange)problemData.TrainingPartition.Clone(),
|
---|
68 | from m in models select (IntRange)problemData.TestPartition.Clone());
|
---|
69 | RecalculateResults();
|
---|
70 | }
|
---|
71 |
|
---|
72 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
73 | : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
|
---|
74 | this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
75 | this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
76 | AddModelsAndPartitions(models, trainingPartitions, testPartitions);
|
---|
77 | RecalculateResults();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
81 | return new RegressionEnsembleSolution(this, cloner);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void RecalculateResults() {
|
---|
85 | CalculateResults();
|
---|
86 | }
|
---|
87 |
|
---|
88 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
89 | get {
|
---|
90 | var rows = ProblemData.TrainingIndizes;
|
---|
91 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
92 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
93 | .ToList();
|
---|
94 | var rowsEnumerator = rows.GetEnumerator();
|
---|
95 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
96 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
97 | int currentRow = rowsEnumerator.Current;
|
---|
98 |
|
---|
99 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
100 | where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
|
---|
101 | select pair.EstimatedValuesEnumerator;
|
---|
102 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public override IEnumerable<double> EstimatedTestValues {
|
---|
108 | get {
|
---|
109 | var rows = ProblemData.TestIndizes;
|
---|
110 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
111 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
112 | .ToList();
|
---|
113 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
114 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
115 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
116 | int currentRow = rowsEnumerator.Current;
|
---|
117 |
|
---|
118 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
119 | where RowIsTestForModel(currentRow, pair.Model)
|
---|
120 | select pair.EstimatedValuesEnumerator;
|
---|
121 |
|
---|
122 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
|
---|
128 | return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
|
---|
129 | (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
|
---|
130 | }
|
---|
131 |
|
---|
132 | private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
|
---|
133 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
134 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
138 | return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
|
---|
139 | select AggregateEstimatedValues(xs);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
143 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
144 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
145 | .ToList();
|
---|
146 |
|
---|
147 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
148 | yield return from enumerator in estimatedValuesEnumerators
|
---|
149 | select enumerator.Current;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
|
---|
154 | return estimatedValues.DefaultIfEmpty(double.NaN).Average();
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | public void AddModelsAndPartitions(IEnumerable<IRegressionSolution> solutions) {
|
---|
159 | foreach (var solution in solutions) {
|
---|
160 | var ensembleSolution = solution as RegressionEnsembleSolution;
|
---|
161 | if (ensembleSolution != null) {
|
---|
162 | var data = from m in ensembleSolution.Model.Models
|
---|
163 | let train = ensembleSolution.trainingPartitions[m]
|
---|
164 | let test = ensembleSolution.testPartitions[m]
|
---|
165 | select new { m, train, test };
|
---|
166 |
|
---|
167 | foreach (var d in data) {
|
---|
168 | Model.Add(d.m);
|
---|
169 | trainingPartitions[d.m] = (IntRange)d.train.Clone();
|
---|
170 | testPartitions[d.m] = (IntRange)d.test.Clone();
|
---|
171 | }
|
---|
172 | } else {
|
---|
173 | Model.Add(solution.Model);
|
---|
174 | trainingPartitions[solution.Model] = (IntRange)solution.ProblemData.TrainingPartition.Clone();
|
---|
175 | testPartitions[solution.Model] = (IntRange)solution.ProblemData.TestPartition.Clone();
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | RecalculateResults();
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void AddModelsAndPartitions(IEnumerable<IRegressionModel> models, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions) {
|
---|
183 | var modelEnumerator = models.GetEnumerator();
|
---|
184 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
185 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
186 |
|
---|
187 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
188 | this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
|
---|
189 | this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
|
---|
190 | }
|
---|
191 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
192 | throw new ArgumentException();
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|