Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionSimpleRLDView.cs @ 17975

Last change on this file since 17975 was 16733, checked in by abeham, 6 years ago

#1614: added simpler variant of RLD view

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