Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7846


Ignore:
Timestamp:
05/18/12 14:08:11 (13 years ago)
Author:
abeham
Message:

#1851:

  • Moved common methods from problem instance management into a ProblemInstanceManager
  • Made experiment creation asynchronous by using a BackgroundWorker
  • Added a progress bar to display the progress as well as the instance that is currently processed
  • Added "select all" and "select none" options which are updated to the state of the listview
Location:
trunk/sources
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.Designer.cs

    r7841 r7846  
    4646    private void InitializeComponent() {
    4747      this.components = new System.ComponentModel.Container();
    48       System.Windows.Forms.ListViewGroup listViewGroup1 = new System.Windows.Forms.ListViewGroup("QAPLIB", System.Windows.Forms.HorizontalAlignment.Left);
    49       System.Windows.Forms.ListViewGroup listViewGroup2 = new System.Windows.Forms.ListViewGroup("TSPLIB", System.Windows.Forms.HorizontalAlignment.Left);
    50       System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("bur26a");
    51       System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("esc32a");
    52       System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("berlin52");
    53       System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("ch130");
    5448      this.okButton = new System.Windows.Forms.Button();
    5549      this.cancelButton = new System.Windows.Forms.Button();
     
    6256      this.instancesListView = new System.Windows.Forms.ListView();
    6357      this.columnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
     58      this.experimentCreationBackgroundWorker = new System.ComponentModel.BackgroundWorker();
     59      this.experimentCreationProgressBar = new System.Windows.Forms.ProgressBar();
     60      this.progressLabel = new System.Windows.Forms.Label();
     61      this.selectAllCheckBox = new System.Windows.Forms.CheckBox();
     62      this.selectNoneCheckBox = new System.Windows.Forms.CheckBox();
    6463      ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).BeginInit();
    6564      this.SuspendLayout();
     
    164163      this.instancesListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
    165164            this.columnHeader});
    166       listViewGroup1.Header = "QAPLIB";
    167       listViewGroup1.Name = "QAPLIBHeader";
    168       listViewGroup2.Header = "TSPLIB";
    169       listViewGroup2.Name = "TSPLIBHeader";
    170       listViewGroup2.Tag = "";
    171       this.instancesListView.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {
    172             listViewGroup1,
    173             listViewGroup2});
    174165      this.instancesListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
    175       listViewItem1.Checked = true;
    176       listViewItem1.Group = listViewGroup1;
    177       listViewItem1.StateImageIndex = 1;
    178       listViewItem2.Checked = true;
    179       listViewItem2.Group = listViewGroup1;
    180       listViewItem2.StateImageIndex = 1;
    181       listViewItem3.Group = listViewGroup2;
    182       listViewItem3.StateImageIndex = 0;
    183       listViewItem4.Group = listViewGroup2;
    184       listViewItem4.StateImageIndex = 0;
    185       this.instancesListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
    186             listViewItem1,
    187             listViewItem2,
    188             listViewItem3,
    189             listViewItem4});
    190       this.instancesListView.Location = new System.Drawing.Point(113, 54);
     166      this.instancesListView.Location = new System.Drawing.Point(113, 77);
    191167      this.instancesListView.Name = "instancesListView";
    192       this.instancesListView.Size = new System.Drawing.Size(156, 199);
     168      this.instancesListView.Size = new System.Drawing.Size(156, 176);
    193169      this.instancesListView.TabIndex = 6;
    194170      this.instancesListView.UseCompatibleStateImageBehavior = false;
    195171      this.instancesListView.View = System.Windows.Forms.View.Details;
     172      this.instancesListView.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.instancesListView_ItemChecked);
    196173      //
    197174      // columnHeader
    198175      //
    199176      this.columnHeader.Width = 150;
     177      //
     178      // experimentCreationBackgroundWorker
     179      //
     180      this.experimentCreationBackgroundWorker.WorkerReportsProgress = true;
     181      this.experimentCreationBackgroundWorker.WorkerSupportsCancellation = true;
     182      this.experimentCreationBackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.experimentCreationBackgroundWorker_DoWork);
     183      this.experimentCreationBackgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.experimentCreationBackgroundWorker_ProgressChanged);
     184      this.experimentCreationBackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.experimentCreationBackgroundWorker_RunWorkerCompleted);
     185      //
     186      // experimentCreationProgressBar
     187      //
     188      this.experimentCreationProgressBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
     189            | System.Windows.Forms.AnchorStyles.Right)));
     190      this.experimentCreationProgressBar.Location = new System.Drawing.Point(15, 259);
     191      this.experimentCreationProgressBar.Name = "experimentCreationProgressBar";
     192      this.experimentCreationProgressBar.Size = new System.Drawing.Size(92, 23);
     193      this.experimentCreationProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
     194      this.experimentCreationProgressBar.TabIndex = 7;
     195      this.experimentCreationProgressBar.Visible = false;
     196      //
     197      // progressLabel
     198      //
     199      this.progressLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
     200      this.progressLabel.BackColor = System.Drawing.SystemColors.Control;
     201      this.progressLabel.Location = new System.Drawing.Point(113, 259);
     202      this.progressLabel.Name = "progressLabel";
     203      this.progressLabel.Size = new System.Drawing.Size(75, 23);
     204      this.progressLabel.TabIndex = 8;
     205      this.progressLabel.Text = "label1";
     206      this.progressLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
     207      this.progressLabel.Visible = false;
     208      //
     209      // selectAllCheckBox
     210      //
     211      this.selectAllCheckBox.AutoSize = true;
     212      this.selectAllCheckBox.Location = new System.Drawing.Point(113, 54);
     213      this.selectAllCheckBox.Name = "selectAllCheckBox";
     214      this.selectAllCheckBox.Size = new System.Drawing.Size(67, 17);
     215      this.selectAllCheckBox.TabIndex = 9;
     216      this.selectAllCheckBox.Text = "select all";
     217      this.selectAllCheckBox.UseVisualStyleBackColor = true;
     218      this.selectAllCheckBox.CheckedChanged += new System.EventHandler(this.selectAllCheckBox_CheckedChanged);
     219      //
     220      // selectNoneCheckBox
     221      //
     222      this.selectNoneCheckBox.AutoSize = true;
     223      this.selectNoneCheckBox.Location = new System.Drawing.Point(186, 54);
     224      this.selectNoneCheckBox.Name = "selectNoneCheckBox";
     225      this.selectNoneCheckBox.Size = new System.Drawing.Size(81, 17);
     226      this.selectNoneCheckBox.TabIndex = 9;
     227      this.selectNoneCheckBox.Text = "select none";
     228      this.selectNoneCheckBox.UseVisualStyleBackColor = true;
     229      this.selectNoneCheckBox.CheckedChanged += new System.EventHandler(this.selectNoneCheckBox_CheckedChanged);
    200230      //
    201231      // CreateExperimentDialog
     
    206236      this.CancelButton = this.cancelButton;
    207237      this.ClientSize = new System.Drawing.Size(281, 294);
     238      this.Controls.Add(this.selectNoneCheckBox);
     239      this.Controls.Add(this.selectAllCheckBox);
    208240      this.Controls.Add(this.instancesListView);
    209241      this.Controls.Add(this.repetitionsNumericUpDown);
     
    214246      this.Controls.Add(this.cancelButton);
    215247      this.Controls.Add(this.okButton);
     248      this.Controls.Add(this.progressLabel);
     249      this.Controls.Add(this.experimentCreationProgressBar);
    216250      this.MaximizeBox = false;
    217251      this.MinimizeBox = false;
     
    221255      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
    222256      this.Text = "Create Experiment";
     257      this.TopMost = true;
     258      this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CreateExperimentDialog_FormClosing);
    223259      ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).EndInit();
    224260      this.ResumeLayout(false);
     
    239275    private System.Windows.Forms.ListView instancesListView;
    240276    private System.Windows.Forms.ColumnHeader columnHeader;
     277    private System.ComponentModel.BackgroundWorker experimentCreationBackgroundWorker;
     278    private System.Windows.Forms.ProgressBar experimentCreationProgressBar;
     279    private System.Windows.Forms.Label progressLabel;
     280    private System.Windows.Forms.CheckBox selectAllCheckBox;
     281    private System.Windows.Forms.CheckBox selectNoneCheckBox;
    241282
    242283  }
  • trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.cs

    r7841 r7846  
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
    2425using System.Linq;
     26using System.Threading;
    2527using System.Windows.Forms;
    2628using HeuristicLab.Optimization;
    27 using HeuristicLab.PluginInfrastructure;
    2829using HeuristicLab.Problems.Instances;
    2930
     
    3738        experiment = null;
    3839        okButton.Enabled = optimizer != null;
     40        FillOrHideInstanceListView();
    3941      }
    4042    }
     
    4547    }
    4648
     49    private bool createBatchRun;
     50    private int repetitions;
     51    private EventWaitHandle backgroundWorkerWaitHandle = new ManualResetEvent(false);
     52    private bool suppressListViewEventHandling;
     53
    4754    public CreateExperimentDialog() : this(null) { }
    4855    public CreateExperimentDialog(IOptimizer optimizer) {
    4956      InitializeComponent();
     57      createBatchRun = createBatchRunCheckBox.Checked;
     58      repetitions = (int)repetitionsNumericUpDown.Value;
    5059      Optimizer = optimizer;
    51       experiment = null;
    52       instancesListView.Items.Clear();
    53       instancesListView.Groups.Clear();
    54       FillOrHideInstanceListView();
    5560    }
    5661
    5762    private void FillOrHideInstanceListView() {
    58       if (Optimizer != null && optimizer is IAlgorithm) {
     63      if (optimizer != null && optimizer is IAlgorithm) {
    5964        var algorithm = (IAlgorithm)Optimizer;
    6065        if (algorithm.Problem != null) {
    61           var instanceProviders = GetProblemInstanceProviders(algorithm.Problem);
     66          var instanceProviders = ProblemInstanceManager.GetProviders(algorithm.Problem);
    6267          if (instanceProviders.Any()) {
    63             foreach (var provider in instanceProviders) {
    64               var group = new ListViewGroup(provider.Name, provider.Name);
    65               group.Tag = provider;
    66               instancesListView.Groups.Add(group);
    67               IEnumerable<IDataDescriptor> descriptors = ((dynamic)provider).GetDataDescriptors();
    68               foreach (var d in descriptors) {
    69                 var item = new ListViewItem(d.Name, group);
    70                 item.Checked = true;
    71                 item.Tag = d;
    72                 instancesListView.Items.Add(item);
    73               }
     68            FillInstanceListView(instanceProviders);
     69            if (instancesListView.Items.Count > 0) {
     70              selectAllCheckBox.Visible = true;
     71              selectNoneCheckBox.Visible = true;
     72              instancesLabel.Visible = true;
     73              instancesListView.Visible = true;
     74              Height = 330;
     75              return;
    7476            }
    75             instancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
    76             if (instancesListView.Items.Count > 0) return;
    7777          }
    7878        }
    7979      }
     80      selectAllCheckBox.Visible = false;
     81      selectNoneCheckBox.Visible = false;
    8082      instancesLabel.Visible = false;
    8183      instancesListView.Visible = false;
     
    8385    }
    8486
    85     private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders(IProblem problem) {
    86       var consumerTypes = problem.GetType().GetInterfaces()
    87         .Where(x => x.IsGenericType
    88           && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
    89 
    90       if (consumerTypes.Any()) {
    91         var instanceTypes = consumerTypes
    92           .Select(x => x.GetGenericArguments().First())
    93           .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
    94 
    95         foreach (var type in instanceTypes) {
    96           foreach (var provider in ApplicationManager.Manager.GetInstances(type))
    97             yield return (IProblemInstanceProvider)provider;
     87    private void FillInstanceListView(IEnumerable<IProblemInstanceProvider> instanceProviders) {
     88      foreach (var provider in instanceProviders) {
     89        var group = new ListViewGroup(provider.Name, provider.Name);
     90        group.Tag = provider;
     91        instancesListView.Groups.Add(group);
     92        foreach (var d in ProblemInstanceManager.GetDataDescriptors(provider)) {
     93          var item = new ListViewItem(d.Name, group);
     94          item.Tag = d;
     95          instancesListView.Items.Add(item);
    9896        }
    9997      }
     98      instancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
     99      selectAllCheckBox.Checked = true;
    100100    }
    101101
    102102    private void createBatchRunCheckBox_CheckedChanged(object sender, EventArgs e) {
    103103      repetitionsNumericUpDown.Enabled = createBatchRunCheckBox.Checked;
     104      createBatchRun = createBatchRunCheckBox.Checked;
    104105    }
    105106    private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
    106107      if (repetitionsNumericUpDown.Text == string.Empty)
    107108        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
     109      repetitions = (int)repetitionsNumericUpDown.Value;
     110    }
     111    private void selectAllCheckBox_CheckedChanged(object sender, EventArgs e) {
     112      if (selectAllCheckBox.Checked) {
     113        selectNoneCheckBox.Checked = false;
     114        if (instancesListView.CheckedItems.Count == instancesListView.Items.Count) return;
     115        try {
     116          suppressListViewEventHandling = true;
     117          foreach (var item in instancesListView.Items.OfType<ListViewItem>()) {
     118            item.Checked = true;
     119          }
     120        } finally { suppressListViewEventHandling = false; }
     121      }
     122    }
     123    private void selectNoneCheckBox_CheckedChanged(object sender, EventArgs e) {
     124      if (selectNoneCheckBox.Checked) {
     125        selectAllCheckBox.Checked = false;
     126        if (instancesListView.CheckedItems.Count == 0) return;
     127        try {
     128          suppressListViewEventHandling = true;
     129          foreach (var item in instancesListView.Items.OfType<ListViewItem>()) {
     130            item.Checked = false;
     131          }
     132        } finally { suppressListViewEventHandling = false; }
     133      }
     134    }
     135    private void instancesListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
     136      if (!suppressListViewEventHandling) {
     137        selectAllCheckBox.Checked = instancesListView.Items.Count == instancesListView.CheckedItems.Count;
     138        selectNoneCheckBox.Checked = instancesListView.CheckedItems.Count == 0;
     139      }
    108140    }
    109141    private void okButton_Click(object sender, EventArgs e) {
    110       experiment = new Experiment();
    111       if (instancesListView.CheckedItems.Count == 0) {
    112         AddOptimizer((IOptimizer)Optimizer.Clone());
     142      SetMode(createExperiment: true);
     143      experimentCreationBackgroundWorker.RunWorkerAsync(GetSelectedInstances());
     144      backgroundWorkerWaitHandle.WaitOne();
     145    }
     146    private void experimentCreationBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
     147      backgroundWorkerWaitHandle.Set();
     148      experimentCreationBackgroundWorker.ReportProgress(0, string.Empty);
     149      var items = (Dictionary<IProblemInstanceProvider, List<IDataDescriptor>>)e.Argument;
     150      var localExperiment = new Experiment();
     151      if (items.Count == 0) {
     152        AddOptimizer((IOptimizer)Optimizer.Clone(), localExperiment);
     153        experimentCreationBackgroundWorker.ReportProgress(100, string.Empty);
    113154      } else {
    114         foreach (var item in instancesListView.CheckedItems.OfType<ListViewItem>()) {
    115           var descriptor = (IDataDescriptor)item.Tag;
    116           var provider = (IProblemInstanceProvider)item.Group.Tag;
    117           var algorithm = (IAlgorithm)Optimizer.Clone();
    118           ((dynamic)algorithm.Problem).Load(((dynamic)provider).LoadData(descriptor));
    119           AddOptimizer(algorithm);
     155        int counter = 0, total = items.SelectMany(x => x.Value).Count();
     156        foreach (var provider in items.Keys) {
     157          foreach (var descriptor in items[provider]) {
     158            var algorithm = (IAlgorithm)Optimizer.Clone();
     159            ProblemInstanceManager.LoadData(provider, descriptor, (IProblemInstanceConsumer)algorithm.Problem);
     160            AddOptimizer(algorithm, localExperiment);
     161            counter++;
     162            experimentCreationBackgroundWorker.ReportProgress((int)Math.Round(100.0 * counter / total), descriptor.Name);
     163            if (experimentCreationBackgroundWorker.CancellationPending) {
     164              e.Cancel = true;
     165              localExperiment = null;
     166              break;
     167            }
     168          }
    120169        }
    121170      }
    122       Experiment.Prepare(true);
    123     }
    124 
    125     private void AddOptimizer(IOptimizer optimizer) {
    126       if (createBatchRunCheckBox.Checked) {
     171      if (localExperiment != null) localExperiment.Prepare(true);
     172      e.Result = localExperiment;
     173    }
     174    private void experimentCreationBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
     175      experimentCreationProgressBar.Value = e.ProgressPercentage;
     176      progressLabel.Text = (string)e.UserState;
     177    }
     178    private void experimentCreationBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
     179      SetMode(createExperiment: false);
     180      if (e.Error != null) MessageBox.Show(e.Error.Message, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
     181      if (!e.Cancelled && e.Error == null) {
     182        experiment = (Experiment)e.Result;
     183        DialogResult = System.Windows.Forms.DialogResult.OK;
     184        Close();
     185      }
     186    }
     187    private void CreateExperimentDialog_FormClosing(object sender, FormClosingEventArgs e) {
     188      if (experimentCreationBackgroundWorker.IsBusy) {
     189        if (DialogResult != System.Windows.Forms.DialogResult.OK)
     190          experimentCreationBackgroundWorker.CancelAsync();
     191        e.Cancel = true;
     192      }
     193    }
     194
     195    private void AddOptimizer(IOptimizer optimizer, Experiment experiment) {
     196      if (createBatchRun) {
    127197        var batchRun = new BatchRun();
    128         batchRun.Repetitions = (int)repetitionsNumericUpDown.Value;
     198        batchRun.Repetitions = repetitions;
    129199        batchRun.Optimizer = optimizer;
    130200        experiment.Optimizers.Add(batchRun);
     
    133203      }
    134204    }
     205
     206    private void SetMode(bool createExperiment) {
     207      createBatchRunCheckBox.Enabled = !createExperiment;
     208      repetitionsNumericUpDown.Enabled = !createExperiment;
     209      selectAllCheckBox.Enabled = !createExperiment;
     210      selectNoneCheckBox.Enabled = !createExperiment;
     211      instancesListView.Enabled = !createExperiment;
     212      okButton.Enabled = !createExperiment;
     213      okButton.Visible = !createExperiment;
     214      progressLabel.Visible = createExperiment;
     215      experimentCreationProgressBar.Visible = createExperiment;
     216    }
     217
     218    private Dictionary<IProblemInstanceProvider, List<IDataDescriptor>> GetSelectedInstances() {
     219      var selectedInstances = new Dictionary<IProblemInstanceProvider, List<IDataDescriptor>>();
     220      foreach (var checkedItem in instancesListView.CheckedItems.OfType<ListViewItem>()) {
     221        if (!selectedInstances.ContainsKey((IProblemInstanceProvider)checkedItem.Group.Tag))
     222          selectedInstances.Add((IProblemInstanceProvider)checkedItem.Group.Tag, new List<IDataDescriptor>());
     223        selectedInstances[(IProblemInstanceProvider)checkedItem.Group.Tag].Add((IDataDescriptor)checkedItem.Tag);
     224      }
     225      return selectedInstances;
     226    }
    135227  }
    136228}
  • trunk/sources/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceConsumerView.cs

    r7805 r7846  
    2828using HeuristicLab.MainForm;
    2929using HeuristicLab.MainForm.WindowsForms;
    30 using HeuristicLab.PluginInfrastructure;
    3130
    3231namespace HeuristicLab.Problems.Instances.Views {
     
    6362      } else {
    6463        problemInstanceProviderComboBox.DisplayMember = "Name";
    65         ProblemInstanceProviders = GetProblemInstanceProviders();
     64        ProblemInstanceProviders = ProblemInstanceManager.GetProviders(Content);
    6665        problemInstanceProviderComboBox.DataSource = ProblemInstanceProviders.OrderBy(x => x.Name).ToList();
    6766      }
    6867      SetEnabledStateOfControls();
    69     }
    70 
    71     private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {
    72       var consumerTypes = Content.GetType().GetInterfaces()
    73         .Where(x => x.IsGenericType
    74           && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));
    75 
    76       if (consumerTypes.Any()) {
    77         var instanceTypes = consumerTypes
    78           .Select(x => x.GetGenericArguments().First())
    79           .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));
    80 
    81         foreach (var type in instanceTypes) {
    82           foreach (var provider in ApplicationManager.Manager.GetInstances(type))
    83             yield return (IProblemInstanceProvider)provider;
    84         }
    85       }
    8668    }
    8769
  • trunk/sources/HeuristicLab.Problems.Instances/3.3/HeuristicLab.Problems.Instances-3.3.csproj

    r7825 r7846  
    105105  </PropertyGroup>
    106106  <ItemGroup>
     107    <Reference Include="Microsoft.CSharp" />
    107108    <Reference Include="System" />
    108109    <Reference Include="System.Core" />
     
    111112  <ItemGroup>
    112113    <None Include="Plugin.cs.frame" />
     114    <Compile Include="ProblemInstanceManager.cs" />
    113115    <Compile Include="IProblemInstanceExporter.cs" />
    114116    <Compile Include="TableFileParser.cs" />
Note: See TracChangeset for help on using the changeset viewer.