Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/02/16 14:10:53 (9 years ago)
Author:
abeham
Message:

#2457:

  • Added stripped-down version of FLA branch
  • Added appropriate calculators
  • Fixed detecting maximization in RLD view
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionRLDView.cs

    r13475 r13583  
    2020#endregion
    2121
     22using HeuristicLab.Analysis;
     23using HeuristicLab.Collections;
     24using HeuristicLab.Core.Views;
     25using HeuristicLab.Data;
     26using HeuristicLab.MainForm;
     27using HeuristicLab.MainForm.WindowsForms;
    2228using System;
    2329using System.Collections.Generic;
     
    2733using System.Linq;
    2834using 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;
    3535
    3636namespace HeuristicLab.Optimization.Views {
     
    8686        VisualProperties = {
    8787          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
    9092        }
    9193      };
     
    9496        VisualProperties = {
    9597          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
    98102        }
    99103      };
     
    223227      var problemNamesDifferent = problems.Select(x => x.ProblemName).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1;
    224228      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;
    226231
    227232      var selectedProblemItem = (ProblemDescription)problemComboBox.SelectedItem;
     
    234239        prob.DisplayProblemName = problemNamesDifferent || allEqual;
    235240        prob.DisplayEvaluator = evaluatorDifferent;
     241        prob.DisplayMaximization = maximizationDifferent;
    236242        problemComboBox.Items.Add(prob);
    237243        if (prob.Equals(selectedProblemItem)) problemComboBox.SelectedItem = prob;
     
    266272    }
    267273
    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>>>>();
    270276
    271277      var table = (string)dataTableComboBox.SelectedItem;
     
    278284      if (selectedProblem == null) return groupedRuns;
    279285
    280       var maximization = IsMaximization();
    281 
    282       var targetsPerProblem = CalculateBestTargetPerProblemInstance(table, maximization);
     286      var targetsPerProblem = CalculateBestTargetPerProblemInstance(table);
    283287
    284288      foreach (var x in (from r in Content
     
    289293                         group r by selectedGroup == AllRuns ? AllRuns : r.Parameters[selectedGroup].ToString() into g
    290294                         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>>>();
    292296        foreach (var y in (from r in x.Item2
    293                            let pd = new ProblemDescription(r).ToString()
     297                           let pd = new ProblemDescription(r)
    294298                           group r by pd into g
    295299                           select Tuple.Create(g.Key, g.ToList()))) {
     
    319323      var colorCount = 0;
    320324      var lineStyleCount = 0;
    321       var maximization = IsMaximization();
    322325
    323326      foreach (var group in groupedRuns) {
     
    331334
    332335            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);
    334337            } 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);
    336339            }
    337340          }
     
    379382    }
    380383
    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)) {
    383386        var key = group + "-" + l;
    384387        if (!hits.ContainsKey(key)) hits.Add(key, new SortedList<double, double>());
    385388        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) {
    387390            if (hits[key].ContainsKey(v.Item1))
    388391              hits[key][v.Item1] += 1.0 / (groupCount * problemCount);
     
    394397    }
    395398
    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) {
    397400      var values = row.Values;
    398401      if (!hits.ContainsKey(group)) hits.Add(group, new SortedList<double, double>());
     
    401404      var j = 0;
    402405      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;
    404407        var current = values[j];
    405         if (maximization && current.Item2 >= target
    406           || !maximization && current.Item2 <= target) {
     408        if (problem.IsMaximization() && current.Item2 >= target
     409          || !problem.IsMaximization() && current.Item2 <= target) {
    407410          if (!hits[group].ContainsKey(current.Item1)) hits[group][current.Item1] = 0;
    408411          hits[group][current.Item1] += 1.0 / (groupCount * problemCount * targets.Length);
     
    416419    }
    417420
    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) {
    419422      ertTableView.Content = null;
    420423      var columns = 1 + targets.Length + 1;
    421424      var matrix = new string[groupedRuns.Count * groupedRuns.Max(x => x.Value.Count) + groupedRuns.Max(x => x.Value.Count), columns];
    422425      var rowCount = 0;
    423       var maximization = IsMaximization();
    424426
    425427      var tableName = (string)dataTableComboBox.SelectedItem;
    426428      if (string.IsNullOrEmpty(tableName)) return;
    427429
    428       var targetsPerProblem = CalculateBestTargetPerProblemInstance(tableName, maximization);
     430      var targetsPerProblem = CalculateBestTargetPerProblemInstance(tableName);
    429431
    430432      var colNames = new string[columns];
     
    436438
    437439      foreach (var problem in problems) {
    438         matrix[rowCount, 0] = problem;
     440        matrix[rowCount, 0] = problem.ToString();
    439441        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);
    441443        }
    442444        matrix[rowCount, columns - 1] = "#succ";
     
    453455          ErtCalculationResult result = default(ErtCalculationResult);
    454456          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());
    456458            matrix[rowCount, i + 1] = result.ToString();
    457459          }
     
    482484      var lineStyleCount = 0;
    483485
    484       var maximization = IsMaximization();
    485       var targetsPerProblem = CalculateBestTargetPerProblemInstance((string)dataTableComboBox.SelectedItem, maximization);
     486      var targetsPerProblem = CalculateBestTargetPerProblemInstance((string)dataTableComboBox.SelectedItem);
    486487
    487488      foreach (var group in groupedRuns) {
     
    493494
    494495            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]);
    496497            } 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]);
    498499            }
    499500          }
     
    547548    }
    548549
    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) {
    550551      foreach (var b in budgets) {
    551552        var key = groupName + "-" + b;
     
    557558            if (prev == null && v.Item1 != b) break;
    558559            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;
    560561            if (hits[key].ContainsKey(tgt))
    561562              hits[key][tgt] += 1.0 / (groupCount * problemCount);
     
    569570    }
    570571
    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) {
    572573      var values = row.Values;
    573574      if (!hits.ContainsKey(groupName)) hits.Add(groupName, new SortedList<double, double>());
     
    581582          if (prev != null || current.Item1 == budgets[i]) {
    582583            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;
    584585            if (!hits[groupName].ContainsKey(tgt)) hits[groupName][tgt] = 0;
    585586            hits[groupName][tgt] += 1.0 / (groupCount * problemCount * budgets.Length);
     
    592593      }
    593594      var lastTgt = values.Last().Item2;
    594       lastTgt = maximization ? bestTarget / lastTgt : lastTgt / bestTarget;
     595      lastTgt = problem.IsMaximization() ? bestTarget / lastTgt : lastTgt / bestTarget;
    595596      if (i < budgets.Length && !hits[groupName].ContainsKey(lastTgt)) hits[groupName][lastTgt] = 0;
    596597      while (i < budgets.Length) {
     
    665666
    666667    private void generateTargetsButton_Click(object sender, EventArgs e) {
    667       var maximization = IsMaximization();
    668668      decimal max = 1, min = 0, count = 10;
    669669      if (targets != null) {
     
    680680        if (dialog.ShowDialog() == DialogResult.OK) {
    681681          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();
    684683            suppressTargetsEvents = true;
    685684            targetsTextBox.Text = string.Join("% ; ", targets);
     
    695694    private void addTargetsAsResultButton_Click(object sender, EventArgs e) {
    696695      var table = (string)dataTableComboBox.SelectedItem;
    697       var maximization = IsMaximization();
    698 
    699       var targetsPerProblem = CalculateBestTargetPerProblemInstance(table, maximization);
     696
     697      var targetsPerProblem = CalculateBestTargetPerProblemInstance(table);
    700698
    701699      foreach (var run in Content) {
     
    707705        var pd = new ProblemDescription(run);
    708706        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];
    710708          var current = values[j];
    711           if (maximization && current.Item2 >= target
    712               || !maximization && current.Item2 <= target) {
     709          if (pd.IsMaximization() && current.Item2 >= target
     710              || !pd.IsMaximization() && current.Item2 <= target) {
    713711            run.Results[table + ".Target" + target] = new DoubleValue(current.Item1);
    714712            i++;
     
    828826
    829827    #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) {
    858829      return (from r in Content
    859830              where r.Visible
    860               let pd = new ProblemDescription(r).ToString()
     831              let pd = new ProblemDescription(r)
    861832              let target = r.Parameters.ContainsKey("BestKnownQuality")
    862833                           && r.Parameters["BestKnownQuality"] is DoubleValue
     
    864835                : ((IndexedDataTable<double>)r.Results[table]).Rows.First().Values.Last().Item2
    865836              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() })
    867838        .ToDictionary(x => x.Problem, x => x.Target);
    868839    }
     
    892863        ProblemName = string.Empty;
    893864        Evaluator = string.Empty;
     865        Maximization = string.Empty;
    894866        DisplayProblemType = false;
    895867        DisplayProblemName = false;
    896868        DisplayEvaluator = false;
     869        DisplayMaximization = false;
    897870        matchAll = true;
    898871      }
     
    902875        ProblemName = GetStringValueOrEmpty(run, "Problem Name");
    903876        Evaluator = GetStringValueOrEmpty(run, "Evaluator");
     877        Maximization = GetMaximizationValueOrEmpty(run, "Maximization");
    904878        DisplayProblemType = !string.IsNullOrEmpty(ProblemType);
    905879        DisplayProblemName = !string.IsNullOrEmpty(ProblemName);
    906880        DisplayEvaluator = !string.IsNullOrEmpty(Evaluator);
     881        DisplayMaximization = !string.IsNullOrEmpty(Maximization);
    907882        matchAll = false;
    908883      }
     
    914889      public bool DisplayEvaluator { get; set; }
    915890      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      }
    916897
    917898      public bool Match(IRun run) {
     
    919900               GetStringValueOrEmpty(run, "Problem Type") == ProblemType
    920901               && GetStringValueOrEmpty(run, "Problem Name") == ProblemName
    921                && GetStringValueOrEmpty(run, "Evaluator") == Evaluator;
     902               && GetStringValueOrEmpty(run, "Evaluator") == Evaluator
     903               && GetMaximizationValueOrEmpty(run, "Maximization") == Maximization;
    922904      }
    923905
    924906      private string GetStringValueOrEmpty(IRun run, string key) {
    925907        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;
    926912      }
    927913
     
    931917        return ProblemType == other.ProblemType
    932918               && ProblemName == other.ProblemName
    933                && Evaluator == other.Evaluator;
     919               && Evaluator == other.Evaluator
     920               && Maximization == other.Maximization;
    934921      }
    935922
    936923      public override int GetHashCode() {
    937         return ProblemType.GetHashCode() ^ ProblemName.GetHashCode() ^ Evaluator.GetHashCode();
     924        return ProblemType.GetHashCode() ^ ProblemName.GetHashCode() ^ Evaluator.GetHashCode() ^ Maximization.GetHashCode();
    938925      }
    939926
     
    942929          (DisplayProblemType ? ProblemType : string.Empty),
    943930          (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)));
    945933      }
    946934    }
Note: See TracChangeset for help on using the changeset viewer.