Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7770 for branches


Ignore:
Timestamp:
04/30/12 12:52:09 (13 years ago)
Author:
sforsten
Message:

#1784:

  • added some regions for readability
  • added import and export methods in DataAnalysisProblem and SymbolicDataAnalysisProblem to reduce code duplication
  • added a recursive and an iterative approach without many linq expression to generate all combinations of list elements in ValueGenerator
Location:
branches/ProblemInstancesRegressionAndClassification
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs

    r7759 r7770  
    111111    }
    112112
     113    #region Import & Export
    113114    public void Load(ClassificationProblemData data) {
    114       Name = data.Name;
    115       Description = data.Description;
    116       ProblemData = new ClassificationProblemData(data.Dataset, data.AllowedInputVariables, data.TargetVariable);
    117       ProblemData.TrainingPartition.Start = data.TrainingPartition.Start;
    118       ProblemData.TrainingPartition.End = data.TrainingPartition.End;
    119       ProblemData.TestPartition.Start = data.TestPartition.Start;
    120       ProblemData.TestPartition.End = data.TestPartition.End;
    121       OnReset();
     115      base.Load(data);
    122116    }
    123117
    124118    public ClassificationProblemData Export() {
    125       ClassificationProblemData claData = new ClassificationProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
    126       claData.Name = Name;
    127       claData.Description = Description;
    128       claData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
    129       claData.TrainingPartition.End = ProblemData.TrainingPartition.End;
    130       claData.TestPartition.Start = ProblemData.TestPartition.Start;
    131       claData.TestPartition.End = ProblemData.TestPartition.End;
    132       return claData;
     119      if (ProblemData is ClassificationProblemData) {
     120        return (ClassificationProblemData)ProblemData;
     121      } else {
     122        ClassificationProblemData claData = new ClassificationProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
     123        return (ClassificationProblemData)base.Export(claData);
     124      }
    133125    }
     126    #endregion
    134127  }
    135128}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs

    r7759 r7770  
    114114    }
    115115
     116    #region Import & Export
    116117    public void Load(RegressionProblemData data) {
    117       Name = data.Name;
    118       Description = data.Description;
    119       ProblemData = new RegressionProblemData(data.Dataset, data.AllowedInputVariables, data.TargetVariable);
    120       ProblemData.TrainingPartition.Start = data.TrainingPartition.Start;
    121       ProblemData.TrainingPartition.End = data.TrainingPartition.End;
    122       ProblemData.TestPartition.Start = data.TestPartition.Start;
    123       ProblemData.TestPartition.End = data.TestPartition.End;
    124       OnReset();
     118      base.Load(data);
    125119    }
    126120
    127121    public RegressionProblemData Export() {
    128       RegressionProblemData regData = new RegressionProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
    129       regData.Name = Name;
    130       regData.Description = Description;
    131       regData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
    132       regData.TrainingPartition.End = ProblemData.TrainingPartition.End;
    133       regData.TestPartition.Start = ProblemData.TestPartition.Start;
    134       regData.TestPartition.End = ProblemData.TestPartition.End;
    135       return regData;
     122      if (ProblemData is RegressionProblemData)
     123        return (RegressionProblemData)ProblemData;
     124      else {
     125        RegressionProblemData regData = new RegressionProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
     126        return (RegressionProblemData)base.Export(regData);
     127      }
    136128    }
     129    #endregion
    137130  }
    138131}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs

    r7758 r7770  
    319319      }
    320320    }
     321
     322    #region Import & Export
     323    protected void Load(T data) {
     324      Name = data.Name;
     325      Description = data.Description;
     326      ProblemData = data;
     327      OnReset();
     328    }
     329
     330    protected T Export(T data) {
     331      data.Name = Name;
     332      data.Description = Description;
     333      data.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
     334      data.TrainingPartition.End = ProblemData.TrainingPartition.End;
     335      data.TestPartition.Start = ProblemData.TestPartition.Start;
     336      data.TestPartition.End = ProblemData.TestPartition.End;
     337      return data;
     338    }
     339    #endregion
    321340  }
    322341}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblem.cs

    r7759 r7770  
    4343    }
    4444
     45    #region Import & Export
    4546    public void Load(ClassificationProblemData data) {
    46       Name = data.Name;
    47       Description = data.Description;
    48       ProblemData = new ClassificationProblemData(data.Dataset, data.AllowedInputVariables, data.TargetVariable);
    49       ProblemData.TrainingPartition.Start = data.TrainingPartition.Start;
    50       ProblemData.TrainingPartition.End = data.TrainingPartition.End;
    51       ProblemData.TestPartition.Start = data.TestPartition.Start;
    52       ProblemData.TestPartition.End = data.TestPartition.End;
    53       OnReset();
     47      base.Load(data);
    5448    }
    5549
    5650    public ClassificationProblemData Export() {
    57       ClassificationProblemData claData = new ClassificationProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
    58       claData.Name = Name;
    59       claData.Description = Description;
    60       claData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
    61       claData.TrainingPartition.End = ProblemData.TrainingPartition.End;
    62       claData.TestPartition.Start = ProblemData.TestPartition.Start;
    63       claData.TestPartition.End = ProblemData.TestPartition.End;
    64       return claData;
     51      if (ProblemData is ClassificationProblemData) {
     52        return (ClassificationProblemData)ProblemData;
     53      } else {
     54        ClassificationProblemData claData = new ClassificationProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
     55        return (ClassificationProblemData)base.Export(claData);
     56      }
    6557    }
     58    #endregion
    6659  }
    6760}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblem.cs

    r7758 r7770  
    9090      if (handler != null) handler(this, EventArgs.Empty);
    9191    }
     92
     93    #region Import & Export
     94    protected void Load(T data) {
     95      Name = data.Name;
     96      Description = data.Description;
     97      ProblemData = data;
     98      OnReset();
     99    }
     100
     101    protected T Export(T data) {
     102      data.Name = Name;
     103      data.Description = Description;
     104      data.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
     105      data.TrainingPartition.End = ProblemData.TrainingPartition.End;
     106      data.TestPartition.Start = ProblemData.TestPartition.Start;
     107      data.TestPartition.End = ProblemData.TestPartition.End;
     108      return data;
     109    }
     110    #endregion
    92111  }
    93112}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblem.cs

    r7759 r7770  
    4343    }
    4444
     45    #region Import & Export
    4546    public void Load(RegressionProblemData data) {
    46       Name = data.Name;
    47       Description = data.Description;
    48       ProblemData = new RegressionProblemData(data.Dataset, data.AllowedInputVariables, data.TargetVariable);
    49       ProblemData.TrainingPartition.Start = data.TrainingPartition.Start;
    50       ProblemData.TrainingPartition.End = data.TrainingPartition.End;
    51       ProblemData.TestPartition.Start = data.TestPartition.Start;
    52       ProblemData.TestPartition.End = data.TestPartition.End;
    53       OnReset();
     47      base.Load(data);
    5448    }
    5549
    5650    public RegressionProblemData Export() {
    57       RegressionProblemData regData = new RegressionProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
    58       regData.Name = Name;
    59       regData.Description = Description;
    60       regData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
    61       regData.TrainingPartition.End = ProblemData.TrainingPartition.End;
    62       regData.TestPartition.Start = ProblemData.TestPartition.Start;
    63       regData.TestPartition.End = ProblemData.TestPartition.End;
    64       return regData;
     51      if (ProblemData is RegressionProblemData) {
     52        return (RegressionProblemData)ProblemData;
     53      } else {
     54        RegressionProblemData regData = new RegressionProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
     55        return (RegressionProblemData)base.Export(regData);
     56      }
    6557    }
     58    #endregion
    6659  }
    6760}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Regression/3.4/ValueGenerator.cs

    r7759 r7770  
    2020#endregion
    2121
    22 
    2322using System;
    2423using System.Collections.Generic;
    2524using System.Linq;
    2625using HeuristicLab.Random;
     26
    2727namespace HeuristicLab.Problems.Instances.Regression {
    2828  public static class ValueGenerator {
     
    4545    }
    4646
    47     public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> sets) {
     47    // iterative approach
     48    public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
     49      List<List<double>> allCombinations = new List<List<double>>();
     50      if (lists.Count < 1) {
     51        return allCombinations;
     52      }
     53
     54      List<IEnumerator<double>> enumerators = new List<IEnumerator<double>>();
     55      foreach (var list in lists) {
     56        enumerators.Add(list.GetEnumerator());
     57      }
     58
     59      bool finished = !enumerators.All(x => x.MoveNext());
     60
     61      while (!finished) {
     62        allCombinations.Add(GetCurrentCombination(enumerators));
     63        finished = MoveNext(enumerators, lists);
     64      }
     65
     66      IEnumerable<IEnumerable<double>> res = (from i in Enumerable.Range(0, lists.Count)
     67                                              select (from list in allCombinations
     68                                                      select list.ElementAt(i)));
     69      return res;
     70    }
     71
     72    private static bool MoveNext(List<IEnumerator<double>> enumerators, List<List<double>> lists) {
     73      int cur = enumerators.Count - 1;
     74      while (cur >= 0 && !enumerators[cur].MoveNext()) {
     75        enumerators[cur] = lists[cur].GetEnumerator();
     76        enumerators[cur].MoveNext();
     77        cur--;
     78      }
     79      return cur < 0;
     80    }
     81
     82    private static List<double> GetCurrentCombination(List<IEnumerator<double>> enumerators) {
     83      List<double> combination = new List<double>();
     84      foreach (var enumerator in enumerators) {
     85        combination.Add(enumerator.Current);
     86      }
     87      return combination;
     88    }
     89
     90    //recursive approach
     91    /*public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
     92      int cur = 0;
     93      List<double> curCombination = new List<double>();
     94      List<List<double>> allCombinations = new List<List<double>>();
     95      if (lists.Count() > cur) {
     96        foreach (var item in lists[cur]) {
     97          curCombination.Clear();
     98          curCombination.Add(item);
     99          GetCombination(lists, cur + 1, curCombination, allCombinations);
     100        }
     101      }
     102
     103      IEnumerable<IEnumerable<double>> res = (from i in Enumerable.Range(0, lists.Count)
     104                                              select (from list in allCombinations
     105                                                      select list.ElementAt(i)));
     106
     107      return res;
     108    }
     109
     110    private static void GetCombination(List<List<double>> lists, int cur, List<double> curCombinations, List<List<double>> allCombinations) {
     111      if (lists.Count > cur) {
     112        foreach (var item in lists[cur]) {
     113          if (curCombinations.Count > cur) {
     114            curCombinations.RemoveAt(cur);
     115          }
     116          curCombinations.Add(item);
     117          GetCombination(lists, cur + 1, curCombinations, allCombinations);
     118        }
     119      } else {
     120        allCombinations.Add(new List<double>(curCombinations));
     121      }
     122    }    */
     123
     124    //original
     125    /*public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> sets) {
    48126
    49127      var combinations = new List<List<double>>();
     
    69147
    70148      return newCombinations.ToList();
    71     }
     149    }    */
    72150  }
    73151}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Views/3.4/ProblemInstanceConsumerView.cs

    r7698 r7770  
    6161    }
    6262
     63    private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
     64      var consumerTypes = Content.GetType().GetInterfaces()
     65        .Where(x => x.IsGenericType
     66          && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
     67
     68      if (consumerTypes.Any()) {
     69        var instanceTypes = consumerTypes
     70          .Select(x => x.GetGenericArguments().First())
     71          .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
     72
     73        foreach (var type in instanceTypes) {
     74          foreach (var provider in ApplicationManager.Manager.GetInstances(type))
     75            yield return (IProblemInstanceProvider)provider;
     76        }
     77      }
     78    }
     79
    6380    public bool ContainsProviders() {
    6481      return problemInstanceProviderComboBox.Items.Count > 0;
     
    102119    }
    103120
    104     private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
    105       var consumerTypes = Content.GetType().GetInterfaces()
    106         .Where(x => x.IsGenericType
    107           && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
    108 
    109       if (consumerTypes.Any()) {
    110         var instanceTypes = consumerTypes
    111           .Select(x => x.GetGenericArguments().First())
    112           .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
    113 
    114         foreach (var type in instanceTypes) {
    115           foreach (var provider in ApplicationManager.Manager.GetInstances(type))
    116             yield return (IProblemInstanceProvider)provider;
    117         }
    118       }
    119     }
    120 
     121    #region ToolTip
    121122    private void SetTooltip() {
    122123      toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
     
    143144      return toolTip;
    144145    }
     146    #endregion
    145147  }
    146148}
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Views/3.4/ProblemInstanceProviderViewGeneric.cs

    r7758 r7770  
    3939    }
    4040
     41    #region Importer & Exporter
    4142    protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
    4243    protected IProblemInstanceConsumer consumer;
     
    5859      }
    5960    }
     61    #endregion
    6062
    6163    public ProblemInstanceProviderViewGeneric() {
     
    9395    protected virtual void loadButton_Click(object sender, EventArgs e) {
    9496      var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
    95       var instance = Content.LoadData(descriptor);
     97      T instance = Content.LoadData(descriptor);
    9698      try {
    9799        GenericConsumer.Load(instance);
Note: See TracChangeset for help on using the changeset viewer.