Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/28/16 15:58:06 (8 years ago)
Author:
bburlacu
Message:

#2288: Improve the CreateTargetVariationExperimentDialog

File:
1 edited

Legend:

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

    r13806 r13814  
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
    2425using System.Linq;
    2526using System.Windows.Forms;
     
    3233  public partial class CreateTargetVariationExperimentDialog : Form {
    3334    public Experiment Experiment { get; private set; }
    34     private int repetitions;
    35     private int combinationGroupSize;
    3635
    3736    public CreateTargetVariationExperimentDialog() : this(null) { }
    3837    public CreateTargetVariationExperimentDialog(IAlgorithm alg) {
    3938      InitializeComponent();
    40       repetitions = (int)repetitionsNumericUpDown.Value;
    41       combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
    4239
    4340      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();
    4846
    4947      Experiment = null;
     
    5149    }
    5250
    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) {
    5652      if (algorithm == null)
    5753        throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance.");
     
    6157
    6258      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) {
    6661        var inputs = variables.Where(x => x != target).ToList();
    67         if (combinationGroupSize > inputs.Count)
    68           combinationGroupSize = inputs.Count;
    69 
    7062        var combinations = inputs.Combinations(combinationGroupSize);
    7163
     
    8173          SetTargetName(problemData, target);
    8274
    83           var batchrun = new BatchRun(string.Format("Batchrun {0}", i + 1)) {
     75          var batchRun = new BatchRun(string.Format("Batchrun {0}", count++)) {
    8476            Repetitions = repetitions,
    8577            Optimizer = alg
    8678          };
    87 
    88           experiment.Optimizers.Add(batchrun);
     79          yield return batchRun;
    8980        }
    9081      }
    91       return experiment;
    9282    }
    9383
     
    117107
    118108    #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();
    126121    }
    127122
     
    132127      var problem = algorithm.Problem as IDataAnalysisProblem;
    133128      if (problem == null) return;
    134       var inputCount = problem.ProblemData.AllowedInputVariables.Count();
     129      var inputCount = problem.ProblemData.AllowedInputVariables.Count() - 1;
    135130      if (combinationGroupSizeNumericUpDown.Value > inputCount)
    136131        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;
    143132    }
    144133
     
    146135      if (combinationGroupSizeNumericUpDown.Text == string.Empty)
    147136        combinationGroupSizeNumericUpDown.Text = combinationGroupSizeNumericUpDown.Value.ToString();
    148       combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
     137
    149138      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    150139      if (activeView == null) return;
    151140      var algorithm = (IAlgorithm)activeView.Content;
    152141      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();
    155199    }
    156200    #endregion
Note: See TracChangeset for help on using the changeset viewer.