Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13757 was 13757, checked in by abeham, 9 years ago

#2547:

  • worked on problem instance mapping
  • started working on improved suggestion algorithm
File size: 10.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.Core.Views;
25using HeuristicLab.Data;
26using HeuristicLab.MainForm;
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 CheckedItemListView<StringValue> characteristicsView;
42 
43    public UnderstandingProblemInstanceView() {
44      InitializeComponent();
45      showCharacteristicsCheckBox.Text = string.Empty;
46      showCharacteristicsCheckBox.Image = VSImageLibrary.Properties;
47      characteristicsView = new CheckedItemListView<StringValue>() {
48        Dock = DockStyle.Fill
49      };
50      mapSplitContainer.Panel2.Controls.Add(characteristicsView);
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      if (Content == null) {
56        problemInstancesView.Content = null;
57        characteristicsView.Content = null;
58        instanceMapChart.Series["InstancesSeries"].Points.Clear();
59        instanceMapChart.Series["CurrentInstanceSeries"].Points.Clear();
60      } else {
61        problemInstancesView.Content = Content.ProblemInstances;
62        characteristicsView.Content = Content.ProblemCharacteristics;
63        UpdateProjectionComboBox();
64        UpdateSizeComboBox();
65        UpdateColorComboBox();
66      }
67    }
68
69    #region Content Event Handlers
70    protected override void OnProblemChanged() {
71      base.OnProblemChanged();
72      SetEnabledStateOfControls();
73    }
74
75    protected override void OnProblemInstancesChanged() {
76      base.OnProblemInstancesChanged();
77      if (Content.ProblemInstances.UpdateOfRunsInProgress) return;
78      UpdateProjectionComboBox();
79      UpdateSizeComboBox();
80      UpdateColorComboBox();
81      UpdateProjection();
82    }
83    #endregion
84
85    private void UpdateProjectionComboBox() {
86      try {
87        SuppressEvents = true;
88        var selected = projectionComboBox.SelectedIndex >= 0 ? (string)projectionComboBox.SelectedItem : null;
89        projectionComboBox.Items.Clear();
90        foreach (var str in GetProjections()) {
91          projectionComboBox.Items.Add(str);
92          if (selected == str) projectionComboBox.SelectedItem = str;
93        }
94        if (selected == null && projectionComboBox.Items.Count > 0)
95          projectionComboBox.SelectedIndex = 0;
96      } finally { SuppressEvents = false; }
97    }
98
99    private void UpdateSizeComboBox() {
100      try {
101        SuppressEvents = true;
102        var selected = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : null;
103        sizeComboBox.Items.Clear();
104        sizeComboBox.Items.Add(string.Empty);
105        foreach (var str in Content.ProblemInstances.ResultNames.Where(x => !(x.StartsWith("Projection.") && (x.EndsWith(".X") || x.EndsWith(".Y"))))) {
106          sizeComboBox.Items.Add(str);
107          if (selected == str) sizeComboBox.SelectedItem = str;
108        }
109        if (selected == null && sizeComboBox.Items.Count > 0)
110          sizeComboBox.SelectedIndex = 0;
111      } finally { SuppressEvents = false; }
112    }
113
114    private void UpdateColorComboBox() {
115      try {
116        SuppressEvents = true;
117        var selected = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : null;
118        colorComboBox.Items.Clear();
119        colorComboBox.Items.Add(string.Empty);
120        foreach (var str in Content.ProblemInstances.ResultNames.Where(x => x.StartsWith("Rank."))) {
121          colorComboBox.Items.Add(str);
122          if (selected == str) colorComboBox.SelectedItem = str;
123        }
124        if (selected == null && colorComboBox.Items.Count > 0)
125          colorComboBox.SelectedIndex = 0;
126      } finally { SuppressEvents = false; }
127    }
128   
129    private IEnumerable<string> GetProjections() {
130      return Content.ProblemInstances
131        .SelectMany(x => x.Results.Where(y => Regex.IsMatch(y.Key, "^Projection[.].*[.][XY]$")))
132        .Select(x => Regex.Match(x.Key, "Projection[.](?<g>.*)[.][XY]").Groups["g"].Value)
133        .Distinct();
134    }
135
136    private void UpdateProjection() {
137      if (InvokeRequired) { Invoke((Action)UpdateProjection); return; }
138
139      var instancesSeries = instanceMapChart.Series["InstancesSeries"];
140      var currentInstanceSeries = instanceMapChart.Series["CurrentInstanceSeries"];
141
142      if (projectionComboBox.SelectedIndex < 0) {
143        instancesSeries.Points.Clear();
144        currentInstanceSeries.Points.Clear();
145        return;
146      }
147
148      var projection = (string)projectionComboBox.SelectedItem;
149      var size = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : string.Empty;
150      var color = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : string.Empty;
151
152      DoProjectProblemInstances(instancesSeries, currentInstanceSeries, projection,
153        size, color, invPropCheckBox.Checked, fromZeroCheckBox.Checked);
154    }
155
156    private void DoProjectProblemInstances(Series instancesSeries, Series currentInstanceSeries, string projection, string size, string color, bool invProp, bool fromZero) {
157      instancesSeries.Points.Clear();
158      currentInstanceSeries.Points.Clear();
159
160      double maxSize = 0, minSize = 0;
161      if (!string.IsNullOrEmpty(size)) {
162        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();
163        if (sizes.Count > 0) {
164          maxSize = sizes.Max(x => x.Value);
165          if (maxSize < 0 && fromZero) {
166            maxSize = 0;
167            minSize = sizes.Min(x => x.Value);
168          } else if (fromZero) {
169            minSize = 0;
170          } else {
171            minSize = sizes.Min(x => x.Value);
172          }
173        }
174      }
175      foreach (var run in Content.ProblemInstances) {
176        var xKey = "Projection." + projection + ".X";
177        var yKey = "Projection." + projection + ".Y";
178        if (!run.Results.ContainsKey(xKey) || !run.Results.ContainsKey(yKey)
179            || !(run.Results[xKey] is Data.DoubleValue) || !(run.Results[yKey] is Data.DoubleValue)) continue;
180        var x = ((Data.DoubleValue)run.Results[xKey]).Value;
181        var y = ((Data.DoubleValue)run.Results[yKey]).Value;
182        var dataPoint = new DataPoint(x, y) {
183          Label = run.Name,
184        };
185        IItem item;
186        if (maxSize > minSize && run.Results.TryGetValue(size, out item)) {
187          var dItem = item as Data.DoubleValue;
188          if (dItem == null && item is IntValue) dItem = new DoubleValue(((IntValue)item).Value);
189          if (dItem != null) {
190            if (double.IsNaN(dItem.Value))
191              dataPoint.MarkerSize = 1;
192            else {
193              if (invProp) dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (maxSize - dItem.Value) / (maxSize - minSize));
194              else dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (dItem.Value - minSize) / (maxSize - minSize));
195            }
196          }
197        } else if (maxSize == minSize) {
198          dataPoint.MarkerSize = instancesSeries.MarkerSize;
199        } else dataPoint.MarkerSize = 1;
200
201        if (!string.IsNullOrEmpty(color)) {
202          if (run.Results.TryGetValue(color, out item)) {
203            var v = (int)(item is DoubleValue ? ((DoubleValue)item).Value : (item is IntValue ? ((IntValue)item).Value : int.MaxValue));
204            switch (v) {
205              case 0: dataPoint.MarkerColor = Color.Green; break;
206              case 1: dataPoint.MarkerColor = Color.LightSeaGreen; break;
207              case 2: dataPoint.MarkerColor = Color.CornflowerBlue; break;
208              case 3: dataPoint.MarkerColor = Color.PaleVioletRed; break;
209              case 4: dataPoint.MarkerColor = Color.IndianRed; break;
210              default: dataPoint.MarkerColor = Color.LightGray; break;
211            }
212          }
213        }
214        if (Content.IsCurrentInstance(run)) currentInstanceSeries.Points.Add(dataPoint);
215        else instancesSeries.Points.Add(dataPoint);
216      }
217    }
218
219    #region Control Event Handlers
220    private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
221      UpdateProjection();
222    }
223
224    private void SizeComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
225      UpdateProjection();
226    }
227
228    private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e) {
229      UpdateProjection();
230    }
231
232    private void InvPropCheckBoxOnCheckedChanged(object sender, EventArgs e) {
233      UpdateProjection();
234    }
235
236    private void fromZeroCheckBox_CheckedChanged(object sender, EventArgs e) {
237      UpdateProjection();
238    }
239
240    private void showCharacteristicsCheckBox_CheckedChanged(object sender, EventArgs e) {
241      mapSplitContainer.Panel2Collapsed = !showCharacteristicsCheckBox.Checked;
242    }
243    #endregion
244  }
245}
Note: See TracBrowser for help on using the repository browser.