[5662] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// Represents regression solutions that contain an ensemble of multiple regression models
|
---|
| 31 | /// </summary>
|
---|
| 32 | [StorableClass]
|
---|
| 33 | [Item("RegressionEnsembleModel", "A regression model that contains an ensemble of multiple regression models")]
|
---|
| 34 | public class RegressionEnsembleModel : NamedItem, IRegressionEnsembleModel {
|
---|
| 35 |
|
---|
| 36 | private List<IRegressionModel> models;
|
---|
| 37 | public IEnumerable<IRegressionModel> Models {
|
---|
| 38 | get { return new List<IRegressionModel>(models); }
|
---|
| 39 | }
|
---|
[6618] | 40 |
|
---|
| 41 | [Storable(Name = "Models")]
|
---|
| 42 | private IEnumerable<IRegressionModel> StorableModels {
|
---|
| 43 | get { return models; }
|
---|
| 44 | set { models = value.ToList(); }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | #region backwards compatiblity 3.3.5
|
---|
| 48 | [Storable(Name = "models", AllowOneWay = true)]
|
---|
| 49 | private List<IRegressionModel> OldStorableModels {
|
---|
| 50 | set { models = value; }
|
---|
| 51 | }
|
---|
| 52 | #endregion
|
---|
| 53 |
|
---|
[5662] | 54 | [StorableConstructor]
|
---|
| 55 | protected RegressionEnsembleModel(bool deserializing) : base(deserializing) { }
|
---|
| 56 | protected RegressionEnsembleModel(RegressionEnsembleModel original, Cloner cloner)
|
---|
| 57 | : base(original, cloner) {
|
---|
| 58 | this.models = original.Models.Select(m => cloner.Clone(m)).ToList();
|
---|
| 59 | }
|
---|
| 60 | public RegressionEnsembleModel(IEnumerable<IRegressionModel> models)
|
---|
| 61 | : base() {
|
---|
| 62 | this.name = ItemName;
|
---|
| 63 | this.description = ItemDescription;
|
---|
| 64 | this.models = new List<IRegressionModel>(models);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 68 | return new RegressionEnsembleModel(this, cloner);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | #region IRegressionEnsembleModel Members
|
---|
| 72 |
|
---|
[6618] | 73 | public void Add(IRegressionModel model) {
|
---|
| 74 | models.Add(model);
|
---|
| 75 | }
|
---|
| 76 | public void Remove(IRegressionModel model) {
|
---|
| 77 | models.Remove(model);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[5662] | 80 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
| 81 | var estimatedValuesEnumerators = (from model in models
|
---|
| 82 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
| 83 | .ToList();
|
---|
| 84 |
|
---|
| 85 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
| 86 | yield return from enumerator in estimatedValuesEnumerators
|
---|
| 87 | select enumerator.Current;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | #endregion
|
---|
| 92 |
|
---|
| 93 | #region IRegressionModel Members
|
---|
| 94 |
|
---|
| 95 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
| 96 | foreach (var estimatedValuesVector in GetEstimatedValueVectors(dataset, rows)) {
|
---|
| 97 | yield return estimatedValuesVector.Average();
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[6618] | 101 | public RegressionEnsembleSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
| 102 | return new RegressionEnsembleSolution(this.Models, problemData);
|
---|
| 103 | }
|
---|
| 104 | IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
| 105 | return CreateRegressionSolution(problemData);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[5662] | 108 | #endregion
|
---|
| 109 | }
|
---|
| 110 | }
|
---|