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