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