Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2457: working on recommendation algorithms

File size: 11.7 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      updateProjectionButton.Text = string.Empty;
48      updateProjectionButton.Image = VSImageLibrary.Refresh;
49      characteristics = new CheckedItemList<StringValue>();
50      characteristics.CheckedItemsChanged += CharacteristicsOnCheckedItemsChanged;
51      characteristicsPanel.Controls.Add(new ViewHost() {
52        Dock = DockStyle.Fill,
53        Content = characteristics
54      });
55    }
56
57    protected override void OnContentChanged() {
58      base.OnContentChanged();
59      if (Content == null) {
60        problemInstancesView.Content = null;
61      } else {
62        problemInstancesView.Content = Content.ProblemInstances;
63      }
64      UpdateCharacteristics();
65      UpdateProjectionComboBox();
66      UpdateSizeComboBox();
67      UpdateColorComboBox();
68    }
69
70    protected override void SetEnabledStateOfControls() {
71      base.SetEnabledStateOfControls();
72      updateProjectionButton.Enabled = Content != null && characteristics.CheckedItems.Any();
73    }
74
75    #region Update Controls
76    private void UpdateCharacteristics() {
77      if (InvokeRequired) { Invoke((Action)UpdateCharacteristics); return; }
78      try {
79        SuppressEvents = true;
80        var @checked = new HashSet<string>(characteristics.CheckedItems.Select(x => x.Value.Value));
81        characteristics.Clear();
82
83        if (Content == null) return;
84        if (@checked.Count == 0 && Content.ProblemInstances.ResultNames.Any(x => x.StartsWith("Characteristic.")))
85          @checked = new HashSet<string>(Content.ProblemInstances.ResultNames.Where(x => x.StartsWith("Characteristic.")));
86
87        foreach (var c in Content.ProblemInstances.ResultNames) {
88          characteristics.Add(new StringValue(c), @checked.Contains(c));
89        }
90      } finally { SuppressEvents = false; }
91    }
92
93    private void UpdateProjectionComboBox() {
94      if (InvokeRequired) { Invoke((Action)UpdateProjectionComboBox); return; }
95      try {
96        SuppressEvents = true;
97        var selected = projectionComboBox.SelectedIndex >= 0 ? (string)projectionComboBox.SelectedItem : null;
98        projectionComboBox.Items.Clear();
99        foreach (var str in GetProjections()) {
100          projectionComboBox.Items.Add(str);
101          if (selected == str) projectionComboBox.SelectedItem = str;
102        }
103        if (selected == null && projectionComboBox.Items.Count > 0)
104          projectionComboBox.SelectedIndex = 0;
105      } finally { SuppressEvents = false; }
106    }
107
108    private void UpdateSizeComboBox() {
109      if (InvokeRequired) { Invoke((Action)UpdateSizeComboBox); return; }
110      try {
111        SuppressEvents = true;
112        var selected = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : null;
113        sizeComboBox.Items.Clear();
114       
115        if (Content == null) return;
116
117        sizeComboBox.Items.Add(string.Empty);
118        foreach (var str in Content.ProblemInstances.ResultNames.Where(x => !(x.StartsWith("Projection.") && (x.EndsWith(".X") || x.EndsWith(".Y"))))) {
119          sizeComboBox.Items.Add(str);
120          if (selected == str) sizeComboBox.SelectedItem = str;
121        }
122        if (selected == null && sizeComboBox.Items.Count > 0)
123          sizeComboBox.SelectedIndex = 0;
124      } finally { SuppressEvents = false; }
125    }
126
127    private void UpdateColorComboBox() {
128      if (InvokeRequired) { Invoke((Action)UpdateColorComboBox); return; }
129      try {
130        SuppressEvents = true;
131        var selected = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : null;
132        colorComboBox.Items.Clear();
133
134        if (Content == null) return;
135
136        colorComboBox.Items.Add(string.Empty);
137        foreach (var str in Content.ProblemInstances.ResultNames.Where(x => x.StartsWith("Rank."))) {
138          colorComboBox.Items.Add(str);
139          if (selected == str) colorComboBox.SelectedItem = str;
140        }
141        if (selected == null && colorComboBox.Items.Count > 0)
142          colorComboBox.SelectedIndex = 0;
143      } finally { SuppressEvents = false; }
144    }
145
146    private void UpdateProjection() {
147      if (InvokeRequired) { Invoke((Action)UpdateProjection); return; }
148
149      var instancesSeries = instanceMapChart.Series["InstancesSeries"];
150      var currentInstanceSeries = instanceMapChart.Series["CurrentInstanceSeries"];
151
152      if (projectionComboBox.SelectedIndex < 0 || Content == null) {
153        instancesSeries.Points.Clear();
154        currentInstanceSeries.Points.Clear();
155        return;
156      }
157
158      var projection = (string)projectionComboBox.SelectedItem;
159      var size = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : string.Empty;
160      var color = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : string.Empty;
161
162      DoProjectProblemInstances(instancesSeries, currentInstanceSeries, projection, size, color, invPropCheckBox.Checked);
163    }
164    #endregion
165
166    #region Content Event Handlers
167    protected override void OnProblemChanged() {
168      base.OnProblemChanged();
169      SetEnabledStateOfControls();
170    }
171
172    protected override void OnProblemInstancesChanged() {
173      base.OnProblemInstancesChanged();
174      if (Content.ProblemInstances.UpdateOfRunsInProgress) return;
175      UpdateCharacteristics();
176      UpdateProjectionComboBox();
177      UpdateSizeComboBox();
178      UpdateColorComboBox();
179      UpdateProjection();
180    }
181    #endregion
182
183    #region Control Event Handlers
184    private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
185      UpdateProjection();
186    }
187
188    private void SizeComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
189      UpdateProjection();
190    }
191
192    private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e) {
193      UpdateProjection();
194    }
195
196    private void InvPropCheckBoxOnCheckedChanged(object sender, EventArgs e) {
197      UpdateProjection();
198    }
199
200    private void showCharacteristicsCheckBox_CheckedChanged(object sender, EventArgs e) {
201      mapSplitContainer.Panel2Collapsed = !showCharacteristicsCheckBox.Checked;
202    }
203
204    private void updateProjectionButton_Click(object sender, EventArgs e) {
205      Content.UpdateInstanceProjection(characteristics.CheckedItems.Select(x => x.Value.Value).ToArray());
206    }
207    #endregion
208
209    #region Other Event Handlers
210    private void CharacteristicsOnCheckedItemsChanged(object sender, EventArgs e) {
211      SetEnabledStateOfControls();
212    }
213    #endregion
214
215    #region Helper Classes and Methods
216    private IEnumerable<string> GetProjections() {
217      if (Content == null) return new string[0];
218      return Content.ProblemInstances.ResultNames
219        .Where(x => Regex.IsMatch(x, "^Projection[.].*[.][XY]$"))
220        .Select(x => Regex.Match(x, "Projection[.](?<g>.*)[.][XY]").Groups["g"].Value)
221        .Distinct();
222    }
223
224    private void DoProjectProblemInstances(Series instancesSeries, Series currentInstanceSeries, string projection, string size, string color, bool invProp) {
225      instancesSeries.Points.Clear();
226      currentInstanceSeries.Points.Clear();
227
228      double maxSize = 0, minSize = 0;
229      if (!string.IsNullOrEmpty(size)) {
230        var sizes = Content.ProblemInstances
231                           .Where(x => x.Results.ContainsKey(size))
232                           .Select(x => x.Results[size])
233                           .Select(x => x is DoubleValue ? ((DoubleValue)x).Value : (x is IntValue ? ((IntValue)x).Value : double.NaN))
234                           .Where(x => !double.IsNaN(x)).ToList();
235        if (sizes.Count > 0) {
236          maxSize = sizes.Max();
237          if (maxSize < 0) {
238            maxSize = 0;
239            minSize = sizes.Min();
240          } else {
241            minSize = sizes.Min();
242          }
243        }
244      }
245      foreach (var run in Content.ProblemInstances) {
246        var xKey = "Projection." + projection + ".X";
247        var yKey = "Projection." + projection + ".Y";
248        if (!run.Results.ContainsKey(xKey) || !run.Results.ContainsKey(yKey)
249            || !(run.Results[xKey] is DoubleValue) || !(run.Results[yKey] is DoubleValue)) continue;
250        var x = ((DoubleValue)run.Results[xKey]).Value;
251        var y = ((DoubleValue)run.Results[yKey]).Value;
252        var dataPoint = new DataPoint(x, y) {
253          Label = run.Name,
254        };
255        IItem item;
256        if (maxSize > minSize && run.Results.TryGetValue(size, out item)) {
257          var dItem = item as DoubleValue;
258          if (dItem == null && item is IntValue) dItem = new DoubleValue(((IntValue)item).Value);
259          if (dItem != null) {
260            if (double.IsNaN(dItem.Value))
261              dataPoint.MarkerSize = 1;
262            else {
263              if (invProp) dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (maxSize - dItem.Value) / (maxSize - minSize));
264              else dataPoint.MarkerSize = (int)Math.Round(5 + 15 * (dItem.Value - minSize) / (maxSize - minSize));
265            }
266          }
267        } else if (maxSize == minSize) {
268          dataPoint.MarkerSize = instancesSeries.MarkerSize;
269        } else dataPoint.MarkerSize = 1;
270
271        if (!string.IsNullOrEmpty(color)) {
272          if (run.Results.TryGetValue(color, out item)) {
273            var v = (int)(item is DoubleValue ? ((DoubleValue)item).Value : (item is IntValue ? ((IntValue)item).Value : int.MaxValue));
274            switch (v) {
275              case 0: dataPoint.MarkerColor = Color.Green; break;
276              case 1: dataPoint.MarkerColor = Color.LightSeaGreen; break;
277              case 2: dataPoint.MarkerColor = Color.CornflowerBlue; break;
278              case 3: dataPoint.MarkerColor = Color.PaleVioletRed; break;
279              case 4: dataPoint.MarkerColor = Color.IndianRed; break;
280              default: dataPoint.MarkerColor = Color.LightGray; break;
281            }
282          }
283        }
284        if (Content.IsCurrentInstance(run)) currentInstanceSeries.Points.Add(dataPoint);
285        else instancesSeries.Points.Add(dataPoint);
286      }
287    }
288    #endregion
289  }
290}
Note: See TracBrowser for help on using the repository browser.