Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.cs @ 14825

Last change on this file since 14825 was 14825, checked in by gkronber, 7 years ago

#2650: merged r14769:14820 from trunk to branch to prepare for branch reintegration

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