Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/26/16 13:07:26 (8 years ago)
Author:
abeham
Message:

#2457: worked on testing recommendation algorithms through x-validation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/PerformanceModelingView.cs

    r13794 r13797  
    3131using System.Collections.Generic;
    3232using System.Linq;
     33using System.Threading.Tasks;
    3334
    3435namespace HeuristicLab.OptimizationExpertSystem {
     
    131132    private void xValidateButton_Click(object sender, EventArgs e) {
    132133      var recommender = (IAlgorithmInstanceRecommender)recommenderComboBox.SelectedItem;
     134      var progress = MainForm.AddOperationProgressToView(this, "Performing Leave-one-out Crossvalidation");
     135
     136      Task.Factory.StartNew(() => { DoCrossvalidate(recommender, progress); }, TaskCreationOptions.LongRunning);
     137    }
     138
     139    private void DoCrossvalidate(IAlgorithmInstanceRecommender recommender, IProgress progress) {
    133140      var features = characteristics.CheckedItems.Select(x => x.Value.Value).ToArray();
    134 
    135141      var trainingSet = Content.ProblemInstances.Where(x => !Content.IsCurrentInstance(x)).ToArray();
    136142
    137143      var absErr = 0.0;
     144      var absLogError = 0.0;
    138145      var confMatrix = new int[6, 6];
    139146      var tau = 0.0;
    140147      // leave one out crossvalidation
     148      var count = 0;
    141149      foreach (var pi in trainingSet) {
     150        progress.Status = pi.Name + "...";
    142151        var model = recommender.TrainModel(trainingSet.Where(x => x != pi).ToArray(), Content, features);
    143152        var predicted = model.GetRanking(pi).ToDictionary(x => x.Key, x => x.Value);
    144153        var observed = Content.GetAlgorithmPerformance(pi);
    145154        absErr += AbsoluteError(observed, predicted);
     155        absLogError += AbsoluteLogError(observed, predicted);
    146156        var confMat = ConfusionMatrix(observed, predicted);
    147         for (var i = 0; i < confMat.GetLength(0); i++)
     157        for (var i = 0; i < confMat.GetLength(0); i++) {
    148158          for (var j = 0; j < confMat.GetLength(1); j++)
    149159            confMatrix[i, j] += confMat[i, j];
     160        }
    150161        tau += KendallsTau(observed, predicted);
    151162        // average NCDG ... relevance determined by clustering (unsuccessful algorithms being penalized with negative relevance)
    152163        // mean reciprocal rank
    153164        // optional: expected reciprocal rank
     165        progress.ProgressValue = ++count / (double)trainingSet.Length;
    154166      }
    155167      absErr /= trainingSet.Length;
     168      absLogError /= trainingSet.Length;
    156169      tau /= trainingSet.Length;
    157170
    158       confusionMatrixView.Content = new IntMatrix(confMatrix);
    159171      absoluteErrorView.Content = new DoubleValue(absErr);
     172      absoluteLogErrorView.Content = new DoubleValue(absLogError);
     173      var description = new[] { "A", "B", "C", "D", "E", "F" };
     174      confusionMatrixView.Content = new IntMatrix(confMatrix) { ColumnNames = description, RowNames = description };
    160175      kendallsTauView.Content = new DoubleValue(tau);
     176
     177      progress.Finish();
    161178    }
    162179
     
    168185        if (double.IsNaN(actual)) actual = int.MaxValue;
    169186        error += Math.Abs(actual - tuple.Value);
     187      }
     188      return error;
     189    }
     190
     191    private static double AbsoluteLogError(Dictionary<IAlgorithm, double> performance, Dictionary<IAlgorithm, double> ranking) {
     192      var error = 0.0;
     193      foreach (var tuple in ranking) {
     194        double actual;
     195        if (!performance.TryGetValue(tuple.Key, out actual)) continue;
     196        if (double.IsNaN(actual)) actual = int.MaxValue;
     197        error += Math.Abs(Math.Log10(actual) - Math.Log10(tuple.Value));
    170198      }
    171199      return error;
Note: See TracChangeset for help on using the changeset viewer.