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