Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/01/11 17:48:53 (13 years ago)
Author:
mkommend
Message:

#1479: Integrated trunk changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs

    r6377 r6618  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     25using HeuristicLab.Collections;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Core;
     28using HeuristicLab.Data;
    2629using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using System;
    28 using HeuristicLab.Data;
    2930
    3031namespace HeuristicLab.Problems.DataAnalysis {
     
    3536  [Item("Regression Ensemble Solution", "A regression solution that contains an ensemble of multiple regression models")]
    3637  // [Creatable("Data Analysis")]
    37   public class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution {
     38  public sealed class RegressionEnsembleSolution : RegressionSolution, IRegressionEnsembleSolution {
    3839    public new IRegressionEnsembleModel Model {
    3940      get { return (IRegressionEnsembleModel)base.Model; }
     41    }
     42
     43    private readonly ItemCollection<IRegressionSolution> regressionSolutions;
     44    public IItemCollection<IRegressionSolution> RegressionSolutions {
     45      get { return regressionSolutions; }
    4046    }
    4147
     
    4652
    4753    [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)
    5073      : base(original, cloner) {
    5174      trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
     
    5780        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
    5881      }
    59       RecalculateResults();
     82
     83      regressionSolutions = cloner.Clone(original.regressionSolutions);
     84      RegisterRegressionSolutionsEventHandler();
    6085    }
    6186
    6287    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      ) { }
    7292
    7393    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)) {
    7595      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
    7696      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
     97      this.regressionSolutions = new ItemCollection<IRegressionSolution>();
     98
     99      List<IRegressionSolution> solutions = new List<IRegressionSolution>();
    77100      var modelEnumerator = models.GetEnumerator();
    78101      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
    79102      var testPartitionEnumerator = testPartitions.GetEnumerator();
     103
    80104      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));
    83112      }
    84113      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
    85114        throw new ArgumentException();
    86115      }
    87       RecalculateResults();
     116
     117      RegisterRegressionSolutionsEventHandler();
     118      regressionSolutions.AddRange(solutions);
    88119    }
    89120
     
    91122      return new RegressionEnsembleSolution(this, cloner);
    92123    }
    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
    94135    public override IEnumerable<double> EstimatedTrainingValues {
    95136      get {
     
    160201      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
    161202    }
     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    }
    162240  }
    163241}
Note: See TracChangeset for help on using the changeset viewer.