[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 |
|
---|
[13751] | 22 | using HeuristicLab.Core;
|
---|
[13718] | 23 | using HeuristicLab.MainForm;
|
---|
| 24 | using HeuristicLab.OptimizationExpertSystem.Common;
|
---|
| 25 | using System;
|
---|
| 26 | using System.Collections.Generic;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using System.Text.RegularExpressions;
|
---|
| 29 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.OptimizationExpertSystem {
|
---|
| 32 | [View("Understanding Problem Instance")]
|
---|
[13722] | 33 | [Content(typeof(KnowledgeCenter), IsDefaultView = false)]
|
---|
[13751] | 34 | public sealed partial class UnderstandingProblemInstanceView : KnowledgeCenterViewBase {
|
---|
| 35 | private bool SuppressEvents { get; set; }
|
---|
| 36 |
|
---|
[13718] | 37 | public UnderstandingProblemInstanceView() {
|
---|
| 38 | InitializeComponent();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | protected override void OnContentChanged() {
|
---|
| 42 | base.OnContentChanged();
|
---|
| 43 | if (Content == null) {
|
---|
| 44 | problemInstancesView.Content = null;
|
---|
| 45 | instanceMapChart.Series["InstancesSeries"].Points.Clear();
|
---|
| 46 | instanceMapChart.Series["CurrentInstanceSeries"].Points.Clear();
|
---|
| 47 | } else {
|
---|
| 48 | problemInstancesView.Content = Content.ProblemInstances;
|
---|
| 49 | UpdateProjectionComboBox();
|
---|
[13751] | 50 | UpdateSizeComboBox();
|
---|
[13718] | 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[13720] | 54 | #region Content Event Handlers
|
---|
| 55 | protected override void OnProblemChanged() {
|
---|
| 56 | base.OnProblemChanged();
|
---|
[13718] | 57 | SetEnabledStateOfControls();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[13720] | 60 | protected override void OnProblemInstancesChanged() {
|
---|
| 61 | base.OnProblemInstancesChanged();
|
---|
| 62 | if (Content.ProblemInstances.UpdateOfRunsInProgress) return;
|
---|
| 63 | UpdateProjectionComboBox();
|
---|
[13751] | 64 | UpdateSizeComboBox();
|
---|
| 65 | UpdateProjection();
|
---|
[13720] | 66 | }
|
---|
[13751] | 67 | #endregion
|
---|
[13720] | 68 |
|
---|
[13751] | 69 | private void UpdateProjectionComboBox() {
|
---|
| 70 | try {
|
---|
| 71 | SuppressEvents = true;
|
---|
| 72 | var selected = projectionComboBox.SelectedIndex >= 0 ? (string)projectionComboBox.SelectedItem : null;
|
---|
| 73 | projectionComboBox.Items.Clear();
|
---|
| 74 | foreach (var str in GetProjections()) {
|
---|
| 75 | projectionComboBox.Items.Add(str);
|
---|
| 76 | if (selected == str) projectionComboBox.SelectedItem = str;
|
---|
| 77 | }
|
---|
| 78 | if (selected == null && projectionComboBox.Items.Count > 0)
|
---|
| 79 | projectionComboBox.SelectedIndex = 0;
|
---|
| 80 | } finally { SuppressEvents = false; }
|
---|
[13718] | 81 | }
|
---|
[13751] | 82 |
|
---|
| 83 | private void UpdateSizeComboBox() {
|
---|
| 84 | try {
|
---|
| 85 | SuppressEvents = true;
|
---|
| 86 | var selected = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : null;
|
---|
| 87 | sizeComboBox.Items.Clear();
|
---|
| 88 | sizeComboBox.Items.Add(string.Empty);
|
---|
| 89 | foreach (var str in Content.ProblemInstances.ResultNames.Where(x => !(x.StartsWith("Projection.") && (x.EndsWith(".X") || x.EndsWith(".Y"))))) {
|
---|
| 90 | sizeComboBox.Items.Add(str);
|
---|
| 91 | if (selected == str) sizeComboBox.SelectedItem = str;
|
---|
| 92 | }
|
---|
| 93 | if (selected == null && sizeComboBox.Items.Count > 0)
|
---|
| 94 | sizeComboBox.SelectedIndex = 0;
|
---|
| 95 | } finally { SuppressEvents = false; }
|
---|
| 96 | }
|
---|
[13720] | 97 |
|
---|
[13751] | 98 | private IEnumerable<string> GetProjections() {
|
---|
[13718] | 99 | return Content.ProblemInstances
|
---|
| 100 | .SelectMany(x => x.Results.Where(y => Regex.IsMatch(y.Key, "^Projection[.].*[.][XY]$")))
|
---|
| 101 | .Select(x => Regex.Match(x.Key, "Projection[.](?<g>.*)[.][XY]").Groups["g"].Value)
|
---|
| 102 | .Distinct();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[13751] | 105 | private void UpdateProjection() {
|
---|
| 106 | if (InvokeRequired) { Invoke((Action)UpdateProjection); return; }
|
---|
| 107 |
|
---|
| 108 | var instancesSeries = instanceMapChart.Series["InstancesSeries"];
|
---|
| 109 | var currentInstanceSeries = instanceMapChart.Series["CurrentInstanceSeries"];
|
---|
| 110 |
|
---|
| 111 | if (projectionComboBox.SelectedIndex < 0) {
|
---|
| 112 | instancesSeries.Points.Clear();
|
---|
| 113 | currentInstanceSeries.Points.Clear();
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | var projection = (string)projectionComboBox.SelectedItem;
|
---|
| 118 | var size = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : string.Empty;
|
---|
| 119 |
|
---|
| 120 | DoProjectProblemInstances(instancesSeries, currentInstanceSeries, projection, size, invPropCheckBox.Checked);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private void DoProjectProblemInstances(Series instancesSeries, Series currentInstanceSeries, string projection, string size, bool invProp) {
|
---|
[13718] | 124 | instancesSeries.Points.Clear();
|
---|
| 125 | currentInstanceSeries.Points.Clear();
|
---|
| 126 |
|
---|
[13751] | 127 | double maxSize = 0, minSize = 0;
|
---|
| 128 | if (!string.IsNullOrEmpty(size)) {
|
---|
| 129 | var sizes = Content.ProblemInstances.Where(x => x.Results.ContainsKey(size)).Select(x => x.Results[size]).OfType<Data.DoubleValue>().Where(x => !double.IsNaN(x.Value)).ToList();
|
---|
| 130 | if (sizes.Count > 0) {
|
---|
| 131 | maxSize = sizes.Max(x => x.Value);
|
---|
| 132 | minSize = sizes.Min(x => x.Value);
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
[13718] | 135 | foreach (var run in Content.ProblemInstances) {
|
---|
| 136 | var xKey = "Projection." + projection + ".X";
|
---|
| 137 | var yKey = "Projection." + projection + ".Y";
|
---|
| 138 | if (!run.Results.ContainsKey(xKey) || !run.Results.ContainsKey(yKey)
|
---|
| 139 | || !(run.Results[xKey] is Data.DoubleValue) || !(run.Results[yKey] is Data.DoubleValue)) continue;
|
---|
| 140 | var x = ((Data.DoubleValue)run.Results[xKey]).Value;
|
---|
| 141 | var y = ((Data.DoubleValue)run.Results[yKey]).Value;
|
---|
| 142 | var dataPoint = new DataPoint(x, y) {
|
---|
[13751] | 143 | Label = run.Name,
|
---|
[13718] | 144 | };
|
---|
[13751] | 145 | IItem item;
|
---|
| 146 | if (maxSize > minSize && run.Results.TryGetValue(size, out item)) {
|
---|
| 147 | var dItem = item as Data.DoubleValue;
|
---|
| 148 | if (dItem != null) {
|
---|
| 149 | if (double.IsNaN(dItem.Value))
|
---|
| 150 | dataPoint.MarkerSize = 1;
|
---|
| 151 | else {
|
---|
| 152 | if (invProp) dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (maxSize - dItem.Value) / (maxSize - minSize));
|
---|
| 153 | else dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (dItem.Value - minSize) / (maxSize - minSize));
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | } else if (maxSize == minSize) {
|
---|
| 157 | dataPoint.MarkerSize = instancesSeries.MarkerSize;
|
---|
| 158 | } else dataPoint.MarkerSize = 1;
|
---|
[13718] | 159 |
|
---|
[13751] | 160 | if (Content.IsCurrentInstance(run)) currentInstanceSeries.Points.Add(dataPoint);
|
---|
| 161 | else instancesSeries.Points.Add(dataPoint);
|
---|
[13718] | 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[13751] | 165 | #region Control Event Handlers
|
---|
[13718] | 166 | private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
|
---|
[13751] | 167 | UpdateProjection();
|
---|
| 168 | }
|
---|
[13718] | 169 |
|
---|
[13751] | 170 | private void SizeComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 171 | UpdateProjection();
|
---|
[13718] | 172 | }
|
---|
| 173 |
|
---|
[13751] | 174 | private void InvPropCheckBoxOnCheckedChanged(object sender, EventArgs e) {
|
---|
| 175 | UpdateProjection();
|
---|
[13718] | 176 | }
|
---|
[13751] | 177 | #endregion
|
---|
[13718] | 178 | }
|
---|
| 179 | }
|
---|