Changeset 13583 for branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.cs
- Timestamp:
- 02/02/16 14:10:53 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.cs
r13475 r13583 20 20 #endregion 21 21 22 using HeuristicLab.Analysis; 23 using HeuristicLab.Collections; 24 using HeuristicLab.Core.Views; 25 using HeuristicLab.Data; 26 using HeuristicLab.MainForm; 27 using HeuristicLab.MainForm.WindowsForms; 22 28 using System; 23 29 using System.Collections.Generic; … … 27 33 using System.Linq; 28 34 using System.Windows.Forms; 29 using HeuristicLab.Analysis;30 using HeuristicLab.Collections;31 using HeuristicLab.Core.Views;32 using HeuristicLab.Data;33 using HeuristicLab.MainForm;34 using HeuristicLab.MainForm.WindowsForms;35 35 36 36 namespace HeuristicLab.Optimization.Views { … … 86 86 VisualProperties = { 87 87 YAxisTitle = "Proportion of reached targets", 88 YAxisMinimumFixedValue = 0, YAxisMinimumAuto = false, 89 YAxisMaximumFixedValue = 1, YAxisMaximumAuto = false 88 YAxisMinimumFixedValue = 0, 89 YAxisMinimumAuto = false, 90 YAxisMaximumFixedValue = 1, 91 YAxisMaximumAuto = false 90 92 } 91 93 }; … … 94 96 VisualProperties = { 95 97 YAxisTitle = "Proportion of unused budgets", 96 YAxisMinimumFixedValue = 0, YAxisMinimumAuto = false, 97 YAxisMaximumFixedValue = 1, YAxisMaximumAuto = false 98 YAxisMinimumFixedValue = 0, 99 YAxisMinimumAuto = false, 100 YAxisMaximumFixedValue = 1, 101 YAxisMaximumAuto = false 98 102 } 99 103 }; … … 223 227 var problemNamesDifferent = problems.Select(x => x.ProblemName).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 224 228 var evaluatorDifferent = problems.Select(x => x.Evaluator).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 225 var allEqual = !problemTypesDifferent && !problemNamesDifferent && !evaluatorDifferent; 229 var maximizationDifferent = problems.Select(x => x.Maximization).Distinct().Count() > 1; 230 var allEqual = !problemTypesDifferent && !problemNamesDifferent && !evaluatorDifferent && !maximizationDifferent; 226 231 227 232 var selectedProblemItem = (ProblemDescription)problemComboBox.SelectedItem; … … 234 239 prob.DisplayProblemName = problemNamesDifferent || allEqual; 235 240 prob.DisplayEvaluator = evaluatorDifferent; 241 prob.DisplayMaximization = maximizationDifferent; 236 242 problemComboBox.Items.Add(prob); 237 243 if (prob.Equals(selectedProblemItem)) problemComboBox.SelectedItem = prob; … … 266 272 } 267 273 268 private Dictionary<string, Dictionary< string, Tuple<double, List<IRun>>>> GroupRuns() {269 var groupedRuns = new Dictionary<string, Dictionary< string, Tuple<double, List<IRun>>>>();274 private Dictionary<string, Dictionary<ProblemDescription, Tuple<double, List<IRun>>>> GroupRuns() { 275 var groupedRuns = new Dictionary<string, Dictionary<ProblemDescription, Tuple<double, List<IRun>>>>(); 270 276 271 277 var table = (string)dataTableComboBox.SelectedItem; … … 278 284 if (selectedProblem == null) return groupedRuns; 279 285 280 var maximization = IsMaximization(); 281 282 var targetsPerProblem = CalculateBestTargetPerProblemInstance(table, maximization); 286 var targetsPerProblem = CalculateBestTargetPerProblemInstance(table); 283 287 284 288 foreach (var x in (from r in Content … … 289 293 group r by selectedGroup == AllRuns ? AllRuns : r.Parameters[selectedGroup].ToString() into g 290 294 select Tuple.Create(g.Key, g.ToList()))) { 291 var pDict = new Dictionary< string, Tuple<double, List<IRun>>>();295 var pDict = new Dictionary<ProblemDescription, Tuple<double, List<IRun>>>(); 292 296 foreach (var y in (from r in x.Item2 293 let pd = new ProblemDescription(r) .ToString()297 let pd = new ProblemDescription(r) 294 298 group r by pd into g 295 299 select Tuple.Create(g.Key, g.ToList()))) { … … 319 323 var colorCount = 0; 320 324 var lineStyleCount = 0; 321 var maximization = IsMaximization();322 325 323 326 foreach (var group in groupedRuns) { … … 331 334 332 335 if (eachOrAllTargetCheckBox.Checked) { 333 CalculateHitsForEachTarget(hits, resultsTable.Rows.First(), maximization, group.Key, group.Value.Count, problem.Value.Item1, problem.Value.Item2.Count);336 CalculateHitsForEachTarget(hits, resultsTable.Rows.First(), problem.Key, group.Key, group.Value.Count, problem.Value.Item1, problem.Value.Item2.Count); 334 337 } else { 335 maxLength = CalculateHitsForAllTargets(hits, resultsTable.Rows.First(), maximization, group.Key, group.Value.Count, problem.Value.Item1, problem.Value.Item2.Count);338 maxLength = CalculateHitsForAllTargets(hits, resultsTable.Rows.First(), problem.Key, group.Key, group.Value.Count, problem.Value.Item1, problem.Value.Item2.Count); 336 339 } 337 340 } … … 379 382 } 380 383 381 private void CalculateHitsForEachTarget(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, bool maximization, string group, int groupCount, double bestTarget, int problemCount) {382 foreach (var l in targets.Select(x => ( maximization? (1 - x) : (1 + x)) * bestTarget)) {384 private void CalculateHitsForEachTarget(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, ProblemDescription problem, string group, int groupCount, double bestTarget, int problemCount) { 385 foreach (var l in targets.Select(x => (problem.IsMaximization() ? (1 - x) : (1 + x)) * bestTarget)) { 383 386 var key = group + "-" + l; 384 387 if (!hits.ContainsKey(key)) hits.Add(key, new SortedList<double, double>()); 385 388 foreach (var v in row.Values) { 386 if ( maximization && v.Item2 >= l || !maximization&& v.Item2 <= l) {389 if (problem.IsMaximization() && v.Item2 >= l || !problem.IsMaximization() && v.Item2 <= l) { 387 390 if (hits[key].ContainsKey(v.Item1)) 388 391 hits[key][v.Item1] += 1.0 / (groupCount * problemCount); … … 394 397 } 395 398 396 private double CalculateHitsForAllTargets(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, bool maximization, string group, int groupCount, double bestTarget, int problemCount) {399 private double CalculateHitsForAllTargets(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, ProblemDescription problem, string group, int groupCount, double bestTarget, int problemCount) { 397 400 var values = row.Values; 398 401 if (!hits.ContainsKey(group)) hits.Add(group, new SortedList<double, double>()); … … 401 404 var j = 0; 402 405 while (i < targets.Length && j < values.Count) { 403 var target = ( maximization? (1 - targets[i]) : (1 + targets[i])) * bestTarget;406 var target = (problem.IsMaximization() ? (1 - targets[i]) : (1 + targets[i])) * bestTarget; 404 407 var current = values[j]; 405 if ( maximization&& current.Item2 >= target406 || ! maximization&& current.Item2 <= target) {408 if (problem.IsMaximization() && current.Item2 >= target 409 || !problem.IsMaximization() && current.Item2 <= target) { 407 410 if (!hits[group].ContainsKey(current.Item1)) hits[group][current.Item1] = 0; 408 411 hits[group][current.Item1] += 1.0 / (groupCount * problemCount * targets.Length); … … 416 419 } 417 420 418 private void UpdateErtTables(Dictionary<string, Dictionary< string, Tuple<double, List<IRun>>>> groupedRuns) {421 private void UpdateErtTables(Dictionary<string, Dictionary<ProblemDescription, Tuple<double, List<IRun>>>> groupedRuns) { 419 422 ertTableView.Content = null; 420 423 var columns = 1 + targets.Length + 1; 421 424 var matrix = new string[groupedRuns.Count * groupedRuns.Max(x => x.Value.Count) + groupedRuns.Max(x => x.Value.Count), columns]; 422 425 var rowCount = 0; 423 var maximization = IsMaximization();424 426 425 427 var tableName = (string)dataTableComboBox.SelectedItem; 426 428 if (string.IsNullOrEmpty(tableName)) return; 427 429 428 var targetsPerProblem = CalculateBestTargetPerProblemInstance(tableName , maximization);430 var targetsPerProblem = CalculateBestTargetPerProblemInstance(tableName); 429 431 430 432 var colNames = new string[columns]; … … 436 438 437 439 foreach (var problem in problems) { 438 matrix[rowCount, 0] = problem ;440 matrix[rowCount, 0] = problem.ToString(); 439 441 for (var i = 0; i < targets.Length; i++) { 440 matrix[rowCount, i + 1] = (targetsPerProblem[problem] * ( maximization? (1 - targets[i]) : (1 + targets[i]))).ToString(CultureInfo.CurrentCulture.NumberFormat);442 matrix[rowCount, i + 1] = (targetsPerProblem[problem] * (problem.IsMaximization() ? (1 - targets[i]) : (1 + targets[i]))).ToString(CultureInfo.CurrentCulture.NumberFormat); 441 443 } 442 444 matrix[rowCount, columns - 1] = "#succ"; … … 453 455 ErtCalculationResult result = default(ErtCalculationResult); 454 456 for (var i = 0; i < targets.Length; i++) { 455 result = ExpectedRuntimeHelper.CalculateErt(runs, tableName, ( maximization ? (1 - targets[i]) : (1 + targets[i])) * group.Value[problem].Item1, maximization);457 result = ExpectedRuntimeHelper.CalculateErt(runs, tableName, (problem.IsMaximization() ? (1 - targets[i]) : (1 + targets[i])) * group.Value[problem].Item1, problem.IsMaximization()); 456 458 matrix[rowCount, i + 1] = result.ToString(); 457 459 } … … 482 484 var lineStyleCount = 0; 483 485 484 var maximization = IsMaximization(); 485 var targetsPerProblem = CalculateBestTargetPerProblemInstance((string)dataTableComboBox.SelectedItem, maximization); 486 var targetsPerProblem = CalculateBestTargetPerProblemInstance((string)dataTableComboBox.SelectedItem); 486 487 487 488 foreach (var group in groupedRuns) { … … 493 494 494 495 if (eachOrAllBudgetsCheckBox.Checked) { 495 CalculateHitsForEachBudget(hits, resultsTable.Rows.First(), maximization, group.Value.Count, group.Key, problem.Value.Item2.Count, targetsPerProblem[problem.Key]);496 CalculateHitsForEachBudget(hits, resultsTable.Rows.First(), group.Value.Count, problem.Key, group.Key, problem.Value.Item2.Count, targetsPerProblem[problem.Key]); 496 497 } else { 497 CalculateHitsForAllBudgets(hits, resultsTable.Rows.First(), maximization, group.Value.Count, group.Key, problem.Value.Item2.Count, targetsPerProblem[problem.Key]);498 CalculateHitsForAllBudgets(hits, resultsTable.Rows.First(), group.Value.Count, problem.Key, group.Key, problem.Value.Item2.Count, targetsPerProblem[problem.Key]); 498 499 } 499 500 } … … 547 548 } 548 549 549 private void CalculateHitsForEachBudget(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, bool maximization, int groupCount, string groupName, int problemCount, double bestTarget) {550 private void CalculateHitsForEachBudget(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, int groupCount, ProblemDescription problem, string groupName, int problemCount, double bestTarget) { 550 551 foreach (var b in budgets) { 551 552 var key = groupName + "-" + b; … … 557 558 if (prev == null && v.Item1 != b) break; 558 559 var tgt = ((prev == null || v.Item1 == b) ? v.Item2 : prev.Item2); 559 tgt = maximization? bestTarget / tgt : tgt / bestTarget;560 tgt = problem.IsMaximization() ? bestTarget / tgt : tgt / bestTarget; 560 561 if (hits[key].ContainsKey(tgt)) 561 562 hits[key][tgt] += 1.0 / (groupCount * problemCount); … … 569 570 } 570 571 571 private void CalculateHitsForAllBudgets(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, bool maximization, int groupCount, string groupName, int problemCount, double bestTarget) {572 private void CalculateHitsForAllBudgets(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, int groupCount, ProblemDescription problem, string groupName, int problemCount, double bestTarget) { 572 573 var values = row.Values; 573 574 if (!hits.ContainsKey(groupName)) hits.Add(groupName, new SortedList<double, double>()); … … 581 582 if (prev != null || current.Item1 == budgets[i]) { 582 583 var tgt = (prev == null || current.Item1 == budgets[i]) ? current.Item2 : prev.Item2; 583 tgt = maximization? bestTarget / tgt : tgt / bestTarget;584 tgt = problem.IsMaximization() ? bestTarget / tgt : tgt / bestTarget; 584 585 if (!hits[groupName].ContainsKey(tgt)) hits[groupName][tgt] = 0; 585 586 hits[groupName][tgt] += 1.0 / (groupCount * problemCount * budgets.Length); … … 592 593 } 593 594 var lastTgt = values.Last().Item2; 594 lastTgt = maximization? bestTarget / lastTgt : lastTgt / bestTarget;595 lastTgt = problem.IsMaximization() ? bestTarget / lastTgt : lastTgt / bestTarget; 595 596 if (i < budgets.Length && !hits[groupName].ContainsKey(lastTgt)) hits[groupName][lastTgt] = 0; 596 597 while (i < budgets.Length) { … … 665 666 666 667 private void generateTargetsButton_Click(object sender, EventArgs e) { 667 var maximization = IsMaximization();668 668 decimal max = 1, min = 0, count = 10; 669 669 if (targets != null) { … … 680 680 if (dialog.ShowDialog() == DialogResult.OK) { 681 681 if (dialog.Values.Any()) { 682 targets = maximization ? dialog.Values.Select(x => (double)x).ToArray() 683 : dialog.Values.Reverse().Select(x => (double)x).ToArray(); 682 targets = dialog.Values.Select(x => (double)x).ToArray(); 684 683 suppressTargetsEvents = true; 685 684 targetsTextBox.Text = string.Join("% ; ", targets); … … 695 694 private void addTargetsAsResultButton_Click(object sender, EventArgs e) { 696 695 var table = (string)dataTableComboBox.SelectedItem; 697 var maximization = IsMaximization(); 698 699 var targetsPerProblem = CalculateBestTargetPerProblemInstance(table, maximization); 696 697 var targetsPerProblem = CalculateBestTargetPerProblemInstance(table); 700 698 701 699 foreach (var run in Content) { … … 707 705 var pd = new ProblemDescription(run); 708 706 while (i < targets.Length && j < values.Count) { 709 var target = ( maximization ? (1 - targets[i]) : (1 + targets[i])) * targetsPerProblem[pd.ToString()];707 var target = (pd.IsMaximization() ? (1 - targets[i]) : (1 + targets[i])) * targetsPerProblem[pd]; 710 708 var current = values[j]; 711 if ( maximization&& current.Item2 >= target712 || ! maximization&& current.Item2 <= target) {709 if (pd.IsMaximization() && current.Item2 >= target 710 || !pd.IsMaximization() && current.Item2 <= target) { 713 711 run.Results[table + ".Target" + target] = new DoubleValue(current.Item1); 714 712 i++; … … 828 826 829 827 #region Helpers 830 // Determines if the RunCollection contains maximization or minimization runs 831 private bool IsMaximization() { 832 if (Content == null) return false; 833 if (Content.Count > 0) { 834 foreach (var run in Content.Where(x => x.Parameters.ContainsKey("Maximization") 835 && x.Parameters["Maximization"] is BoolValue)) { 836 if (((BoolValue)run.Parameters["Maximization"]).Value) { 837 return true; 838 } else { 839 return false; 840 } 841 } 842 if (dataTableComboBox.SelectedIndex >= 0) { 843 var selectedTable = (string)dataTableComboBox.SelectedItem; 844 foreach (var run in Content.Where(x => x.Results.ContainsKey(selectedTable))) { 845 var table = run.Results[selectedTable] as IndexedDataTable<double>; 846 if (table == null) continue; 847 var firstRowValues = table.Rows.First().Values; 848 if (firstRowValues.Count < 2) continue; 849 return firstRowValues[0].Item2 < firstRowValues[firstRowValues.Count - 1].Item2; 850 } 851 } 852 } 853 // assume minimization by default 854 return false; 855 } 856 857 private Dictionary<string, double> CalculateBestTargetPerProblemInstance(string table, bool maximization) { 828 private Dictionary<ProblemDescription, double> CalculateBestTargetPerProblemInstance(string table) { 858 829 return (from r in Content 859 830 where r.Visible 860 let pd = new ProblemDescription(r) .ToString()831 let pd = new ProblemDescription(r) 861 832 let target = r.Parameters.ContainsKey("BestKnownQuality") 862 833 && r.Parameters["BestKnownQuality"] is DoubleValue … … 864 835 : ((IndexedDataTable<double>)r.Results[table]).Rows.First().Values.Last().Item2 865 836 group target by pd into g 866 select new { Problem = g.Key, Target = maximization? g.Max() : g.Min() })837 select new { Problem = g.Key, Target = g.Key.IsMaximization() ? g.Max() : g.Min() }) 867 838 .ToDictionary(x => x.Problem, x => x.Target); 868 839 } … … 892 863 ProblemName = string.Empty; 893 864 Evaluator = string.Empty; 865 Maximization = string.Empty; 894 866 DisplayProblemType = false; 895 867 DisplayProblemName = false; 896 868 DisplayEvaluator = false; 869 DisplayMaximization = false; 897 870 matchAll = true; 898 871 } … … 902 875 ProblemName = GetStringValueOrEmpty(run, "Problem Name"); 903 876 Evaluator = GetStringValueOrEmpty(run, "Evaluator"); 877 Maximization = GetMaximizationValueOrEmpty(run, "Maximization"); 904 878 DisplayProblemType = !string.IsNullOrEmpty(ProblemType); 905 879 DisplayProblemName = !string.IsNullOrEmpty(ProblemName); 906 880 DisplayEvaluator = !string.IsNullOrEmpty(Evaluator); 881 DisplayMaximization = !string.IsNullOrEmpty(Maximization); 907 882 matchAll = false; 908 883 } … … 914 889 public bool DisplayEvaluator { get; set; } 915 890 public string Evaluator { get; set; } 891 public bool DisplayMaximization { get; set; } 892 public string Maximization { get; set; } 893 894 public bool IsMaximization() { 895 return Maximization == "MAX"; 896 } 916 897 917 898 public bool Match(IRun run) { … … 919 900 GetStringValueOrEmpty(run, "Problem Type") == ProblemType 920 901 && GetStringValueOrEmpty(run, "Problem Name") == ProblemName 921 && GetStringValueOrEmpty(run, "Evaluator") == Evaluator; 902 && GetStringValueOrEmpty(run, "Evaluator") == Evaluator 903 && GetMaximizationValueOrEmpty(run, "Maximization") == Maximization; 922 904 } 923 905 924 906 private string GetStringValueOrEmpty(IRun run, string key) { 925 907 return run.Parameters.ContainsKey(key) ? ((StringValue)run.Parameters[key]).Value : string.Empty; 908 } 909 910 private string GetMaximizationValueOrEmpty(IRun run, string key) { 911 return run.Parameters.ContainsKey(key) ? (((BoolValue)run.Parameters[key]).Value ? "MAX" : "MIN") : string.Empty; 926 912 } 927 913 … … 931 917 return ProblemType == other.ProblemType 932 918 && ProblemName == other.ProblemName 933 && Evaluator == other.Evaluator; 919 && Evaluator == other.Evaluator 920 && Maximization == other.Maximization; 934 921 } 935 922 936 923 public override int GetHashCode() { 937 return ProblemType.GetHashCode() ^ ProblemName.GetHashCode() ^ Evaluator.GetHashCode() ;924 return ProblemType.GetHashCode() ^ ProblemName.GetHashCode() ^ Evaluator.GetHashCode() ^ Maximization.GetHashCode(); 938 925 } 939 926 … … 942 929 (DisplayProblemType ? ProblemType : string.Empty), 943 930 (DisplayProblemName ? ProblemName : string.Empty), 944 (DisplayEvaluator ? Evaluator : string.Empty) }.Where(x => !string.IsNullOrEmpty(x))); 931 (DisplayEvaluator ? Evaluator : string.Empty), 932 (DisplayMaximization ? Maximization : string.Empty)}.Where(x => !string.IsNullOrEmpty(x))); 945 933 } 946 934 }
Note: See TracChangeset
for help on using the changeset viewer.