Changeset 13814 for branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperimentDialog.cs
- Timestamp:
- 04/28/16 15:58:06 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperimentDialog.cs
r13806 r13814 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.Linq; 25 26 using System.Windows.Forms; … … 32 33 public partial class CreateTargetVariationExperimentDialog : Form { 33 34 public Experiment Experiment { get; private set; } 34 private int repetitions;35 private int combinationGroupSize;36 35 37 36 public CreateTargetVariationExperimentDialog() : this(null) { } 38 37 public CreateTargetVariationExperimentDialog(IAlgorithm alg) { 39 38 InitializeComponent(); 40 repetitions = (int)repetitionsNumericUpDown.Value;41 combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;42 39 43 40 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)); 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(); 48 46 49 47 Experiment = null; … … 51 49 } 52 50 53 public Experiment CreateVariableCombinations(IAlgorithm algorithm) { 54 var experiment = new Experiment("Target Variation Experiment"); 55 51 public static IEnumerable<IOptimizer> CreateVariableCombinations(IAlgorithm algorithm, int combinationGroupSize, int repetitions) { 56 52 if (algorithm == null) 57 53 throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance."); … … 61 57 62 58 var variables = problemData.AllowedInputVariables.ToList(); 63 64 for (int i = 0; i < variables.Count; i++) { 65 var target = variables[i]; 59 int count = 1; 60 foreach (var target in variables) { 66 61 var inputs = variables.Where(x => x != target).ToList(); 67 if (combinationGroupSize > inputs.Count)68 combinationGroupSize = inputs.Count;69 70 62 var combinations = inputs.Combinations(combinationGroupSize); 71 63 … … 81 73 SetTargetName(problemData, target); 82 74 83 var batch run = new BatchRun(string.Format("Batchrun {0}", i + 1)) {75 var batchRun = new BatchRun(string.Format("Batchrun {0}", count++)) { 84 76 Repetitions = repetitions, 85 77 Optimizer = alg 86 78 }; 87 88 experiment.Optimizers.Add(batchrun); 79 yield return batchRun; 89 80 } 90 81 } 91 return experiment;92 82 } 93 83 … … 117 107 118 108 #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 } 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(); 126 121 } 127 122 … … 132 127 var problem = algorithm.Problem as IDataAnalysisProblem; 133 128 if (problem == null) return; 134 var inputCount = problem.ProblemData.AllowedInputVariables.Count() ;129 var inputCount = problem.ProblemData.AllowedInputVariables.Count() - 1; 135 130 if (combinationGroupSizeNumericUpDown.Value > inputCount) 136 131 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 132 } 144 133 … … 146 135 if (combinationGroupSizeNumericUpDown.Text == string.Empty) 147 136 combinationGroupSizeNumericUpDown.Text = combinationGroupSizeNumericUpDown.Value.ToString(); 148 combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value; 137 149 138 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; 150 139 if (activeView == null) return; 151 140 var algorithm = (IAlgorithm)activeView.Content; 152 141 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)); 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 foreach (var optimizer in CreateVariableCombinations(algorithm, combinationGroupSize, repetitions)) { 159 experiment.Optimizers.Add(optimizer); 160 progress++; 161 worker.ReportProgress((int)Math.Round(100d * progress / totalNumberOfCombinations)); 162 } 163 Experiment = experiment; 164 } 165 166 private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { 167 if (InvokeRequired) { 168 Invoke((Action<int>)UpdateProgress, e.ProgressPercentage); 169 } else { 170 UpdateProgress(e.ProgressPercentage); 171 } 172 } 173 174 private void UpdateProgress(int percentage) { 175 progressBar.Value = percentage; 176 } 177 178 private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 179 if (e.Cancelled || e.Error != null) 180 return; 181 DialogResult = DialogResult.OK; 182 Close(); 183 } 184 185 private void okButton_Click(object sender, EventArgs args) { 186 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; 187 if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IAlgorithm) && !activeView.Locked) { 188 okButton.Enabled = false; 189 combinationsLabel.Visible = false; 190 combinationCountLabel.Visible = false; 191 progressBar.Visible = true; 192 worker.RunWorkerAsync(); 193 } 194 } 195 196 private void cancelButton_Click(object sender, EventArgs e) { 197 if (worker.IsBusy) 198 worker.CancelAsync(); 155 199 } 156 200 #endregion
Note: See TracChangeset
for help on using the changeset viewer.