[5662] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5662] | 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 |
|
---|
[13697] | 22 | using System;
|
---|
[5662] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[5662] | 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 30 | /// <summary>
|
---|
| 31 | /// Represents regression solutions that contain an ensemble of multiple regression models
|
---|
| 32 | /// </summary>
|
---|
[16565] | 33 | [StorableType("202C33A2-2B7A-42E2-B3F3-BE1C9A9B5B84")]
|
---|
[5662] | 34 | [Item("RegressionEnsembleModel", "A regression model that contains an ensemble of multiple regression models")]
|
---|
[13941] | 35 | public sealed class RegressionEnsembleModel : RegressionModel, IRegressionEnsembleModel {
|
---|
| 36 | public override IEnumerable<string> VariablesUsedForPrediction {
|
---|
[13921] | 37 | get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
|
---|
| 38 | }
|
---|
[5662] | 39 |
|
---|
| 40 | private List<IRegressionModel> models;
|
---|
| 41 | public IEnumerable<IRegressionModel> Models {
|
---|
| 42 | get { return new List<IRegressionModel>(models); }
|
---|
| 43 | }
|
---|
[6603] | 44 |
|
---|
| 45 | [Storable(Name = "Models")]
|
---|
| 46 | private IEnumerable<IRegressionModel> StorableModels {
|
---|
| 47 | get { return models; }
|
---|
| 48 | set { models = value.ToList(); }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[13704] | 51 | private List<double> modelWeights;
|
---|
| 52 | public IEnumerable<double> ModelWeights {
|
---|
| 53 | get { return modelWeights; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [Storable(Name = "ModelWeights")]
|
---|
| 57 | private IEnumerable<double> StorableModelWeights {
|
---|
| 58 | get { return modelWeights; }
|
---|
| 59 | set { modelWeights = value.ToList(); }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[13700] | 62 | [Storable]
|
---|
| 63 | private bool averageModelEstimates = true;
|
---|
| 64 | public bool AverageModelEstimates {
|
---|
| 65 | get { return averageModelEstimates; }
|
---|
| 66 | set {
|
---|
| 67 | if (averageModelEstimates != value) {
|
---|
| 68 | averageModelEstimates = value;
|
---|
[13704] | 69 | OnChanged();
|
---|
[13700] | 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[6603] | 74 | #region backwards compatiblity 3.3.5
|
---|
[16796] | 75 | [Storable(OldName = "models")]
|
---|
[6603] | 76 | private List<IRegressionModel> OldStorableModels {
|
---|
| 77 | set { models = value; }
|
---|
| 78 | }
|
---|
| 79 | #endregion
|
---|
| 80 |
|
---|
[13704] | 81 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 82 | private void AfterDeserialization() {
|
---|
| 83 | // BackwardsCompatibility 3.3.14
|
---|
| 84 | #region Backwards compatible code, remove with 3.4
|
---|
| 85 | if (modelWeights == null || !modelWeights.Any())
|
---|
| 86 | modelWeights = new List<double>(models.Select(m => 1.0));
|
---|
| 87 | #endregion
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[5662] | 90 | [StorableConstructor]
|
---|
[16565] | 91 | private RegressionEnsembleModel(StorableConstructorFlag _) : base(_) { }
|
---|
[13700] | 92 | private RegressionEnsembleModel(RegressionEnsembleModel original, Cloner cloner)
|
---|
[5662] | 93 | : base(original, cloner) {
|
---|
[13700] | 94 | this.models = original.Models.Select(cloner.Clone).ToList();
|
---|
[13704] | 95 | this.modelWeights = new List<double>(original.ModelWeights);
|
---|
[13700] | 96 | this.averageModelEstimates = original.averageModelEstimates;
|
---|
[5662] | 97 | }
|
---|
[13700] | 98 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 99 | return new RegressionEnsembleModel(this, cloner);
|
---|
| 100 | }
|
---|
[6666] | 101 |
|
---|
| 102 | public RegressionEnsembleModel() : this(Enumerable.Empty<IRegressionModel>()) { }
|
---|
[13704] | 103 | public RegressionEnsembleModel(IEnumerable<IRegressionModel> models) : this(models, models.Select(m => 1.0)) { }
|
---|
| 104 | public RegressionEnsembleModel(IEnumerable<IRegressionModel> models, IEnumerable<double> modelWeights)
|
---|
[13941] | 105 | : base(string.Empty) {
|
---|
[5662] | 106 | this.name = ItemName;
|
---|
| 107 | this.description = ItemDescription;
|
---|
[13704] | 108 |
|
---|
[5662] | 109 | this.models = new List<IRegressionModel>(models);
|
---|
[13704] | 110 | this.modelWeights = new List<double>(modelWeights);
|
---|
[13941] | 111 |
|
---|
| 112 | if (this.models.Any()) this.TargetVariable = this.models.First().TargetVariable;
|
---|
[5662] | 113 | }
|
---|
| 114 |
|
---|
[6520] | 115 | public void Add(IRegressionModel model) {
|
---|
[13941] | 116 | if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = model.TargetVariable;
|
---|
[13704] | 117 | Add(model, 1.0);
|
---|
| 118 | }
|
---|
| 119 | public void Add(IRegressionModel model, double weight) {
|
---|
[13941] | 120 | if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = model.TargetVariable;
|
---|
| 121 |
|
---|
[6520] | 122 | models.Add(model);
|
---|
[13704] | 123 | modelWeights.Add(weight);
|
---|
| 124 | OnChanged();
|
---|
[6520] | 125 | }
|
---|
[13700] | 126 |
|
---|
[13704] | 127 | public void AddRange(IEnumerable<IRegressionModel> models) {
|
---|
| 128 | AddRange(models, models.Select(m => 1.0));
|
---|
| 129 | }
|
---|
| 130 | public void AddRange(IEnumerable<IRegressionModel> models, IEnumerable<double> weights) {
|
---|
[13941] | 131 | if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = models.First().TargetVariable;
|
---|
| 132 |
|
---|
[13704] | 133 | this.models.AddRange(models);
|
---|
| 134 | modelWeights.AddRange(weights);
|
---|
| 135 | OnChanged();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[6612] | 138 | public void Remove(IRegressionModel model) {
|
---|
[13704] | 139 | var index = models.IndexOf(model);
|
---|
| 140 | models.RemoveAt(index);
|
---|
| 141 | modelWeights.RemoveAt(index);
|
---|
[13941] | 142 |
|
---|
| 143 | if (!models.Any()) TargetVariable = string.Empty;
|
---|
[13704] | 144 | OnChanged();
|
---|
[6612] | 145 | }
|
---|
[13704] | 146 | public void RemoveRange(IEnumerable<IRegressionModel> models) {
|
---|
| 147 | foreach (var model in models) {
|
---|
| 148 | var index = this.models.IndexOf(model);
|
---|
| 149 | this.models.RemoveAt(index);
|
---|
| 150 | modelWeights.RemoveAt(index);
|
---|
| 151 | }
|
---|
[13941] | 152 |
|
---|
| 153 | if (!models.Any()) TargetVariable = string.Empty;
|
---|
[13704] | 154 | OnChanged();
|
---|
| 155 | }
|
---|
[6520] | 156 |
|
---|
[13704] | 157 | public double GetModelWeight(IRegressionModel model) {
|
---|
| 158 | var index = models.IndexOf(model);
|
---|
| 159 | return modelWeights[index];
|
---|
| 160 | }
|
---|
| 161 | public void SetModelWeight(IRegressionModel model, double weight) {
|
---|
| 162 | var index = models.IndexOf(model);
|
---|
| 163 | modelWeights[index] = weight;
|
---|
| 164 | OnChanged();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[13715] | 167 | #region evaluation
|
---|
[12509] | 168 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(IDataset dataset, IEnumerable<int> rows) {
|
---|
[5662] | 169 | var estimatedValuesEnumerators = (from model in models
|
---|
[13705] | 170 | let weight = GetModelWeight(model)
|
---|
| 171 | select model.GetEstimatedValues(dataset, rows).Select(e => weight * e)
|
---|
| 172 | .GetEnumerator()).ToList();
|
---|
[5662] | 173 |
|
---|
| 174 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
| 175 | yield return from enumerator in estimatedValuesEnumerators
|
---|
| 176 | select enumerator.Current;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[13941] | 180 | public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[13715] | 181 | double weightsSum = modelWeights.Sum();
|
---|
| 182 | var summedEstimates = from estimatedValuesVector in GetEstimatedValueVectors(dataset, rows)
|
---|
| 183 | select estimatedValuesVector.DefaultIfEmpty(double.NaN).Sum();
|
---|
| 184 |
|
---|
| 185 | if (AverageModelEstimates)
|
---|
| 186 | return summedEstimates.Select(v => v / weightsSum);
|
---|
| 187 | else
|
---|
| 188 | return summedEstimates;
|
---|
| 189 |
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[13697] | 192 | public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows, Func<int, IRegressionModel, bool> modelSelectionPredicate) {
|
---|
| 193 | var estimatedValuesEnumerators = GetEstimatedValueVectors(dataset, rows).GetEnumerator();
|
---|
| 194 | var rowsEnumerator = rows.GetEnumerator();
|
---|
| 195 |
|
---|
| 196 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.MoveNext()) {
|
---|
[13715] | 197 | var estimatedValueEnumerator = estimatedValuesEnumerators.Current.GetEnumerator();
|
---|
[13697] | 198 | int currentRow = rowsEnumerator.Current;
|
---|
[13715] | 199 | double weightsSum = 0.0;
|
---|
| 200 | double filteredEstimatesSum = 0.0;
|
---|
[13697] | 201 |
|
---|
[13715] | 202 | for (int m = 0; m < models.Count; m++) {
|
---|
| 203 | estimatedValueEnumerator.MoveNext();
|
---|
| 204 | var model = models[m];
|
---|
| 205 | if (!modelSelectionPredicate(currentRow, model)) continue;
|
---|
[13697] | 206 |
|
---|
[13715] | 207 | filteredEstimatesSum += estimatedValueEnumerator.Current;
|
---|
| 208 | weightsSum += modelWeights[m];
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | if (AverageModelEstimates)
|
---|
| 212 | yield return filteredEstimatesSum / weightsSum;
|
---|
| 213 | else
|
---|
| 214 | yield return filteredEstimatesSum;
|
---|
[13697] | 215 | }
|
---|
| 216 | }
|
---|
[13700] | 217 |
|
---|
[13715] | 218 | #endregion
|
---|
[13700] | 219 |
|
---|
[13704] | 220 | public event EventHandler Changed;
|
---|
| 221 | private void OnChanged() {
|
---|
| 222 | var handler = Changed;
|
---|
[13700] | 223 | if (handler != null)
|
---|
| 224 | handler(this, EventArgs.Empty);
|
---|
| 225 | }
|
---|
[5662] | 226 |
|
---|
| 227 |
|
---|
[13941] | 228 | public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
[13698] | 229 | return new RegressionEnsembleSolution(this, new RegressionEnsembleProblemData(problemData));
|
---|
[6603] | 230 | }
|
---|
[5662] | 231 | }
|
---|
| 232 | }
|
---|