Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6627


Ignore:
Timestamp:
08/04/11 08:27:35 (13 years ago)
Author:
abeham
Message:

#1541

  • updated from trunk
Location:
branches/QAPAlgorithms
Files:
1 deleted
30 edited
8 copied

Legend:

Unmodified
Added
Removed
  • branches/QAPAlgorithms

  • branches/QAPAlgorithms/HeuristicLab.Analysis

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/QAPAlgorithms/HeuristicLab.Analysis.Views/3.3/HeatMapView.resx

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/QAPAlgorithms/HeuristicLab.Core.Views/3.3/TypeSelector.cs

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/QAPAlgorithms/HeuristicLab.Encodings.PermutationEncoding

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/QAPAlgorithms/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.cs

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/QAPAlgorithms/HeuristicLab.Persistence

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis

  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj

    r6611 r6627  
    110110  </ItemGroup>
    111111  <ItemGroup>
     112    <Compile Include="Classification\ClassificationEnsembleSolutionModelView.cs">
     113      <SubType>UserControl</SubType>
     114    </Compile>
     115    <Compile Include="Classification\ClassificationEnsembleSolutionModelView.Designer.cs">
     116      <DependentUpon>ClassificationEnsembleSolutionModelView.cs</DependentUpon>
     117    </Compile>
     118    <Compile Include="Regression\RegressionEnsembleSolutionModelView.cs">
     119      <SubType>UserControl</SubType>
     120    </Compile>
     121    <Compile Include="Regression\RegressionEnsembleSolutionModelView.Designer.cs">
     122      <DependentUpon>RegressionEnsembleSolutionModelView.cs</DependentUpon>
     123    </Compile>
    112124    <Compile Include="Solution Views\ClassificationSolutionView.cs">
    113125      <SubType>UserControl</SubType>
     
    158170      <DependentUpon>ClusteringSolutionView.cs</DependentUpon>
    159171    </Compile>
     172    <Compile Include="Solution Views\ClassificationEnsembleSolutionView.cs">
     173      <SubType>UserControl</SubType>
     174    </Compile>
     175    <Compile Include="Solution Views\ClassificationEnsembleSolutionView.Designer.cs">
     176      <DependentUpon>ClassificationEnsembleSolutionView.cs</DependentUpon>
     177    </Compile>
    160178    <Compile Include="Solution Views\DiscriminantFunctionClassificationSolutionView.cs">
    161179      <SubType>UserControl</SubType>
     
    164182      <DependentUpon>DiscriminantFunctionClassificationSolutionView.cs</DependentUpon>
    165183    </Compile>
    166     <Compile Include="Interfaces\IDataAnalysisSolutionEvaluationView.cs" />
    167184    <Compile Include="Interfaces\IDiscriminantFunctionClassificationSolutionEvaluationView.cs" />
    168185    <Compile Include="Interfaces\IClusteringSolutionEvaluationView.cs" />
     
    182199    <Compile Include="Interfaces\IRegressionSolutionEvaluationView.cs" />
    183200    <Compile Include="MenuItems\CreateEnsembleMenuItem.cs" />
     201    <Compile Include="Solution Views\RegressionEnsembleSolutionView.cs">
     202      <SubType>UserControl</SubType>
     203    </Compile>
     204    <Compile Include="Solution Views\RegressionEnsembleSolutionView.Designer.cs">
     205      <DependentUpon>RegressionEnsembleSolutionView.cs</DependentUpon>
     206    </Compile>
    184207    <Compile Include="Solution Views\RegressionSolutionView.cs">
    185208      <SubType>UserControl</SubType>
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Interfaces/IClassificationSolutionEvaluationView.cs

    r5829 r6627  
    2222
    2323namespace HeuristicLab.Problems.DataAnalysis.Views {
    24   public interface IClassificationSolutionEvaluationView : IDataAnalysisSolutionEvaluationView {
     24  public interface IClassificationSolutionEvaluationView {
    2525    new IClassificationSolution Content { get; set; }
    2626  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Interfaces/IClusteringSolutionEvaluationView.cs

    r5829 r6627  
    2222
    2323namespace HeuristicLab.Problems.DataAnalysis.Views {
    24   public interface IClusteringSolutionEvaluationView : IDataAnalysisSolutionEvaluationView {
     24  public interface IClusteringSolutionEvaluationView {
    2525    new IClusteringSolution Content { get; set; }
    2626  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Interfaces/IDiscriminantFunctionClassificationSolutionEvaluationView.cs

    r5829 r6627  
    2222
    2323namespace HeuristicLab.Problems.DataAnalysis.Views {
    24   public interface IDiscriminantFunctionClassificationSolutionEvaluationView : IDataAnalysisSolutionEvaluationView {
     24  public interface IDiscriminantFunctionClassificationSolutionEvaluationView {
    2525    new IDiscriminantFunctionClassificationSolution Content { get; set; }
    2626  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Interfaces/IRegressionSolutionEvaluationView.cs

    r5829 r6627  
    2222
    2323namespace HeuristicLab.Problems.DataAnalysis.Views {
    24   public interface IRegressionSolutionEvaluationView : IDataAnalysisSolutionEvaluationView {
     24  public interface IRegressionSolutionEvaluationView {
    2525    new IRegressionSolution Content { get; set; }
    2626  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems/CreateEnsembleMenuItem.cs

    r6611 r6627  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    24 using System.Collections.Generic;
    2525using System.Windows.Forms;
     26using HeuristicLab.Core;
    2627using HeuristicLab.MainForm;
    2728using HeuristicLab.Optimization;
    2829using HeuristicLab.Optimizer;
    29 using HeuristicLab.Core;
    3030
    3131namespace HeuristicLab.Problems.DataAnalysis.MenuItems {
     
    7575            var ensemble = new RegressionEnsembleSolution(Enumerable.Empty<IRegressionModel>(), problemData);
    7676            ensemble.Name = group.Key + " ensemble";
    77             ensemble.AddModelsAndPartitions(group.OfType<IRegressionSolution>());
     77            var nestedSolutions = group.OfType<RegressionEnsembleSolution>().SelectMany(e => e.RegressionSolutions);
     78            var solutions = group.Where(s => !(s is RegressionEnsembleSolution)).OfType<IRegressionSolution>();
     79            ensemble.AddRegressionSolutions(nestedSolutions.Concat(solutions));
    7880            MainFormManager.MainForm.ShowContent(ensemble);
    7981          } else if (group.All(s => s is IClassificationSolution)) {
     
    8587            var ensemble = new ClassificationEnsembleSolution(Enumerable.Empty<IClassificationModel>(), problemData);
    8688            ensemble.Name = group.Key + " ensemble";
    87             ensemble.AddModelsAndPartitions(group.OfType<IClassificationSolution>());
     89            var nestedSolutions = group.OfType<ClassificationEnsembleSolution>().SelectMany(e => e.ClassificationSolutions);
     90            var solutions = group.Where(s => !(s is ClassificationEnsembleSolution)).OfType<IClassificationSolution>();
     91            ensemble.AddClassificationSolutions(nestedSolutions.Concat(solutions));
    8892            MainFormManager.MainForm.ShowContent(ensemble);
    8993          }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/ClassificationSolutionView.cs

    r6611 r6627  
    3434      var classificationSolutionEvaluationViewTypes = ApplicationManager.Manager.GetTypes(typeof(IClassificationSolutionEvaluationView), true);
    3535      foreach (Type viewType in classificationSolutionEvaluationViewTypes)
    36         AddViewListViewItem(viewType);
     36        AddViewListViewItem(viewType, HeuristicLab.Common.Resources.VSImageLibrary.Graph);
    3737    }
    3838
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/ClusteringSolutionView.cs

    r6611 r6627  
    3434      var clusteringSolutionEvaluationViewTypes = ApplicationManager.Manager.GetTypes(typeof(IClusteringSolutionEvaluationView), true);
    3535      foreach (Type viewType in clusteringSolutionEvaluationViewTypes)
    36         AddViewListViewItem(viewType);
     36        AddViewListViewItem(viewType, HeuristicLab.Common.Resources.VSImageLibrary.Graph);
    3737    }
    3838
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/DataAnalysisSolutionView.cs

    r6611 r6627  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
     25using System.Linq;
    2426using System.Windows.Forms;
    25 using HeuristicLab.Common.Resources;
    2627using HeuristicLab.MainForm;
    2728using HeuristicLab.Optimization.Views;
     
    4647
    4748      //cache old viewTypes;
    48       var viewTypes = new List<Type>();
     49      var viewTypes = new List<Tuple<Type, Image>>();
    4950      foreach (ListViewItem item in ItemsListView.Items) {
    5051        var viewType = item.Tag as Type;
    51         if (viewType != null) viewTypes.Add(viewType);
     52        if (viewType != null) viewTypes.Add(Tuple.Create(viewType, imageList.Images[item.ImageIndex]));
    5253      }
    5354
     
    5556
    5657      //readd viewTypes
    57       foreach (Type viewType in viewTypes)
    58         AddViewListViewItem(viewType);
     58      foreach (var tuple in viewTypes)
     59        AddViewListViewItem(tuple.Item1, tuple.Item2);
    5960
    6061      //recover selection
     
    8586    }
    8687
    87     protected void AddViewListViewItem(Type viewType) {
    88       if (!typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(viewType))
    89         throw new ArgumentException("Given type " + viewType + " is not a IDataAnalysisSolutionEvaluationView.");
    90 
     88    protected void AddViewListViewItem(Type viewType, Image image) {
    9189      ListViewItem listViewItem = new ListViewItem();
    9290      listViewItem.Text = ViewAttribute.GetViewName(viewType);
    93       itemsListView.SmallImageList.Images.Add(VSImageLibrary.Graph);
     91      itemsListView.SmallImageList.Images.Add(image);
    9492      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
    9593      listViewItem.Tag = viewType;
     
    10098
    10199    protected void RemoveViewListViewItem(Type viewType) {
    102       List<ListViewItem> itemsToRemove = new List<ListViewItem>(); ;
    103       foreach (ListViewItem item in itemsListView.Items)
    104         if (item.Tag as Type == typeof(IDataAnalysisSolutionEvaluationView))
    105           itemsToRemove.Add(item);
     100      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
    106101
    107102      foreach (ListViewItem item in itemsToRemove)
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/DiscriminantFunctionClassificationSolutionView.cs

    r6611 r6627  
    3434      var classificationSolutionEvaluationViewTypes = ApplicationManager.Manager.GetTypes(typeof(IClassificationSolutionEvaluationView), true);
    3535      foreach (Type viewType in classificationSolutionEvaluationViewTypes)
    36         AddViewListViewItem(viewType);
     36        AddViewListViewItem(viewType, HeuristicLab.Common.Resources.VSImageLibrary.Graph);
    3737      RemoveViewListViewItem(typeof(ClassificationSolutionEstimatedClassValuesView));
    3838
    3939      var regressionSolutionEvaluationViewTypes = ApplicationManager.Manager.GetTypes(typeof(IDiscriminantFunctionClassificationSolutionEvaluationView), true);
    4040      foreach (Type viewType in regressionSolutionEvaluationViewTypes)
    41         AddViewListViewItem(viewType);
     41        AddViewListViewItem(viewType, HeuristicLab.Common.Resources.VSImageLibrary.Graph);
    4242    }
    4343
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/RegressionSolutionView.cs

    r6611 r6627  
    3434      var regressionSolutionEvaluationViewTypes = ApplicationManager.Manager.GetTypes(typeof(IRegressionSolutionEvaluationView), true);
    3535      foreach (Type viewType in regressionSolutionEvaluationViewTypes)
    36         AddViewListViewItem(viewType);
     36        AddViewListViewItem(viewType, HeuristicLab.Common.Resources.VSImageLibrary.Graph);
    3737    }
    3838
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleModel.cs

    r6611 r6627  
    6161      models.Add(model);
    6262    }
     63    public void Remove(IClassificationModel model) {
     64      models.Remove(model);
     65    }
    6366
    6467    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleSolution.cs

    r6611 r6627  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HeuristicLab.Collections;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    3637  // [Creatable("Data Analysis")]
    3738  public sealed class ClassificationEnsembleSolution : ClassificationSolution, IClassificationEnsembleSolution {
    38 
    3939    public new IClassificationEnsembleModel Model {
    40       set { base.Model = value; }
    4140      get { return (IClassificationEnsembleModel)base.Model; }
     41    }
     42
     43    private readonly ItemCollection<IClassificationSolution> classificationSolutions;
     44    public IItemCollection<IClassificationSolution> ClassificationSolutions {
     45      get { return classificationSolutions; }
    4246    }
    4347
     
    4751    private Dictionary<IClassificationModel, IntRange> testPartitions;
    4852
    49 
    5053    [StorableConstructor]
    51     private ClassificationEnsembleSolution(bool deserializing) : base(deserializing) { }
     54    private ClassificationEnsembleSolution(bool deserializing)
     55      : base(deserializing) {
     56      classificationSolutions = new ItemCollection<IClassificationSolution>();
     57    }
     58    [StorableHook(HookType.AfterDeserialization)]
     59    private void AfterDeserialization() {
     60      foreach (var model in Model.Models) {
     61        IClassificationProblemData problemData = (IClassificationProblemData)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        classificationSolutions.Add(model.CreateClassificationSolution(problemData));
     68      }
     69      RegisterClassificationSolutionsEventHandler();
     70    }
     71
    5272    private ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
    5373      : base(original, cloner) {
     
    6080        testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
    6181      }
    62       RecalculateResults();
    63     }
     82
     83      classificationSolutions = cloner.Clone(original.classificationSolutions);
     84      RegisterClassificationSolutionsEventHandler();
     85    }
     86
    6487    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
    65       : base(new ClassificationEnsembleModel(models), new ClassificationEnsembleProblemData(problemData)) {
    66       this.name = ItemName;
    67       this.description = ItemDescription;
    68       trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
    69       testPartitions = new Dictionary<IClassificationModel, IntRange>();
    70       foreach (var model in models) {
    71         trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
    72         testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
    73       }
    74       RecalculateResults();
    75     }
     88      : this(models, problemData,
     89             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
     90             models.Select(m => (IntRange)problemData.TestPartition.Clone())
     91      ) { }
    7692
    7793    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
    78       : base(new ClassificationEnsembleModel(models), new ClassificationEnsembleProblemData(problemData)) {
     94      : base(new ClassificationEnsembleModel(Enumerable.Empty<IClassificationModel>()), new ClassificationEnsembleProblemData(problemData)) {
    7995      this.trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
    8096      this.testPartitions = new Dictionary<IClassificationModel, IntRange>();
    81       AddModelsAndPartitions(models,
    82         trainingPartitions,
    83         testPartitions);
    84       RecalculateResults();
     97      this.classificationSolutions = new ItemCollection<IClassificationSolution>();
     98
     99      List<IClassificationSolution> solutions = new List<IClassificationSolution>();
     100      var modelEnumerator = models.GetEnumerator();
     101      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
     102      var testPartitionEnumerator = testPartitions.GetEnumerator();
     103
     104      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
     105        var p = (IClassificationProblemData)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.CreateClassificationSolution(p));
     112      }
     113      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
     114        throw new ArgumentException();
     115      }
     116
     117      RegisterClassificationSolutionsEventHandler();
     118      classificationSolutions.AddRange(solutions);
    85119    }
    86120
     
    88122      return new ClassificationEnsembleSolution(this, cloner);
    89123    }
     124    private void RegisterClassificationSolutionsEventHandler() {
     125      classificationSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsAdded);
     126      classificationSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsRemoved);
     127      classificationSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CollectionReset);
     128    }
    90129
    91130    protected override void RecalculateResults() {
     
    93132    }
    94133
     134    #region Evaluation
    95135    public override IEnumerable<double> EstimatedTrainingClassValues {
    96136      get {
     
    166206      .First();
    167207    }
    168 
    169     public void AddModelsAndPartitions(IEnumerable<IClassificationSolution> solutions) {
    170       foreach (var solution in solutions) {
    171         var ensembleSolution = solution as ClassificationEnsembleSolution;
    172         if (ensembleSolution != null) {
    173           var data = from m in ensembleSolution.Model.Models
    174                      let train = ensembleSolution.trainingPartitions[m]
    175                      let test = ensembleSolution.testPartitions[m]
    176                      select new { m, train, test };
    177 
    178           foreach (var d in data) {
    179             Model.Add(d.m);
    180             trainingPartitions[d.m] = (IntRange)d.train.Clone();
    181             testPartitions[d.m] = (IntRange)d.test.Clone();
    182           }
    183         } else {
    184           Model.Add(solution.Model);
    185           trainingPartitions[solution.Model] = (IntRange)solution.ProblemData.TrainingPartition.Clone();
    186           testPartitions[solution.Model] = (IntRange)solution.ProblemData.TestPartition.Clone();
    187         }
    188       }
    189 
     208    #endregion
     209
     210    public void AddClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
     211      classificationSolutions.AddRange(solutions);
     212    }
     213    public void RemoveClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
     214      classificationSolutions.RemoveRange(solutions);
     215    }
     216
     217    private void classificationSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
     218      foreach (var solution in e.Items) AddClassificationSolution(solution);
    190219      RecalculateResults();
    191220    }
    192 
    193     private void AddModelsAndPartitions(IEnumerable<IClassificationModel> models, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions) {
    194       var modelEnumerator = models.GetEnumerator();
    195       var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
    196       var testPartitionEnumerator = testPartitions.GetEnumerator();
    197 
    198       while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
    199         this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
    200         this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
    201       }
    202       if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
    203         throw new ArgumentException();
    204       }
     221    private void classificationSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
     222      foreach (var solution in e.Items) RemoveClassificationSolution(solution);
     223      RecalculateResults();
     224    }
     225    private void classificationSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
     226      foreach (var solution in e.OldItems) RemoveClassificationSolution(solution);
     227      foreach (var solution in e.Items) AddClassificationSolution(solution);
     228      RecalculateResults();
     229    }
     230
     231    private void AddClassificationSolution(IClassificationSolution solution) {
     232      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
     233      Model.Add(solution.Model);
     234      trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
     235      testPartitions[solution.Model] = solution.ProblemData.TestPartition;
     236    }
     237
     238    private void RemoveClassificationSolution(IClassificationSolution solution) {
     239      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
     240      Model.Remove(solution.Model);
     241      trainingPartitions.Remove(solution.Model);
     242      testPartitions.Remove(solution.Model);
    205243    }
    206244  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleModel.cs

    r6611 r6627  
    7474      models.Add(model);
    7575    }
     76    public void Remove(IRegressionModel model) {
     77      models.Remove(model);
     78    }
    7679
    7780    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows) {
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleSolution.cs

    r6611 r6627  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HeuristicLab.Collections;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    4041    }
    4142
     43    private readonly ItemCollection<IRegressionSolution> regressionSolutions;
     44    public IItemCollection<IRegressionSolution> RegressionSolutions {
     45      get { return regressionSolutions; }
     46    }
     47
    4248    [Storable]
    4349    private Dictionary<IRegressionModel, IntRange> trainingPartitions;
     
    4652
    4753    [StorableConstructor]
    48     private RegressionEnsembleSolution(bool deserializing) : base(deserializing) { }
     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
    4972    private RegressionEnsembleSolution(RegressionEnsembleSolution original, Cloner cloner)
    5073      : base(original, cloner) {
     
    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       AddModelsAndPartitions(models,
    67         from m in models select (IntRange)problemData.TrainingPartition.Clone(),
    68         from m in models select (IntRange)problemData.TestPartition.Clone());
    69       RecalculateResults();
    70     }
     88      : this(models, problemData,
     89             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
     90             models.Select(m => (IntRange)problemData.TestPartition.Clone())
     91      ) { }
    7192
    7293    public RegressionEnsembleSolution(IEnumerable<IRegressionModel> models, IRegressionProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
    73       : base(new RegressionEnsembleModel(models), new RegressionEnsembleProblemData(problemData)) {
     94      : base(new RegressionEnsembleModel(Enumerable.Empty<IRegressionModel>()), new RegressionEnsembleProblemData(problemData)) {
    7495      this.trainingPartitions = new Dictionary<IRegressionModel, IntRange>();
    7596      this.testPartitions = new Dictionary<IRegressionModel, IntRange>();
    76       AddModelsAndPartitions(models, trainingPartitions, testPartitions);
    77       RecalculateResults();
     97      this.regressionSolutions = new ItemCollection<IRegressionSolution>();
     98
     99      List<IRegressionSolution> solutions = new List<IRegressionSolution>();
     100      var modelEnumerator = models.GetEnumerator();
     101      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
     102      var testPartitionEnumerator = testPartitions.GetEnumerator();
     103
     104      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
     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));
     112      }
     113      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
     114        throw new ArgumentException();
     115      }
     116
     117      RegisterRegressionSolutionsEventHandler();
     118      regressionSolutions.AddRange(solutions);
    78119    }
    79120
     
    81122      return new RegressionEnsembleSolution(this, cloner);
    82123    }
     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    }
    83129
    84130    protected override void RecalculateResults() {
     
    86132    }
    87133
     134    #region Evaluation
    88135    public override IEnumerable<double> EstimatedTrainingValues {
    89136      get {
     
    154201      return estimatedValues.DefaultIfEmpty(double.NaN).Average();
    155202    }
    156 
    157 
    158     public void AddModelsAndPartitions(IEnumerable<IRegressionSolution> solutions) {
    159       foreach (var solution in solutions) {
    160         var ensembleSolution = solution as RegressionEnsembleSolution;
    161         if (ensembleSolution != null) {
    162           var data = from m in ensembleSolution.Model.Models
    163                      let train = ensembleSolution.trainingPartitions[m]
    164                      let test = ensembleSolution.testPartitions[m]
    165                      select new { m, train, test };
    166 
    167           foreach (var d in data) {
    168             Model.Add(d.m);
    169             trainingPartitions[d.m] = (IntRange)d.train.Clone();
    170             testPartitions[d.m] = (IntRange)d.test.Clone();
    171           }
    172         } else {
    173           Model.Add(solution.Model);
    174           trainingPartitions[solution.Model] = (IntRange)solution.ProblemData.TrainingPartition.Clone();
    175           testPartitions[solution.Model] = (IntRange)solution.ProblemData.TestPartition.Clone();
    176         }
    177       }
    178 
     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);
    179215      RecalculateResults();
    180216    }
    181 
    182     private void AddModelsAndPartitions(IEnumerable<IRegressionModel> models, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions) {
    183       var modelEnumerator = models.GetEnumerator();
    184       var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
    185       var testPartitionEnumerator = testPartitions.GetEnumerator();
    186 
    187       while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
    188         this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
    189         this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
    190       }
    191       if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
    192         throw new ArgumentException();
    193       }
     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);
    194239    }
    195240  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationEnsembleModel.cs

    r6569 r6627  
    2424  public interface IClassificationEnsembleModel : IClassificationModel {
    2525    void Add(IClassificationModel model);
     26    void Remove(IClassificationModel model);
    2627    IEnumerable<IClassificationModel> Models { get; }
    2728    IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows);
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationEnsembleSolution.cs

    r6239 r6627  
    2121
    2222using System.Collections.Generic;
     23using HeuristicLab.Core;
    2324namespace HeuristicLab.Problems.DataAnalysis {
    2425  public interface IClassificationEnsembleSolution : IClassificationSolution {
    2526    new IClassificationEnsembleModel Model { get; }
     27    IItemCollection<IClassificationSolution> ClassificationSolutions { get; }
    2628    IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows);
    2729  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionEnsembleModel.cs

    r6569 r6627  
    2424  public interface IRegressionEnsembleModel : IRegressionModel {
    2525    void Add(IRegressionModel model);
     26    void Remove(IRegressionModel model);
    2627    IEnumerable<IRegressionModel> Models { get; }
    2728    IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows);
  • branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionEnsembleSolution.cs

    r6233 r6627  
    2121
    2222using System.Collections.Generic;
     23using HeuristicLab.Core;
    2324namespace HeuristicLab.Problems.DataAnalysis {
    2425  public interface IRegressionEnsembleSolution : IRegressionSolution {
    2526    new IRegressionEnsembleModel Model { get; }
     27    IItemCollection<IRegressionSolution> RegressionSolutions { get; }
    2628    IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(Dataset dataset, IEnumerable<int> rows);
    2729  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.QuadraticAssignment/3.3/QAPPermutationProximityCalculator.cs

    r6416 r6627  
    4444
    4545    public static double CalculatePhenotypeDistance(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) {
    46       Dictionary<string, int> alleles = new Dictionary<string, int>();
    47       int distance = 0;
    48       for (int x = 0; x < a.Length; x++) {
    49         for (int y = 0; y < a.Length; y++) {
    50           string alleleA = weights[x, y].ToString() + ">" + distances[a[x], a[y]].ToString();
    51           string alleleB = weights[x, y].ToString() + ">" + distances[b[x], b[y]].ToString();
    52           if (alleleA == alleleB) continue;
     46      Dictionary<double, Dictionary<double, int>> alleles = new Dictionary<double, Dictionary<double, int>>();
     47      int distance = 0, len = a.Length;
     48      for (int x = 0; x < len; x++) {
     49        for (int y = 0; y < len; y++) {
     50          // there's a limited universe of double values as they're all drawn from the same matrix
     51          double dA = distances[a[x], a[y]], dB = distances[b[x], b[y]];
     52          if (dA == dB) continue;
     53
     54          Dictionary<double, int> dAlleles;
     55          if (!alleles.ContainsKey(weights[x, y])) {
     56            dAlleles = new Dictionary<double, int>();
     57            alleles.Add(weights[x, y], dAlleles);
     58          } else dAlleles = alleles[weights[x, y]];
    5359
    5460          int countA = 1, countB = -1;
    55           if (alleles.ContainsKey(alleleA)) countA += alleles[alleleA];
    56           if (alleles.ContainsKey(alleleB)) countB += alleles[alleleB];
     61
     62          if (dAlleles.ContainsKey(dA)) countA += dAlleles[dA];
     63          if (dAlleles.ContainsKey(dB)) countB += dAlleles[dB];
    5764
    5865          if (countA <= 0) distance--; // we've found in A an allele that was present in B
    5966          else distance++; // we've found in A a new allele
    60           alleles[alleleA] = countA;
     67          dAlleles[dA] = countA;
    6168
    6269          if (countB >= 0) distance--; // we've found in B an allele that was present in A
    6370          else distance++; // we've found in B a new allele
    64           alleles[alleleB] = countB;
     71          dAlleles[dB] = countB;
    6572        }
    6673      }
    67       return distance / (double)(2 * a.Length * a.Length);
     74      return distance / (double)(2 * len * len);
    6875    }
    6976  }
  • branches/QAPAlgorithms/HeuristicLab.Problems.VehicleRouting

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/QAPAlgorithms/HeuristicLab.Problems.VehicleRouting.Views

    • Property svn:mergeinfo changed (with no actual effect on merging)
Note: See TracChangeset for help on using the changeset viewer.