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