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