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

Last change on this file since 7985 was 7985, checked in by abeham, 9 years ago

#1851: Allow to specify each value of numeric parameters explicitly as well as provide a dialog for generating the values

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