Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2288_HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/CreateTargetVariationExperimentDialog.cs @ 17189

Last change on this file since 17189 was 15929, checked in by bburlacu, 7 years ago

#2288: Rename branch and update references.

File size: 9.3 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.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28using HeuristicLab.MainForm;
29using HeuristicLab.Optimization;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.VariableInteractionNetworks {
33  public partial class CreateTargetVariationExperimentDialog : Form {
34    public Experiment Experiment { get; private set; }
35
36    public CreateTargetVariationExperimentDialog() : this(null) { }
37    public CreateTargetVariationExperimentDialog(IAlgorithm alg) {
38      InitializeComponent();
39
40      var problem = alg.Problem as IDataAnalysisProblem;
41      var inputCount = problem.ProblemData.AllowedInputVariables.Count() - 1; // -1 because one will be the target
42      if (combinationGroupSizeNumericUpDown.Value > inputCount)
43        combinationGroupSizeNumericUpDown.Value = inputCount;
44      var combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
45      combinationCountLabel.Text = ((inputCount + 1) * EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize)).ToString();
46
47      Experiment = null;
48      okButton.Enabled = alg != null;
49    }
50
51    public static IEnumerable<IOptimizer> CreateVariableCombinations(IAlgorithm algorithm, int combinationGroupSize, int repetitions) {
52      if (algorithm == null)
53        throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance.");
54
55      var problem = (IDataAnalysisProblem)algorithm.Problem;
56      var problemData = problem.ProblemData;
57
58      var variables = problemData.AllowedInputVariables.ToList();
59      int count = 1;
60      foreach (var target in variables) {
61        var inputs = variables.Where(x => x != target).ToList();
62        var combinations = inputs.Combinations(combinationGroupSize);
63
64        foreach (var combination in combinations) {
65          var h = new HashSet<string>(combination);
66          var alg = (IAlgorithm)algorithm.Clone();
67          alg.Name += " {Target = " + target + ", Inputs = (" + combination.Aggregate((a, b) => a + "," + b) + ")}";
68          problem = (IDataAnalysisProblem)alg.Problem;
69          problemData = problem.ProblemData;
70          foreach (var item in problemData.InputVariables) {
71            problemData.InputVariables.SetItemCheckedState(item, h.Contains(item.Value));
72          }
73          SetTargetName(problemData, target);
74
75          var batchRun = new BatchRun(string.Format("Batchrun {0}", count++)) {
76            Repetitions = repetitions,
77            Optimizer = alg
78          };
79          yield return batchRun;
80        }
81      }
82    }
83
84    private static string GetTargetName(IDataAnalysisProblemData problemData) {
85      var regressionProblemData = problemData as IRegressionProblemData;
86      if (regressionProblemData != null)
87        return regressionProblemData.TargetVariable;
88      var classificationProblemData = problemData as IClassificationProblemData;
89      if (classificationProblemData != null)
90        return classificationProblemData.TargetVariable;
91      throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
92    }
93
94    private static void SetTargetName(IDataAnalysisProblemData problemData, string targetName) {
95      var regressionProblemData = problemData as IRegressionProblemData;
96      if (regressionProblemData != null) {
97        regressionProblemData.TargetVariable = targetName;
98        return;
99      }
100      var classificationProblemData = problemData as IClassificationProblemData;
101      if (classificationProblemData != null) {
102        classificationProblemData.TargetVariable = targetName;
103        return;
104      }
105      throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
106    }
107
108    #region events
109    private void CreateTargetVariationExperimentDialog_FormClosing(object sender, FormClosingEventArgs e) {
110      if (worker.IsBusy) {
111        if (DialogResult != DialogResult.OK) {
112          if (worker.IsBusy) worker.CancelAsync();
113        }
114        e.Cancel = true;
115      }
116    }
117
118    private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
119      if (repetitionsNumericUpDown.Text == string.Empty)
120        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
121    }
122
123    private void combinationGroupSizeNumericUpDown_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
124      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
125      if (activeView == null) return;
126      var algorithm = (IAlgorithm)activeView.Content;
127      var problem = algorithm.Problem as IDataAnalysisProblem;
128      if (problem == null) return;
129      var inputCount = problem.ProblemData.AllowedInputVariables.Count() - 1;
130      if (combinationGroupSizeNumericUpDown.Value > inputCount)
131        combinationGroupSizeNumericUpDown.Value = inputCount;
132    }
133
134    private void combinationGroupSizeNumericUpDown_Validated(object sender, EventArgs e) {
135      if (combinationGroupSizeNumericUpDown.Text == string.Empty)
136        combinationGroupSizeNumericUpDown.Text = combinationGroupSizeNumericUpDown.Value.ToString();
137
138      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
139      if (activeView == null) return;
140      var algorithm = (IAlgorithm)activeView.Content;
141      var problem = (IDataAnalysisProblem)algorithm.Problem;
142      var inputCount = problem.ProblemData.AllowedInputVariables.Count() - 1;
143      if (combinationGroupSizeNumericUpDown.Value > inputCount)
144        combinationGroupSizeNumericUpDown.Value = inputCount;
145      combinationCountLabel.Text = ((inputCount + 1) * EnumerableExtensions.BinomialCoefficient(inputCount, (int)combinationGroupSizeNumericUpDown.Value)).ToString();
146    }
147
148    private void worker_DoWork(object sender, DoWorkEventArgs e) {
149      IContentView activeView = (IContentView)MainFormManager.MainForm.ActiveView;
150      var algorithm = (IAlgorithm)activeView.Content;
151      var problem = (IDataAnalysisProblem)algorithm.Problem;
152      var experiment = new Experiment("Target Variation Experiment");
153      var repetitions = (int)repetitionsNumericUpDown.Value;
154      var inputCount = problem.ProblemData.AllowedInputVariables.Count() - 1;
155      var combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
156      var totalNumberOfCombinations = (inputCount + 1) * EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize);
157      int progress = 0;
158      try {
159        foreach (var optimizer in CreateVariableCombinations(algorithm, combinationGroupSize, repetitions)) {
160          if (worker.CancellationPending)
161            throw new OperationCanceledException();
162          experiment.Optimizers.Add(optimizer);
163          progress++;
164          worker.ReportProgress((int)Math.Round(100d * progress / totalNumberOfCombinations));
165        }
166        Experiment = experiment;
167      } catch (OperationCanceledException) {
168        e.Cancel = true;
169        UpdateProgress(0);
170        progressBar.Visible = false;
171        okButton.Enabled = true;
172        combinationsLabel.Visible = true;
173        combinationCountLabel.Visible = true;
174      }
175    }
176
177    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
178      if (InvokeRequired) {
179        Invoke((Action<int>)UpdateProgress, e.ProgressPercentage);
180      } else {
181        UpdateProgress(e.ProgressPercentage);
182      }
183    }
184
185    private void UpdateProgress(int percentage) {
186      progressBar.Value = percentage;
187    }
188
189    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
190      if (e.Cancelled || e.Error != null)
191        return;
192      DialogResult = DialogResult.OK;
193      Close();
194    }
195
196    private void okButton_Click(object sender, EventArgs args) {
197      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
198      if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IAlgorithm) && !activeView.Locked) {
199        okButton.Enabled = false;
200        combinationsLabel.Visible = false;
201        combinationCountLabel.Visible = false;
202        progressBar.Visible = true;
203        worker.RunWorkerAsync();
204      }
205    }
206
207    private void cancelButton_Click(object sender, EventArgs e) {
208      if (worker.IsBusy)
209        worker.CancelAsync();
210    }
211    #endregion
212  }
213}
Note: See TracBrowser for help on using the repository browser.