source: branches/GeneralizedQAP/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.cs @ 15676

Last change on this file since 15676 was 15676, checked in by abeham, 4 years ago

#1614:

  • Refactored RLD view, added additional classes
File size: 61.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System;
23using System.Collections;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Drawing;
27using System.Globalization;
28using System.Linq;
29using System.Windows.Forms;
30using System.Windows.Forms.DataVisualization.Charting;
31using HeuristicLab.Analysis;
32using HeuristicLab.Collections;
33using HeuristicLab.Core;
34using HeuristicLab.Core.Views;
35using HeuristicLab.Data;
36using HeuristicLab.MainForm;
37using HeuristicLab.MainForm.WindowsForms;
38
39namespace HeuristicLab.Optimization.Views {
40  [View("Run-length Distribution View")]
41  [Content(typeof(RunCollection), false)]
42  public partial class RunCollectionRLDView : ItemView {
43    private List<Series> invisibleTargetSeries;
44
45    private const string AllInstances = "All Instances";
46
47    private static readonly Color[] colors = new[] {
48      Color.FromArgb(0x40, 0x6A, 0xB7),
49      Color.FromArgb(0xB1, 0x6D, 0x01),
50      Color.FromArgb(0x4E, 0x8A, 0x06),
51      Color.FromArgb(0x75, 0x50, 0x7B),
52      Color.FromArgb(0x72, 0x9F, 0xCF),
53      Color.FromArgb(0xA4, 0x00, 0x00),
54      Color.FromArgb(0xAD, 0x7F, 0xA8),
55      Color.FromArgb(0x29, 0x50, 0xCF),
56      Color.FromArgb(0x90, 0xB0, 0x60),
57      Color.FromArgb(0xF5, 0x89, 0x30),
58      Color.FromArgb(0x55, 0x57, 0x53),
59      Color.FromArgb(0xEF, 0x59, 0x59),
60      Color.FromArgb(0xED, 0xD4, 0x30),
61      Color.FromArgb(0x63, 0xC2, 0x16),
62    };
63    private static readonly ChartDashStyle[] lineStyles = new[] {
64      ChartDashStyle.Solid,
65      ChartDashStyle.Dash,
66      ChartDashStyle.DashDot,
67      ChartDashStyle.Dot
68    };
69    private static readonly DataRowVisualProperties.DataRowLineStyle[] hlLineStyles = new[] {
70      DataRowVisualProperties.DataRowLineStyle.Solid,
71      DataRowVisualProperties.DataRowLineStyle.Dash,
72      DataRowVisualProperties.DataRowLineStyle.DashDot,
73      DataRowVisualProperties.DataRowLineStyle.Dot
74    };
75
76    public new RunCollection Content {
77      get { return (RunCollection)base.Content; }
78      set { base.Content = value; }
79    }
80
81    private List<AlgorithmInstance> groups;
82
83    private double[] targets;
84    private double[] budgets;
85    private bool targetsAreRelative = true;
86    private bool showLabelsInTargetChart = true;
87    private readonly BindingList<ProblemInstance> problems;
88
89    private bool suppressUpdates;
90    private readonly IndexedDataTable<double> byCostDataTable;
91    public IndexedDataTable<double> ByCostDataTable {
92      get { return byCostDataTable; }
93    }
94
95    public RunCollectionRLDView() {
96      InitializeComponent();
97      invisibleTargetSeries = new List<Series>();
98
99      targetChart.CustomizeAllChartAreas();
100      targetChart.ChartAreas[0].CursorX.Interval = 1;
101      targetChart.SuppressExceptions = true;
102      byCostDataTable = new IndexedDataTable<double>("ECDF by Cost", "A data table containing the ECDF of function values (relative to best-known).") {
103        VisualProperties = {
104          YAxisTitle = "Proportion of runs",
105          YAxisMinimumFixedValue = 0,
106          YAxisMinimumAuto = false,
107          YAxisMaximumFixedValue = 1,
108          YAxisMaximumAuto = false
109        }
110      };
111      byCostViewHost.Content = byCostDataTable;
112      suppressUpdates = true;
113      relativeOrAbsoluteComboBox.SelectedItem = targetsAreRelative ? "relative" : "absolute";
114      problems = new BindingList<ProblemInstance>();
115      problemComboBox.DataSource = new BindingSource() { DataSource = problems };
116      problemComboBox.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
117      suppressUpdates = false;
118    }
119
120    #region Content events
121    protected override void RegisterContentEvents() {
122      base.RegisterContentEvents();
123      Content.ItemsAdded += Content_ItemsAdded;
124      Content.ItemsRemoved += Content_ItemsRemoved;
125      Content.CollectionReset += Content_CollectionReset;
126      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
127      Content.OptimizerNameChanged += Content_AlgorithmNameChanged;
128    }
129    protected override void DeregisterContentEvents() {
130      Content.ItemsAdded -= Content_ItemsAdded;
131      Content.ItemsRemoved -= Content_ItemsRemoved;
132      Content.CollectionReset -= Content_CollectionReset;
133      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
134      Content.OptimizerNameChanged -= Content_AlgorithmNameChanged;
135      base.DeregisterContentEvents();
136    }
137
138    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
139      if (suppressUpdates) return;
140      if (InvokeRequired) {
141        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
142        return;
143      }
144      UpdateGroupAndProblemComboBox();
145      UpdateDataTableComboBox();
146      GroupRuns();
147      foreach (var run in e.Items)
148        RegisterRunEvents(run);
149    }
150    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
151      if (suppressUpdates) return;
152      if (InvokeRequired) {
153        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
154        return;
155      }
156      UpdateGroupAndProblemComboBox();
157      UpdateDataTableComboBox();
158      GroupRuns();
159      foreach (var run in e.Items)
160        DeregisterRunEvents(run);
161    }
162    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
163      if (suppressUpdates) return;
164      if (InvokeRequired) {
165        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
166        return;
167      }
168      UpdateGroupAndProblemComboBox();
169      UpdateDataTableComboBox();
170      GroupRuns();
171      foreach (var run in e.OldItems)
172        DeregisterRunEvents(run);
173    }
174    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
175      if (InvokeRequired)
176        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
177      else UpdateCaption();
178    }
179    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
180      if (InvokeRequired) {
181        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
182        return;
183      }
184      suppressUpdates = Content.UpdateOfRunsInProgress;
185      if (!suppressUpdates) {
186        UpdateDataTableComboBox();
187        UpdateGroupAndProblemComboBox();
188        UpdateRuns();
189      }
190    }
191
192    private void RegisterRunEvents(IRun run) {
193      run.PropertyChanged += run_PropertyChanged;
194    }
195    private void DeregisterRunEvents(IRun run) {
196      run.PropertyChanged -= run_PropertyChanged;
197    }
198    private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
199      if (suppressUpdates) return;
200      if (InvokeRequired) {
201        Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
202      } else {
203        if (e.PropertyName == "Visible")
204          UpdateRuns();
205      }
206    }
207    #endregion
208
209    protected override void OnContentChanged() {
210      base.OnContentChanged();
211      dataTableComboBox.Items.Clear();
212      groupComboBox.Items.Clear();
213      targetChart.ChartAreas[0].AxisX.IsLogarithmic = false;
214      targetChart.Series.Clear();
215      invisibleTargetSeries.Clear();
216      byCostDataTable.VisualProperties.XAxisLogScale = false;
217      byCostDataTable.Rows.Clear();
218
219      UpdateCaption();
220      if (Content != null) {
221        UpdateGroupAndProblemComboBox();
222        UpdateDataTableComboBox();
223        GroupRuns();
224      }
225    }
226
227
228    private void UpdateGroupAndProblemComboBox() {
229      var selectedGroupItem = (string)groupComboBox.SelectedItem;
230
231      var groupings = Content.ParameterNames.OrderBy(x => x).ToArray();
232      groupComboBox.Items.Clear();
233      groupComboBox.Items.Add(AllInstances);
234      groupComboBox.Items.AddRange(groupings);
235      if (selectedGroupItem != null && groupComboBox.Items.Contains(selectedGroupItem)) {
236        groupComboBox.SelectedItem = selectedGroupItem;
237      } else if (groupComboBox.Items.Count > 0) {
238        groupComboBox.SelectedItem = groupComboBox.Items[0];
239      }
240
241      var table = (string)dataTableComboBox.SelectedItem;
242      var problemDict = CalculateBestTargetPerProblemInstance(table);
243
244      var problemTypesDifferent = problemDict.Keys.Select(x => x.ProblemType).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1;
245      var problemNamesDifferent = problemDict.Keys.Select(x => x.ProblemName).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1;
246      var evaluatorDifferent = problemDict.Keys.Select(x => x.Evaluator).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1;
247      var maximizationDifferent = problemDict.Keys.Select(x => x.Maximization).Distinct().Count() > 1;
248      var allEqual = !problemTypesDifferent && !problemNamesDifferent && !evaluatorDifferent && !maximizationDifferent;
249
250      var selectedProblemItem = (ProblemInstance)problemComboBox.SelectedItem;
251      problemComboBox.DataSource = null;
252      problemComboBox.Items.Clear();
253      problems.Clear();
254      problems.Add(ProblemInstance.MatchAll);
255      problemComboBox.DataSource = new BindingSource() { DataSource = problems };
256      problemComboBox.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
257      if (problems[0].Equals(selectedProblemItem)) problemComboBox.SelectedItem = problems[0];
258      foreach (var p in problemDict.ToList()) {
259        p.Key.BestKnownQuality = p.Value;
260        p.Key.DisplayProblemType = problemTypesDifferent;
261        p.Key.DisplayProblemName = problemNamesDifferent || allEqual;
262        p.Key.DisplayEvaluator = evaluatorDifferent;
263        p.Key.DisplayMaximization = maximizationDifferent;
264        problems.Add(p.Key);
265        if (p.Key.Equals(selectedProblemItem)) problemComboBox.SelectedItem = p.Key;
266      }
267
268      SetEnabledStateOfControls();
269    }
270
271    private void UpdateDataTableComboBox() {
272      string selectedItem = (string)dataTableComboBox.SelectedItem;
273
274      dataTableComboBox.Items.Clear();
275      var dataTables = (from run in Content
276                        from result in run.Results
277                        where result.Value is IndexedDataTable<double>
278                        select result.Key).Distinct().ToArray();
279
280      dataTableComboBox.Items.AddRange(dataTables);
281      if (selectedItem != null && dataTableComboBox.Items.Contains(selectedItem)) {
282        dataTableComboBox.SelectedItem = selectedItem;
283      } else if (dataTableComboBox.Items.Count > 0) {
284        dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
285      }
286    }
287
288    protected override void SetEnabledStateOfControls() {
289      base.SetEnabledStateOfControls();
290      groupComboBox.Enabled = Content != null;
291      problemComboBox.Enabled = Content != null && problemComboBox.Items.Count > 1;
292      dataTableComboBox.Enabled = Content != null && dataTableComboBox.Items.Count > 1;
293      addTargetsAsResultButton.Enabled = Content != null && targets != null && dataTableComboBox.SelectedIndex >= 0;
294      addBudgetsAsResultButton.Enabled = Content != null && budgets != null && dataTableComboBox.SelectedIndex >= 0;
295      generateTargetsButton.Enabled = targets != null;
296    }
297
298    private void GroupRuns() {
299      groups = new List<AlgorithmInstance>();
300
301      var table = (string)dataTableComboBox.SelectedItem;
302      if (string.IsNullOrEmpty(table)) return;
303
304      var selectedGroup = (string)groupComboBox.SelectedItem;
305      if (string.IsNullOrEmpty(selectedGroup)) return;
306
307      var selectedProblem = (ProblemInstance)problemComboBox.SelectedItem;
308      if (selectedProblem == null) return;
309
310      foreach (var alg in from r in Content
311                          where (selectedGroup == AllInstances || r.Parameters.ContainsKey(selectedGroup))
312                            && r.Visible
313                            && selectedProblem.Match(r)
314                            && r.Results.ContainsKey(table)
315                            && r.Results[table] is IndexedDataTable<double>
316                          let key = selectedGroup == AllInstances ? AllInstances : r.Parameters[selectedGroup].ToString()
317                          group r by key into g
318                          select g) {
319        var trials = from run in alg
320                     from pd in problems
321                     where pd.Equals(new ProblemInstance(run))
322                     let cgraph = ((IndexedDataTable<double>)run.Results[table])
323                     group cgraph by pd into g
324                     select g;
325
326        groups.Add(new AlgorithmInstance(alg.Key, trials));
327      }
328    }
329
330    #region Performance analysis by (multiple) target(s)
331    private void UpdateResultsByTarget() {
332      // necessary to reset log scale -> empty chart cannot use log scaling
333      targetChart.ChartAreas[0].AxisX.IsLogarithmic = false;
334      targetChart.Series.Clear();
335      invisibleTargetSeries.Clear();
336     
337      var table = (string)dataTableComboBox.SelectedItem;
338      if (string.IsNullOrEmpty(table)) return;
339
340      if (targets == null) GenerateDefaultTargets();
341     
342      if (groups.Count == 0) return;
343
344      var xAxisTitles = new HashSet<string>();
345
346      // hits describes the number of target hits at a certain time for a certain group
347      var hits = new Dictionary<string, SortedList<double, int>>();
348      // misses describes the number of target misses after a certain time for a certain group
349      // for instance when a run ends, but has not achieved all targets, misses describes
350      // how many targets have been left open at the point when the run ended
351      var misses = new Dictionary<string, SortedList<double, int>>();
352      var totalRuns = new Dictionary<string, int>();
353
354      var aggregate = aggregateTargetsCheckBox.Checked;
355      double minEff = double.MaxValue, maxEff = double.MinValue;
356      foreach (var alg in groups) {
357        var noRuns = 0;
358        SortedList<double, int> epdfHits = null, epdfMisses = null;
359        if (aggregate) {
360          hits[alg.Name] = epdfHits = new SortedList<double, int>();
361          misses[alg.Name] = epdfMisses = new SortedList<double, int>();
362        }
363        foreach (var problem in alg.GetCases()) {
364          var max = problem.IsMaximization();
365          var absTargets = GetAbsoluteTargets(problem).ToArray();
366          foreach (var run in alg.GetTrials(problem)) {
367            noRuns++;
368            xAxisTitles.Add(run.XAxisName);
369
370            var efforts = absTargets.Select(t => GetEffortToHitTarget(run, t, max)).ToArray();
371            minEff = Math.Min(minEff, efforts.Min(x => x.Item2));
372            maxEff = Math.Max(maxEff, efforts.Max(x => x.Item2));
373            for (var idx = 0; idx < efforts.Length; idx++) {
374              var e = efforts[idx];
375              if (!aggregate) {
376                var key = alg.Name + "@" + (targetsAreRelative
377                            ? (targets[idx] * 100).ToString(CultureInfo.CurrentCulture.NumberFormat) + "%"
378                            : targets[idx].ToString(CultureInfo.CurrentCulture.NumberFormat));
379                if (!hits.TryGetValue(key, out epdfHits))
380                  hits[key] = epdfHits = new SortedList<double, int>();
381                if (!misses.TryGetValue(key, out epdfMisses))
382                  misses[key] = epdfMisses = new SortedList<double, int>();
383                totalRuns[key] = noRuns;
384              };
385              var list = e.Item1 ? epdfHits : epdfMisses;
386              int v;
387              if (list.TryGetValue(e.Item2, out v))
388                list[e.Item2] = v + 1;
389              else list[e.Item2] = 1;
390            }
391          }
392        }
393        if (aggregate) totalRuns[alg.Name] = noRuns;
394      }
395
396      UpdateTargetChartAxisXBounds(minEff, maxEff);
397
398      DrawTargetsEcdf(hits, misses, totalRuns);
399
400      if (targets.Length == 1) {
401        if (targetsAreRelative)
402          targetChart.ChartAreas[0].AxisY.Title = "Probability to be " + (targets[0] * 100) + "% worse than best";
403        else targetChart.ChartAreas[0].AxisY.Title = "Probability to reach at least a fitness of " + targets[0];
404      } else targetChart.ChartAreas[0].AxisY.Title = "Proportion of reached targets";
405      targetChart.ChartAreas[0].AxisX.Title = string.Join(" / ", xAxisTitles);
406      targetChart.ChartAreas[0].AxisX.IsLogarithmic = CanDisplayLogarithmic();
407      targetChart.ChartAreas[0].CursorY.Interval = 0.05;
408
409      UpdateErtTables();
410    }
411
412    private void DrawTargetsEcdf(Dictionary<string, SortedList<double, int>> hits, Dictionary<string, SortedList<double, int>> misses, Dictionary<string, int> noRuns) {
413      var colorCount = 0;
414      var lineStyleCount = 0;
415     
416      var showMarkers = markerCheckBox.Checked;
417      foreach (var list in hits) {
418        var row = new Series(list.Key) {
419          ChartType = SeriesChartType.StepLine,
420          BorderWidth = 3,
421          Color = colors[colorCount],
422          BorderDashStyle = lineStyles[lineStyleCount],
423        };
424        var rowShade = new Series(list.Key + "-range") {
425          IsVisibleInLegend = false,
426          ChartType = SeriesChartType.Range,
427          Color = Color.FromArgb(32, colors[colorCount]),
428          YValuesPerPoint = 2
429        };
430
431        var ecdf = 0.0;
432        var missedecdf = 0.0;
433        var iter = misses[list.Key].GetEnumerator();
434        var moreMisses = iter.MoveNext();
435        var totalTargets = noRuns[list.Key];
436        if (aggregateTargetsCheckBox.Checked) totalTargets *= targets.Length;
437        var movingTargets = totalTargets;
438        var labelPrinted = false;
439        foreach (var h in list.Value) {
440          var prevmissedecdf = missedecdf;
441          while (moreMisses && iter.Current.Key <= h.Key) {
442            if (!labelPrinted && row.Points.Count > 0) {
443              var point = row.Points.Last();
444              if (showLabelsInTargetChart)
445                point.Label = row.Name;
446              point.MarkerStyle = MarkerStyle.Cross;
447              point.MarkerBorderWidth = 1;
448              point.MarkerSize = 10;
449              labelPrinted = true;
450              rowShade.Points.Add(new DataPoint(point.XValue, new[] {ecdf / totalTargets, (ecdf + missedecdf) / totalTargets}));
451            }
452            missedecdf += iter.Current.Value;
453            movingTargets -= iter.Current.Value;
454            if (row.Points.Count > 0 && row.Points.Last().XValue == iter.Current.Key) {
455              row.Points.Last().SetValueY(ecdf / movingTargets);
456              row.Points.Last().Color = Color.FromArgb(255 - (int)Math.Floor(255 * (prevmissedecdf / totalTargets)), colors[colorCount]);
457            } else {
458              var dp = new DataPoint(iter.Current.Key, ecdf / movingTargets) {
459                Color = Color.FromArgb(255 - (int)Math.Floor(255 * (prevmissedecdf / totalTargets)), colors[colorCount])
460              };
461              if (showMarkers) {
462                dp.MarkerStyle = MarkerStyle.Circle;
463                dp.MarkerBorderWidth = 1;
464                dp.MarkerSize = 5;
465              }
466              row.Points.Add(dp);
467              prevmissedecdf = missedecdf;
468            }
469            if (boundShadingCheckBox.Checked) {
470              if (rowShade.Points.Count > 0 && rowShade.Points.Last().XValue == iter.Current.Key)
471                rowShade.Points.Last().SetValueY(ecdf / totalTargets, (ecdf + missedecdf) / totalTargets);
472              else rowShade.Points.Add(new DataPoint(iter.Current.Key, new[] {ecdf / totalTargets, (ecdf + missedecdf) / totalTargets}));
473            }
474            moreMisses = iter.MoveNext();
475            if (!labelPrinted) {
476              var point = row.Points.Last();
477              if (showLabelsInTargetChart)
478                point.Label = row.Name;
479              point.MarkerStyle = MarkerStyle.Cross;
480              point.MarkerBorderWidth = 1;
481              point.MarkerSize = 10;
482              labelPrinted = true;
483            }
484          }
485          ecdf += h.Value;
486          if (row.Points.Count > 0 && row.Points.Last().XValue == h.Key) {
487            row.Points.Last().SetValueY(ecdf / movingTargets);
488            row.Points.Last().Color = Color.FromArgb(255 - (int)Math.Floor(255 * (missedecdf / totalTargets)), colors[colorCount]);
489          } else {
490            var dp = new DataPoint(h.Key, ecdf / movingTargets) {
491              Color = Color.FromArgb(255 - (int)Math.Floor(255 * (missedecdf / totalTargets)), colors[colorCount])
492            };
493            if (showMarkers) {
494              dp.MarkerStyle = MarkerStyle.Circle;
495              dp.MarkerBorderWidth = 1;
496              dp.MarkerSize = 5;
497            }
498            row.Points.Add(dp);
499          }
500          if (missedecdf > 0 && boundShadingCheckBox.Checked) {
501            if (rowShade.Points.Count > 0 && rowShade.Points.Last().XValue == h.Key)
502              rowShade.Points.Last().SetValueY(ecdf / totalTargets, (ecdf + missedecdf) / totalTargets);
503            else rowShade.Points.Add(new DataPoint(h.Key, new[] {ecdf / totalTargets, (ecdf + missedecdf) / totalTargets}));
504          }
505        }
506
507        while (moreMisses) {
508          // if there are misses beyond the last hit we extend the shaded area
509          missedecdf += iter.Current.Value;
510          var dp = new DataPoint(iter.Current.Key, ecdf / movingTargets) {
511            Color = Color.FromArgb(255 - (int)Math.Floor(255 * (missedecdf / totalTargets)), colors[colorCount])
512          };
513          if (showMarkers) {
514            dp.MarkerStyle = MarkerStyle.Circle;
515            dp.MarkerBorderWidth = 1;
516            dp.MarkerSize = 5;
517          }
518          row.Points.Add(dp);
519          if (boundShadingCheckBox.Checked) {
520            rowShade.Points.Add(new DataPoint(iter.Current.Key, new[] {ecdf / totalTargets, (ecdf + missedecdf) / totalTargets}));
521          }
522          moreMisses = iter.MoveNext();
523
524          if (!labelPrinted && row.Points.Count > 0) {
525            var point = row.Points.Last();
526            if (showLabelsInTargetChart)
527              point.Label = row.Name;
528            point.MarkerStyle = MarkerStyle.Cross;
529            point.MarkerBorderWidth = 1;
530            point.MarkerSize = 10;
531            labelPrinted = true;
532          }
533        }
534
535        if (!labelPrinted) {
536          var point = row.Points.Last();
537          if (showLabelsInTargetChart)
538            point.Label = row.Name;
539          point.MarkerStyle = MarkerStyle.Cross;
540          point.MarkerBorderWidth = 1;
541          point.MarkerSize = 10;
542          rowShade.Points.Add(new DataPoint(point.XValue, new[] {ecdf / totalTargets, (ecdf + missedecdf) / totalTargets}));
543          labelPrinted = true;
544        }
545
546        ConfigureSeries(row);
547        targetChart.Series.Add(rowShade);
548        targetChart.Series.Add(row);
549
550        colorCount = (colorCount + 1) % colors.Length;
551        if (colorCount == 0) lineStyleCount = (lineStyleCount + 1) % lineStyles.Length;
552      }
553    }
554
555    private void UpdateTargetChartAxisXBounds(double minEff, double maxEff) {
556      var minZeros = (int)Math.Floor(Math.Log10(minEff));
557      var maxZeros = (int)Math.Floor(Math.Log10(maxEff));
558      var axisMin = (decimal)Math.Pow(10, minZeros);
559      var axisMax = (decimal)Math.Pow(10, maxZeros);
560      if (!targetLogScalingCheckBox.Checked) {
561        var minAdd = (decimal)Math.Pow(10, minZeros - 1) * 2;
562        var maxAdd = (decimal)Math.Pow(10, maxZeros - 1) * 2;
563        while (axisMin + minAdd < (decimal)minEff) axisMin += minAdd;
564        while (axisMax <= (decimal)maxEff) axisMax += maxAdd;
565      } else axisMax = (decimal)Math.Pow(10, (int)Math.Ceiling(Math.Log10(maxEff)));
566      targetChart.ChartAreas[0].AxisX.Minimum = (double)axisMin;
567      targetChart.ChartAreas[0].AxisX.Maximum = (double)axisMax;
568    }
569
570    private IEnumerable<double> GetAbsoluteTargets(ProblemInstance pInstance) {
571      if (!targetsAreRelative) return targets;
572      var maximization = pInstance.IsMaximization();
573      var bestKnown = pInstance.BestKnownQuality;
574      if (double.IsNaN(bestKnown)) throw new ArgumentException("Problem instance does not have a defined best - known quality.");
575      IEnumerable<double> tmp = null;
576      if (bestKnown > 0) {
577        tmp = targets.Select(x => (maximization ? (1 - x) : (1 + x)) * bestKnown);
578      } else if (bestKnown < 0) {
579        tmp = targets.Select(x => (!maximization ? (1 - x) : (1 + x)) * bestKnown);
580      } else {
581        // relative to 0 is impossible
582        tmp = targets;
583      }
584      return tmp;
585    }
586
587    private double[] GetAbsoluteTargetsWorstToBest(ProblemInstance pInstance) {
588      if (double.IsNaN(pInstance.BestKnownQuality)) throw new ArgumentException("Problem instance does not have a defined best-known quality.");
589      var absTargets = GetAbsoluteTargets(pInstance);
590      return (pInstance.IsMaximization()
591        ? absTargets.OrderBy(x => x) : absTargets.OrderByDescending(x => x)).ToArray();
592    }
593
594    private void GenerateDefaultTargets() {
595      targets = new[] { 0.1, 0.095, 0.09, 0.085, 0.08, 0.075, 0.07, 0.065, 0.06, 0.055, 0.05, 0.045, 0.04, 0.035, 0.03, 0.025, 0.02, 0.015, 0.01, 0.005, 0 };
596      SynchronizeTargetTextBox();
597    }
598
599    private Tuple<bool, double> GetEffortToHitTarget(
600        ConvergenceGraph cgraph,
601        double absTarget, bool maximization) {
602      if (cgraph.Points == 0)
603        throw new ArgumentException("Convergence graph is empty.", "convergenceGraph");
604     
605      var index = cgraph.BinarySearch(new ConvergenceGraphPoint(0.0, absTarget), new TargetComparer(maximization));
606      if (index >= 0) {
607        return Tuple.Create(true, cgraph[index].Runlength);
608      } else {
609        index = ~index;
610        if (index >= cgraph.Points)
611          return Tuple.Create(false, cgraph.Last().Runlength);
612        return Tuple.Create(true, cgraph[index].Runlength);
613      }
614    }
615
616    private void UpdateErtTables() {
617      ertTableView.Content = null;
618      var columns = targets.Length + 1;
619      var totalRows = groups.Count * groups.Max(x => x.GetNumberOfCases()) + groups.Max(x => x.GetNumberOfCases());
620      var matrix = new StringMatrix(totalRows, columns);
621      var rowNames = new List<string>();
622      matrix.ColumnNames = targets.Select(x => targetsAreRelative ? (100 * x).ToString() + "%" : x.ToString())
623        .Concat(new[] { "#succ" }).ToList();
624      var rowCount = 0;
625
626      var tableName = (string)dataTableComboBox.SelectedItem;
627      if (string.IsNullOrEmpty(tableName)) return;
628     
629      var problems = groups.SelectMany(x => x.GetCases()).Distinct().ToList();
630
631      foreach (var problem in problems) {
632        if (double.IsNaN(problem.BestKnownQuality)) continue;
633        rowNames.Add(problem.ToString());
634        var max = problem.IsMaximization();
635        var absTargets = GetAbsoluteTargetsWorstToBest(problem);
636        if (targetsAreRelative) {
637          // print out the absolute target values
638          for (var i = 0; i < absTargets.Length; i++) {
639            matrix[rowCount, i] = absTargets[i].ToString("##,0.0", CultureInfo.CurrentCulture.NumberFormat);
640          }
641        }
642        rowCount++;
643
644        foreach (var alg in groups) {
645          rowNames.Add(alg.Name);
646          var runs = alg.GetTrials(problem).ToList();
647          if (runs.Count == 0) {
648            matrix[rowCount, columns - 1] = "N/A";
649            rowCount++;
650            continue;
651          }
652          var result = default(ErtCalculationResult);
653          for (var i = 0; i < absTargets.Length; i++) {
654            result = ExpectedRuntimeHelper.CalculateErt(runs.Select(x => x.ToTuples()), absTargets[i], max);
655            matrix[rowCount, i] = result.ToString();
656          }
657          matrix[rowCount, columns - 1] = targets.Length > 0 ? result.SuccessfulRuns + "/" + result.TotalRuns : "-";
658          rowCount++;
659        }
660      }
661      matrix.RowNames = rowNames;
662      ertTableView.Content = matrix;
663      ertTableView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
664    }
665    #endregion
666
667    #region Performance analysis by (multiple) budget(s)
668    private void UpdateResultsByCost() {
669      // necessary to reset log scale -> empty chart cannot use log scaling
670      byCostDataTable.VisualProperties.XAxisLogScale = false;
671      byCostDataTable.Rows.Clear();
672
673      var table = (string)dataTableComboBox.SelectedItem;
674      if (string.IsNullOrEmpty(table)) return;
675
676      if (budgets == null) GenerateDefaultBudgets(table);
677     
678      if (groups.Count == 0) return;
679
680      var colorCount = 0;
681      var lineStyleCount = 0;
682     
683      foreach (var alg in groups) {
684        var hits = new Dictionary<string, SortedList<double, double>>();
685
686        foreach (var problem in alg.GetCases()) {
687          foreach (var run in alg.GetTrials(problem)) {
688            CalculateHitsForEachBudget(hits, run, problem, alg.Name);
689          }
690        }
691
692        foreach (var list in hits) {
693          var row = new IndexedDataRow<double>(list.Key) {
694            VisualProperties = {
695              ChartType = DataRowVisualProperties.DataRowChartType.StepLine,
696              LineWidth = 2,
697              Color = colors[colorCount],
698              LineStyle = hlLineStyles[lineStyleCount],
699              StartIndexZero = false
700            }
701          };
702
703          var total = 0.0;
704          var count = list.Value.Count;
705          foreach (var h in list.Value) {
706            total += h.Value;
707            row.Values.Add(Tuple.Create(h.Key, total / (double)count));
708          }
709
710          byCostDataTable.Rows.Add(row);
711        }
712        colorCount = (colorCount + 1) % colors.Length;
713        if (colorCount == 0) lineStyleCount = (lineStyleCount + 1) % lineStyles.Length;
714      }
715
716      byCostDataTable.VisualProperties.XAxisTitle = "Targets to Best-Known Ratio";
717      byCostDataTable.VisualProperties.XAxisLogScale = byCostDataTable.Rows.Count > 0 && budgetLogScalingCheckBox.Checked;
718    }
719
720    private void GenerateDefaultBudgets(string table) {
721      var runs = Content;
722      var min = runs.Select(x => ((IndexedDataTable<double>)x.Results[table]).Rows.First().Values.Select(y => y.Item1).Min()).Min();
723      var max = runs.Select(x => ((IndexedDataTable<double>)x.Results[table]).Rows.First().Values.Select(y => y.Item1).Max()).Max();
724      var points = 3;
725      budgets = Enumerable.Range(1, points).Select(x => min + (x / (double)points) * (max - min)).ToArray();
726      suppressBudgetsEvents = true;
727      budgetsTextBox.Text = string.Join(" ; ", budgets);
728      suppressBudgetsEvents = false;
729    }
730
731    private void CalculateHitsForEachBudget(Dictionary<string, SortedList<double, double>> hits, ConvergenceGraph cgraph, ProblemInstance problem, string groupName) {
732      var max = problem.IsMaximization();
733      var prevIndex = 0;
734      foreach (var b in budgets) {
735        var key = groupName + "-" + b;
736        var index = cgraph.BinarySearch(prevIndex, cgraph.Points - prevIndex, new ConvergenceGraphPoint(b, 0.0), new CostComparer());
737        if (index < 0) {
738          index = ~index;
739          if (index >= cgraph.Points) break; // the run wasn't long enough to use up budget b (or any subsequent larger one)
740        }
741        if (!hits.ContainsKey(key)) hits.Add(key, new SortedList<double, double>());
742        var v = cgraph[index];
743        var relTgt = CalculateRelativeDifference(max, problem.BestKnownQuality, v.Quality) + 1;
744        if (hits[key].ContainsKey(relTgt))
745          hits[key][relTgt]++;
746        else hits[key][relTgt] = 1.0;
747        prevIndex = index;
748      }
749    }
750    #endregion
751
752    private void UpdateCaption() {
753      Caption = Content != null ? Content.OptimizerName + " RLD View" : ViewAttribute.GetViewName(GetType());
754    }
755
756    private void SynchronizeTargetTextBox() {
757      if (InvokeRequired) Invoke((Action)SynchronizeTargetTextBox);
758      else {
759        suppressTargetsEvents = true;
760        try {
761          if (targetsAreRelative)
762            targetsTextBox.Text = string.Join("% ; ", targets.Select(x => x * 100)) + "%";
763          else targetsTextBox.Text = string.Join(" ; ", targets);
764        } finally { suppressTargetsEvents = false; }
765      }
766    }
767
768    private void groupComboBox_SelectedIndexChanged(object sender, EventArgs e) {
769      UpdateRuns();
770      SetEnabledStateOfControls();
771    }
772    private void problemComboBox_SelectedIndexChanged(object sender, EventArgs e) {
773      UpdateRuns();
774      SetEnabledStateOfControls();
775    }
776    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
777      if (dataTableComboBox.SelectedIndex >= 0)
778        GenerateDefaultBudgets((string)dataTableComboBox.SelectedItem);
779      UpdateBestKnownQualities();
780      UpdateRuns();
781      SetEnabledStateOfControls();
782    }
783
784    private void logScalingCheckBox_CheckedChanged(object sender, EventArgs e) {
785      UpdateResultsByTarget();
786      byCostDataTable.VisualProperties.XAxisLogScale = byCostDataTable.Rows.Count > 0 && budgetLogScalingCheckBox.Checked;
787    }
788
789    private void boundShadingCheckBox_CheckedChanged(object sender, EventArgs e) {
790      UpdateResultsByTarget();
791    }
792
793    #region Event handlers for target analysis
794    private bool suppressTargetsEvents;
795    private void targetsTextBox_Validating(object sender, CancelEventArgs e) {
796      if (suppressTargetsEvents) return;
797      var targetStrings = targetsTextBox.Text.Split(new[] { '%', ';', '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
798      var targetList = new List<decimal>();
799      foreach (var ts in targetStrings) {
800        decimal t;
801        if (!decimal.TryParse(ts, out t)) {
802          errorProvider.SetError(targetsTextBox, "Not all targets can be parsed: " + ts);
803          e.Cancel = true;
804          return;
805        }
806        if (targetsAreRelative)
807          targetList.Add(t / 100);
808        else targetList.Add(t);
809      }
810      if (targetList.Count == 0) {
811        errorProvider.SetError(targetsTextBox, "Give at least one target value!");
812        e.Cancel = true;
813        return;
814      }
815      e.Cancel = false;
816      errorProvider.SetError(targetsTextBox, null);
817      targets = targetsAreRelative ? targetList.Select(x => (double)x).OrderByDescending(x => x).ToArray() : targetList.Select(x => (double)x).ToArray();
818
819      SynchronizeTargetTextBox();
820      UpdateResultsByTarget();
821      SetEnabledStateOfControls();
822    }
823
824    private void aggregateTargetsCheckBox_CheckedChanged(object sender, EventArgs e) {
825      SuspendRepaint();
826      try {
827        UpdateResultsByTarget();
828      } finally { ResumeRepaint(true); }
829    }
830
831    private void relativeOrAbsoluteComboBox_SelectedIndexChanged(object sender, EventArgs e) {
832      if (suppressUpdates) return;
833      var pd = (ProblemInstance)problemComboBox.SelectedItem;
834      if (!double.IsNaN(pd.BestKnownQuality)) {
835        var max = pd.IsMaximization();
836        if (targetsAreRelative) targets = GetAbsoluteTargets(pd).ToArray();
837        else {
838          // Rounding to 5 digits since it's certainly appropriate for this application
839          if (pd.BestKnownQuality > 0) {
840            targets = targets.Select(x => Math.Round(max ? 1.0 - (x / pd.BestKnownQuality) : (x / pd.BestKnownQuality) - 1.0, 5)).ToArray();
841          } else if (pd.BestKnownQuality < 0) {
842            targets = targets.Select(x => Math.Round(!max ? 1.0 - (x / pd.BestKnownQuality) : (x / pd.BestKnownQuality) - 1.0, 5)).ToArray();
843          }
844        }
845      }
846      targetsAreRelative = (string)relativeOrAbsoluteComboBox.SelectedItem == "relative";
847      SynchronizeTargetTextBox();
848
849      try {
850        SuspendRepaint();
851        UpdateResultsByTarget();
852      } finally { ResumeRepaint(true); }
853    }
854
855    private void generateTargetsButton_Click(object sender, EventArgs e) {
856      if (targets == null) return;
857      decimal max = 10, min = 0, count = 10;
858      max = (decimal)targets.Max();
859      min = (decimal)targets.Min();
860      count = targets.Length - 1;
861      if (targetsAreRelative) {
862        max *= 100;
863        min *= 100;
864      }
865      using (var dialog = new DefineArithmeticProgressionDialog(false, min, max, (max - min) / count)) {
866        if (dialog.ShowDialog() == DialogResult.OK) {
867          if (dialog.Values.Any()) {
868            targets = targetsAreRelative
869              ? dialog.Values.OrderByDescending(x => x).Select(x => (double)x / 100.0).ToArray()
870              : dialog.Values.Select(x => (double)x).ToArray();
871
872            SynchronizeTargetTextBox();
873            UpdateResultsByTarget();
874            SetEnabledStateOfControls();
875          }
876        }
877      }
878    }
879
880    private void addTargetsAsResultButton_Click(object sender, EventArgs e) {
881      var table = (string)dataTableComboBox.SelectedItem;
882      if (string.IsNullOrEmpty(table)) return;
883     
884      foreach (var run in Content) {
885        if (!run.Results.ContainsKey(table)) continue;
886        var resultsTable = (IndexedDataTable<double>)run.Results[table];
887        var values = resultsTable.Rows.First().Values;
888        var pd = new ProblemInstance(run);
889        pd = problems.Single(x => x.Equals(pd));
890        if (double.IsNaN(pd.BestKnownQuality)) continue;
891
892        var max = pd.IsMaximization();
893        var absTargets = GetAbsoluteTargetsWorstToBest(pd);
894        var cgraph = new ConvergenceGraph(resultsTable, max);
895
896        var prevIndex = 0;
897        for (var i = 0; i < absTargets.Length; i++) {
898          var absTarget = absTargets[i];
899          var index = cgraph.BinarySearch(prevIndex, values.Count - prevIndex, new ConvergenceGraphPoint(0.0, absTarget), new TargetComparer(max));
900          if (index < 0) {
901            index = ~index;
902            if (index >= values.Count) break; // the target (and subsequent ones) wasn't achieved
903          }
904          var target = targetsAreRelative ? (targets[i] * 100) : absTarget;
905          run.Results[table + (targetsAreRelative ? ".RelTarget " : ".AbsTarget ") + target + (targetsAreRelative ? "%" : string.Empty)] = new DoubleValue(values[index].Item1);
906          prevIndex = index;
907        }
908      }
909    }
910
911    private void markerCheckBox_CheckedChanged(object sender, EventArgs e) {
912      SuspendRepaint();
913      try {
914        UpdateResultsByTarget();
915      } finally { ResumeRepaint(true); }
916    }
917
918    private void showLabelsCheckBox_CheckedChanged(object sender, EventArgs e) {
919      showLabelsInTargetChart = showLabelsCheckBox.Checked;
920      SuspendRepaint();
921      try {
922        UpdateResultsByTarget();
923      } finally { ResumeRepaint(true); }
924    }
925    #endregion
926
927    #region Event handlers for cost analysis
928    private bool suppressBudgetsEvents;
929    private void budgetsTextBox_Validating(object sender, CancelEventArgs e) {
930      if (suppressBudgetsEvents) return;
931      var budgetStrings = budgetsTextBox.Text.Split(new[] { ';', '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
932      var budgetList = new List<double>();
933      foreach (var ts in budgetStrings) {
934        double b;
935        if (!double.TryParse(ts, out b)) {
936          errorProvider.SetError(budgetsTextBox, "Not all budgets can be parsed: " + ts);
937          e.Cancel = true;
938          return;
939        }
940        budgetList.Add(b);
941      }
942      if (budgetList.Count == 0) {
943        errorProvider.SetError(budgetsTextBox, "Give at least one budget value!");
944        e.Cancel = true;
945        return;
946      }
947      e.Cancel = false;
948      errorProvider.SetError(budgetsTextBox, null);
949      budgets = budgetList.OrderBy(x => x).ToArray();
950      try {
951        suppressBudgetsEvents = true;
952        budgetsTextBox.Text = string.Join(" ; ", budgets);
953      } finally { suppressBudgetsEvents = false; }
954      UpdateResultsByCost();
955      SetEnabledStateOfControls();
956    }
957
958    private void generateBudgetsButton_Click(object sender, EventArgs e) {
959      decimal max = 1, min = 0, count = 10;
960      if (budgets != null) {
961        max = (decimal)budgets.Max();
962        min = (decimal)budgets.Min();
963        count = budgets.Length;
964      } else if (Content.Count > 0 && dataTableComboBox.SelectedIndex >= 0) {
965        var table = (string)dataTableComboBox.SelectedItem;
966        min = (decimal)Content.Where(x => x.Results.ContainsKey(table)).Select(x => ((IndexedDataTable<double>)x.Results[table]).Rows.First().Values.Min(y => y.Item1)).Min();
967        max = (decimal)Content.Where(x => x.Results.ContainsKey(table)).Select(x => ((IndexedDataTable<double>)x.Results[table]).Rows.First().Values.Max(y => y.Item1)).Max();
968        count = 3;
969      }
970      using (var dialog = new DefineArithmeticProgressionDialog(false, min, max, (max - min) / count)) {
971        if (dialog.ShowDialog() == DialogResult.OK) {
972          if (dialog.Values.Any()) {
973            budgets = dialog.Values.OrderBy(x => x).Select(x => (double)x).ToArray();
974
975            try {
976              suppressBudgetsEvents = true;
977              budgetsTextBox.Text = string.Join(" ; ", budgets);
978            } finally { suppressBudgetsEvents = false; }
979
980            UpdateResultsByCost();
981            SetEnabledStateOfControls();
982          }
983        }
984      }
985    }
986
987    private void addBudgetsAsResultButton_Click(object sender, EventArgs e) {
988      var table = (string)dataTableComboBox.SelectedItem;
989
990      foreach (var run in Content) {
991        if (!run.Results.ContainsKey(table)) continue;
992        var resultsTable = (IndexedDataTable<double>)run.Results[table];
993        var values = resultsTable.Rows.First().Values;
994        var pd = new ProblemInstance(run);
995        pd = problems.Single(x => x.Equals(pd));
996        var max = pd.IsMaximization();
997        var cgraph = new ConvergenceGraph(resultsTable, max);
998        var prevIndex = 0;
999        foreach (var b in budgets) {
1000          var index = cgraph.BinarySearch(prevIndex, values.Count - prevIndex, new ConvergenceGraphPoint(b, 0.0), new CostComparer());
1001          if (index < 0) {
1002            index = ~index;
1003            if (index >= values.Count) break; // the run wasn't long enough to use up budget b (or any subsequent larger one)
1004          }
1005          var v = values[index];
1006          var tgt = targetsAreRelative ? CalculateRelativeDifference(pd.IsMaximization(), pd.BestKnownQuality, v.Item2) : v.Item2;
1007          run.Results[table + (targetsAreRelative ? ".CostForRelTarget " : ".CostForAbsTarget ") + b] = new DoubleValue(tgt);
1008          prevIndex = index;
1009        }
1010      }
1011    }
1012    #endregion
1013
1014    #region Helpers
1015    private double CalculateRelativeDifference(bool maximization, double bestKnown, double fit) {
1016      if (bestKnown == 0) {
1017        // no relative difference with respect to bestKnown possible
1018        return maximization ? -fit : fit;
1019      }
1020      var absDiff = (fit - bestKnown);
1021      var relDiff = absDiff / bestKnown;
1022      if (maximization) {
1023        return bestKnown > 0 ? -relDiff : relDiff;
1024      } else {
1025        return bestKnown > 0 ? relDiff : -relDiff;
1026      }
1027    }
1028
1029    private Dictionary<ProblemInstance, double> CalculateBestTargetPerProblemInstance(string table) {
1030      if (table == null) table = string.Empty;
1031      var dict = new Dictionary<ProblemInstance, double>();
1032      foreach (var k in from r in Content where r.Visible let pd = new ProblemInstance(r) group r by pd into g select new { Problem = g.Key, Runs = g.ToList() }) {
1033        var pd = new ProblemInstance(k.Runs[0]);
1034        var values = GetQualityValues(k.Runs, table).ToList();
1035        var target = double.NaN;
1036        if (values.Count > 0) {
1037          target = pd.IsMaximization() ? values.Max() : values.Min();
1038        }
1039        dict[pd] = target;
1040      }
1041      return dict;
1042    }
1043
1044    private IEnumerable<double> GetQualityValues(List<IRun> runs, string table) {
1045      foreach (var r in runs) {
1046        IItem item;
1047        if (r.Parameters.TryGetValue("BestKnownQuality", out item)) {
1048          var dval = item as DoubleValue;
1049          if (dval != null && !double.IsNaN(dval.Value))
1050            yield return dval.Value;
1051        }
1052        if (r.Results.TryGetValue(table, out item)) {
1053          var dt = item as IndexedDataTable<double>;
1054          if (dt != null) {
1055            var last = dt.Rows.First().Values.Last().Item2;
1056            if (!double.IsNaN(last))
1057              yield return last;
1058          }
1059        }
1060      }
1061    }
1062
1063    private void UpdateRuns() {
1064      if (InvokeRequired) {
1065        Invoke((Action)UpdateRuns);
1066        return;
1067      }
1068      SuspendRepaint();
1069      try {
1070        GroupRuns();
1071        UpdateResultsByTarget();
1072        UpdateResultsByCost();
1073      } finally { ResumeRepaint(true); }
1074    }
1075
1076    private void UpdateBestKnownQualities() {
1077      var table = (string)dataTableComboBox.SelectedItem;
1078      if (string.IsNullOrEmpty(table)) return;
1079
1080      var targetsPerProblem = CalculateBestTargetPerProblemInstance(table);
1081      foreach (var pd in problems) {
1082        double bkq;
1083        if (targetsPerProblem.TryGetValue(pd, out bkq))
1084          pd.BestKnownQuality = bkq;
1085        else pd.BestKnownQuality = double.NaN;
1086      }
1087    }
1088    #endregion
1089
1090    private void ConfigureSeries(Series series) {
1091      series.SmartLabelStyle.Enabled = showLabelsInTargetChart;
1092      series.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.No;
1093      series.SmartLabelStyle.CalloutLineAnchorCapStyle = LineAnchorCapStyle.None;
1094      series.SmartLabelStyle.CalloutLineColor = series.Color;
1095      series.SmartLabelStyle.CalloutLineWidth = 2;
1096      series.SmartLabelStyle.CalloutStyle = LabelCalloutStyle.Underlined;
1097      series.SmartLabelStyle.IsOverlappedHidden = false;
1098      series.SmartLabelStyle.MaxMovingDistance = 200;
1099      series.ToolTip = series.LegendText + " X = #VALX, Y = #VALY";
1100    }
1101
1102    private void chart_MouseDown(object sender, MouseEventArgs e) {
1103      HitTestResult result = targetChart.HitTest(e.X, e.Y);
1104      if (result.ChartElementType == ChartElementType.LegendItem) {
1105        ToggleTargetChartSeriesVisible(result.Series);
1106      }
1107    }
1108    private void chart_MouseMove(object sender, MouseEventArgs e) {
1109      HitTestResult result = targetChart.HitTest(e.X, e.Y);
1110      if (result.ChartElementType == ChartElementType.LegendItem)
1111        this.Cursor = Cursors.Hand;
1112      else
1113        this.Cursor = Cursors.Default;
1114    }
1115    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
1116      foreach (LegendItem legendItem in e.LegendItems) {
1117        var series = targetChart.Series[legendItem.SeriesName];
1118        if (series != null) {
1119          bool seriesIsInvisible = invisibleTargetSeries.Any(x => x.Name == series.Name);
1120          foreach (LegendCell cell in legendItem.Cells) {
1121            cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
1122          }
1123        }
1124      }
1125    }
1126
1127    private void ToggleTargetChartSeriesVisible(Series series) {
1128      var indexList = invisibleTargetSeries.FindIndex(x => x.Name == series.Name);
1129      var indexChart = targetChart.Series.IndexOf(series);
1130      if (targetChart.Series.Count == 1) targetChart.ChartAreas[0].AxisX.IsLogarithmic = false;
1131      targetChart.Series.RemoveAt(indexChart);
1132      var s = indexList >= 0 ? invisibleTargetSeries[indexList] : new Series(series.Name) {
1133        Color = series.Color,
1134        ChartType = series.ChartType,
1135        BorderWidth = series.BorderWidth,
1136        BorderDashStyle = series.BorderDashStyle
1137      };
1138      if (indexList < 0) {
1139        // hide
1140        invisibleTargetSeries.Add(series);
1141        var shadeSeries = targetChart.Series.FirstOrDefault(x => x.Name == series.Name + "-range");
1142        if (shadeSeries != null) {
1143          if (targetChart.Series.Count == 1) targetChart.ChartAreas[0].AxisX.IsLogarithmic = false;
1144          targetChart.Series.Remove(shadeSeries);
1145          invisibleTargetSeries.Add(shadeSeries);
1146          indexChart--;
1147        }
1148      } else {
1149        // show
1150        invisibleTargetSeries.RemoveAt(indexList);
1151        var shadeSeries = invisibleTargetSeries.FirstOrDefault(x => x.Name == series.Name + "-range");
1152        if (shadeSeries != null) {
1153          invisibleTargetSeries.Remove(shadeSeries);
1154          InsertOrAddSeries(indexChart, shadeSeries);
1155          indexChart++;
1156        }
1157      }
1158      InsertOrAddSeries(indexChart, s);
1159      targetChart.ChartAreas[0].AxisX.IsLogarithmic = CanDisplayLogarithmic();
1160    }
1161
1162    private bool CanDisplayLogarithmic() {
1163      return targetLogScalingCheckBox.Checked
1164        && targetChart.Series.Count > 0 // must have a series
1165        && targetChart.Series.Any(x => x.Points.Count > 0) // at least one series must have points
1166        && targetChart.Series.All(s => s.Points.All(p => p.XValue > 0)); // all points must be positive
1167    }
1168
1169    private void InsertOrAddSeries(int index, Series s) {
1170      if (targetChart.Series.Count <= index)
1171        targetChart.Series.Add(s);
1172      else targetChart.Series.Insert(index, s);
1173    }
1174
1175    private class AlgorithmInstance : INotifyPropertyChanged {
1176      private string name;
1177      public string Name {
1178        get { return name; }
1179        set {
1180          if (name == value) return;
1181          name = value;
1182          OnPropertyChanged("Name");
1183        }
1184      }
1185
1186      private Dictionary<ProblemInstance, List<ConvergenceGraph>> performanceData;
1187
1188      public int GetNumberOfCases() {
1189        return performanceData.Count;
1190      }
1191
1192      public IEnumerable<ProblemInstance> GetCases() {
1193        return performanceData.Keys;
1194      }
1195
1196      public int GetNumberOfTrials(ProblemInstance p) {
1197        if (p == ProblemInstance.MatchAll) return performanceData.Select(x => x.Value.Count).Sum();
1198        List<ConvergenceGraph> trials;
1199        if (performanceData.TryGetValue(p, out trials))
1200          return trials.Count;
1201
1202        return 0;
1203      }
1204
1205      public IEnumerable<ConvergenceGraph> GetTrials(ProblemInstance p) {
1206        if (p == ProblemInstance.MatchAll) return performanceData.SelectMany(x => x.Value);
1207        List<ConvergenceGraph> trials;
1208        if (performanceData.TryGetValue(p, out trials))
1209          return trials;
1210
1211        return Enumerable.Empty<ConvergenceGraph>();
1212      }
1213
1214      public AlgorithmInstance(string name, IEnumerable<IGrouping<ProblemInstance, IndexedDataTable<double>>> trials) {
1215        this.name = name;
1216
1217        performanceData = new Dictionary<ProblemInstance, List<ConvergenceGraph>>();
1218        foreach (var t in trials) {
1219          if (double.IsNaN(t.Key.BestKnownQuality)) continue;
1220          var max = t.Key.IsMaximization();
1221          performanceData[t.Key] = t.Select(c => new ConvergenceGraph(c, max)).ToList();
1222        }
1223      }
1224
1225      public override bool Equals(object obj) {
1226        var other = obj as AlgorithmInstance;
1227        if (other == null) return false;
1228        return name == other.name;
1229      }
1230
1231      public override int GetHashCode() {
1232        return name.GetHashCode();
1233      }
1234
1235      public event PropertyChangedEventHandler PropertyChanged;
1236      protected virtual void OnPropertyChanged(string propertyName = null) {
1237        var handler = PropertyChanged;
1238        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
1239      }
1240    }
1241
1242    private class ProblemInstance : INotifyPropertyChanged {
1243      private readonly bool matchAll;
1244      public static readonly ProblemInstance MatchAll = new ProblemInstance() {
1245        ProblemName = "All with Best-Known"
1246      };
1247
1248      private ProblemInstance() {
1249        ProblemType = string.Empty;
1250        ProblemName = string.Empty;
1251        Evaluator = string.Empty;
1252        Maximization = string.Empty;
1253        DisplayProblemType = false;
1254        DisplayProblemName = false;
1255        DisplayEvaluator = false;
1256        DisplayMaximization = false;
1257        matchAll = true;
1258        BestKnownQuality = double.NaN;
1259      }
1260
1261      public ProblemInstance(IRun run) {
1262        ProblemType = GetStringValueOrEmpty(run, "Problem Type");
1263        ProblemName = GetStringValueOrEmpty(run, "Problem Name");
1264        Evaluator = GetStringValueOrEmpty(run, "Evaluator");
1265        Maximization = GetMaximizationValueOrEmpty(run, "Maximization");
1266        DisplayProblemType = !string.IsNullOrEmpty(ProblemType);
1267        DisplayProblemName = !string.IsNullOrEmpty(ProblemName);
1268        DisplayEvaluator = !string.IsNullOrEmpty(Evaluator);
1269        DisplayMaximization = !string.IsNullOrEmpty(Maximization);
1270        matchAll = false;
1271        BestKnownQuality = GetDoubleValueOrNaN(run, "BestKnownQuality");
1272      }
1273
1274      private bool displayProblemType;
1275      public bool DisplayProblemType {
1276        get { return displayProblemType; }
1277        set {
1278          if (displayProblemType == value) return;
1279          displayProblemType = value;
1280          OnPropertyChanged("DisplayProblemType");
1281        }
1282      }
1283      private string problemType;
1284      public string ProblemType {
1285        get { return problemType; }
1286        set {
1287          if (problemType == value) return;
1288          problemType = value;
1289          OnPropertyChanged("ProblemType");
1290        }
1291      }
1292      private bool displayProblemName;
1293      public bool DisplayProblemName {
1294        get { return displayProblemName; }
1295        set {
1296          if (displayProblemName == value) return;
1297          displayProblemName = value;
1298          OnPropertyChanged("DisplayProblemName");
1299        }
1300      }
1301      private string problemName;
1302      public string ProblemName {
1303        get { return problemName; }
1304        set {
1305          if (problemName == value) return;
1306          problemName = value;
1307          OnPropertyChanged("ProblemName");
1308        }
1309      }
1310      private bool displayEvaluator;
1311      public bool DisplayEvaluator {
1312        get { return displayEvaluator; }
1313        set {
1314          if (displayEvaluator == value) return;
1315          displayEvaluator = value;
1316          OnPropertyChanged("DisplayEvaluator");
1317        }
1318      }
1319      private string evaluator;
1320      public string Evaluator {
1321        get { return evaluator; }
1322        set {
1323          if (evaluator == value) return;
1324          evaluator = value;
1325          OnPropertyChanged("Evaluator");
1326        }
1327      }
1328      private bool displayMaximization;
1329      public bool DisplayMaximization {
1330        get { return displayMaximization; }
1331        set {
1332          if (displayMaximization == value) return;
1333          displayMaximization = value;
1334          OnPropertyChanged("DisplayMaximization");
1335        }
1336      }
1337      private string maximization;
1338      public string Maximization {
1339        get { return maximization; }
1340        set {
1341          if (maximization == value) return;
1342          maximization = value;
1343          OnPropertyChanged("Maximization");
1344        }
1345      }
1346      private double bestKnownQuality;
1347      public double BestKnownQuality {
1348        get { return bestKnownQuality; }
1349        set {
1350          if (bestKnownQuality == value) return;
1351          bestKnownQuality = value;
1352          OnPropertyChanged("BestKnownQuality");
1353        }
1354      }
1355
1356      public bool IsMaximization() {
1357        return Maximization == "MAX";
1358      }
1359
1360      public bool Match(IRun run) {
1361        return matchAll ||
1362               GetStringValueOrEmpty(run, "Problem Type") == ProblemType
1363               && GetStringValueOrEmpty(run, "Problem Name") == ProblemName
1364               && GetStringValueOrEmpty(run, "Evaluator") == Evaluator
1365               && GetMaximizationValueOrEmpty(run, "Maximization") == Maximization;
1366      }
1367
1368      private double GetDoubleValueOrNaN(IRun run, string key) {
1369        IItem param;
1370        if (run.Parameters.TryGetValue(key, out param)) {
1371          var dv = param as DoubleValue;
1372          return dv != null ? dv.Value : double.NaN;
1373        }
1374        return double.NaN;
1375      }
1376
1377      private string GetStringValueOrEmpty(IRun run, string key) {
1378        IItem param;
1379        if (run.Parameters.TryGetValue(key, out param)) {
1380          var sv = param as StringValue;
1381          return sv != null ? sv.Value : string.Empty;
1382        }
1383        return string.Empty;
1384      }
1385
1386      private string GetMaximizationValueOrEmpty(IRun run, string key) {
1387        IItem param;
1388        if (run.Parameters.TryGetValue(key, out param)) {
1389          var bv = param as BoolValue;
1390          return bv != null ? (bv.Value ? "MAX" : "MIN") : string.Empty;
1391        }
1392        return string.Empty;
1393      }
1394
1395      public override bool Equals(object obj) {
1396        var other = obj as ProblemInstance;
1397        if (other == null) return false;
1398        return ProblemType == other.ProblemType
1399               && ProblemName == other.ProblemName
1400               && Evaluator == other.Evaluator
1401               && Maximization == other.Maximization;
1402      }
1403
1404      public override int GetHashCode() {
1405        return ProblemType.GetHashCode() ^ ProblemName.GetHashCode() ^ Evaluator.GetHashCode() ^ Maximization.GetHashCode();
1406      }
1407
1408      public override string ToString() {
1409        return string.Join("  --  ", new[] {
1410          (DisplayProblemType ? ProblemType : string.Empty),
1411          (DisplayProblemName ? ProblemName : string.Empty),
1412          (DisplayEvaluator ? Evaluator : string.Empty),
1413          (DisplayMaximization ? Maximization : string.Empty),
1414          !double.IsNaN(BestKnownQuality) ? BestKnownQuality.ToString(CultureInfo.CurrentCulture.NumberFormat) : string.Empty }.Where(x => !string.IsNullOrEmpty(x)));
1415      }
1416
1417      public event PropertyChangedEventHandler PropertyChanged;
1418      protected virtual void OnPropertyChanged(string propertyName = null) {
1419        var handler = PropertyChanged;
1420        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
1421      }
1422    }
1423
1424    private class CostComparer : Comparer<ConvergenceGraphPoint> {
1425      public override int Compare(ConvergenceGraphPoint x, ConvergenceGraphPoint y) {
1426        return x.Runlength.CompareTo(y.Runlength);
1427      }
1428    }
1429
1430    private class TargetComparer : Comparer<ConvergenceGraphPoint> {
1431      public bool Maximization { get; private set; }
1432      public TargetComparer(bool maximization) {
1433        Maximization = maximization;
1434      }
1435
1436      public override int Compare(ConvergenceGraphPoint x, ConvergenceGraphPoint y) {
1437        return Maximization ? x.Quality.CompareTo(y.Quality) : y.Quality.CompareTo(x.Quality);
1438      }
1439    }
1440
1441    private class ConvergenceGraph : IEnumerable<ConvergenceGraphPoint> {
1442      private List<ConvergenceGraphPoint> data;
1443      private bool maximization;
1444      private string xAxisLabel;
1445
1446      public string XAxisName { get { return xAxisLabel; } }
1447
1448      public int Points {
1449        get { return data.Count; }
1450      }
1451
1452      public double TotalRunlength {
1453        get { return data.Last().Runlength; }
1454      }
1455
1456      public double BestQuality {
1457        get { return data.Last().Quality; }
1458      }
1459
1460      public double QualityAt(double runlength) {
1461        var point = data.SkipWhile(x => x.Runlength < runlength).FirstOrDefault();
1462        if (point == null) return double.NaN;
1463        return point.Quality;
1464      }
1465
1466      public double RunlengthFor(double quality) {
1467        var point = (maximization ? data.SkipWhile(x => x.Quality < quality) : data.SkipWhile(x => x.Quality > quality)).FirstOrDefault();
1468        if (point == null) return double.NaN;
1469        return point.Runlength;
1470      }
1471
1472      public ConvergenceGraphPoint this[int point] {
1473        get { return data[point]; }
1474      }
1475
1476      public ConvergenceGraph(IndexedDataTable<double> table, bool maximization) {
1477        data = table.Rows.First().Values.Select(x => new ConvergenceGraphPoint() { Runlength = x.Item1, Quality = x.Item2 }).ToList();
1478        xAxisLabel = table.VisualProperties.XAxisTitle;
1479        this.maximization = maximization;
1480      }
1481
1482      public IEnumerator<ConvergenceGraphPoint> GetEnumerator() {
1483        return data.GetEnumerator();
1484      }
1485
1486      IEnumerator IEnumerable.GetEnumerator() {
1487        return data.GetEnumerator();
1488      }
1489
1490      public int BinarySearch(ConvergenceGraphPoint item, IComparer<ConvergenceGraphPoint> comparer) {
1491        return data.BinarySearch(item, comparer);
1492      }
1493
1494      public int BinarySearch(int index, int count, ConvergenceGraphPoint point, IComparer<ConvergenceGraphPoint> comparer) {
1495        return data.BinarySearch(index, count, point, comparer);
1496      }
1497
1498      public IEnumerable<Tuple<double, double>> ToTuples() {
1499        return data.Select(x => Tuple.Create(x.Runlength, x.Quality));
1500      }
1501    }
1502
1503    private class ConvergenceGraphPoint {
1504      public double Runlength { get; set; }
1505      public double Quality { get; set; }
1506
1507      public ConvergenceGraphPoint() {
1508
1509      }
1510      public ConvergenceGraphPoint(double runlength, double quality) {
1511        Runlength = runlength;
1512        Quality = quality;
1513      }
1514    }
1515  }
1516}
Note: See TracBrowser for help on using the repository browser.