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