source: branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperimentDialog.cs @ 13806

Last change on this file since 13806 was 13806, checked in by bburlacu, 5 years ago

#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 size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.VariableInteractionNetworks {
32  public partial class CreateTargetVariationExperimentDialog : Form {
33    public Experiment Experiment { get; private set; }
34    private int repetitions;
35    private int combinationGroupSize;
36
37    public CreateTargetVariationExperimentDialog() : this(null) { }
38    public CreateTargetVariationExperimentDialog(IAlgorithm alg) {
39      InitializeComponent();
40      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));
48
49      Experiment = null;
50      okButton.Enabled = alg != null;
51    }
52
53    public Experiment CreateVariableCombinations(IAlgorithm algorithm) {
54      var experiment = new Experiment("Target Variation Experiment");
55
56      if (algorithm == null)
57        throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance.");
58
59      var problem = (IDataAnalysisProblem)algorithm.Problem;
60      var problemData = problem.ProblemData;
61
62      var variables = problemData.AllowedInputVariables.ToList();
63
64      for (int i = 0; i < variables.Count; i++) {
65        var target = variables[i];
66        var inputs = variables.Where(x => x != target).ToList();
67        if (combinationGroupSize > inputs.Count)
68          combinationGroupSize = inputs.Count;
69
70        var combinations = inputs.Combinations(combinationGroupSize);
71
72        foreach (var combination in combinations) {
73          var h = new HashSet<string>(combination);
74          var alg = (IAlgorithm)algorithm.Clone();
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));
80          }
81          SetTargetName(problemData, target);
82
83          var batchrun = new BatchRun(string.Format("Batchrun {0}", i + 1)) {
84            Repetitions = repetitions,
85            Optimizer = alg
86          };
87
88          experiment.Optimizers.Add(batchrun);
89        }
90      }
91      return experiment;
92    }
93
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
119    private void okButton_Click(object sender, EventArgs e) {
120      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
121      if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IAlgorithm) && !activeView.Locked) {
122        Experiment = CreateVariableCombinations((IAlgorithm)activeView.Content);
123        DialogResult = DialogResult.OK;
124        Close();
125      }
126    }
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
157  }
158}
Note: See TracBrowser for help on using the repository browser.