Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.cs @ 17690

Last change on this file since 17690 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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