[5816] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5816] | 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 |
|
---|
[7738] | 101 | public RegressionEnsembleSolution(IRegressionProblemData problemData)
|
---|
| 102 | : this(Enumerable.Empty<IRegressionModel>(), problemData) {
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[5816] | 105 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
|
---|
[6612] | 106 | : this(models, problemData,
|
---|
| 107 | models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
|
---|
| 108 | models.Select(m => (IntRange)problemData.TestPartition.Clone())
|
---|
| 109 | ) { }
|
---|
[5816] | 110 |
|
---|
| 111 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
[6612] | 112 | : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
|
---|
[5816] | 113 | this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 114 | this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
[6612] | 115 | this.regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 116 |
|
---|
| 117 | List<IRegressionSolution> solutions = new List<IRegressionSolution>();
|
---|
| 118 | var modelEnumerator = models.GetEnumerator();
|
---|
| 119 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
| 120 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
| 121 |
|
---|
| 122 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
| 123 | var p = (IRegressionProblemData)problemData.Clone();
|
---|
| 124 | p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
|
---|
| 125 | p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
|
---|
| 126 | p.TestPartition.Start = testPartitionEnumerator.Current.Start;
|
---|
| 127 | p.TestPartition.End = testPartitionEnumerator.Current.End;
|
---|
| 128 |
|
---|
| 129 | solutions.Add(modelEnumerator.Current.CreateRegressionSolution(p));
|
---|
| 130 | }
|
---|
| 131 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
| 132 | throw new ArgumentException();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | RegisterRegressionSolutionsEventHandler();
|
---|
| 136 | regressionSolutions.AddRange(solutions);
|
---|
[5816] | 137 | }
|
---|
| 138 |
|
---|
| 139 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 140 | return new RegressionEnsembleSolution(this, cloner);
|
---|
| 141 | }
|
---|
[6612] | 142 | private void RegisterRegressionSolutionsEventHandler() {
|
---|
| 143 | regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
|
---|
| 144 | regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
|
---|
| 145 | regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
|
---|
| 146 | }
|
---|
[5816] | 147 |
|
---|
[6588] | 148 | protected override void RecalculateResults() {
|
---|
| 149 | CalculateResults();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[6612] | 152 | #region Evaluation
|
---|
[5816] | 153 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
| 154 | get {
|
---|
[6238] | 155 | var rows = ProblemData.TrainingIndizes;
|
---|
[5816] | 156 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
[6184] | 157 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
[5816] | 158 | .ToList();
|
---|
[6184] | 159 | var rowsEnumerator = rows.GetEnumerator();
|
---|
[6238] | 160 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
[6184] | 161 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
[5816] | 162 | int currentRow = rowsEnumerator.Current;
|
---|
| 163 |
|
---|
| 164 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 165 | where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
|
---|
[5816] | 166 | select pair.EstimatedValuesEnumerator;
|
---|
| 167 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | public override IEnumerable<double> EstimatedTestValues {
|
---|
| 173 | get {
|
---|
[6238] | 174 | var rows = ProblemData.TestIndizes;
|
---|
[5816] | 175 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
[6238] | 176 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
[5816] | 177 | .ToList();
|
---|
| 178 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
[6238] | 179 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
[6184] | 180 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
[5816] | 181 | int currentRow = rowsEnumerator.Current;
|
---|
| 182 |
|
---|
| 183 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 184 | where RowIsTestForModel(currentRow, pair.Model)
|
---|
[5816] | 185 | select pair.EstimatedValuesEnumerator;
|
---|
| 186 |
|
---|
| 187 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[6254] | 192 | private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
|
---|
| 193 | return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
|
---|
| 194 | (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
|
---|
| 198 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
| 199 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[5816] | 202 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
| 203 | return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
|
---|
| 204 | select AggregateEstimatedValues(xs);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
[6982] | 208 | if (!Model.Models.Any()) yield break;
|
---|
[5816] | 209 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
| 210 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
| 211 | .ToList();
|
---|
| 212 |
|
---|
| 213 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
| 214 | yield return from enumerator in estimatedValuesEnumerators
|
---|
| 215 | select enumerator.Current;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
|
---|
[6238] | 220 | return estimatedValues.DefaultIfEmpty(double.NaN).Average();
|
---|
[6254] | 221 | }
|
---|
[6612] | 222 | #endregion
|
---|
[6520] | 223 |
|
---|
[6666] | 224 | protected override void OnProblemDataChanged() {
|
---|
| 225 | IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
|
---|
| 226 | ProblemData.AllowedInputVariables,
|
---|
| 227 | ProblemData.TargetVariable);
|
---|
| 228 | problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 229 | problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 230 | problemData.TestPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 231 | problemData.TestPartition.End = ProblemData.TestPartition.End;
|
---|
| 232 |
|
---|
| 233 | foreach (var solution in RegressionSolutions) {
|
---|
| 234 | if (solution is RegressionEnsembleSolution)
|
---|
| 235 | solution.ProblemData = ProblemData;
|
---|
| 236 | else
|
---|
| 237 | solution.ProblemData = problemData;
|
---|
| 238 | }
|
---|
| 239 | foreach (var trainingPartition in trainingPartitions.Values) {
|
---|
| 240 | trainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 241 | trainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 242 | }
|
---|
| 243 | foreach (var testPartition in testPartitions.Values) {
|
---|
| 244 | testPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 245 | testPartition.End = ProblemData.TestPartition.End;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | base.OnProblemDataChanged();
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[6612] | 251 | public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 252 | regressionSolutions.AddRange(solutions);
|
---|
| 253 | }
|
---|
| 254 | public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 255 | regressionSolutions.RemoveRange(solutions);
|
---|
| 256 | }
|
---|
[6520] | 257 |
|
---|
[6612] | 258 | private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 259 | foreach (var solution in e.Items) AddRegressionSolution(solution);
|
---|
[6520] | 260 | RecalculateResults();
|
---|
| 261 | }
|
---|
[6612] | 262 | private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 263 | foreach (var solution in e.Items) RemoveRegressionSolution(solution);
|
---|
| 264 | RecalculateResults();
|
---|
| 265 | }
|
---|
| 266 | private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 267 | foreach (var solution in e.OldItems) RemoveRegressionSolution(solution);
|
---|
| 268 | foreach (var solution in e.Items) AddRegressionSolution(solution);
|
---|
| 269 | RecalculateResults();
|
---|
| 270 | }
|
---|
[6520] | 271 |
|
---|
[6612] | 272 | private void AddRegressionSolution(IRegressionSolution solution) {
|
---|
| 273 | if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 274 | Model.Add(solution.Model);
|
---|
| 275 | trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
|
---|
| 276 | testPartitions[solution.Model] = solution.ProblemData.TestPartition;
|
---|
| 277 | }
|
---|
[6520] | 278 |
|
---|
[6612] | 279 | private void RemoveRegressionSolution(IRegressionSolution solution) {
|
---|
| 280 | if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 281 | Model.Remove(solution.Model);
|
---|
| 282 | trainingPartitions.Remove(solution.Model);
|
---|
| 283 | testPartitions.Remove(solution.Model);
|
---|
[6520] | 284 | }
|
---|
[5816] | 285 | }
|
---|
| 286 | }
|
---|