[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")]
|
---|
| 37 | // [Creatable("Data Analysis")]
|
---|
[6592] | 38 | public sealed class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution {
|
---|
[5816] | 39 | public new IRegressionEnsembleModel Model {
|
---|
| 40 | get { return (IRegressionEnsembleModel)base.Model; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[6612] | 43 | private readonly ItemCollection<IRegressionSolution> regressionSolutions;
|
---|
| 44 | public IItemCollection<IRegressionSolution> RegressionSolutions {
|
---|
| 45 | get { return regressionSolutions; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[5816] | 48 | [Storable]
|
---|
| 49 | private Dictionary<IRegressionModel, IntRange> trainingPartitions;
|
---|
| 50 | [Storable]
|
---|
| 51 | private Dictionary<IRegressionModel, IntRange> testPartitions;
|
---|
| 52 |
|
---|
| 53 | [StorableConstructor]
|
---|
[6612] | 54 | private RegressionEnsembleSolution(bool deserializing)
|
---|
| 55 | : base(deserializing) {
|
---|
| 56 | regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 57 | }
|
---|
| 58 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 59 | private void AfterDeserialization() {
|
---|
| 60 | foreach (var model in Model.Models) {
|
---|
| 61 | IRegressionProblemData problemData = (IRegressionProblemData)ProblemData.Clone();
|
---|
| 62 | problemData.TrainingPartition.Start = trainingPartitions[model].Start;
|
---|
| 63 | problemData.TrainingPartition.End = trainingPartitions[model].End;
|
---|
| 64 | problemData.TestPartition.Start = testPartitions[model].Start;
|
---|
| 65 | problemData.TestPartition.End = testPartitions[model].End;
|
---|
| 66 |
|
---|
| 67 | regressionSolutions.Add(model.CreateRegressionSolution(problemData));
|
---|
| 68 | }
|
---|
| 69 | RegisterRegressionSolutionsEventHandler();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[6592] | 72 | private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
|
---|
[5816] | 73 | : base(original, cloner) {
|
---|
[6239] | 74 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 75 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
[6302] | 76 | foreach (var pair in original.trainingPartitions) {
|
---|
| 77 | trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
[6239] | 78 | }
|
---|
[6302] | 79 | foreach (var pair in original.testPartitions) {
|
---|
| 80 | testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
| 81 | }
|
---|
[6612] | 82 |
|
---|
| 83 | regressionSolutions = cloner.Clone(original.regressionSolutions);
|
---|
| 84 | RegisterRegressionSolutionsEventHandler();
|
---|
[5816] | 85 | }
|
---|
[6239] | 86 |
|
---|
[5816] | 87 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData)
|
---|
[6612] | 88 | : this(models, problemData,
|
---|
| 89 | models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
|
---|
| 90 | models.Select(m => (IntRange)problemData.TestPartition.Clone())
|
---|
| 91 | ) { }
|
---|
[5816] | 92 |
|
---|
| 93 | public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
[6612] | 94 | : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
|
---|
[5816] | 95 | this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 96 | this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
[6612] | 97 | this.regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 98 |
|
---|
| 99 | List<IRegressionSolution> solutions = new List<IRegressionSolution>();
|
---|
| 100 | var modelEnumerator = models.GetEnumerator();
|
---|
| 101 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
| 102 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
| 103 |
|
---|
| 104 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
| 105 | var p = (IRegressionProblemData)problemData.Clone();
|
---|
| 106 | p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
|
---|
| 107 | p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
|
---|
| 108 | p.TestPartition.Start = testPartitionEnumerator.Current.Start;
|
---|
| 109 | p.TestPartition.End = testPartitionEnumerator.Current.End;
|
---|
| 110 |
|
---|
| 111 | solutions.Add(modelEnumerator.Current.CreateRegressionSolution(p));
|
---|
| 112 | }
|
---|
| 113 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
| 114 | throw new ArgumentException();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | RegisterRegressionSolutionsEventHandler();
|
---|
| 118 | regressionSolutions.AddRange(solutions);
|
---|
[5816] | 119 | }
|
---|
| 120 |
|
---|
| 121 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 122 | return new RegressionEnsembleSolution(this, cloner);
|
---|
| 123 | }
|
---|
[6612] | 124 | private void RegisterRegressionSolutionsEventHandler() {
|
---|
| 125 | regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
|
---|
| 126 | regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
|
---|
| 127 | regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
|
---|
| 128 | }
|
---|
[5816] | 129 |
|
---|
[6588] | 130 | protected override void RecalculateResults() {
|
---|
| 131 | CalculateResults();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[6612] | 134 | #region Evaluation
|
---|
[5816] | 135 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
| 136 | get {
|
---|
[6238] | 137 | var rows = ProblemData.TrainingIndizes;
|
---|
[5816] | 138 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
[6184] | 139 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
[5816] | 140 | .ToList();
|
---|
[6184] | 141 | var rowsEnumerator = rows.GetEnumerator();
|
---|
[6238] | 142 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
[6184] | 143 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
[5816] | 144 | int currentRow = rowsEnumerator.Current;
|
---|
| 145 |
|
---|
| 146 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 147 | where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
|
---|
[5816] | 148 | select pair.EstimatedValuesEnumerator;
|
---|
| 149 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public override IEnumerable<double> EstimatedTestValues {
|
---|
| 155 | get {
|
---|
[6238] | 156 | var rows = ProblemData.TestIndizes;
|
---|
[5816] | 157 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
[6238] | 158 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
[5816] | 159 | .ToList();
|
---|
| 160 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
[6238] | 161 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
[6184] | 162 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
[5816] | 163 | int currentRow = rowsEnumerator.Current;
|
---|
| 164 |
|
---|
| 165 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 166 | where RowIsTestForModel(currentRow, pair.Model)
|
---|
[5816] | 167 | select pair.EstimatedValuesEnumerator;
|
---|
| 168 |
|
---|
| 169 | yield return AggregateEstimatedValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[6254] | 174 | private bool RowIsTrainingForModel(int currentRow, IRegressionModel model) {
|
---|
| 175 | return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
|
---|
| 176 | (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
|
---|
| 180 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
| 181 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[5816] | 184 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
| 185 | return from xs in GetEstimatedValueVectors(ProblemData.Dataset, rows)
|
---|
| 186 | select AggregateEstimatedValues(xs);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
| 190 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
| 191 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
| 192 | .ToList();
|
---|
| 193 |
|
---|
| 194 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
| 195 | yield return from enumerator in estimatedValuesEnumerators
|
---|
| 196 | select enumerator.Current;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | private double AggregateEstimatedValues(IEnumerable<double> estimatedValues) {
|
---|
[6238] | 201 | return estimatedValues.DefaultIfEmpty(double.NaN).Average();
|
---|
[6254] | 202 | }
|
---|
[6612] | 203 | #endregion
|
---|
[6520] | 204 |
|
---|
[6612] | 205 | public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 206 | regressionSolutions.AddRange(solutions);
|
---|
| 207 | }
|
---|
| 208 | public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 209 | regressionSolutions.RemoveRange(solutions);
|
---|
| 210 | }
|
---|
[6520] | 211 |
|
---|
[6612] | 212 | private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 213 | foreach (var solution in e.Items) AddRegressionSolution(solution);
|
---|
[6520] | 214 | RecalculateResults();
|
---|
| 215 | }
|
---|
[6612] | 216 | private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 217 | foreach (var solution in e.Items) RemoveRegressionSolution(solution);
|
---|
| 218 | RecalculateResults();
|
---|
| 219 | }
|
---|
| 220 | private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
| 221 | foreach (var solution in e.OldItems) RemoveRegressionSolution(solution);
|
---|
| 222 | foreach (var solution in e.Items) AddRegressionSolution(solution);
|
---|
| 223 | RecalculateResults();
|
---|
| 224 | }
|
---|
[6520] | 225 |
|
---|
[6612] | 226 | private void AddRegressionSolution(IRegressionSolution solution) {
|
---|
| 227 | if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 228 | Model.Add(solution.Model);
|
---|
| 229 | trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
|
---|
| 230 | testPartitions[solution.Model] = solution.ProblemData.TestPartition;
|
---|
| 231 | }
|
---|
[6520] | 232 |
|
---|
[6612] | 233 | private void RemoveRegressionSolution(IRegressionSolution solution) {
|
---|
| 234 | if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 235 | Model.Remove(solution.Model);
|
---|
| 236 | trainingPartitions.Remove(solution.Model);
|
---|
| 237 | testPartitions.Remove(solution.Model);
|
---|
[6520] | 238 | }
|
---|
[5816] | 239 | }
|
---|
| 240 | }
|
---|