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