[13718] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[13752] | 22 | using HeuristicLab.Common.Resources;
|
---|
[13751] | 23 | using HeuristicLab.Core;
|
---|
[13752] | 24 | using HeuristicLab.Data;
|
---|
[13718] | 25 | using HeuristicLab.MainForm;
|
---|
[13787] | 26 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[13718] | 27 | using HeuristicLab.OptimizationExpertSystem.Common;
|
---|
| 28 | using System;
|
---|
| 29 | using System.Collections.Generic;
|
---|
[13757] | 30 | using System.Drawing;
|
---|
[13718] | 31 | using System.Linq;
|
---|
| 32 | using System.Text.RegularExpressions;
|
---|
[13752] | 33 | using System.Windows.Forms;
|
---|
[13718] | 34 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.OptimizationExpertSystem {
|
---|
| 37 | [View("Understanding Problem Instance")]
|
---|
[13722] | 38 | [Content(typeof(KnowledgeCenter), IsDefaultView = false)]
|
---|
[13751] | 39 | public sealed partial class UnderstandingProblemInstanceView : KnowledgeCenterViewBase {
|
---|
| 40 | private bool SuppressEvents { get; set; }
|
---|
[13787] | 41 | private readonly CheckedItemList<StringValue> characteristics;
|
---|
[13752] | 42 |
|
---|
[13718] | 43 | public UnderstandingProblemInstanceView() {
|
---|
| 44 | InitializeComponent();
|
---|
[13752] | 45 | showCharacteristicsCheckBox.Text = string.Empty;
|
---|
| 46 | showCharacteristicsCheckBox.Image = VSImageLibrary.Properties;
|
---|
[13787] | 47 | characteristics = new CheckedItemList<StringValue>();
|
---|
| 48 | characteristics.CheckedItemsChanged += CharacteristicsOnCheckedItemsChanged;
|
---|
| 49 | mapSplitContainer.Panel2.Controls.Add(new ViewHost() {
|
---|
| 50 | Dock = DockStyle.Fill,
|
---|
| 51 | Content = characteristics
|
---|
| 52 | });
|
---|
[13718] | 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override void OnContentChanged() {
|
---|
| 56 | base.OnContentChanged();
|
---|
[13787] | 57 | characteristics.Clear();
|
---|
[13718] | 58 | if (Content == null) {
|
---|
| 59 | problemInstancesView.Content = null;
|
---|
| 60 | instanceMapChart.Series["InstancesSeries"].Points.Clear();
|
---|
| 61 | instanceMapChart.Series["CurrentInstanceSeries"].Points.Clear();
|
---|
| 62 | } else {
|
---|
| 63 | problemInstancesView.Content = Content.ProblemInstances;
|
---|
[13787] | 64 | UpdateCharacteristics();
|
---|
[13718] | 65 | UpdateProjectionComboBox();
|
---|
[13751] | 66 | UpdateSizeComboBox();
|
---|
[13757] | 67 | UpdateColorComboBox();
|
---|
[13718] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[13787] | 71 | protected override void SetEnabledStateOfControls() {
|
---|
| 72 | base.SetEnabledStateOfControls();
|
---|
[13718] | 73 | }
|
---|
| 74 |
|
---|
[13787] | 75 | #region Update Controls
|
---|
| 76 | private void UpdateCharacteristics() {
|
---|
| 77 | var @checked = new HashSet<string>(characteristics.CheckedItems.Select(x => x.Value.Value));
|
---|
| 78 | characteristics.Clear();
|
---|
| 79 | foreach (var c in Content.ProblemInstances.ResultNames) {
|
---|
| 80 | characteristics.Add(new StringValue(c), @checked.Contains(c));
|
---|
| 81 | }
|
---|
[13720] | 82 | }
|
---|
| 83 |
|
---|
[13751] | 84 | private void UpdateProjectionComboBox() {
|
---|
| 85 | try {
|
---|
| 86 | SuppressEvents = true;
|
---|
| 87 | var selected = projectionComboBox.SelectedIndex >= 0 ? (string)projectionComboBox.SelectedItem : null;
|
---|
| 88 | projectionComboBox.Items.Clear();
|
---|
| 89 | foreach (var str in GetProjections()) {
|
---|
| 90 | projectionComboBox.Items.Add(str);
|
---|
| 91 | if (selected == str) projectionComboBox.SelectedItem = str;
|
---|
| 92 | }
|
---|
| 93 | if (selected == null && projectionComboBox.Items.Count > 0)
|
---|
| 94 | projectionComboBox.SelectedIndex = 0;
|
---|
| 95 | } finally { SuppressEvents = false; }
|
---|
[13718] | 96 | }
|
---|
[13751] | 97 |
|
---|
| 98 | private void UpdateSizeComboBox() {
|
---|
| 99 | try {
|
---|
| 100 | SuppressEvents = true;
|
---|
| 101 | var selected = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : null;
|
---|
| 102 | sizeComboBox.Items.Clear();
|
---|
| 103 | sizeComboBox.Items.Add(string.Empty);
|
---|
| 104 | foreach (var str in Content.ProblemInstances.ResultNames.Where(x => !(x.StartsWith("Projection.") && (x.EndsWith(".X") || x.EndsWith(".Y"))))) {
|
---|
| 105 | sizeComboBox.Items.Add(str);
|
---|
| 106 | if (selected == str) sizeComboBox.SelectedItem = str;
|
---|
| 107 | }
|
---|
| 108 | if (selected == null && sizeComboBox.Items.Count > 0)
|
---|
| 109 | sizeComboBox.SelectedIndex = 0;
|
---|
| 110 | } finally { SuppressEvents = false; }
|
---|
| 111 | }
|
---|
[13757] | 112 |
|
---|
| 113 | private void UpdateColorComboBox() {
|
---|
| 114 | try {
|
---|
| 115 | SuppressEvents = true;
|
---|
| 116 | var selected = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : null;
|
---|
| 117 | colorComboBox.Items.Clear();
|
---|
| 118 | colorComboBox.Items.Add(string.Empty);
|
---|
| 119 | foreach (var str in Content.ProblemInstances.ResultNames.Where(x => x.StartsWith("Rank."))) {
|
---|
| 120 | colorComboBox.Items.Add(str);
|
---|
| 121 | if (selected == str) colorComboBox.SelectedItem = str;
|
---|
| 122 | }
|
---|
| 123 | if (selected == null && colorComboBox.Items.Count > 0)
|
---|
| 124 | colorComboBox.SelectedIndex = 0;
|
---|
| 125 | } finally { SuppressEvents = false; }
|
---|
| 126 | }
|
---|
[13718] | 127 |
|
---|
[13751] | 128 | private void UpdateProjection() {
|
---|
| 129 | if (InvokeRequired) { Invoke((Action)UpdateProjection); return; }
|
---|
| 130 |
|
---|
| 131 | var instancesSeries = instanceMapChart.Series["InstancesSeries"];
|
---|
| 132 | var currentInstanceSeries = instanceMapChart.Series["CurrentInstanceSeries"];
|
---|
| 133 |
|
---|
| 134 | if (projectionComboBox.SelectedIndex < 0) {
|
---|
| 135 | instancesSeries.Points.Clear();
|
---|
| 136 | currentInstanceSeries.Points.Clear();
|
---|
| 137 | return;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | var projection = (string)projectionComboBox.SelectedItem;
|
---|
| 141 | var size = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : string.Empty;
|
---|
[13757] | 142 | var color = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : string.Empty;
|
---|
[13751] | 143 |
|
---|
[13757] | 144 | DoProjectProblemInstances(instancesSeries, currentInstanceSeries, projection,
|
---|
| 145 | size, color, invPropCheckBox.Checked, fromZeroCheckBox.Checked);
|
---|
[13751] | 146 | }
|
---|
[13787] | 147 | #endregion
|
---|
[13751] | 148 |
|
---|
[13787] | 149 | #region Content Event Handlers
|
---|
| 150 | protected override void OnProblemChanged() {
|
---|
| 151 | base.OnProblemChanged();
|
---|
| 152 | SetEnabledStateOfControls();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | protected override void OnProblemInstancesChanged() {
|
---|
| 156 | base.OnProblemInstancesChanged();
|
---|
| 157 | if (Content.ProblemInstances.UpdateOfRunsInProgress) return;
|
---|
| 158 | try {
|
---|
| 159 | SuppressEvents = true;
|
---|
| 160 | UpdateCharacteristics();
|
---|
| 161 | } finally { SuppressEvents = false; }
|
---|
| 162 | UpdateProjectionComboBox();
|
---|
| 163 | UpdateSizeComboBox();
|
---|
| 164 | UpdateColorComboBox();
|
---|
| 165 | UpdateProjection();
|
---|
| 166 | }
|
---|
| 167 | #endregion
|
---|
| 168 |
|
---|
| 169 | #region Control Event Handlers
|
---|
| 170 | private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 171 | UpdateProjection();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | private void SizeComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 175 | UpdateProjection();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 179 | UpdateProjection();
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | private void InvPropCheckBoxOnCheckedChanged(object sender, EventArgs e) {
|
---|
| 183 | UpdateProjection();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | private void fromZeroCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 187 | UpdateProjection();
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | private void showCharacteristicsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 191 | mapSplitContainer.Panel2Collapsed = !showCharacteristicsCheckBox.Checked;
|
---|
| 192 | }
|
---|
| 193 | #endregion
|
---|
| 194 |
|
---|
| 195 | #region Other Event Handlers
|
---|
| 196 | private void CharacteristicsOnCheckedItemsChanged(object sender, EventArgs e) {
|
---|
| 197 | if (SuppressEvents) return;
|
---|
| 198 | if (characteristics.CheckedItems.Any())
|
---|
| 199 | Content.UpdateInstanceProjection(characteristics.CheckedItems.Select(x => x.Value.Value).ToArray());
|
---|
| 200 | }
|
---|
| 201 | #endregion
|
---|
| 202 |
|
---|
| 203 | #region Helper Classes and Methods
|
---|
| 204 | private IEnumerable<string> GetProjections() {
|
---|
| 205 | return Content.ProblemInstances.ResultNames
|
---|
| 206 | .Where(x => Regex.IsMatch(x, "^Projection[.].*[.][XY]$"))
|
---|
| 207 | .Select(x => Regex.Match(x, "Projection[.](?<g>.*)[.][XY]").Groups["g"].Value)
|
---|
| 208 | .Distinct();
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[13757] | 211 | private void DoProjectProblemInstances(Series instancesSeries, Series currentInstanceSeries, string projection, string size, string color, bool invProp, bool fromZero) {
|
---|
[13718] | 212 | instancesSeries.Points.Clear();
|
---|
| 213 | currentInstanceSeries.Points.Clear();
|
---|
| 214 |
|
---|
[13751] | 215 | double maxSize = 0, minSize = 0;
|
---|
| 216 | if (!string.IsNullOrEmpty(size)) {
|
---|
[13787] | 217 | var sizes = Content.ProblemInstances.Where(x => x.Results.ContainsKey(size)).Select(x => x.Results[size]).OfType<DoubleValue>().Where(x => !double.IsNaN(x.Value)).ToList();
|
---|
[13751] | 218 | if (sizes.Count > 0) {
|
---|
| 219 | maxSize = sizes.Max(x => x.Value);
|
---|
[13757] | 220 | if (maxSize < 0 && fromZero) {
|
---|
| 221 | maxSize = 0;
|
---|
| 222 | minSize = sizes.Min(x => x.Value);
|
---|
| 223 | } else if (fromZero) {
|
---|
| 224 | minSize = 0;
|
---|
| 225 | } else {
|
---|
| 226 | minSize = sizes.Min(x => x.Value);
|
---|
| 227 | }
|
---|
[13751] | 228 | }
|
---|
| 229 | }
|
---|
[13718] | 230 | foreach (var run in Content.ProblemInstances) {
|
---|
| 231 | var xKey = "Projection." + projection + ".X";
|
---|
| 232 | var yKey = "Projection." + projection + ".Y";
|
---|
| 233 | if (!run.Results.ContainsKey(xKey) || !run.Results.ContainsKey(yKey)
|
---|
[13787] | 234 | || !(run.Results[xKey] is DoubleValue) || !(run.Results[yKey] is DoubleValue)) continue;
|
---|
| 235 | var x = ((DoubleValue)run.Results[xKey]).Value;
|
---|
| 236 | var y = ((DoubleValue)run.Results[yKey]).Value;
|
---|
[13718] | 237 | var dataPoint = new DataPoint(x, y) {
|
---|
[13751] | 238 | Label = run.Name,
|
---|
[13718] | 239 | };
|
---|
[13751] | 240 | IItem item;
|
---|
| 241 | if (maxSize > minSize && run.Results.TryGetValue(size, out item)) {
|
---|
[13787] | 242 | var dItem = item as DoubleValue;
|
---|
[13757] | 243 | if (dItem == null && item is IntValue) dItem = new DoubleValue(((IntValue)item).Value);
|
---|
[13751] | 244 | if (dItem != null) {
|
---|
| 245 | if (double.IsNaN(dItem.Value))
|
---|
| 246 | dataPoint.MarkerSize = 1;
|
---|
| 247 | else {
|
---|
| 248 | if (invProp) dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (maxSize - dItem.Value) / (maxSize - minSize));
|
---|
| 249 | else dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (dItem.Value - minSize) / (maxSize - minSize));
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | } else if (maxSize == minSize) {
|
---|
| 253 | dataPoint.MarkerSize = instancesSeries.MarkerSize;
|
---|
| 254 | } else dataPoint.MarkerSize = 1;
|
---|
[13718] | 255 |
|
---|
[13757] | 256 | if (!string.IsNullOrEmpty(color)) {
|
---|
| 257 | if (run.Results.TryGetValue(color, out item)) {
|
---|
| 258 | var v = (int)(item is DoubleValue ? ((DoubleValue)item).Value : (item is IntValue ? ((IntValue)item).Value : int.MaxValue));
|
---|
| 259 | switch (v) {
|
---|
| 260 | case 0: dataPoint.MarkerColor = Color.Green; break;
|
---|
| 261 | case 1: dataPoint.MarkerColor = Color.LightSeaGreen; break;
|
---|
| 262 | case 2: dataPoint.MarkerColor = Color.CornflowerBlue; break;
|
---|
| 263 | case 3: dataPoint.MarkerColor = Color.PaleVioletRed; break;
|
---|
| 264 | case 4: dataPoint.MarkerColor = Color.IndianRed; break;
|
---|
| 265 | default: dataPoint.MarkerColor = Color.LightGray; break;
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
[13751] | 269 | if (Content.IsCurrentInstance(run)) currentInstanceSeries.Points.Add(dataPoint);
|
---|
| 270 | else instancesSeries.Points.Add(dataPoint);
|
---|
[13718] | 271 | }
|
---|
| 272 | }
|
---|
[13751] | 273 | #endregion
|
---|
[13718] | 274 | }
|
---|
| 275 | }
|
---|