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