Changeset 14647 for trunk/sources
- Timestamp:
- 02/06/17 22:50:29 (8 years ago)
- Location:
- trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.Designer.cs
r14185 r14647 66 66 this.targetChart = new HeuristicLab.Visualization.ChartControlsExtensions.EnhancedChart(); 67 67 this.boundShadingCheckBox = new System.Windows.Forms.CheckBox(); 68 this.targetsRelativeCheckBox = new System.Windows.Forms.CheckBox(); 68 69 this.byCostTabPage = new System.Windows.Forms.TabPage(); 69 70 this.byCostViewHost = new HeuristicLab.MainForm.WindowsForms.ViewHost(); … … 143 144 this.targetsTextBox.Location = new System.Drawing.Point(59, 8); 144 145 this.targetsTextBox.Name = "targetsTextBox"; 145 this.targetsTextBox.Size = new System.Drawing.Size(2 87, 20);146 this.targetsTextBox.Size = new System.Drawing.Size(211, 20); 146 147 this.targetsTextBox.TabIndex = 1; 147 148 this.toolTip.SetToolTip(this.targetsTextBox, "The order of the targets is important, first to-hit targets\r\nshould be given firs" + … … 221 222 this.aggregateTargetsCheckBox.Checked = true; 222 223 this.aggregateTargetsCheckBox.CheckState = System.Windows.Forms.CheckState.Checked; 223 this.aggregateTargetsCheckBox.Location = new System.Drawing.Point(35 2, 10);224 this.aggregateTargetsCheckBox.Location = new System.Drawing.Point(353, 10); 224 225 this.aggregateTargetsCheckBox.Name = "aggregateTargetsCheckBox"; 225 226 this.aggregateTargetsCheckBox.Size = new System.Drawing.Size(74, 17); … … 249 250 this.byTargetTabPage.Controls.Add(this.targetLogScalingCheckBox); 250 251 this.byTargetTabPage.Controls.Add(this.targetsLabel); 252 this.byTargetTabPage.Controls.Add(this.targetsRelativeCheckBox); 251 253 this.byTargetTabPage.Controls.Add(this.aggregateTargetsCheckBox); 252 254 this.byTargetTabPage.Controls.Add(this.targetsTextBox); … … 304 306 this.boundShadingCheckBox.UseVisualStyleBackColor = true; 305 307 this.boundShadingCheckBox.CheckedChanged += new System.EventHandler(this.boundShadingCheckBox_CheckedChanged); 308 // 309 // targetsRelativeCheckBox 310 // 311 this.targetsRelativeCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 312 this.targetsRelativeCheckBox.Appearance = System.Windows.Forms.Appearance.Button; 313 this.targetsRelativeCheckBox.Checked = true; 314 this.targetsRelativeCheckBox.CheckState = System.Windows.Forms.CheckState.Checked; 315 this.targetsRelativeCheckBox.Location = new System.Drawing.Point(276, 6); 316 this.targetsRelativeCheckBox.Name = "targetsRelativeCheckBox"; 317 this.targetsRelativeCheckBox.Size = new System.Drawing.Size(65, 23); 318 this.targetsRelativeCheckBox.TabIndex = 2; 319 this.targetsRelativeCheckBox.Text = "relative"; 320 this.targetsRelativeCheckBox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 321 this.targetsRelativeCheckBox.UseVisualStyleBackColor = true; 322 this.targetsRelativeCheckBox.CheckedChanged += new System.EventHandler(this.targetsRelativeCheckBox_CheckedChanged); 306 323 // 307 324 // byCostTabPage … … 473 490 private MainForm.WindowsForms.ViewHost byCostViewHost; 474 491 private System.Windows.Forms.CheckBox boundShadingCheckBox; 492 private System.Windows.Forms.CheckBox targetsRelativeCheckBox; 475 493 } 476 494 } -
trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.cs
r14185 r14647 30 30 using HeuristicLab.Analysis; 31 31 using HeuristicLab.Collections; 32 using HeuristicLab.Core; 32 33 using HeuristicLab.Core.Views; 33 34 using HeuristicLab.Data; … … 79 80 private double[] targets; 80 81 private double[] budgets; 82 private bool targetsAreRelative = true; 83 private readonly BindingList<ProblemInstance> problems; 81 84 82 85 private bool suppressUpdates; … … 103 106 }; 104 107 byCostViewHost.Content = byCostDataTable; 108 targetsRelativeCheckBox.Checked = targetsAreRelative; 109 problems = new BindingList<ProblemInstance>(); 110 problemComboBox.DataSource = new BindingSource() { DataSource = problems }; 111 problemComboBox.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged; 105 112 suppressUpdates = false; 106 113 } … … 223 230 } 224 231 225 var problems = new HashSet<ProblemDescription>(); 226 foreach (var run in Content) { 227 problems.Add(new ProblemDescription(run)); 228 } 229 230 var problemTypesDifferent = problems.Select(x => x.ProblemType).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 231 var problemNamesDifferent = problems.Select(x => x.ProblemName).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 232 var evaluatorDifferent = problems.Select(x => x.Evaluator).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 233 var maximizationDifferent = problems.Select(x => x.Maximization).Distinct().Count() > 1; 232 var table = (string)dataTableComboBox.SelectedItem; 233 var problemDict = CalculateBestTargetPerProblemInstance(table); 234 235 var problemTypesDifferent = problemDict.Keys.Select(x => x.ProblemType).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 236 var problemNamesDifferent = problemDict.Keys.Select(x => x.ProblemName).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 237 var evaluatorDifferent = problemDict.Keys.Select(x => x.Evaluator).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1; 238 var maximizationDifferent = problemDict.Keys.Select(x => x.Maximization).Distinct().Count() > 1; 234 239 var allEqual = !problemTypesDifferent && !problemNamesDifferent && !evaluatorDifferent && !maximizationDifferent; 235 240 236 var selectedProblemItem = (Problem Description)problemComboBox.SelectedItem;237 problem ComboBox.Items.Clear();238 problem ComboBox.Items.Add(ProblemDescription.MatchAll);239 if ( selectedProblemItem == null || selectedProblemItem == ProblemDescription.MatchAll)240 problemComboBox.SelectedIndex = 0;241 foreach (var prob in problems.OrderBy(x => x.ToString()).ToList()) {242 p rob.DisplayProblemType = problemTypesDifferent;243 p rob.DisplayProblemName = problemNamesDifferent || allEqual;244 p rob.DisplayEvaluator = evaluatorDifferent;245 p rob.DisplayMaximization = maximizationDifferent;246 problem ComboBox.Items.Add(prob);247 if (p rob.Equals(selectedProblemItem)) problemComboBox.SelectedItem = prob;241 var selectedProblemItem = (ProblemInstance)problemComboBox.SelectedItem; 242 problems.Clear(); 243 problems.Add(ProblemInstance.MatchAll); 244 if (problems[0].Equals(selectedProblemItem)) problemComboBox.SelectedItem = problems[0]; 245 foreach (var p in problemDict.ToList()) { 246 p.Key.BestKnownQuality = p.Value; 247 p.Key.DisplayProblemType = problemTypesDifferent; 248 p.Key.DisplayProblemName = problemNamesDifferent || allEqual; 249 p.Key.DisplayEvaluator = evaluatorDifferent; 250 p.Key.DisplayMaximization = maximizationDifferent; 251 problems.Add(p.Key); 252 if (p.Key.Equals(selectedProblemItem)) problemComboBox.SelectedItem = p.Key; 248 253 } 249 254 SetEnabledStateOfControls(); … … 276 281 } 277 282 278 private Dictionary<string, Dictionary<Problem Description, Tuple<double, List<IRun>>>> GroupRuns() {279 var groupedRuns = new Dictionary<string, Dictionary<Problem Description, Tuple<double, List<IRun>>>>();283 private Dictionary<string, Dictionary<ProblemInstance, List<IRun>>> GroupRuns() { 284 var groupedRuns = new Dictionary<string, Dictionary<ProblemInstance, List<IRun>>>(); 280 285 281 286 var table = (string)dataTableComboBox.SelectedItem; … … 285 290 if (string.IsNullOrEmpty(selectedGroup)) return groupedRuns; 286 291 287 var selectedProblem = (Problem Description)problemComboBox.SelectedItem;292 var selectedProblem = (ProblemInstance)problemComboBox.SelectedItem; 288 293 if (selectedProblem == null) return groupedRuns; 289 290 var targetsPerProblem = CalculateBestTargetPerProblemInstance(table); 291 294 292 295 foreach (var x in (from r in Content 293 296 where (selectedGroup == AllRuns || r.Parameters.ContainsKey(selectedGroup)) … … 297 300 group r by selectedGroup == AllRuns ? AllRuns : r.Parameters[selectedGroup].ToString() into g 298 301 select Tuple.Create(g.Key, g.ToList()))) { 299 var pDict = new Dictionary<ProblemDescription, Tuple<double, List<IRun>>>(); 300 foreach (var y in (from r in x.Item2 301 let pd = new ProblemDescription(r) 302 group r by pd into g 303 select Tuple.Create(g.Key, g.ToList()))) { 304 pDict[y.Item1] = Tuple.Create(targetsPerProblem[y.Item1], y.Item2); 305 } 306 groupedRuns[x.Item1] = pDict; 302 var pDict = problems.ToDictionary(r => r, _ => new List<IRun>()); 303 foreach (var y in x.Item2) { 304 var pd = new ProblemInstance(y); 305 List<IRun> l; 306 if (pDict.TryGetValue(pd, out l)) l.Add(y); 307 } 308 groupedRuns[x.Item1] = pDict.Where(a => a.Value.Count > 0).ToDictionary(a => a.Key, a => a.Value); 307 309 } 308 310 … … 333 335 // have data for a certain problem instance anymore this is a special case when 334 336 // aggregating over multiple problem instances 335 var maxEfforts = new Dictionary<Problem Description, double>();337 var maxEfforts = new Dictionary<ProblemInstance, double>(); 336 338 double minEff = double.MaxValue, maxEff = double.MinValue; 337 339 foreach (var group in groupedRuns) { … … 341 343 problemSpecificMaxEff = 0; 342 344 } 343 var bestKnownTarget = problem.Value.Item1;344 345 var max = problem.Key.IsMaximization(); 345 var worstTarget = (max ? (1 - targets.Max()) : (1 + targets.Max())) * bestKnownTarget; 346 var bestTarget = (max ? (1 - targets.Min()) : (1 + targets.Min())) * bestKnownTarget; 347 foreach (var run in problem.Value.Item2) { 346 var absTargets = GetAbsoluteTargetsWorstToBest(max, problem.Key.BestKnownQuality); 347 var worstTarget = absTargets.First(); 348 var bestTarget = absTargets.Last(); 349 foreach (var run in problem.Value) { 348 350 var row = ((IndexedDataTable<double>)run.Results[table]).Rows.First().Values; 349 351 var a = row.FirstOrDefault(x => max ? x.Item2 >= worstTarget : x.Item2 <= worstTarget); … … 384 386 var noRuns = 0; 385 387 foreach (var problem in group.Value) { 386 foreach (var run in problem.Value .Item2) {388 foreach (var run in problem.Value) { 387 389 var resultsTable = (IndexedDataTable<double>)run.Results[table]; 388 390 xAxisTitles.Add(resultsTable.VisualProperties.XAxisTitle); 389 391 390 392 if (aggregateTargetsCheckBox.Checked) { 391 var length = CalculateHitsForAllTargets(hits, misses, resultsTable.Rows.First(), problem.Key, group.Key, problem. Value.Item1, maxEff);393 var length = CalculateHitsForAllTargets(hits, misses, resultsTable.Rows.First(), problem.Key, group.Key, problem.Key.BestKnownQuality, maxEff); 392 394 maxLength = Math.Max(length, maxLength); 393 395 } else { 394 CalculateHitsForEachTarget(hits, misses, resultsTable.Rows.First(), problem.Key, group.Key, problem. Value.Item1, maxEff);396 CalculateHitsForEachTarget(hits, misses, resultsTable.Rows.First(), problem.Key, group.Key, problem.Key.BestKnownQuality, maxEff); 395 397 } 396 398 noRuns++; … … 407 409 IsVisibleInLegend = false, 408 410 ChartType = SeriesChartType.Range, 409 Color = Color.FromArgb(32, colors[colorCount]) 411 Color = Color.FromArgb(32, colors[colorCount]), 412 YValuesPerPoint = 2 410 413 }; 411 414 … … 475 478 } 476 479 477 if (targets.Length == 1) 478 targetChart.ChartAreas[0].AxisY.Title = "Probability to be " + (targets[0] * 100) + "% worse than best"; 479 else targetChart.ChartAreas[0].AxisY.Title = "Proportion of reached targets"; 480 if (targets.Length == 1) { 481 if (targetsAreRelative) 482 targetChart.ChartAreas[0].AxisY.Title = "Probability to be " + (targets[0] * 100) + "% worse than best"; 483 else targetChart.ChartAreas[0].AxisY.Title = "Probability to reach at least a fitness of " + targets[0]; 484 } else targetChart.ChartAreas[0].AxisY.Title = "Proportion of reached targets"; 480 485 targetChart.ChartAreas[0].AxisX.Title = string.Join(" / ", xAxisTitles); 481 486 targetChart.ChartAreas[0].AxisX.IsLogarithmic = CanDisplayLogarithmic(); 482 487 targetChart.ChartAreas[0].CursorY.Interval = 0.05; 483 488 UpdateErtTables(groupedRuns); 489 } 490 491 private IEnumerable<double> GetAbsoluteTargets(bool maximization, double bestKnown) { 492 if (!targetsAreRelative) return targets; 493 IEnumerable<double> tmp = null; 494 if (bestKnown > 0) { 495 tmp = targets.Select(x => (maximization ? (1 - x) : (1 + x)) * bestKnown); 496 } else if (bestKnown < 0) { 497 tmp = targets.Select(x => (!maximization ? (1 - x) : (1 + x)) * bestKnown); 498 } else { 499 // relative to 0 is impossible 500 tmp = targets; 501 } 502 return tmp; 503 } 504 505 private double[] GetAbsoluteTargetsWorstToBest(bool maximization, double bestKnown) { 506 var absTargets = GetAbsoluteTargets(maximization, bestKnown); 507 return maximization ? absTargets.OrderBy(x => x).ToArray() : absTargets.OrderByDescending(x => x).ToArray(); 484 508 } 485 509 … … 493 517 private void CalculateHitsForEachTarget(Dictionary<string, SortedList<double, int>> hits, 494 518 Dictionary<string, SortedList<double, int>> misses, 495 IndexedDataRow<double> row, Problem Descriptionproblem,519 IndexedDataRow<double> row, ProblemInstance problem, 496 520 string group, double bestTarget, double maxEffort) { 497 foreach (var t in targets.Select(x => Tuple.Create((problem.IsMaximization() ? (1 - x) : (1 + x)) * bestTarget, x))) { 521 var maximization = problem.IsMaximization(); 522 foreach (var t in targets.Zip(GetAbsoluteTargets(maximization, bestTarget), (rt, at) => Tuple.Create(at, rt))) { 498 523 var l = t.Item1; 499 var key = group + "_" + (t .Item2 * 100) + "%_" + l;524 var key = group + "_" + (targetsAreRelative ? (t.Item2 * 100) + "%_" + l : l.ToString()); 500 525 if (!hits.ContainsKey(key)) { 501 526 hits.Add(key, new SortedList<double, int>()); … … 505 530 foreach (var v in row.Values) { 506 531 if (v.Item1 > maxEffort) break; 507 if ( problem.IsMaximization() && v.Item2 >= l || !problem.IsMaximization()&& v.Item2 <= l) {532 if (maximization && v.Item2 >= l || !maximization && v.Item2 <= l) { 508 533 if (hits[key].ContainsKey(v.Item1)) 509 534 hits[key][v.Item1]++; … … 524 549 private double CalculateHitsForAllTargets(Dictionary<string, SortedList<double, int>> hits, 525 550 Dictionary<string, SortedList<double, int>> misses, 526 IndexedDataRow<double> row, Problem Descriptionproblem,551 IndexedDataRow<double> row, ProblemInstance problem, 527 552 string group, double bestTarget, double maxEffort) { 528 553 var values = row.Values; … … 534 559 var i = 0; 535 560 var j = 0; 536 while (i < targets.Length && j < values.Count) { 537 var target = (problem.IsMaximization() ? (1 - targets[i]) : (1 + targets[i])) * bestTarget; 561 var max = problem.IsMaximization(); 562 var absTargets = GetAbsoluteTargetsWorstToBest(max, bestTarget); 563 while (i < absTargets.Length && j < values.Count) { 564 var target = absTargets[i]; 538 565 var current = values[j]; 539 566 if (current.Item1 > maxEffort) break; … … 549 576 if (j == values.Count) j--; 550 577 var effort = Math.Min(values[j].Item1, maxEffort); 551 if (i < targets.Length) {578 if (i < absTargets.Length) { 552 579 if (misses[group].ContainsKey(effort)) 553 misses[group][effort] += targets.Length - i;554 else misses[group][effort] = targets.Length - i;580 misses[group][effort] += absTargets.Length - i; 581 else misses[group][effort] = absTargets.Length - i; 555 582 } 556 583 return effort; 557 584 } 558 585 559 private void UpdateErtTables(Dictionary<string, Dictionary<Problem Description, Tuple<double, List<IRun>>>> groupedRuns) {586 private void UpdateErtTables(Dictionary<string, Dictionary<ProblemInstance, List<IRun>>> groupedRuns) { 560 587 ertTableView.Content = null; 561 588 var columns = 1 + targets.Length + 1; … … 565 592 var tableName = (string)dataTableComboBox.SelectedItem; 566 593 if (string.IsNullOrEmpty(tableName)) return; 567 568 var targetsPerProblem = CalculateBestTargetPerProblemInstance(tableName); 569 570 var colNames = new string[columns]; 571 colNames[0] = colNames[colNames.Length - 1] = string.Empty; 572 for (var i = 0; i < targets.Length; i++) { 573 colNames[i + 1] = targets[i].ToString("0.0%"); 574 } 594 575 595 var problems = groupedRuns.SelectMany(x => x.Value.Keys).Distinct().ToList(); 576 596 577 597 foreach (var problem in problems) { 598 var max = problem.IsMaximization(); 578 599 matrix[rowCount, 0] = problem.ToString(); 579 for (var i = 0; i < targets.Length; i++) { 580 matrix[rowCount, i + 1] = (targetsPerProblem[problem] * (problem.IsMaximization() ? (1 - targets[i]) : (1 + targets[i]))).ToString(CultureInfo.CurrentCulture.NumberFormat); 600 var absTargets = GetAbsoluteTargetsWorstToBest(max, problem.BestKnownQuality); 601 for (var i = 0; i < absTargets.Length; i++) { 602 matrix[rowCount, i + 1] = absTargets[i].ToString(CultureInfo.CurrentCulture.NumberFormat); 581 603 } 582 604 matrix[rowCount, columns - 1] = "#succ"; … … 590 612 continue; 591 613 } 592 var runs = group.Value[problem] .Item2;593 ErtCalculationResultresult = default(ErtCalculationResult);594 for (var i = 0; i < targets.Length; i++) {595 result = ExpectedRuntimeHelper.CalculateErt(runs, tableName, (problem.IsMaximization() ? (1 - targets[i]) : (1 + targets[i])) * group.Value[problem].Item1, problem.IsMaximization());614 var runs = group.Value[problem]; 615 var result = default(ErtCalculationResult); 616 for (var i = 0; i < absTargets.Length; i++) { 617 result = ExpectedRuntimeHelper.CalculateErt(runs, tableName, absTargets[i], max); 596 618 matrix[rowCount, i + 1] = result.ToString(); 597 619 } … … 600 622 } 601 623 } 602 ertTableView.Content = new StringMatrix(matrix) { ColumnNames = colNames };624 ertTableView.Content = new StringMatrix(matrix); 603 625 ertTableView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 604 626 } … … 621 643 var colorCount = 0; 622 644 var lineStyleCount = 0; 623 624 var targetsPerProblem = CalculateBestTargetPerProblemInstance((string)dataTableComboBox.SelectedItem); 625 645 626 646 foreach (var group in groupedRuns) { 627 647 var hits = new Dictionary<string, SortedList<double, double>>(); 628 648 629 649 foreach (var problem in group.Value) { 630 foreach (var run in problem.Value .Item2) {650 foreach (var run in problem.Value) { 631 651 var resultsTable = (IndexedDataTable<double>)run.Results[table]; 632 652 633 653 if (eachOrAllBudgetsCheckBox.Checked) { 634 CalculateHitsForEachBudget(hits, resultsTable.Rows.First(), group.Value.Count, problem.Key, group.Key, problem.Value. Item2.Count, targetsPerProblem[problem.Key]);654 CalculateHitsForEachBudget(hits, resultsTable.Rows.First(), group.Value.Count, problem.Key, group.Key, problem.Value.Count); 635 655 } else { 636 CalculateHitsForAllBudgets(hits, resultsTable.Rows.First(), group.Value.Count, problem.Key, group.Key, problem.Value. Item2.Count, targetsPerProblem[problem.Key]);656 CalculateHitsForAllBudgets(hits, resultsTable.Rows.First(), group.Value.Count, problem.Key, group.Key, problem.Value.Count); 637 657 } 638 658 } … … 667 687 668 688 private void GenerateDefaultBudgets(string table) { 669 var runs = GroupRuns().SelectMany(x => x.Value.Values).SelectMany(x => x .Item2).ToList();689 var runs = GroupRuns().SelectMany(x => x.Value.Values).SelectMany(x => x).ToList(); 670 690 var min = runs.Select(x => ((IndexedDataTable<double>)x.Results[table]).Rows.First().Values.Select(y => y.Item1).Min()).Min(); 671 691 var max = runs.Select(x => ((IndexedDataTable<double>)x.Results[table]).Rows.First().Values.Select(y => y.Item1).Max()).Max(); … … 687 707 } 688 708 689 private void CalculateHitsForEachBudget(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, int groupCount, ProblemDescription problem, string groupName, int problemCount, double bestTarget) { 709 private void CalculateHitsForEachBudget(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, int groupCount, ProblemInstance problem, string groupName, int problemCount) { 710 var max = problem.IsMaximization(); 690 711 foreach (var b in budgets) { 691 712 var key = groupName + "-" + b; … … 697 718 if (prev == null && v.Item1 != b) break; 698 719 var tgt = ((prev == null || v.Item1 == b) ? v.Item2 : prev.Item2); 699 tgt = problem.IsMaximization() ? bestTarget / tgt : tgt / bestTarget;700 if (hits[key].ContainsKey( tgt))701 hits[key][ tgt] += 1.0 / (groupCount * problemCount);702 else hits[key][ tgt] = 1.0 / (groupCount * problemCount);720 var relTgt = CalculateRelativeDifference(max, problem.BestKnownQuality, tgt); 721 if (hits[key].ContainsKey(relTgt)) 722 hits[key][relTgt] += 1.0 / (groupCount * problemCount); 723 else hits[key][relTgt] = 1.0 / (groupCount * problemCount); 703 724 break; 704 725 } … … 709 730 } 710 731 711 private void CalculateHitsForAllBudgets(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, int groupCount, Problem Description problem, string groupName, int problemCount, double bestTarget) {732 private void CalculateHitsForAllBudgets(Dictionary<string, SortedList<double, double>> hits, IndexedDataRow<double> row, int groupCount, ProblemInstance problem, string groupName, int problemCount) { 712 733 var values = row.Values; 713 734 if (!hits.ContainsKey(groupName)) hits.Add(groupName, new SortedList<double, double>()); … … 716 737 var j = 0; 717 738 Tuple<double, double> prev = null; 739 var max = problem.IsMaximization(); 718 740 while (i < budgets.Length && j < values.Count) { 719 741 var current = values[j]; … … 721 743 if (prev != null || current.Item1 == budgets[i]) { 722 744 var tgt = (prev == null || current.Item1 == budgets[i]) ? current.Item2 : prev.Item2; 723 tgt = problem.IsMaximization() ? bestTarget / tgt : tgt / bestTarget;724 if (!hits[groupName].ContainsKey( tgt)) hits[groupName][tgt] = 0;725 hits[groupName][ tgt] += 1.0 / (groupCount * problemCount * budgets.Length);745 var relTgt = CalculateRelativeDifference(max, problem.BestKnownQuality, tgt); 746 if (!hits[groupName].ContainsKey(relTgt)) hits[groupName][relTgt] = 0; 747 hits[groupName][relTgt] += 1.0 / (groupCount * problemCount * budgets.Length); 726 748 } 727 749 i++; … … 732 754 } 733 755 var lastTgt = values.Last().Item2; 734 lastTgt = problem.IsMaximization() ? bestTarget / lastTgt : lastTgt / bestTarget;735 if (i < budgets.Length && !hits[groupName].ContainsKey(last Tgt)) hits[groupName][lastTgt] = 0;756 var lastRelTgt = CalculateRelativeDifference(max, problem.BestKnownQuality, lastTgt); 757 if (i < budgets.Length && !hits[groupName].ContainsKey(lastRelTgt)) hits[groupName][lastRelTgt] = 0; 736 758 while (i < budgets.Length) { 737 hits[groupName][last Tgt] += 1.0 / (groupCount * problemCount * budgets.Length);759 hits[groupName][lastRelTgt] += 1.0 / (groupCount * problemCount * budgets.Length); 738 760 i++; 739 761 } … … 756 778 if (dataTableComboBox.SelectedIndex >= 0) 757 779 GenerateDefaultBudgets((string)dataTableComboBox.SelectedItem); 780 UpdateBestKnownQualities(); 758 781 UpdateRuns(); 759 782 SetEnabledStateOfControls(); … … 782 805 return; 783 806 } 784 targetList.Add(t / 100); 807 if (targetsAreRelative) 808 targetList.Add(t / 100); 809 else targetList.Add(t); 785 810 } 786 811 if (targetList.Count == 0) { … … 791 816 e.Cancel = false; 792 817 errorProvider.SetError(targetsTextBox, null); 793 targets = target List.Select(x => (double)x).OrderByDescending(x =>x).ToArray();818 targets = targetsAreRelative ? targetList.Select(x => (double)x).OrderByDescending(x => x).ToArray() : targetList.Select(x => (double)x).ToArray(); 794 819 suppressTargetsEvents = true; 795 try { targetsTextBox.Text = string.Join("% ; ", targets.Select(x => x * 100)) + "%"; } finally { suppressTargetsEvents = false; } 820 try { 821 if (targetsAreRelative) 822 targetsTextBox.Text = string.Join("% ; ", targets.Select(x => x * 100)) + "%"; 823 else targetsTextBox.Text = string.Join(" ; ", targets); 824 } finally { suppressTargetsEvents = false; } 796 825 797 826 UpdateResultsByTarget(); … … 804 833 UpdateResultsByTarget(); 805 834 } finally { ResumeRepaint(true); } 835 } 836 837 private void targetsRelativeCheckBox_CheckedChanged(object sender, EventArgs e) { 838 targetsRelativeCheckBox.Text = targetsRelativeCheckBox.Checked ? "relative" : "absolute"; 839 if (suppressUpdates) return; 840 var pd = (ProblemInstance)problemComboBox.SelectedItem; 841 if (!double.IsNaN(pd.BestKnownQuality)) { 842 var max = pd.IsMaximization(); 843 if (targetsAreRelative) targets = GetAbsoluteTargets(max, pd.BestKnownQuality).ToArray(); 844 else { 845 // Rounding to 5 digits since it's certainly appropriate for this application 846 if (pd.BestKnownQuality > 0) { 847 targets = targets.Select(x => Math.Round(max ? 1.0 - (x / pd.BestKnownQuality) : (x / pd.BestKnownQuality) - 1.0, 5)).ToArray(); 848 } else if (pd.BestKnownQuality < 0) { 849 targets = targets.Select(x => Math.Round(!max ? 1.0 - (x / pd.BestKnownQuality) : (x / pd.BestKnownQuality) - 1.0, 5)).ToArray(); 850 } 851 } 852 } 853 targetsAreRelative = targetsRelativeCheckBox.Checked; 854 SuspendRepaint(); 855 suppressTargetsEvents = true; 856 try { 857 if (targetsAreRelative) 858 targetsTextBox.Text = string.Join("% ; ", targets.Select(x => x * 100)) + "%"; 859 else targetsTextBox.Text = string.Join(" ; ", targets); 860 861 UpdateResultsByTarget(); 862 } finally { suppressTargetsEvents = false; ResumeRepaint(true); } 806 863 } 807 864 … … 823 880 targets = dialog.Values.Select(x => (double)x).ToArray(); 824 881 suppressTargetsEvents = true; 825 targetsTextBox.Text = string.Join("% ; ", targets); 882 if (targetsAreRelative) 883 targetsTextBox.Text = string.Join("% ; ", targets); 884 else targetsTextBox.Text = string.Join(" ; ", targets); 826 885 suppressTargetsEvents = false; 827 886 … … 835 894 private void addTargetsAsResultButton_Click(object sender, EventArgs e) { 836 895 var table = (string)dataTableComboBox.SelectedItem; 837 838 var targetsPerProblem = CalculateBestTargetPerProblemInstance(table); 896 if (string.IsNullOrEmpty(table)) return; 897 898 var targetsPerProblem = problems.ToDictionary(x => x, x => x.BestKnownQuality); 839 899 840 900 foreach (var run in Content) { … … 844 904 var i = 0; 845 905 var j = 0; 846 var pd = new ProblemDescription(run); 847 while (i < targets.Length && j < values.Count) { 848 var target = (pd.IsMaximization() ? (1 - targets[i]) : (1 + targets[i])) * targetsPerProblem[pd]; 906 var pd = new ProblemInstance(run); 907 var max = pd.IsMaximization(); 908 var absTargets = GetAbsoluteTargetsWorstToBest(max, targetsPerProblem[pd]); 909 while (i < absTargets.Length && j < values.Count) { 910 var target = absTargets[i]; 849 911 var current = values[j]; 850 if (pd.IsMaximization() && current.Item2 >= target 851 || !pd.IsMaximization() && current.Item2 <= target) { 912 if (max && current.Item2 >= target || !max && current.Item2 <= target) { 852 913 run.Results[table + ".Target" + target] = new DoubleValue(current.Item1); 853 914 i++; … … 967 1028 968 1029 #region Helpers 969 private Dictionary<ProblemDescription, double> CalculateBestTargetPerProblemInstance(string table) { 1030 private double CalculateRelativeDifference(bool maximization, double bestKnown, double fit) { 1031 if (bestKnown == 0) { 1032 // no relative difference with respect to bestKnown possible 1033 return maximization ? -fit : fit; 1034 } 1035 var absDiff = (fit - bestKnown); 1036 var relDiff = absDiff / bestKnown; 1037 if (maximization) { 1038 return bestKnown > 0 ? -relDiff : relDiff; 1039 } else { 1040 return bestKnown > 0 ? relDiff : -relDiff; 1041 } 1042 } 1043 1044 private Dictionary<ProblemInstance, double> CalculateBestTargetPerProblemInstance(string table) { 1045 if (table == null) table = string.Empty; 970 1046 return (from r in Content 971 1047 where r.Visible 972 let pd = new Problem Description(r)1048 let pd = new ProblemInstance(r) 973 1049 let target = r.Parameters.ContainsKey("BestKnownQuality") 974 1050 && r.Parameters["BestKnownQuality"] is DoubleValue 975 1051 ? ((DoubleValue)r.Parameters["BestKnownQuality"]).Value 976 : ((IndexedDataTable<double>)r.Results[table]).Rows.First().Values.Last().Item2 1052 : (r.Results.ContainsKey(table) && r.Results[table] is IndexedDataTable<double> 1053 ? ((IndexedDataTable<double>)r.Results[table]).Rows.First().Values.Last().Item2 1054 : double.NaN) 977 1055 group target by pd into g 978 select new { Problem = g.Key, Target = g. Key.IsMaximization() ? g.Max() : g.Min()})1056 select new { Problem = g.Key, Target = g.Any(x => !double.IsNaN(x)) ? (g.Key.IsMaximization() ? g.Max() : g.Where(x => !double.IsNaN(x)).Min()) : double.NaN }) 979 1057 .ToDictionary(x => x.Problem, x => x.Target); 980 1058 } … … 990 1068 UpdateResultsByCost(); 991 1069 } finally { ResumeRepaint(true); } 1070 } 1071 1072 private void UpdateBestKnownQualities() { 1073 var table = (string)dataTableComboBox.SelectedItem; 1074 if (string.IsNullOrEmpty(table)) return; 1075 1076 var targetsPerProblem = CalculateBestTargetPerProblemInstance(table); 1077 foreach (var pd in problems) { 1078 double bkq; 1079 if (targetsPerProblem.TryGetValue(pd, out bkq)) 1080 pd.BestKnownQuality = bkq; 1081 else pd.BestKnownQuality = double.NaN; 1082 } 1083 //problemComboBox.ResetBindings(); 992 1084 } 993 1085 #endregion … … 1078 1170 } 1079 1171 1080 private class Problem Description{1172 private class ProblemInstance : INotifyPropertyChanged { 1081 1173 private readonly bool matchAll; 1082 public static readonly Problem Description MatchAll = new ProblemDescription() {1174 public static readonly ProblemInstance MatchAll = new ProblemInstance() { 1083 1175 ProblemName = "All with Best-Known" 1084 1176 }; 1085 1177 1086 private Problem Description() {1178 private ProblemInstance() { 1087 1179 ProblemType = string.Empty; 1088 1180 ProblemName = string.Empty; … … 1094 1186 DisplayMaximization = false; 1095 1187 matchAll = true; 1096 } 1097 1098 public ProblemDescription(IRun run) { 1188 BestKnownQuality = double.NaN; 1189 } 1190 1191 public ProblemInstance(IRun run) { 1099 1192 ProblemType = GetStringValueOrEmpty(run, "Problem Type"); 1100 1193 ProblemName = GetStringValueOrEmpty(run, "Problem Name"); … … 1106 1199 DisplayMaximization = !string.IsNullOrEmpty(Maximization); 1107 1200 matchAll = false; 1108 } 1109 1110 public bool DisplayProblemType { get; set; } 1111 public string ProblemType { get; set; } 1112 public bool DisplayProblemName { get; set; } 1113 public string ProblemName { get; set; } 1114 public bool DisplayEvaluator { get; set; } 1115 public string Evaluator { get; set; } 1116 public bool DisplayMaximization { get; set; } 1117 public string Maximization { get; set; } 1201 BestKnownQuality = GetDoubleValueOrNaN(run, "BestKnownQuality"); 1202 } 1203 1204 private bool displayProblemType; 1205 public bool DisplayProblemType { 1206 get { return displayProblemType; } 1207 set { 1208 if (displayProblemType == value) return; 1209 displayProblemType = value; 1210 OnPropertyChanged("DisplayProblemType"); 1211 } 1212 } 1213 private string problemType; 1214 public string ProblemType { 1215 get { return problemType; } 1216 set { 1217 if (problemType == value) return; 1218 problemType = value; 1219 OnPropertyChanged("ProblemType"); 1220 } 1221 } 1222 private bool displayProblemName; 1223 public bool DisplayProblemName { 1224 get { return displayProblemName; } 1225 set { 1226 if (displayProblemName == value) return; 1227 displayProblemName = value; 1228 OnPropertyChanged("DisplayProblemName"); 1229 } 1230 } 1231 private string problemName; 1232 public string ProblemName { 1233 get { return problemName; } 1234 set { 1235 if (problemName == value) return; 1236 problemName = value; 1237 OnPropertyChanged("ProblemName"); 1238 } 1239 } 1240 private bool displayEvaluator; 1241 public bool DisplayEvaluator { 1242 get { return displayEvaluator; } 1243 set { 1244 if (displayEvaluator == value) return; 1245 displayEvaluator = value; 1246 OnPropertyChanged("DisplayEvaluator"); 1247 } 1248 } 1249 private string evaluator; 1250 public string Evaluator { 1251 get { return evaluator; } 1252 set { 1253 if (evaluator == value) return; 1254 evaluator = value; 1255 OnPropertyChanged("Evaluator"); 1256 } 1257 } 1258 private bool displayMaximization; 1259 public bool DisplayMaximization { 1260 get { return displayMaximization; } 1261 set { 1262 if (displayMaximization == value) return; 1263 displayMaximization = value; 1264 OnPropertyChanged("DisplayMaximization"); 1265 } 1266 } 1267 private string maximization; 1268 public string Maximization { 1269 get { return maximization; } 1270 set { 1271 if (maximization == value) return; 1272 maximization = value; 1273 OnPropertyChanged("Maximization"); 1274 } 1275 } 1276 private double bestKnownQuality; 1277 public double BestKnownQuality { 1278 get { return bestKnownQuality; } 1279 set { 1280 if (bestKnownQuality == value) return; 1281 bestKnownQuality = value; 1282 OnPropertyChanged("BestKnownQuality"); 1283 } 1284 } 1118 1285 1119 1286 public bool IsMaximization() { … … 1129 1296 } 1130 1297 1298 private double GetDoubleValueOrNaN(IRun run, string key) { 1299 IItem param; 1300 if (run.Parameters.TryGetValue(key, out param)) { 1301 var dv = param as DoubleValue; 1302 return dv != null ? dv.Value : double.NaN; 1303 } 1304 return double.NaN; 1305 } 1306 1131 1307 private string GetStringValueOrEmpty(IRun run, string key) { 1132 return run.Parameters.ContainsKey(key) ? ((StringValue)run.Parameters[key]).Value : string.Empty; 1308 IItem param; 1309 if (run.Parameters.TryGetValue(key, out param)) { 1310 var sv = param as StringValue; 1311 return sv != null ? sv.Value : string.Empty; 1312 } 1313 return string.Empty; 1133 1314 } 1134 1315 1135 1316 private string GetMaximizationValueOrEmpty(IRun run, string key) { 1136 return run.Parameters.ContainsKey(key) ? (((BoolValue)run.Parameters[key]).Value ? "MAX" : "MIN") : string.Empty; 1317 IItem param; 1318 if (run.Parameters.TryGetValue(key, out param)) { 1319 var bv = param as BoolValue; 1320 return bv != null ? (bv.Value ? "MAX" : "MIN") : string.Empty; 1321 } 1322 return string.Empty; 1137 1323 } 1138 1324 1139 1325 public override bool Equals(object obj) { 1140 var other = obj as Problem Description;1326 var other = obj as ProblemInstance; 1141 1327 if (other == null) return false; 1142 1328 return ProblemType == other.ProblemType … … 1155 1341 (DisplayProblemName ? ProblemName : string.Empty), 1156 1342 (DisplayEvaluator ? Evaluator : string.Empty), 1157 (DisplayMaximization ? Maximization : string.Empty)}.Where(x => !string.IsNullOrEmpty(x))); 1343 (DisplayMaximization ? Maximization : string.Empty), 1344 !double.IsNaN(BestKnownQuality) ? BestKnownQuality.ToString(CultureInfo.CurrentCulture.NumberFormat) : string.Empty }.Where(x => !string.IsNullOrEmpty(x))); 1345 } 1346 1347 public event PropertyChangedEventHandler PropertyChanged; 1348 protected virtual void OnPropertyChanged(string propertyName = null) { 1349 var handler = PropertyChanged; 1350 if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 1158 1351 } 1159 1352 }
Note: See TracChangeset
for help on using the changeset viewer.