Changeset 6618 for branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs
- Timestamp:
- 08/01/11 17:48:53 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs
r6377 r6618 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; 25 using HeuristicLab.Collections; 24 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; 28 using HeuristicLab.Data; 26 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System;28 using HeuristicLab.Data;29 30 30 31 namespace HeuristicLab.Problems.DataAnalysis { … … 35 36 [Item("Regression Ensemble Solution", "A regression solution that contains an ensemble of multiple regression models")] 36 37 // [Creatable("Data Analysis")] 37 public class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution {38 public sealed class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution { 38 39 public new IRegressionEnsembleModel Model { 39 40 get { return (IRegressionEnsembleModel)base.Model; } 41 } 42 43 private readonly ItemCollection<IRegressionSolution> regressionSolutions; 44 public IItemCollection<IRegressionSolution> RegressionSolutions { 45 get { return regressionSolutions; } 40 46 } 41 47 … … 46 52 47 53 [StorableConstructor] 48 protected RegressionEnsembleSolution(bool deserializing) : base(deserializing) { } 49 protected RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner) 54 private RegressionEnsembleSolution(bool deserializing) 55 : base(deserializing) { 56 regressionSolutions = new ItemCollection<IRegressionSolution>(); 57 } 58 [StorableHook(HookType.AfterDeserialization)] 59 private void AfterDeserialization() { 60 foreach (var model in Model.Models) { 61 IRegressionProblemData problemData = (IRegressionProblemData)ProblemData.Clone(); 62 problemData.TrainingPartition.Start = trainingPartitions[model].Start; 63 problemData.TrainingPartition.End = trainingPartitions[model].End; 64 problemData.TestPartition.Start = testPartitions[model].Start; 65 problemData.TestPartition.End = testPartitions[model].End; 66 67 regressionSolutions.Add(model.CreateRegressionSolution(problemData)); 68 } 69 RegisterRegressionSolutionsEventHandler(); 70 } 71 72 private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner) 50 73 : base(original, cloner) { 51 74 trainingPartitions = new Dictionary<IRegressionModel, IntRange>(); … … 57 80 testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value); 58 81 } 59 RecalculateResults(); 82 83 regressionSolutions = cloner.Clone(original.regressionSolutions); 84 RegisterRegressionSolutionsEventHandler(); 60 85 } 61 86 62 87 public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData) 63 : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) { 64 trainingPartitions = new Dictionary<IRegressionModel, IntRange>(); 65 testPartitions = new Dictionary<IRegressionModel, IntRange>(); 66 foreach (var model in models) { 67 trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone(); 68 testPartitions[model] = (IntRange)problemData.TestPartition.Clone(); 69 } 70 RecalculateResults(); 71 } 88 : this(models, problemData, 89 models.Select(m => (IntRange)problemData.TrainingPartition.Clone()), 90 models.Select(m => (IntRange)problemData.TestPartition.Clone()) 91 ) { } 72 92 73 93 public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions) 74 : base(new RegressionEnsembleModel( models), new RegressionEnsembleProblemData(problemData)) {94 : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) { 75 95 this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>(); 76 96 this.testPartitions = new Dictionary<IRegressionModel, IntRange>(); 97 this.regressionSolutions = new ItemCollection<IRegressionSolution>(); 98 99 List<IRegressionSolution> solutions = new List<IRegressionSolution>(); 77 100 var modelEnumerator = models.GetEnumerator(); 78 101 var trainingPartitionEnumerator = trainingPartitions.GetEnumerator(); 79 102 var testPartitionEnumerator = testPartitions.GetEnumerator(); 103 80 104 while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) { 81 this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone(); 82 this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone(); 105 var p = (IRegressionProblemData)problemData.Clone(); 106 p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start; 107 p.TrainingPartition.End = trainingPartitionEnumerator.Current.End; 108 p.TestPartition.Start = testPartitionEnumerator.Current.Start; 109 p.TestPartition.End = testPartitionEnumerator.Current.End; 110 111 solutions.Add(modelEnumerator.Current.CreateRegressionSolution(p)); 83 112 } 84 113 if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) { 85 114 throw new ArgumentException(); 86 115 } 87 RecalculateResults(); 116 117 RegisterRegressionSolutionsEventHandler(); 118 regressionSolutions.AddRange(solutions); 88 119 } 89 120 … … 91 122 return new RegressionEnsembleSolution(this, cloner); 92 123 } 93 124 private void RegisterRegressionSolutionsEventHandler() { 125 regressionSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsAdded); 126 regressionSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_ItemsRemoved); 127 regressionSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IRegressionSolution>(regressionSolutions_CollectionReset); 128 } 129 130 protected override void RecalculateResults() { 131 CalculateResults(); 132 } 133 134 #region Evaluation 94 135 public override IEnumerable<double> EstimatedTrainingValues { 95 136 get { … … 160 201 return estimatedValues.DefaultIfEmpty(double.NaN).Average(); 161 202 } 203 #endregion 204 205 public void AddRegressionSolutions(IEnumerable<IRegressionSolution> solutions) { 206 solutions.OfType<RegressionEnsembleSolution>().SelectMany(ensemble => ensemble.RegressionSolutions); 207 regressionSolutions.AddRange(solutions); 208 } 209 public void RemoveRegressionSolutions(IEnumerable<IRegressionSolution> solutions) { 210 regressionSolutions.RemoveRange(solutions); 211 } 212 213 private void regressionSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) { 214 foreach (var solution in e.Items) AddRegressionSolution(solution); 215 RecalculateResults(); 216 } 217 private void regressionSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) { 218 foreach (var solution in e.Items) RemoveRegressionSolution(solution); 219 RecalculateResults(); 220 } 221 private void regressionSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRegressionSolution> e) { 222 foreach (var solution in e.OldItems) RemoveRegressionSolution(solution); 223 foreach (var solution in e.Items) AddRegressionSolution(solution); 224 RecalculateResults(); 225 } 226 227 private void AddRegressionSolution(IRegressionSolution solution) { 228 if (Model.Models.Contains(solution.Model)) throw new ArgumentException(); 229 Model.Add(solution.Model); 230 trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition; 231 testPartitions[solution.Model] = solution.ProblemData.TestPartition; 232 } 233 234 private void RemoveRegressionSolution(IRegressionSolution solution) { 235 if (!Model.Models.Contains(solution.Model)) throw new ArgumentException(); 236 Model.Remove(solution.Model); 237 trainingPartitions.Remove(solution.Model); 238 testPartitions.Remove(solution.Model); 239 } 162 240 } 163 241 }
Note: See TracChangeset
for help on using the changeset viewer.