[13664] | 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 |
|
---|
| 22 | using System;
|
---|
[13788] | 23 | using System.Collections.Generic;
|
---|
[13814] | 24 | using System.ComponentModel;
|
---|
[12320] | 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
[13806] | 27 | using HeuristicLab.Common;
|
---|
[13664] | 28 | using HeuristicLab.MainForm;
|
---|
[12320] | 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 31 |
|
---|
[13664] | 32 | namespace HeuristicLab.VariableInteractionNetworks {
|
---|
| 33 | public partial class CreateTargetVariationExperimentDialog : Form {
|
---|
| 34 | public Experiment Experiment { get; private set; }
|
---|
[12320] | 35 |
|
---|
[13664] | 36 | public CreateTargetVariationExperimentDialog() : this(null) { }
|
---|
| 37 | public CreateTargetVariationExperimentDialog(IAlgorithm alg) {
|
---|
| 38 | InitializeComponent();
|
---|
[12320] | 39 |
|
---|
[13806] | 40 | var problem = alg.Problem as IDataAnalysisProblem;
|
---|
[13814] | 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();
|
---|
[13806] | 46 |
|
---|
[13664] | 47 | Experiment = null;
|
---|
| 48 | okButton.Enabled = alg != null;
|
---|
| 49 | }
|
---|
[12320] | 50 |
|
---|
[13814] | 51 | public static IEnumerable<IOptimizer> CreateVariableCombinations(IAlgorithm algorithm, int combinationGroupSize, int repetitions) {
|
---|
[13664] | 52 | if (algorithm == null)
|
---|
| 53 | throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance.");
|
---|
[12320] | 54 |
|
---|
[13806] | 55 | var problem = (IDataAnalysisProblem)algorithm.Problem;
|
---|
| 56 | var problemData = problem.ProblemData;
|
---|
[12320] | 57 |
|
---|
[13806] | 58 | var variables = problemData.AllowedInputVariables.ToList();
|
---|
[13814] | 59 | int count = 1;
|
---|
| 60 | foreach (var target in variables) {
|
---|
[13788] | 61 | var inputs = variables.Where(x => x != target).ToList();
|
---|
[13806] | 62 | var combinations = inputs.Combinations(combinationGroupSize);
|
---|
| 63 |
|
---|
[13788] | 64 | foreach (var combination in combinations) {
|
---|
| 65 | var h = new HashSet<string>(combination);
|
---|
| 66 | var alg = (IAlgorithm)algorithm.Clone();
|
---|
[13806] | 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));
|
---|
[13788] | 72 | }
|
---|
[13806] | 73 | SetTargetName(problemData, target);
|
---|
[12320] | 74 |
|
---|
[13814] | 75 | var batchRun = new BatchRun(string.Format("Batchrun {0}", count++)) {
|
---|
[13788] | 76 | Repetitions = repetitions,
|
---|
| 77 | Optimizer = alg
|
---|
| 78 | };
|
---|
[13814] | 79 | yield return batchRun;
|
---|
[13788] | 80 | }
|
---|
[13664] | 81 | }
|
---|
[12320] | 82 | }
|
---|
[13664] | 83 |
|
---|
[13806] | 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
|
---|
[13814] | 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;
|
---|
[13664] | 115 | }
|
---|
| 116 | }
|
---|
[13806] | 117 |
|
---|
[13814] | 118 | private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
|
---|
| 119 | if (repetitionsNumericUpDown.Text == string.Empty)
|
---|
| 120 | repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[13806] | 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;
|
---|
[13814] | 129 | var inputCount = problem.ProblemData.AllowedInputVariables.Count() - 1;
|
---|
[13806] | 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();
|
---|
[13814] | 137 |
|
---|
[13806] | 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;
|
---|
[13814] | 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();
|
---|
[13806] | 146 | }
|
---|
[13814] | 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;
|
---|
[13821] | 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;
|
---|
[13814] | 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 | }
|
---|
[13806] | 211 | #endregion
|
---|
[13664] | 212 | }
|
---|
| 213 | }
|
---|