Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.cs @ 7975

Last change on this file since 7975 was 7975, checked in by gkronber, 12 years ago

#1851: fixed cross-thread exception related to the backgroundworker for creating experiments.

File size: 35.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Globalization;
26using System.Linq;
27using System.Text;
28using System.Threading;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Problems.Instances;
35
36namespace HeuristicLab.Optimizer {
37  public partial class CreateExperimentDialog : Form {
38    private enum DialogMode { Normal = 1, DiscoveringInstances = 2, CreatingExperiment = 3, PreparingExperiment = 4 };
39
40    private IOptimizer optimizer;
41    public IOptimizer Optimizer {
42      get { return optimizer; }
43      set {
44        optimizer = value;
45        Experiment = null;
46        okButton.Enabled = optimizer != null;
47        SetTabControlVisibility();
48        FillInstanceTreeViewAsync();
49        FillParametersListView();
50      }
51    }
52
53    public Experiment Experiment { get; private set; }
54
55    private bool createBatchRun;
56    private int repetitions;
57    private Dictionary<IProblemInstanceProvider, HashSet<IDataDescriptor>> instances;
58    private Dictionary<IValueParameter, Tuple<int, int, int>> intParameters;
59    private Dictionary<IValueParameter, Tuple<double, double, double>> doubleParameters;
60    private HashSet<IValueParameter> boolParameters;
61    private Dictionary<IValueParameter, HashSet<IItem>> multipleChoiceParameters;
62
63    private StringBuilder failedInstances;
64    private EventWaitHandle backgroundWorkerWaitHandle = new ManualResetEvent(false);
65    private bool suppressTreeViewEventHandling, suppressCheckAllNoneEventHandling;
66
67    public CreateExperimentDialog() : this(null) { }
68    public CreateExperimentDialog(IOptimizer optimizer) {
69      InitializeComponent();
70      instanceDiscoveryProgressLabel.BackColor = instancesTabPage.BackColor;
71      createBatchRun = createBatchRunCheckBox.Checked;
72      repetitions = (int)repetitionsNumericUpDown.Value;
73      // do not set the Optimizer property here, because we want to delay instance discovery to the time when the form loads
74      this.optimizer = optimizer;
75      Experiment = null;
76      okButton.Enabled = optimizer != null;
77
78      instances = new Dictionary<IProblemInstanceProvider, HashSet<IDataDescriptor>>();
79      intParameters = new Dictionary<IValueParameter, Tuple<int, int, int>>();
80      doubleParameters = new Dictionary<IValueParameter, Tuple<double, double, double>>();
81      boolParameters = new HashSet<IValueParameter>();
82      multipleChoiceParameters = new Dictionary<IValueParameter, HashSet<IItem>>();
83    }
84
85    #region Event handlers
86    private void CreateExperimentDialog_Load(object sender, EventArgs e) {
87      SetTabControlVisibility();
88      FillInstanceTreeViewAsync();
89      FillParametersListView();
90    }
91
92    private void CreateExperimentDialog_FormClosing(object sender, FormClosingEventArgs e) {
93      if (experimentCreationBackgroundWorker.IsBusy) {
94        if (DialogResult != System.Windows.Forms.DialogResult.OK) {
95          if (experimentCreationBackgroundWorker.IsBusy) experimentCreationBackgroundWorker.CancelAsync();
96          if (instanceDiscoveryBackgroundWorker.IsBusy) instanceDiscoveryBackgroundWorker.CancelAsync();
97        }
98        e.Cancel = true;
99      }
100    }
101
102    private void okButton_Click(object sender, EventArgs e) {
103      SetMode(DialogMode.CreatingExperiment);
104      experimentCreationBackgroundWorker.RunWorkerAsync();
105      backgroundWorkerWaitHandle.WaitOne(); // make sure the background worker has started before exiting
106    }
107
108    #region Parameters variation
109    private void parametersListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
110      var parameter = (IValueParameter)e.Item.Tag;
111      var isConstrainedValueParameter = typeof(OptionalConstrainedValueParameter<>).Equals(parameter.GetType().GetGenericTypeDefinition())
112        || typeof(ConstrainedValueParameter<>).Equals(parameter.GetType().GetGenericTypeDefinition());
113
114      if (!isConstrainedValueParameter && parameter.Value == null) {
115        if (e.Item.Checked) e.Item.Checked = false;
116        return;
117      }
118
119      if (isConstrainedValueParameter) {
120        if (e.Item.Checked) {
121          multipleChoiceParameters.Add(parameter, new HashSet<IItem>());
122        } else {
123          multipleChoiceParameters.Remove(parameter);
124        }
125      }
126
127      var intValue = parameter.Value as ValueTypeValue<int>;
128      if (intValue != null) {
129        if (e.Item.Checked) {
130          int minimum = intValue.Value;
131          int maximum = intValue.Value;
132          int step = 1;
133          intParameters.Add(parameter, new Tuple<int, int, int>(minimum, maximum, step));
134        } else intParameters.Remove(parameter);
135      }
136
137      var doubleValue = parameter.Value as ValueTypeValue<double>;
138      if (doubleValue != null) {
139        if (e.Item.Checked) {
140          double minimum = doubleValue.Value;
141          double maximum = doubleValue.Value;
142          double step = 1;
143          doubleParameters.Add(parameter, new Tuple<double, double, double>(minimum, maximum, step));
144        } else doubleParameters.Remove(parameter);
145      }
146
147      var boolValue = parameter.Value as ValueTypeValue<bool>;
148      if (boolValue != null) {
149        if (e.Item.Checked) boolParameters.Add(parameter);
150        else boolParameters.Remove(parameter);
151      }
152
153      UpdateVariationsLabel();
154      if (e.Item.Selected) UpdateDetailsView(parameter);
155      else e.Item.Selected = true;
156    }
157
158    private void parametersListView_SelectedIndexChanged(object sender, EventArgs e) {
159      if (parametersListView.SelectedItems.Count == 0) {
160        ClearDetailsView();
161      } else {
162        var parameter = parametersListView.SelectedItems[0].Tag as IValueParameter;
163        UpdateDetailsView(parameter);
164      }
165    }
166
167    private void UpdateDetailsView(IValueParameter parameter) {
168      ClearDetailsView();
169
170      var isConstrainedValueParameter =
171        typeof(OptionalConstrainedValueParameter<>).IsAssignableFrom(parameter.GetType().GetGenericTypeDefinition())
172        || typeof(ConstrainedValueParameter<>).Equals(parameter.GetType().GetGenericTypeDefinition());
173
174      if (isConstrainedValueParameter) {
175        detailsTypeLabel.Text = "Choices:";
176        choicesListView.Enabled = true;
177        choicesListView.Visible = true;
178        choicesListView.Tag = parameter;
179
180        if (!multipleChoiceParameters.ContainsKey(parameter)) return;
181        dynamic constrainedValuedParameter = parameter;
182        dynamic validValues = constrainedValuedParameter.ValidValues;
183        foreach (var choice in validValues) {
184          choicesListView.Items.Add(new ListViewItem(choice.ToString()) {
185            Tag = choice,
186            Checked = multipleChoiceParameters[parameter].Contains(choice)
187          });
188        }
189        return;
190      }
191
192      if (!(parameter.Value is ValueTypeValue<bool>)) {
193        minimumLabel.Visible = true; minimumTextBox.Visible = true;
194        maximumLabel.Visible = true; maximumTextBox.Visible = true;
195        stepSizeLabel.Visible = true; stepSizeTextBox.Visible = true;
196      } else detailsTypeLabel.Text = "Boolean parameter: True / False";
197
198      var intValue = parameter.Value as ValueTypeValue<int>;
199      if (intValue != null) {
200        detailsTypeLabel.Text = "Integer parameter:";
201        if (!intParameters.ContainsKey(parameter)) return;
202        string min = intParameters[parameter].Item1.ToString();
203        string max = intParameters[parameter].Item2.ToString();
204        string step = intParameters[parameter].Item3.ToString();
205        UpdateMinMaxStepSize(parameter, min, max, step);
206        return;
207      }
208
209      var doubleValue = parameter.Value as ValueTypeValue<double>;
210      if (doubleValue != null) {
211        detailsTypeLabel.Text = "Double parameter:";
212        if (!doubleParameters.ContainsKey(parameter)) return;
213        string min = doubleParameters[parameter].Item1.ToString();
214        string max = doubleParameters[parameter].Item2.ToString();
215        string step = doubleParameters[parameter].Item3.ToString();
216        UpdateMinMaxStepSize(parameter, min, max, step);
217        return;
218      }
219    }
220
221    #region Detail controls
222    private void choiceListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
223      var parameter = (IValueParameter)choicesListView.Tag;
224      if (e.Item.Checked) {
225        multipleChoiceParameters[parameter].Add((IItem)e.Item.Tag);
226      } else multipleChoiceParameters[parameter].Remove((IItem)e.Item.Tag);
227
228      UpdateVariationsLabel();
229    }
230
231    private void detailsTextBox_Validating(object sender, CancelEventArgs e) {
232      var parameter = (IValueParameter)((TextBox)sender).Tag;
233      errorProvider.Clear();
234
235      var intValue = parameter.Value as ValueTypeValue<int>;
236      if (intValue != null) {
237        int value;
238        if (!int.TryParse(((TextBox)sender).Text, out value)) {
239          errorProvider.SetError(((TextBox)sender), "Please enter a valid integer number.");
240          e.Cancel = true;
241        } else {
242          var before = intParameters[parameter];
243          var after = default(Tuple<int, int, int>);
244          if (sender == minimumTextBox) after = new Tuple<int, int, int>(value, before.Item2, before.Item3);
245          else if (sender == maximumTextBox) after = new Tuple<int, int, int>(before.Item1, value, before.Item3);
246          else if (sender == stepSizeTextBox) after = new Tuple<int, int, int>(before.Item1, before.Item2, value);
247          intParameters[parameter] = after;
248        }
249      }
250
251      var doubleValue = parameter.Value as ValueTypeValue<double>;
252      if (doubleValue != null) {
253        double value;
254        if (!double.TryParse(((TextBox)sender).Text, NumberStyles.Float, CultureInfo.CurrentCulture.NumberFormat, out value)) {
255          errorProvider.SetError(((TextBox)sender), "Please enter a valid number.");
256          e.Cancel = true;
257        } else {
258          var before = doubleParameters[parameter];
259          var after = default(Tuple<double, double, double>);
260          if (sender == minimumTextBox) after = new Tuple<double, double, double>(value, before.Item2, before.Item3);
261          else if (sender == maximumTextBox) after = new Tuple<double, double, double>(before.Item1, value, before.Item3);
262          else if (sender == stepSizeTextBox) after = new Tuple<double, double, double>(before.Item1, before.Item2, value);
263          doubleParameters[parameter] = after;
264        }
265      }
266
267      UpdateVariationsLabel();
268    }
269    #endregion
270    #endregion
271
272    #region Instances
273    private void instancesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
274      if (!suppressTreeViewEventHandling) {
275        if (e.Node.Nodes.Count > 0) { // provider node was (un)checked
276          SyncProviderNode(e.Node);
277        } else { // descriptor node was (un)checked
278          SyncInstanceNode(e.Node);
279        }
280
281        suppressCheckAllNoneEventHandling = true;
282        try {
283          var treeViewNodes = instancesTreeView.Nodes.OfType<TreeNode>().SelectMany(x => x.Nodes.OfType<TreeNode>());
284          selectAllCheckBox.Checked = treeViewNodes.Count() == instances.SelectMany(x => x.Value).Count();
285          selectNoneCheckBox.Checked = !treeViewNodes.Any(x => x.Checked);
286        }
287        finally { suppressCheckAllNoneEventHandling = false; }
288        UpdateVariationsLabel();
289      }
290    }
291
292    private void SyncProviderNode(TreeNode node) {
293      suppressTreeViewEventHandling = true;
294      try {
295        foreach (TreeNode n in node.Nodes) {
296          if (n.Checked != node.Checked) {
297            n.Checked = node.Checked;
298            SyncInstanceNode(n, false);
299          }
300        }
301      }
302      finally { suppressTreeViewEventHandling = false; }
303    }
304
305    private void SyncInstanceNode(TreeNode node, bool providerCheck = true) {
306      var provider = (IProblemInstanceProvider)node.Parent.Tag;
307      var descriptor = (IDataDescriptor)node.Tag;
308      if (node.Checked) {
309        if (!instances.ContainsKey(provider))
310          instances.Add(provider, new HashSet<IDataDescriptor>());
311        instances[provider].Add(descriptor);
312      } else {
313        if (instances.ContainsKey(provider)) {
314          instances[provider].Remove(descriptor);
315          if (instances[provider].Count == 0)
316            instances.Remove(provider);
317        }
318      }
319      if (providerCheck) {
320        bool allChecked = node.Parent.Nodes.OfType<TreeNode>().All(x => x.Checked);
321        suppressTreeViewEventHandling = true;
322        try {
323          node.Parent.Checked = allChecked;
324        }
325        finally { suppressTreeViewEventHandling = false; }
326      }
327    }
328
329    private void selectAllCheckBox_CheckedChanged(object sender, EventArgs e) {
330      if (!suppressCheckAllNoneEventHandling) {
331        if (selectAllCheckBox.Checked) {
332          suppressCheckAllNoneEventHandling = true;
333          try { selectNoneCheckBox.Checked = false; }
334          finally { suppressCheckAllNoneEventHandling = false; }
335          try {
336            suppressTreeViewEventHandling = true;
337            foreach (TreeNode node in instancesTreeView.Nodes) {
338              if (!node.Checked) {
339                node.Checked = true;
340                SyncProviderNode(node);
341              }
342            }
343          }
344          finally { suppressTreeViewEventHandling = false; }
345        }
346      }
347    }
348
349    private void selectNoneCheckBox_CheckedChanged(object sender, EventArgs e) {
350      if (!suppressCheckAllNoneEventHandling) {
351        if (selectNoneCheckBox.Checked) {
352          suppressCheckAllNoneEventHandling = true;
353          try { selectAllCheckBox.Checked = false; }
354          finally { suppressCheckAllNoneEventHandling = false; }
355          try {
356            suppressTreeViewEventHandling = true;
357            foreach (TreeNode node in instancesTreeView.Nodes) {
358              if (node.Checked) {
359                node.Checked = false;
360                SyncProviderNode(node);
361              }
362            }
363          }
364          finally { suppressTreeViewEventHandling = false; }
365        }
366      }
367    }
368    #endregion
369
370    private void createBatchRunCheckBox_CheckedChanged(object sender, EventArgs e) {
371      repetitionsNumericUpDown.Enabled = createBatchRunCheckBox.Checked;
372      createBatchRun = createBatchRunCheckBox.Checked;
373    }
374
375    private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
376      if (repetitionsNumericUpDown.Text == string.Empty)
377        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
378      repetitions = (int)repetitionsNumericUpDown.Value;
379    }
380
381    private void experimentsLabel_TextChanged(object sender, EventArgs e) {
382      long number;
383      if (long.TryParse(variationsLabel.Text, NumberStyles.AllowThousands, CultureInfo.CurrentCulture.NumberFormat, out number)) {
384        if (number > 1000) warningProvider.SetError(variationsLabel, "Consider reducing the number of variations!");
385        else warningProvider.SetError(variationsLabel, null);
386      }
387    }
388    #endregion
389
390    #region Helpers
391    private void SetTabControlVisibility() {
392      bool isAlgorithm = optimizer != null && optimizer is IAlgorithm;
393      bool instancesAvailable = isAlgorithm
394        && ((IAlgorithm)optimizer).Problem != null
395        && ProblemInstanceManager.GetProviders(((IAlgorithm)optimizer).Problem).Any();
396      if (instancesAvailable && tabControl.TabCount == 1)
397        tabControl.TabPages.Add(instancesTabPage);
398      else if (!instancesAvailable && tabControl.TabCount == 2)
399        tabControl.TabPages.Remove(instancesTabPage);
400      tabControl.Visible = isAlgorithm;
401      if (isAlgorithm) {
402        variationsLabel.Visible = true;
403        experimentsToCreateDescriptionLabel.Visible = true;
404        Height = 430;
405      } else {
406        variationsLabel.Visible = false;
407        experimentsToCreateDescriptionLabel.Visible = false;
408        Height = 130;
409      }
410    }
411
412    private void FillParametersListView() {
413      parametersListView.Items.Clear();
414      intParameters.Clear();
415      doubleParameters.Clear();
416      boolParameters.Clear();
417      multipleChoiceParameters.Clear();
418
419      if (Optimizer is IAlgorithm) {
420        var parameters = ((IAlgorithm)optimizer).Parameters;
421        foreach (var param in parameters) {
422          var valueParam = param as IValueParameter;
423          if (valueParam != null && (valueParam.Value is ValueTypeValue<bool>
424              || valueParam.Value is ValueTypeValue<int>
425              || valueParam.Value is ValueTypeValue<double>)
426            || typeof(OptionalConstrainedValueParameter<>).IsAssignableFrom(param.GetType().GetGenericTypeDefinition())
427            || typeof(ConstrainedValueParameter<>).IsAssignableFrom(param.GetType().GetGenericTypeDefinition()))
428            parametersListView.Items.Add(new ListViewItem(param.Name) { Tag = param });
429        }
430      }
431    }
432
433    private void FillInstanceTreeViewAsync() {
434      instances.Clear();
435      instancesTreeView.Nodes.Clear();
436
437      if (Optimizer is IAlgorithm && ((IAlgorithm)Optimizer).Problem != null) {
438        SetMode(DialogMode.DiscoveringInstances);
439        instanceDiscoveryBackgroundWorker.RunWorkerAsync();
440      }
441    }
442
443    private void AddOptimizer(IOptimizer optimizer, Experiment experiment) {
444      if (createBatchRun) {
445        var batchRun = new BatchRun();
446        batchRun.Repetitions = repetitions;
447        batchRun.Optimizer = optimizer;
448        experiment.Optimizers.Add(batchRun);
449      } else {
450        experiment.Optimizers.Add(optimizer);
451      }
452    }
453
454    private int GetNumberOfVariations() {
455      int instancesCount = 1;
456      if (instances.Values.Any())
457        instancesCount = Math.Max(instances.Values.SelectMany(x => x).Count(), 1);
458
459      int intParameterVariations = 1;
460      foreach (var intParam in intParameters.Values) {
461        if (intParam.Item3 == 0) continue;
462        intParameterVariations *= (intParam.Item2 - intParam.Item1) / intParam.Item3 + 1;
463      }
464      int doubleParameterVariations = 1;
465      foreach (var doubleParam in doubleParameters.Values) {
466        if (doubleParam.Item3 == 0) continue;
467        doubleParameterVariations *= (int)Math.Floor((doubleParam.Item2 - doubleParam.Item1) / doubleParam.Item3) + 1;
468      }
469      int boolParameterVariations = 1;
470      foreach (var boolParam in boolParameters) {
471        boolParameterVariations *= 2;
472      }
473      int choiceParameterVariations = 1;
474      foreach (var choiceParam in multipleChoiceParameters.Values) {
475        choiceParameterVariations *= Math.Max(choiceParam.Count, 1);
476      }
477
478      return (instancesCount * intParameterVariations * doubleParameterVariations * boolParameterVariations * choiceParameterVariations);
479    }
480
481    private void SetMode(DialogMode mode) {
482      createBatchRunCheckBox.Enabled = mode == DialogMode.Normal;
483      repetitionsNumericUpDown.Enabled = mode == DialogMode.Normal;
484      parametersSplitContainer.Enabled = mode == DialogMode.Normal || mode == DialogMode.DiscoveringInstances;
485      selectAllCheckBox.Enabled = mode == DialogMode.Normal;
486      selectNoneCheckBox.Enabled = mode == DialogMode.Normal;
487      instancesTreeView.Enabled = mode == DialogMode.Normal;
488      instancesTreeView.Visible = mode == DialogMode.Normal || mode == DialogMode.CreatingExperiment || mode == DialogMode.PreparingExperiment;
489      okButton.Enabled = mode == DialogMode.Normal;
490      okButton.Visible = mode != DialogMode.CreatingExperiment && mode != DialogMode.PreparingExperiment;
491      cancelButton.Enabled = mode != DialogMode.PreparingExperiment;
492      instanceDiscoveryProgressLabel.Visible = mode == DialogMode.DiscoveringInstances;
493      instanceDiscoveryProgressBar.Visible = mode == DialogMode.DiscoveringInstances;
494      experimentCreationProgressBar.Visible = mode == DialogMode.CreatingExperiment || mode == DialogMode.PreparingExperiment;
495    }
496
497    private void ClearDetailsView() {
498      minimumLabel.Visible = false;
499      minimumTextBox.Text = string.Empty;
500      minimumTextBox.Enabled = false;
501      minimumTextBox.Visible = false;
502      maximumLabel.Visible = false;
503      maximumTextBox.Text = string.Empty;
504      maximumTextBox.Enabled = false;
505      maximumTextBox.Visible = false;
506      stepSizeLabel.Visible = false;
507      stepSizeTextBox.Text = string.Empty;
508      stepSizeTextBox.Enabled = false;
509      stepSizeTextBox.Visible = false;
510      choicesListView.Items.Clear();
511      choicesListView.Enabled = false;
512      choicesListView.Visible = false;
513    }
514
515    private void UpdateMinMaxStepSize(IValueParameter parameter, string min, string max, string step) {
516      minimumLabel.Visible = true;
517      minimumTextBox.Text = min;
518      minimumTextBox.Enabled = true;
519      minimumTextBox.Visible = true;
520      minimumTextBox.Tag = parameter;
521      maximumLabel.Visible = true;
522      maximumTextBox.Text = max;
523      maximumTextBox.Enabled = true;
524      maximumTextBox.Visible = true;
525      maximumTextBox.Tag = parameter;
526      stepSizeLabel.Visible = true;
527      stepSizeTextBox.Text = step;
528      stepSizeTextBox.Enabled = true;
529      stepSizeTextBox.Visible = true;
530      stepSizeTextBox.Tag = parameter;
531    }
532
533    private void UpdateVariationsLabel() {
534      variationsLabel.Text = GetNumberOfVariations().ToString("#,#", CultureInfo.CurrentCulture);
535    }
536
537    #region Retrieve parameter combinations
538    private IEnumerable<Dictionary<IValueParameter, int>> GetIntParameterConfigurations() {
539      var configuration = new Dictionary<IValueParameter, int>();
540      var indices = new Dictionary<IValueParameter, int>();
541      bool finished;
542      do {
543        foreach (var p in intParameters) {
544          if (!indices.ContainsKey(p.Key)) indices.Add(p.Key, 0);
545          var value = p.Value.Item1 + p.Value.Item3 * indices[p.Key];
546          configuration[p.Key] = value;
547        }
548        yield return configuration;
549
550        finished = true;
551        foreach (var p in intParameters.Keys) {
552          var newValue = intParameters[p].Item1 + intParameters[p].Item3 * (indices[p] + 1);
553          if (newValue > intParameters[p].Item2 || intParameters[p].Item3 == 0)
554            indices[p] = 0;
555          else {
556            indices[p]++;
557            finished = false;
558            break;
559          }
560        }
561      } while (!finished);
562    }
563
564    private IEnumerable<Dictionary<IValueParameter, double>> GetDoubleParameterConfigurations() {
565      var configuration = new Dictionary<IValueParameter, double>();
566      var indices = new Dictionary<IValueParameter, int>();
567      bool finished;
568      do {
569        foreach (var p in doubleParameters) {
570          if (!indices.ContainsKey(p.Key)) indices.Add(p.Key, 0);
571          var value = p.Value.Item1 + p.Value.Item3 * indices[p.Key];
572          configuration[p.Key] = value;
573        }
574        yield return configuration;
575
576        finished = true;
577        foreach (var p in doubleParameters.Keys) {
578          var newValue = doubleParameters[p].Item1 + doubleParameters[p].Item3 * (indices[p] + 1);
579          if (newValue > doubleParameters[p].Item2 || doubleParameters[p].Item3 == 0)
580            indices[p] = 0;
581          else {
582            indices[p]++;
583            finished = false;
584            break;
585          }
586        }
587      } while (!finished);
588    }
589
590    private IEnumerable<Dictionary<IValueParameter, bool>> GetBoolParameterConfigurations() {
591      var configuration = new Dictionary<IValueParameter, bool>();
592      bool finished;
593      do {
594        finished = true;
595        foreach (var p in boolParameters) {
596          if (!configuration.ContainsKey(p)) configuration.Add(p, false);
597          else {
598            if (configuration[p]) {
599              configuration[p] = false;
600            } else {
601              configuration[p] = true;
602              finished = false;
603              break;
604            }
605          }
606        }
607        yield return configuration;
608      } while (!finished);
609    }
610
611    private IEnumerable<Dictionary<IValueParameter, IItem>> GetMultipleChoiceConfigurations() {
612      var configuration = new Dictionary<IValueParameter, IItem>();
613      var enumerators = new Dictionary<IValueParameter, IEnumerator<IItem>>();
614      bool finished;
615      do {
616        foreach (var p in multipleChoiceParameters.Keys.ToArray()) {
617          if (!enumerators.ContainsKey(p)) {
618            enumerators.Add(p, multipleChoiceParameters[p].GetEnumerator());
619            if (!enumerators[p].MoveNext()) {
620              multipleChoiceParameters.Remove(p);
621              continue;
622            }
623          }
624          configuration[p] = enumerators[p].Current;
625        }
626
627        finished = true;
628        foreach (var p in multipleChoiceParameters.Keys) {
629          if (!enumerators[p].MoveNext()) {
630            enumerators[p] = multipleChoiceParameters[p].GetEnumerator();
631            enumerators[p].MoveNext();
632          } else {
633            finished = false;
634            break;
635          }
636        }
637        yield return configuration;
638      } while (!finished);
639    }
640    #endregion
641    #endregion
642
643    #region Background workers
644    #region Instance discovery
645    private void instanceDiscoveryBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
646      var instanceProviders = ProblemInstanceManager.GetProviders(((IAlgorithm)Optimizer).Problem).ToArray();
647      var nodes = new TreeNode[instanceProviders.Length];
648      for (int i = 0; i < instanceProviders.Length; i++) {
649        var provider = instanceProviders[i];
650        nodes[i] = new TreeNode(provider.Name) { Tag = provider };
651      }
652      e.Result = nodes;
653      for (int i = 0; i < nodes.Length; i++) {
654        var providerNode = nodes[i];
655        var provider = providerNode.Tag as IProblemInstanceProvider;
656        double progress = i / (double)nodes.Length;
657        instanceDiscoveryBackgroundWorker.ReportProgress((int)(100 * progress), provider.Name);
658        var descriptors = ProblemInstanceManager.GetDataDescriptors(provider).ToArray();
659        for (int j = 0; j < descriptors.Length; j++) {
660          #region Check cancellation request
661          if (instanceDiscoveryBackgroundWorker.CancellationPending) {
662            e.Cancel = true;
663            return;
664          }
665          #endregion
666          var node = new TreeNode(descriptors[j].Name) { Tag = descriptors[j] };
667          providerNode.Nodes.Add(node);
668        }
669      }
670      instanceDiscoveryBackgroundWorker.ReportProgress(100, string.Empty);
671    }
672
673    private void instanceDiscoveryBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
674      if (instanceDiscoveryProgressBar.Value != e.ProgressPercentage)
675        instanceDiscoveryProgressBar.Value = e.ProgressPercentage;
676      instanceDiscoveryProgressLabel.Text = (string)e.UserState;
677      Application.DoEvents();
678    }
679
680    private void instanceDiscoveryBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
681      try {
682        instancesTreeView.Nodes.AddRange((TreeNode[])e.Result);
683        foreach (TreeNode node in instancesTreeView.Nodes)
684          node.Collapse();
685        selectNoneCheckBox.Checked = true;
686      }
687      catch { }
688      try {
689        SetMode(DialogMode.Normal);
690        if (e.Error != null) MessageBox.Show(e.Error.Message, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
691      }
692      catch { }
693    }
694    #endregion
695
696    #region Experiment creation
697    private void experimentCreationBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
698      backgroundWorkerWaitHandle.Set(); // notify the ok button that we're busy now
699      failedInstances = new StringBuilder();
700      var localExperiment = new Experiment();
701
702      int counter = 0, totalVariations = GetNumberOfVariations();
703      if (instances.Count == 0) {
704        AddParameterVariations(localExperiment, ref counter, totalVariations);
705        experimentCreationBackgroundWorker.ReportProgress(100, string.Empty);
706
707      } else {
708        foreach (var provider in instances.Keys) {
709          foreach (var descriptor in instances[provider]) {
710            #region Check cancellation request
711            if (experimentCreationBackgroundWorker.CancellationPending) {
712              e.Cancel = true;
713              localExperiment = null;
714              return;
715            }
716            #endregion
717            var algorithm = (IAlgorithm)Optimizer.Clone();
718            bool failed = false;
719            try {
720              ProblemInstanceManager.LoadData(provider, descriptor, (IProblemInstanceConsumer)algorithm.Problem);
721            }
722            catch (Exception ex) {
723              failedInstances.AppendLine(descriptor.Name + ": " + ex.Message);
724              failed = true;
725            }
726            if (!failed) {
727              AddParameterVariations(localExperiment, ref counter, totalVariations);
728            } else experimentCreationBackgroundWorker.ReportProgress((int)Math.Round((100.0 * counter) / totalVariations), "Loading failed (" + descriptor.Name + ")");
729          }
730        }
731      }
732      if (localExperiment != null) {
733        // don't do GUI stuff in do_work
734        // this step can take some time
735        experimentCreationBackgroundWorker.ReportProgress(-1);
736        localExperiment.Prepare(true);
737        experimentCreationBackgroundWorker.ReportProgress(100);
738      }
739      Experiment = localExperiment;
740    }
741
742    private void AddParameterVariations(Experiment localExperiment, ref int counter, int totalVariations) {
743      var variations = experimentCreationBackgroundWorker_CalculateParameterVariations(optimizer);
744      foreach (var v in variations) {
745        AddOptimizer(v, localExperiment);
746        counter++;
747        experimentCreationBackgroundWorker.ReportProgress((int)Math.Round((100.0 * counter) / totalVariations), string.Empty);
748      }
749    }
750
751    private IEnumerable<IOptimizer> experimentCreationBackgroundWorker_CalculateParameterVariations(IOptimizer optimizer) {
752      if (!boolParameters.Any() && !intParameters.Any() && !doubleParameters.Any() && !multipleChoiceParameters.Any()) {
753        yield return (IOptimizer)optimizer.Clone();
754        yield break;
755      }
756      bool finished;
757      var mcEnumerator = GetMultipleChoiceConfigurations().GetEnumerator();
758      var boolEnumerator = GetBoolParameterConfigurations().GetEnumerator();
759      var intEnumerator = GetIntParameterConfigurations().GetEnumerator();
760      var doubleEnumerator = GetDoubleParameterConfigurations().GetEnumerator();
761      mcEnumerator.MoveNext(); boolEnumerator.MoveNext(); intEnumerator.MoveNext(); doubleEnumerator.MoveNext();
762      do {
763        var variant = (IAlgorithm)optimizer.Clone();
764        variant.Name += " {";
765        finished = true;
766        if (doubleParameters.Any()) {
767          foreach (var d in doubleEnumerator.Current) {
768            var value = (ValueTypeValue<double>)((IValueParameter)variant.Parameters[d.Key.Name]).Value;
769            value.Value = d.Value;
770            variant.Name += d.Key.Name + "=" + d.Value.ToString() + ", ";
771          }
772          if (finished) {
773            if (doubleEnumerator.MoveNext()) {
774              finished = false;
775            } else {
776              doubleEnumerator = GetDoubleParameterConfigurations().GetEnumerator();
777              doubleEnumerator.MoveNext();
778            }
779          }
780        }
781        if (intParameters.Any()) {
782          foreach (var i in intEnumerator.Current) {
783            var value = (ValueTypeValue<int>)((IValueParameter)variant.Parameters[i.Key.Name]).Value;
784            value.Value = i.Value;
785            variant.Name += i.Key.Name + "=" + i.Value.ToString() + ", ";
786          }
787          if (finished) {
788            if (intEnumerator.MoveNext()) {
789              finished = false;
790            } else {
791              intEnumerator = GetIntParameterConfigurations().GetEnumerator();
792              intEnumerator.MoveNext();
793            }
794          }
795        }
796        if (boolParameters.Any()) {
797          foreach (var b in boolEnumerator.Current) {
798            var value = (ValueTypeValue<bool>)((IValueParameter)variant.Parameters[b.Key.Name]).Value;
799            value.Value = b.Value;
800            variant.Name += b.Key.Name + "=" + b.Value.ToString() + ", ";
801          }
802          if (finished) {
803            if (boolEnumerator.MoveNext()) {
804              finished = false;
805            } else {
806              boolEnumerator = GetBoolParameterConfigurations().GetEnumerator();
807              boolEnumerator.MoveNext();
808            }
809          }
810        }
811        if (multipleChoiceParameters.Any()) {
812          foreach (var m in mcEnumerator.Current) {
813            dynamic variantParam = variant.Parameters[m.Key.Name];
814            var variantEnumerator = ((IEnumerable<object>)variantParam.ValidValues).GetEnumerator();
815            var originalEnumerator = ((IEnumerable<object>)((dynamic)m.Key).ValidValues).GetEnumerator();
816            while (variantEnumerator.MoveNext() && originalEnumerator.MoveNext()) {
817              if (m.Value == (IItem)originalEnumerator.Current) {
818                variantParam.Value = (dynamic)variantEnumerator.Current;
819                if (m.Value is INamedItem)
820                  variant.Name += m.Key.Name + "=" + ((INamedItem)m.Value).Name + ", ";
821                else variant.Name += m.Key.Name + "=" + m.Value.ToString() + ", ";
822                break;
823              }
824            }
825          }
826          if (finished) {
827            if (mcEnumerator.MoveNext()) {
828              finished = false;
829            } else {
830              mcEnumerator = GetMultipleChoiceConfigurations().GetEnumerator();
831              mcEnumerator.MoveNext();
832            }
833          }
834        }
835        variant.Name = variant.Name.Substring(0, variant.Name.Length - 2) + "}";
836        yield return variant;
837      } while (!finished);
838    }
839
840    private void experimentCreationBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
841      if (e.ProgressPercentage >= 0 && e.ProgressPercentage <= 100) {
842        experimentCreationProgressBar.Style = ProgressBarStyle.Continuous;
843        experimentCreationProgressBar.Value = e.ProgressPercentage;
844      } else {
845        SetMode(DialogMode.PreparingExperiment);
846
847        experimentCreationProgressBar.Style = ProgressBarStyle.Marquee;
848      }
849      Application.DoEvents();
850    }
851
852    private void experimentCreationBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
853      try {
854        SetMode(DialogMode.Normal);
855        if (e.Error != null) MessageBox.Show(e.Error.Message, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
856        if (failedInstances.Length > 0) MessageBox.Show("Some instances could not be loaded: " + Environment.NewLine + failedInstances.ToString(), "Some instances failed to load", MessageBoxButtons.OK, MessageBoxIcon.Error);
857        if (!e.Cancelled && e.Error == null) {
858          DialogResult = System.Windows.Forms.DialogResult.OK;
859          Close();
860        }
861      }
862      catch { }
863    }
864    #endregion
865    #endregion
866  }
867}
Note: See TracBrowser for help on using the repository browser.