1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 | }
|
---|
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 |
|
---|
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 |
|
---|
61 | public RegressionEnsembleModel() : this(Enumerable.Empty<IRegressionModel>()) { }
|
---|
62 | public RegressionEnsembleModel(IEnumerable<IRegressionModel> models)
|
---|
63 | : base() {
|
---|
64 | this.name = ItemName;
|
---|
65 | this.description = ItemDescription;
|
---|
66 | this.models = new List<IRegressionModel>(models);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
70 | return new RegressionEnsembleModel(this, cloner);
|
---|
71 | }
|
---|
72 |
|
---|
73 | #region IRegressionEnsembleModel Members
|
---|
74 |
|
---|
75 | public void Add(IRegressionModel model) {
|
---|
76 | models.Add(model);
|
---|
77 | }
|
---|
78 | public void Remove(IRegressionModel model) {
|
---|
79 | models.Remove(model);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
83 | var estimatedValuesEnumerators = (from model in models
|
---|
84 | select model.GetEstimatedValues(dataset, rows).GetEnumerator())
|
---|
85 | .ToList();
|
---|
86 |
|
---|
87 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
88 | yield return from enumerator in estimatedValuesEnumerators
|
---|
89 | select enumerator.Current;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | #region IRegressionModel Members
|
---|
96 |
|
---|
97 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
98 | foreach (var estimatedValuesVector in GetEstimatedValueVectors(dataset, rows)) {
|
---|
99 | yield return estimatedValuesVector.Average();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public RegressionEnsembleSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
104 | return new RegressionEnsembleSolution(this.Models, new RegressionEnsembleProblemData(problemData));
|
---|
105 | }
|
---|
106 | IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
107 | return CreateRegressionSolution(problemData);
|
---|
108 | }
|
---|
109 |
|
---|
110 | #endregion
|
---|
111 | }
|
---|
112 | }
|
---|