Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/27/16 17:17:00 (8 years ago)
Author:
bburlacu
Message:

#2288: Remove TinySet.cs in favor of a more general method for generating k-combinations. Improve target variation experiment generation. Refactored code and avoided some corner case exceptions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperimentDialog.cs

    r13788 r13806  
    2424using System.Linq;
    2525using System.Windows.Forms;
     26using HeuristicLab.Common;
    2627using HeuristicLab.MainForm;
    2728using HeuristicLab.Optimization;
     
    3233    public Experiment Experiment { get; private set; }
    3334    private int repetitions;
    34     private int binomialCoefficient;
     35    private int combinationGroupSize;
    3536
    3637    public CreateTargetVariationExperimentDialog() : this(null) { }
     
    3839      InitializeComponent();
    3940      repetitions = (int)repetitionsNumericUpDown.Value;
     41      combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
     42
     43      var problem = alg.Problem as IDataAnalysisProblem;
     44      var inputCount = problem.ProblemData.AllowedInputVariables.Count();
     45      if (combinationGroupSize > inputCount)
     46        combinationGroupSize = inputCount;
     47      warningLabel.Text = string.Format("Warning: this will create {0} combinations.", EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize));
    4048
    4149      Experiment = null;
    4250      okButton.Enabled = alg != null;
    43     }
    44 
    45     private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
    46       if (repetitionsNumericUpDown.Text == string.Empty)
    47         repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
    48       repetitions = (int)repetitionsNumericUpDown.Value;
    49     }
    50 
    51     private void binomialCoefficientNumericUpDown_Validated(object sender, EventArgs e) {
    52       if (binomialCoeffNumericUpDown.Text == string.Empty)
    53         binomialCoeffNumericUpDown.Text = binomialCoeffNumericUpDown.Value.ToString();
    54       binomialCoefficient = (int)binomialCoeffNumericUpDown.Value;
    5551    }
    5652
     
    6157        throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance.");
    6258
    63       var regressionProblem = algorithm.Problem as IRegressionProblem;
    64       if (regressionProblem == null)
    65         throw new ArgumentNullException("The algorithm does not contain a valid problem instance.");
     59      var problem = (IDataAnalysisProblem)algorithm.Problem;
     60      var problemData = problem.ProblemData;
    6661
    67       var problemData = regressionProblem.ProblemData;
    68       var variables = problemData.Dataset.DoubleVariables.ToList();
     62      var variables = problemData.AllowedInputVariables.ToList();
    6963
    7064      for (int i = 0; i < variables.Count; i++) {
    7165        var target = variables[i];
    7266        var inputs = variables.Where(x => x != target).ToList();
    73         var combinations = Util.Combinations(inputs, binomialCoefficient);
     67        if (combinationGroupSize > inputs.Count)
     68          combinationGroupSize = inputs.Count;
     69
     70        var combinations = inputs.Combinations(combinationGroupSize);
    7471
    7572        foreach (var combination in combinations) {
    7673          var h = new HashSet<string>(combination);
    7774          var alg = (IAlgorithm)algorithm.Clone();
    78           var pd = ((IRegressionProblem)alg.Problem).ProblemData;
    79           pd.TargetVariable = target;
    80           alg.Name += "{Target = " + target + ", Inputs = (" + combination.Aggregate((a, b) => a + "," + b) + ")}";
    81 
    82           foreach (var item in pd.InputVariables) {
    83             pd.InputVariables.SetItemCheckedState(item, h.Contains(item.Value));
     75          alg.Name += " {Target = " + target + ", Inputs = (" + combination.Aggregate((a, b) => a + "," + b) + ")}";
     76          problem = (IDataAnalysisProblem)alg.Problem;
     77          problemData = problem.ProblemData;
     78          foreach (var item in problemData.InputVariables) {
     79            problemData.InputVariables.SetItemCheckedState(item, h.Contains(item.Value));
    8480          }
     81          SetTargetName(problemData, target);
    8582
    8683          var batchrun = new BatchRun(string.Format("Batchrun {0}", i + 1)) {
     
    9592    }
    9693
     94    private static string GetTargetName(IDataAnalysisProblemData problemData) {
     95      var regressionProblemData = problemData as IRegressionProblemData;
     96      if (regressionProblemData != null)
     97        return regressionProblemData.TargetVariable;
     98      var classificationProblemData = problemData as IClassificationProblemData;
     99      if (classificationProblemData != null)
     100        return classificationProblemData.TargetVariable;
     101      throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
     102    }
     103
     104    private static void SetTargetName(IDataAnalysisProblemData problemData, string targetName) {
     105      var regressionProblemData = problemData as IRegressionProblemData;
     106      if (regressionProblemData != null) {
     107        regressionProblemData.TargetVariable = targetName;
     108        return;
     109      }
     110      var classificationProblemData = problemData as IClassificationProblemData;
     111      if (classificationProblemData != null) {
     112        classificationProblemData.TargetVariable = targetName;
     113        return;
     114      }
     115      throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
     116    }
     117
     118    #region events
    97119    private void okButton_Click(object sender, EventArgs e) {
    98120      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    99       if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked) {
     121      if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IAlgorithm) && !activeView.Locked) {
    100122        Experiment = CreateVariableCombinations((IAlgorithm)activeView.Content);
    101123        DialogResult = DialogResult.OK;
     
    103125      }
    104126    }
     127
     128    private void combinationGroupSizeNumericUpDown_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
     129      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     130      if (activeView == null) return;
     131      var algorithm = (IAlgorithm)activeView.Content;
     132      var problem = algorithm.Problem as IDataAnalysisProblem;
     133      if (problem == null) return;
     134      var inputCount = problem.ProblemData.AllowedInputVariables.Count();
     135      if (combinationGroupSizeNumericUpDown.Value > inputCount)
     136        combinationGroupSizeNumericUpDown.Value = inputCount;
     137    }
     138
     139    private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
     140      if (repetitionsNumericUpDown.Text == string.Empty)
     141        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
     142      repetitions = (int)repetitionsNumericUpDown.Value;
     143    }
     144
     145    private void combinationGroupSizeNumericUpDown_Validated(object sender, EventArgs e) {
     146      if (combinationGroupSizeNumericUpDown.Text == string.Empty)
     147        combinationGroupSizeNumericUpDown.Text = combinationGroupSizeNumericUpDown.Value.ToString();
     148      combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
     149      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     150      if (activeView == null) return;
     151      var algorithm = (IAlgorithm)activeView.Content;
     152      var problem = (IDataAnalysisProblem)algorithm.Problem;
     153      var inputCount = problem.ProblemData.AllowedInputVariables.Count();
     154      warningLabel.Text = string.Format("Warning: this will create {0} combinations.", EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize));
     155    }
     156    #endregion
    105157  }
    106158}
Note: See TracChangeset for help on using the changeset viewer.