[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 |
|
---|
[6588] | 22 | using System;
|
---|
[5816] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[6612] | 25 | using HeuristicLab.Collections;
|
---|
[5816] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[6588] | 28 | using HeuristicLab.Data;
|
---|
[5816] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Represents regression solutions that contain an ensemble of multiple regression models
|
---|
| 34 | /// </summary>
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [Item("Regression Ensemble Solution", "A regression solution that contains an ensemble of multiple regression models")]
|
---|
[6666] | 37 | [Creatable("Data Analysis - Ensembles")]
|
---|
[6592] | 38 | public sealed class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution {
|
---|
[5816] | 39 | public new IRegressionEnsembleModel Model {
|
---|
| 40 | get { return (IRegressionEnsembleModel)base.Model; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[6666] | 43 | public new RegressionEnsembleProblemData ProblemData {
|
---|
| 44 | get { return (RegressionEnsembleProblemData)base.ProblemData; }
|
---|
| 45 | set { base.ProblemData = value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[6612] | 48 | private readonly ItemCollection<IRegressionSolution> regressionSolutions;
|
---|
| 49 | public IItemCollection<IRegressionSolution> RegressionSolutions {
|
---|
| 50 | get { return regressionSolutions; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[5816] | 53 | [Storable]
|
---|
| 54 | private Dictionary<IRegressionModel, IntRange> trainingPartitions;
|
---|
| 55 | [Storable]
|
---|
| 56 | private Dictionary<IRegressionModel, IntRange> testPartitions;
|
---|
| 57 |
|
---|
| 58 | [StorableConstructor]
|
---|
[6612] | 59 | private RegressionEnsembleSolution(bool deserializing)
|
---|
| 60 | : base(deserializing) {
|
---|
| 61 | regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 62 | }
|
---|
| 63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 64 | private void AfterDeserialization() {
|
---|
| 65 | foreach (var model in Model.Models) {
|
---|
[6982] | 66 | IRegressionProblemData problemData = (IRegressionProblemData)ProblemData.Clone();
|
---|
[6612] | 67 | problemData.TrainingPartition.Start = trainingPartitions[model].Start;
|
---|
| 68 | problemData.TrainingPartition.End = trainingPartitions[model].End;
|
---|
| 69 | problemData.TestPartition.Start = testPartitions[model].Start;
|
---|
| 70 | problemData.TestPartition.End = testPartitions[model].End;
|
---|
| 71 |
|
---|
| 72 | regressionSolutions.Add(model.CreateRegressionSolution(problemData));
|
---|
| 73 | }
|
---|
| 74 | RegisterRegressionSolutionsEventHandler();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[6592] | 77 | private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
|
---|
[5816] | 78 | : base(original, cloner) {
|
---|
[6239] | 79 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 80 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
[6302] | 81 | foreach (var pair in original.trainingPartitions) {
|
---|
| 82 | trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
[6239] | 83 | }
|
---|
[6302] | 84 | foreach (var pair in original.testPartitions) {
|
---|
| 85 | testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
| 86 | }
|
---|
[6612] | 87 |
|
---|
| 88 | regressionSolutions = cloner.Clone(original.regressionSolutions);
|
---|
| 89 | RegisterRegressionSolutionsEventHandler();
|
---|
[5816] | 90 | }
|
---|
[6239] | 91 |
|
---|
[6666] | 92 | public RegressionEnsembleSolution()
|
---|
| 93 | : base(new RegressionEnsembleModel(), RegressionEnsembleProblemData.EmptyProblemData) {
|
---|
| 94 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 95 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 96 | regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 97 |
|
---|
| 98 | RegisterRegressionSolutionsEventHandler();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[5816] | 101 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
|
---|
[6612] | 102 | : this(models, problemData,
|
---|
| 103 | models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
|
---|
| 104 | models.Select(m => (IntRange)problemData.TestPartition.Clone())
|
---|
| 105 | ) { }
|
---|
[5816] | 106 |
|
---|
| 107 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
[6612] | 108 | : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
|
---|
[5816] | 109 | this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 110 | this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
[6612] | 111 | this.regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 112 |
|
---|
| 113 | List<IRegressionSolution> solutions = new List<IRegressionSolution>();
|
---|
| 114 | var modelEnumerator = models.GetEnumerator();
|
---|
| 115 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
| 116 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
| 117 |
|
---|
| 118 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
| 119 | var p = (IRegressionProblemData)problemData.Clone();
|
---|
| 120 | p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
|
---|
| 121 | p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
|
---|
| 122 | p.TestPartition.Start = testPartitionEnumerator.Current.Start;
|
---|
| 123 | p.TestPartition.End = testPartitionEnumerator.Current.End;
|
---|
| 124 |
|
---|
| 125 | solutions.Add(modelEnumerator.Current.CreateRegressionSolution(p));
|
---|
| 126 | }
|
---|
| 127 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
| 128 | throw new ArgumentException();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | RegisterRegressionSolutionsEventHandler();
|
---|
| 132 | regressionSolutions.AddRange(solutions);
|
---|
[5816] | 133 | }
|
---|
| 134 |
|
---|
| 135 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 136 | return new RegressionEnsembleSolution(this, cloner);
|
---|
| 137 | }
|
---|
[6612] | 138 | private void RegisterRegressionSolutionsEventHandler() {
|
---|
| 139 | regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
|
---|
| 140 | regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
|
---|
| 141 | regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
|
---|
| 142 | }
|
---|
[5816] | 143 |
|
---|
[6588] | 144 | protected override void RecalculateResults() {
|
---|
| 145 | CalculateResults();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[6612] | 148 | #region Evaluation
|
---|
[5816] | 149 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
| 150 | get {
|
---|
[6238] | 151 | var rows = ProblemData.TrainingIndizes;
|
---|
[5816] | 152 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
[6184] | 153 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
[5816] | 154 | .ToList();
|
---|
[6184] | 155 | var rowsEnumerator = rows.GetEnumerator();
|
---|
[6238] | 156 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
[6184] | 157 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
[5816] | 158 | int currentRow = rowsEnumerator.Current;
|
---|
| 159 |
|
---|
| 160 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 161 | where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
|
---|
[5816] | 162 | select pair.EstimatedValuesEnumerator;
|
---|
| 163 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public override IEnumerable<double> EstimatedTestValues {
|
---|
| 169 | get {
|
---|
[6238] | 170 | var rows = ProblemData.TestIndizes;
|
---|
[5816] | 171 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
[6238] | 172 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
[5816] | 173 | .ToList();
|
---|
| 174 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
[6238] | 175 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
[6184] | 176 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
[5816] | 177 | int currentRow = rowsEnumerator.Current;
|
---|
| 178 |
|
---|
| 179 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 180 | where RowIsTestForModel(currentRow, pair.Model)
|
---|
[5816] | 181 | select pair.EstimatedValuesEnumerator;
|
---|
| 182 |
|
---|
| 183 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[6254] | 188 | private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
|
---|
| 189 | return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
|
---|
| 190 | (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
|
---|
| 194 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
| 195 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[5816] | 198 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
| 199 | return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
|
---|
| 200 | select AggregateEstimatedValues(xs);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
[6982] | 204 | if (!Model.Models.Any()) yield break;
|
---|
[5816] | 205 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
| 206 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
| 207 | .ToList();
|
---|
| 208 |
|
---|
| 209 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
| 210 | yield return from enumerator in estimatedValuesEnumerators
|
---|
| 211 | select enumerator.Current;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
|
---|
[6238] | 216 | return estimatedValues.DefaultIfEmpty(double.NaN).Average();
|
---|
[6254] | 217 | }
|
---|
[6612] | 218 | #endregion
|
---|
[6520] | 219 |
|
---|
[6666] | 220 | protected override void OnProblemDataChanged() {
|
---|
| 221 | IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
|
---|
| 222 | ProblemData.AllowedInputVariables,
|
---|
| 223 | ProblemData.TargetVariable);
|
---|
| 224 | problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 225 | problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 226 | problemData.TestPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 227 | problemData.TestPartition.End = ProblemData.TestPartition.End;
|
---|
| 228 |
|
---|
| 229 | foreach (var solution in RegressionSolutions) {
|
---|
| 230 | if (solution is RegressionEnsembleSolution)
|
---|
| 231 | solution.ProblemData = ProblemData;
|
---|
| 232 | else
|
---|
| 233 | solution.ProblemData = problemData;
|
---|
| 234 | }
|
---|
| 235 | foreach (var trainingPartition in trainingPartitions.Values) {
|
---|
| 236 | trainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 237 | trainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 238 | }
|
---|
| 239 | foreach (var testPartition in testPartitions.Values) {
|
---|
| 240 | testPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 241 | testPartition.End = ProblemData.TestPartition.End;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | base.OnProblemDataChanged();
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[6612] | 247 | public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 248 | regressionSolutions.AddRange(solutions);
|
---|
| 249 | }
|
---|
| 250 | public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 251 | regressionSolutions.RemoveRange(solutions);
|
---|
| 252 | }
|
---|
[6520] | 253 |
|
---|
[6612] | 254 | private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 255 | foreach (var solution in e.Items) AddRegressionSolution(solution);
|
---|
[6520] | 256 | RecalculateResults();
|
---|
| 257 | }
|
---|
[6612] | 258 | private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 259 | foreach (var solution in e.Items) RemoveRegressionSolution(solution);
|
---|
| 260 | RecalculateResults();
|
---|
| 261 | }
|
---|
| 262 | private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 263 | foreach (var solution in e.OldItems) RemoveRegressionSolution(solution);
|
---|
| 264 | foreach (var solution in e.Items) AddRegressionSolution(solution);
|
---|
| 265 | RecalculateResults();
|
---|
| 266 | }
|
---|
[6520] | 267 |
|
---|
[6612] | 268 | private void AddRegressionSolution(IRegressionSolution solution) {
|
---|
| 269 | if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 270 | Model.Add(solution.Model);
|
---|
| 271 | trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
|
---|
| 272 | testPartitions[solution.Model] = solution.ProblemData.TestPartition;
|
---|
| 273 | }
|
---|
[6520] | 274 |
|
---|
[6612] | 275 | private void RemoveRegressionSolution(IRegressionSolution solution) {
|
---|
| 276 | if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 277 | Model.Remove(solution.Model);
|
---|
| 278 | trainingPartitions.Remove(solution.Model);
|
---|
| 279 | testPartitions.Remove(solution.Model);
|
---|
[6520] | 280 | }
|
---|
[5816] | 281 | }
|
---|
| 282 | }
|
---|