[5816] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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")]
|
---|
[12708] | 37 | [Creatable(CreatableAttribute.Categories.DataAnalysisEnsembles, Priority = 100)]
|
---|
[8724] | 38 | public sealed class RegressionEnsembleSolution : RegressionSolutionBase, IRegressionEnsembleSolution {
|
---|
[8167] | 39 | private readonly Dictionary<int, double> trainingEvaluationCache = new Dictionary<int, double>();
|
---|
| 40 | private readonly Dictionary<int, double> testEvaluationCache = new Dictionary<int, double>();
|
---|
[8724] | 41 | private readonly Dictionary<int, double> evaluationCache = new Dictionary<int, double>();
|
---|
[8151] | 42 |
|
---|
[5816] | 43 | public new IRegressionEnsembleModel Model {
|
---|
| 44 | get { return (IRegressionEnsembleModel)base.Model; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[6666] | 47 | public new RegressionEnsembleProblemData ProblemData {
|
---|
| 48 | get { return (RegressionEnsembleProblemData)base.ProblemData; }
|
---|
| 49 | set { base.ProblemData = value; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[13049] | 52 | [Storable]
|
---|
[6612] | 53 | private readonly ItemCollection<IRegressionSolution> regressionSolutions;
|
---|
| 54 | public IItemCollection<IRegressionSolution> RegressionSolutions {
|
---|
| 55 | get { return regressionSolutions; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[5816] | 58 | [Storable]
|
---|
[8152] | 59 | private readonly Dictionary<IRegressionModel, IntRange> trainingPartitions;
|
---|
[5816] | 60 | [Storable]
|
---|
[8152] | 61 | private readonly Dictionary<IRegressionModel, IntRange> testPartitions;
|
---|
[5816] | 62 |
|
---|
| 63 | [StorableConstructor]
|
---|
[6612] | 64 | private RegressionEnsembleSolution(bool deserializing)
|
---|
| 65 | : base(deserializing) {
|
---|
| 66 | regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 67 | }
|
---|
| 68 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 69 | private void AfterDeserialization() {
|
---|
[13049] | 70 | if (!regressionSolutions.Any()) {
|
---|
| 71 | foreach (var model in Model.Models) {
|
---|
| 72 | IRegressionProblemData problemData = (IRegressionProblemData)ProblemData.Clone();
|
---|
| 73 | problemData.TrainingPartition.Start = trainingPartitions[model].Start;
|
---|
| 74 | problemData.TrainingPartition.End = trainingPartitions[model].End;
|
---|
| 75 | problemData.TestPartition.Start = testPartitions[model].Start;
|
---|
| 76 | problemData.TestPartition.End = testPartitions[model].End;
|
---|
[6612] | 77 |
|
---|
[13049] | 78 | regressionSolutions.Add(model.CreateRegressionSolution(problemData));
|
---|
| 79 | }
|
---|
[6612] | 80 | }
|
---|
[13976] | 81 |
|
---|
| 82 | RegisterModelEvents();
|
---|
[6612] | 83 | RegisterRegressionSolutionsEventHandler();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[6592] | 86 | private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
|
---|
[5816] | 87 | : base(original, cloner) {
|
---|
[6239] | 88 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 89 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
[6302] | 90 | foreach (var pair in original.trainingPartitions) {
|
---|
| 91 | trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
[6239] | 92 | }
|
---|
[6302] | 93 | foreach (var pair in original.testPartitions) {
|
---|
| 94 | testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
| 95 | }
|
---|
[6612] | 96 |
|
---|
[13976] | 97 | evaluationCache = new Dictionary<int, double>(original.ProblemData.Dataset.Rows);
|
---|
[8174] | 98 | trainingEvaluationCache = new Dictionary<int, double>(original.ProblemData.TrainingIndices.Count());
|
---|
| 99 | testEvaluationCache = new Dictionary<int, double>(original.ProblemData.TestIndices.Count());
|
---|
| 100 |
|
---|
[6612] | 101 | regressionSolutions = cloner.Clone(original.regressionSolutions);
|
---|
[13976] | 102 | RegisterModelEvents();
|
---|
[6612] | 103 | RegisterRegressionSolutionsEventHandler();
|
---|
[5816] | 104 | }
|
---|
[6239] | 105 |
|
---|
[6666] | 106 | public RegressionEnsembleSolution()
|
---|
| 107 | : base(new RegressionEnsembleModel(), RegressionEnsembleProblemData.EmptyProblemData) {
|
---|
| 108 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 109 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 110 | regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
| 111 |
|
---|
[13976] | 112 | RegisterModelEvents();
|
---|
[6666] | 113 | RegisterRegressionSolutionsEventHandler();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[7738] | 116 | public RegressionEnsembleSolution(IRegressionProblemData problemData)
|
---|
[13976] | 117 | : this(new RegressionEnsembleModel(), problemData) {
|
---|
[7738] | 118 | }
|
---|
| 119 |
|
---|
[13976] | 120 | public RegressionEnsembleSolution(IRegressionEnsembleModel model, IRegressionProblemData problemData)
|
---|
| 121 | : base(model, new RegressionEnsembleProblemData(problemData)) {
|
---|
| 122 | trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 123 | testPartitions = new Dictionary<IRegressionModel, IntRange>();
|
---|
| 124 | regressionSolutions = new ItemCollection<IRegressionSolution>();
|
---|
[5816] | 125 |
|
---|
[13976] | 126 | evaluationCache = new Dictionary<int, double>(problemData.Dataset.Rows);
|
---|
| 127 | trainingEvaluationCache = new Dictionary<int, double>(problemData.TrainingIndices.Count());
|
---|
| 128 | testEvaluationCache = new Dictionary<int, double>(problemData.TestIndices.Count());
|
---|
[6612] | 129 |
|
---|
| 130 |
|
---|
[13976] | 131 | var solutions = model.Models.Select(m => m.CreateRegressionSolution((IRegressionProblemData)problemData.Clone()));
|
---|
| 132 | foreach (var solution in solutions) {
|
---|
| 133 | regressionSolutions.Add(solution);
|
---|
| 134 | trainingPartitions.Add(solution.Model, solution.ProblemData.TrainingPartition);
|
---|
| 135 | testPartitions.Add(solution.Model, solution.ProblemData.TestPartition);
|
---|
[6612] | 136 | }
|
---|
| 137 |
|
---|
[13976] | 138 | RecalculateResults();
|
---|
| 139 | RegisterModelEvents();
|
---|
[6612] | 140 | RegisterRegressionSolutionsEventHandler();
|
---|
[5816] | 141 | }
|
---|
| 142 |
|
---|
[13976] | 143 |
|
---|
[5816] | 144 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 145 | return new RegressionEnsembleSolution(this, cloner);
|
---|
| 146 | }
|
---|
[13976] | 147 |
|
---|
| 148 | private void RegisterModelEvents() {
|
---|
| 149 | Model.Changed += Model_Changed;
|
---|
| 150 | }
|
---|
[6612] | 151 | private void RegisterRegressionSolutionsEventHandler() {
|
---|
| 152 | regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded);
|
---|
| 153 | regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved);
|
---|
| 154 | regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset);
|
---|
| 155 | }
|
---|
[5816] | 156 |
|
---|
[6612] | 157 | #region Evaluation
|
---|
[8724] | 158 | public override IEnumerable<double> EstimatedValues {
|
---|
| 159 | get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[5816] | 162 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
[8152] | 163 | get {
|
---|
| 164 | var rows = ProblemData.TrainingIndices;
|
---|
[8167] | 165 | var rowsToEvaluate = rows.Except(trainingEvaluationCache.Keys);
|
---|
[13976] | 166 |
|
---|
[8152] | 167 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
[13976] | 168 | var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate, (r, m) => RowIsTrainingForModel(r, m) && !RowIsTestForModel(r, m)).GetEnumerator();
|
---|
[8152] | 169 |
|
---|
| 170 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
[8167] | 171 | trainingEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
[8152] | 172 | }
|
---|
| 173 |
|
---|
[8167] | 174 | return rows.Select(row => trainingEvaluationCache[row]);
|
---|
[8152] | 175 | }
|
---|
[5816] | 176 | }
|
---|
| 177 |
|
---|
| 178 | public override IEnumerable<double> EstimatedTestValues {
|
---|
[8152] | 179 | get {
|
---|
| 180 | var rows = ProblemData.TestIndices;
|
---|
[8167] | 181 | var rowsToEvaluate = rows.Except(testEvaluationCache.Keys);
|
---|
[8152] | 182 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
[13976] | 183 | var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate, RowIsTestForModel).GetEnumerator();
|
---|
[8152] | 184 |
|
---|
| 185 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
[8167] | 186 | testEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
[8152] | 187 | }
|
---|
| 188 |
|
---|
[8167] | 189 | return rows.Select(row => testEvaluationCache[row]);
|
---|
[8152] | 190 | }
|
---|
[8151] | 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 | private bool RowIsTestForModel(int currentRow, IRegressionModel model) {
|
---|
| 197 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
| 198 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[5816] | 201 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
[8167] | 202 | var rowsToEvaluate = rows.Except(evaluationCache.Keys);
|
---|
[8152] | 203 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
[13976] | 204 | var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
|
---|
[8152] | 205 |
|
---|
| 206 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
[8167] | 207 | evaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
[8152] | 208 | }
|
---|
| 209 |
|
---|
[8167] | 210 | return rows.Select(row => evaluationCache[row]);
|
---|
[5816] | 211 | }
|
---|
| 212 |
|
---|
[13976] | 213 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(IEnumerable<int> rows) {
|
---|
| 214 | return Model.GetEstimatedValueVectors(ProblemData.Dataset, rows);
|
---|
[5816] | 215 | }
|
---|
[6612] | 216 | #endregion
|
---|
[6520] | 217 |
|
---|
[6666] | 218 | protected override void OnProblemDataChanged() {
|
---|
[8167] | 219 | trainingEvaluationCache.Clear();
|
---|
| 220 | testEvaluationCache.Clear();
|
---|
| 221 | evaluationCache.Clear();
|
---|
[6666] | 222 | IRegressionProblemData problemData = new RegressionProblemData(ProblemData.Dataset,
|
---|
| 223 | ProblemData.AllowedInputVariables,
|
---|
| 224 | ProblemData.TargetVariable);
|
---|
| 225 | problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 226 | problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 227 | problemData.TestPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 228 | problemData.TestPartition.End = ProblemData.TestPartition.End;
|
---|
| 229 |
|
---|
| 230 | foreach (var solution in RegressionSolutions) {
|
---|
| 231 | if (solution is RegressionEnsembleSolution)
|
---|
| 232 | solution.ProblemData = ProblemData;
|
---|
| 233 | else
|
---|
| 234 | solution.ProblemData = problemData;
|
---|
| 235 | }
|
---|
| 236 | foreach (var trainingPartition in trainingPartitions.Values) {
|
---|
| 237 | trainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 238 | trainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 239 | }
|
---|
| 240 | foreach (var testPartition in testPartitions.Values) {
|
---|
| 241 | testPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 242 | testPartition.End = ProblemData.TestPartition.End;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | base.OnProblemDataChanged();
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[13976] | 248 | private void Model_Changed(object sender, EventArgs e) {
|
---|
| 249 | var modelSet = new HashSet<IRegressionModel>(Model.Models);
|
---|
| 250 | foreach (var model in Model.Models) {
|
---|
| 251 | if (!trainingPartitions.ContainsKey(model)) trainingPartitions.Add(model, ProblemData.TrainingPartition);
|
---|
| 252 | if (!testPartitions.ContainsKey(model)) testPartitions.Add(model, ProblemData.TrainingPartition);
|
---|
| 253 | }
|
---|
| 254 | foreach (var model in trainingPartitions.Keys) {
|
---|
| 255 | if (modelSet.Contains(model)) continue;
|
---|
| 256 | trainingPartitions.Remove(model);
|
---|
| 257 | testPartitions.Remove(model);
|
---|
| 258 | }
|
---|
[8152] | 259 |
|
---|
[8167] | 260 | trainingEvaluationCache.Clear();
|
---|
| 261 | testEvaluationCache.Clear();
|
---|
| 262 | evaluationCache.Clear();
|
---|
[13976] | 263 |
|
---|
| 264 | OnModelChanged();
|
---|
[6612] | 265 | }
|
---|
[13976] | 266 |
|
---|
| 267 | public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 268 | regressionSolutions.AddRange(solutions);
|
---|
| 269 | }
|
---|
[6612] | 270 | public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) {
|
---|
| 271 | regressionSolutions.RemoveRange(solutions);
|
---|
| 272 | }
|
---|
[6520] | 273 |
|
---|
[6612] | 274 | private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
[13976] | 275 | foreach (var solution in e.Items) {
|
---|
| 276 | trainingPartitions.Add(solution.Model, solution.ProblemData.TrainingPartition);
|
---|
| 277 | testPartitions.Add(solution.Model, solution.ProblemData.TestPartition);
|
---|
| 278 | }
|
---|
| 279 | Model.AddRange(e.Items.Select(s => s.Model));
|
---|
[6520] | 280 | }
|
---|
[6612] | 281 | private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
[13976] | 282 | foreach (var solution in e.Items) {
|
---|
| 283 | trainingPartitions.Remove(solution.Model);
|
---|
| 284 | testPartitions.Remove(solution.Model);
|
---|
| 285 | }
|
---|
| 286 | Model.RemoveRange(e.Items.Select(s => s.Model));
|
---|
[6612] | 287 | }
|
---|
| 288 | private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) {
|
---|
[13976] | 289 | foreach (var solution in e.OldItems) {
|
---|
| 290 | trainingPartitions.Remove(solution.Model);
|
---|
| 291 | testPartitions.Remove(solution.Model);
|
---|
| 292 | }
|
---|
| 293 | Model.RemoveRange(e.OldItems.Select(s => s.Model));
|
---|
[6520] | 294 |
|
---|
[13976] | 295 | foreach (var solution in e.Items) {
|
---|
| 296 | trainingPartitions.Add(solution.Model, solution.ProblemData.TrainingPartition);
|
---|
| 297 | testPartitions.Add(solution.Model, solution.ProblemData.TestPartition);
|
---|
| 298 | }
|
---|
| 299 | Model.AddRange(e.Items.Select(s => s.Model));
|
---|
[6612] | 300 | }
|
---|
[5816] | 301 | }
|
---|
| 302 | }
|
---|