Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/UnderstandingProblemInstanceView.cs @ 13787

Last change on this file since 13787 was 13787, checked in by abeham, 8 years ago

#2457: worked on performance modeling

File size: 11.0 KB
Line 
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
22using HeuristicLab.Common.Resources;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27using HeuristicLab.OptimizationExpertSystem.Common;
28using System;
29using System.Collections.Generic;
30using System.Drawing;
31using System.Linq;
32using System.Text.RegularExpressions;
33using System.Windows.Forms;
34using System.Windows.Forms.DataVisualization.Charting;
35
36namespace HeuristicLab.OptimizationExpertSystem {
37  [View("Understanding Problem Instance")]
38  [Content(typeof(KnowledgeCenter), IsDefaultView = false)]
39  public sealed partial class UnderstandingProblemInstanceView : KnowledgeCenterViewBase {
40    private bool SuppressEvents { get; set; }
41    private readonly CheckedItemList<StringValue> characteristics;
42 
43    public UnderstandingProblemInstanceView() {
44      InitializeComponent();
45      showCharacteristicsCheckBox.Text = string.Empty;
46      showCharacteristicsCheckBox.Image = VSImageLibrary.Properties;
47      characteristics = new CheckedItemList<StringValue>();
48      characteristics.CheckedItemsChanged += CharacteristicsOnCheckedItemsChanged;
49      mapSplitContainer.Panel2.Controls.Add(new ViewHost() {
50        Dock = DockStyle.Fill,
51        Content = characteristics
52      });
53    }
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      characteristics.Clear();
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;
64        UpdateCharacteristics();
65        UpdateProjectionComboBox();
66        UpdateSizeComboBox();
67        UpdateColorComboBox();
68      }
69    }
70
71    protected override void SetEnabledStateOfControls() {
72      base.SetEnabledStateOfControls();
73    }
74
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      }
82    }
83
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; }
96    }
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    }
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    }
127
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;
142      var color = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : string.Empty;
143
144      DoProjectProblemInstances(instancesSeries, currentInstanceSeries, projection,
145        size, color, invPropCheckBox.Checked, fromZeroCheckBox.Checked);
146    }
147    #endregion
148
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
211    private void DoProjectProblemInstances(Series instancesSeries, Series currentInstanceSeries, string projection, string size, string color, bool invProp, bool fromZero) {
212      instancesSeries.Points.Clear();
213      currentInstanceSeries.Points.Clear();
214
215      double maxSize = 0, minSize = 0;
216      if (!string.IsNullOrEmpty(size)) {
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();
218        if (sizes.Count > 0) {
219          maxSize = sizes.Max(x => x.Value);
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          }
228        }
229      }
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)
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;
237        var dataPoint = new DataPoint(x, y) {
238          Label = run.Name,
239        };
240        IItem item;
241        if (maxSize > minSize && run.Results.TryGetValue(size, out item)) {
242          var dItem = item as DoubleValue;
243          if (dItem == null && item is IntValue) dItem = new DoubleValue(((IntValue)item).Value);
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;
255
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        }
269        if (Content.IsCurrentInstance(run)) currentInstanceSeries.Points.Add(dataPoint);
270        else instancesSeries.Points.Add(dataPoint);
271      }
272    }
273    #endregion
274  }
275}
Note: See TracBrowser for help on using the repository browser.