Changeset 13803
- Timestamp:
- 04/27/16 16:21:07 (9 years ago)
- Location:
- branches/PerformanceComparison
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Analysis/3.3/QualityAnalysis/ExpectedRuntimeHelper.cs
r13774 r13803 1 using HeuristicLab.Optimization; 1 using HeuristicLab.Common; 2 using HeuristicLab.Optimization; 2 3 using System; 3 4 using System.Collections.Generic; … … 24 25 } 25 26 26 var ert = double. NaN;27 var ert = double.PositiveInfinity; 27 28 29 var nRuns = successful.Count + unsuccessful.Count; 28 30 if (successful.Count > 0) { 29 if (unsuccessful.Count == 0) ert = successful.Average(); 30 else { 31 var ps = successful.Count / (double)(successful.Count + unsuccessful.Count); 32 ert = successful.Average() + ((1.0 - ps) / ps) * unsuccessful.Average(); 33 } 31 var succAvg = successful.Average(); 32 var succDev = successful.StandardDeviation(); 33 successful.RemoveAll(x => x < succAvg - 2 * succDev); 34 unsuccessful.RemoveAll(x => x < succAvg - 2 * succDev); 35 nRuns = successful.Count + unsuccessful.Count; 36 37 ert = successful.Average() / (successful.Count / (double)nRuns); 34 38 } 35 return new ErtCalculationResult(successful.Count, (successful.Count + unsuccessful.Count), ert);39 return new ErtCalculationResult(successful.Count, nRuns, ert); 36 40 } 37 41 -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
r13797 r13803 654 654 var bkq = ((DoubleValue)prob.Parameters["BestKnownQuality"]).Value; 655 655 var ert = ExpectedRuntimeHelper.CalculateErt(pr.ToList(), "QualityPerEvaluations", GetTarget(bkq, target, max), max).ExpectedRuntime; 656 if (double.Is NaN(ert)) ert = int.MaxValue;656 if (double.IsInfinity(ert)) ert = int.MaxValue; 657 657 ds.AddRow(new object[] { pr.Key }.Concat(f.Cast<object>()).Concat(new object[] { ert })); 658 658 } … … 720 720 var values = pr.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single()) 721 721 .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", GetTarget(bkq, target, max), max).ExpectedRuntime); 722 var ranks = ClusteringHelper<long>.Cluster(nClasses, values, x => double.Is NaN(x.Value))722 var ranks = ClusteringHelper<long>.Cluster(nClasses, values, x => double.IsInfinity(x.Value)) 723 723 .GetByCluster().ToList(); 724 724 foreach (var c in ranks) { -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/KNearestNeighborModel.cs
r13794 r13803 49 49 var feature = KnowledgeCenter.GetFeatures(new [] { problemInstance }, characteristics, medianValues)[0]; 50 50 var nearestK = features.Select((f, i) => new { ProblemInstanceIndex = i, Feature = f }) 51 .OrderBy(x => x.Feature.Select((f, i) => (f - feature[i]) * (f - feature[i])).Sum()) 52 .Take(K); 51 .OrderBy(x => x.Feature.Select((f, i) => (f - feature[i]) * (f - feature[i])).Sum()); 53 52 54 var performances = new Dictionary<IAlgorithm, List<double>>(); 53 var performances = new Dictionary<IAlgorithm, Performance>(); 54 55 var k = 0; 55 56 foreach (var next in nearestK) { 57 if (k >= K) break; 56 58 var perfs = performance[problemInstanceMap.GetByFirst(next.ProblemInstanceIndex)]; 57 59 if (perfs.Count == 0) continue; … … 59 61 foreach (var p in perfs) { 60 62 var ert = p.Value; 61 if (double.IsNaN(ert)) ert = int.MaxValue; 62 List<double> erts; 63 if (!performances.TryGetValue(p.Key, out erts)) { 64 performances[p.Key] = new List<double>() { ert }; ; 65 } else erts.Add(ert); 63 Performance perf; 64 if (!performances.TryGetValue(p.Key, out perf)) { 65 perf = new Performance(); 66 performances[p.Key] = perf; 67 } 68 perf.Add(ert); 66 69 } 70 71 k++; 67 72 } 68 73 69 return performances.Select(x => new { Alg = x.Key, Perf = x.Value. Average() })74 return performances.Select(x => new { Alg = x.Key, Perf = x.Value.ExpectedRuntime() }) 70 75 .OrderBy(x => x.Perf) 71 76 .Select(x => new KeyValuePair<IAlgorithm, double>(x.Alg, x.Perf)); 72 77 } 78 79 private class Performance { 80 private readonly List<double> successful; 81 private int runs; 82 public int Fails { get { return runs - successful.Count; } } 83 84 public Performance() { 85 successful = new List<double>(); 86 } 87 88 public void Add(double ert) { 89 if (!double.IsInfinity(ert)) successful.Add(ert); 90 runs++; 91 } 92 93 public double ExpectedRuntime() { 94 if (successful.Count == 0) return int.MaxValue; 95 return successful.Average() / (successful.Count / (double)runs); 96 } 97 } 73 98 } 74 99 } -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Recommenders/OverallBestRecommender.cs
r13797 r13803 63 63 if (!pis.TryGetValue(problemRuns.Key, out bkq)) continue; 64 64 var ert = ExpectedRuntimeHelper.CalculateErt(problemRuns.ToList(), "QualityPerEvaluations", kc.GetTarget(bkq, kc.MinimumTarget.Value, kc.Maximization), kc.Maximization).ExpectedRuntime; 65 if (double.Is NaN(ert)) ert = int.MaxValue;65 if (double.IsInfinity(ert)) ert = int.MaxValue; 66 66 avgERT += ert; 67 67 count++; -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/PerformanceModelingView.Designer.cs
r13797 r13803 55 55 this.parameterCollectionView = new HeuristicLab.Core.Views.ParameterCollectionView(); 56 56 this.crossvalidationTabPage = new System.Windows.Forms.TabPage(); 57 this.topNComboBox = new System.Windows.Forms.ComboBox(); 58 this.topNlabel = new System.Windows.Forms.Label(); 57 59 this.kendallsTauLabel = new System.Windows.Forms.Label(); 58 60 this.predictedLabel = new System.Windows.Forms.Label(); 59 this.actualLabel = new System.Windows.Forms.Label();60 61 this.absoluteLogErrorLabel = new System.Windows.Forms.Label(); 61 62 this.absoluteErrorLabel = new System.Windows.Forms.Label(); … … 67 68 this.minTargetView = new HeuristicLab.Data.Views.StringConvertibleValueView(); 68 69 this.minimumTargetLabel = new System.Windows.Forms.Label(); 70 this.ncdgView = new HeuristicLab.Data.Views.StringConvertibleValueView(); 71 this.ndcgLabel = new System.Windows.Forms.Label(); 69 72 this.tabControl.SuspendLayout(); 70 73 this.characteristicsTabPage.SuspendLayout(); … … 139 142 this.characteristicsViewHost.Name = "characteristicsViewHost"; 140 143 this.characteristicsViewHost.ReadOnly = false; 141 this.characteristicsViewHost.Size = new System.Drawing.Size( 680, 283);144 this.characteristicsViewHost.Size = new System.Drawing.Size(940, 442); 142 145 this.characteristicsViewHost.TabIndex = 1; 143 146 this.characteristicsViewHost.ViewsLabelVisible = true; … … 180 183 // crossvalidationTabPage 181 184 // 185 this.crossvalidationTabPage.Controls.Add(this.topNComboBox); 186 this.crossvalidationTabPage.Controls.Add(this.topNlabel); 187 this.crossvalidationTabPage.Controls.Add(this.ndcgLabel); 182 188 this.crossvalidationTabPage.Controls.Add(this.kendallsTauLabel); 183 189 this.crossvalidationTabPage.Controls.Add(this.predictedLabel); 184 this.crossvalidationTabPage.Controls.Add(this.actualLabel);185 190 this.crossvalidationTabPage.Controls.Add(this.absoluteLogErrorLabel); 186 191 this.crossvalidationTabPage.Controls.Add(this.absoluteErrorLabel); 192 this.crossvalidationTabPage.Controls.Add(this.ncdgView); 187 193 this.crossvalidationTabPage.Controls.Add(this.kendallsTauView); 188 194 this.crossvalidationTabPage.Controls.Add(this.absoluteLogErrorView); … … 198 204 this.crossvalidationTabPage.UseVisualStyleBackColor = true; 199 205 // 206 // topNComboBox 207 // 208 this.topNComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 209 this.topNComboBox.FormattingEnabled = true; 210 this.topNComboBox.Location = new System.Drawing.Point(331, 9); 211 this.topNComboBox.Name = "topNComboBox"; 212 this.topNComboBox.Size = new System.Drawing.Size(76, 21); 213 this.topNComboBox.TabIndex = 5; 214 this.topNComboBox.SelectedIndexChanged += new System.EventHandler(this.topNComboBox_SelectedIndexChanged); 215 // 216 // topNlabel 217 // 218 this.topNlabel.AutoSize = true; 219 this.topNlabel.Location = new System.Drawing.Point(192, 12); 220 this.topNlabel.Name = "topNlabel"; 221 this.topNlabel.Size = new System.Drawing.Size(133, 13); 222 this.topNlabel.TabIndex = 4; 223 this.topNlabel.Text = "Evaluate only best ranked:"; 224 // 200 225 // kendallsTauLabel 201 226 // 202 227 this.kendallsTauLabel.AutoSize = true; 203 this.kendallsTauLabel.Location = new System.Drawing.Point(519, 165);228 this.kendallsTauLabel.Location = new System.Drawing.Point(519, 224); 204 229 this.kendallsTauLabel.Margin = new System.Windows.Forms.Padding(3); 205 230 this.kendallsTauLabel.Name = "kendallsTauLabel"; … … 219 244 this.predictedLabel.Text = "PREDICTED"; 220 245 // 221 // actualLabel222 //223 this.actualLabel.AutoSize = true;224 this.actualLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));225 this.actualLabel.Location = new System.Drawing.Point(16, 155);226 this.actualLabel.Margin = new System.Windows.Forms.Padding(3);227 this.actualLabel.Name = "actualLabel";228 this.actualLabel.Size = new System.Drawing.Size(15, 78);229 this.actualLabel.TabIndex = 3;230 this.actualLabel.Text = "A\r\nC\r\nT\r\nU\r\nA\r\nL";231 //232 246 // absoluteLogErrorLabel 233 247 // … … 236 250 this.absoluteLogErrorLabel.Margin = new System.Windows.Forms.Padding(3); 237 251 this.absoluteLogErrorLabel.Name = "absoluteLogErrorLabel"; 238 this.absoluteLogErrorLabel.Size = new System.Drawing.Size(1 27, 13);252 this.absoluteLogErrorLabel.Size = new System.Drawing.Size(162, 13); 239 253 this.absoluteLogErrorLabel.TabIndex = 3; 240 this.absoluteLogErrorLabel.Text = "Mean Absolute Log Error :";254 this.absoluteLogErrorLabel.Text = "Mean Absolute Log Error (top N):"; 241 255 // 242 256 // absoluteErrorLabel … … 246 260 this.absoluteErrorLabel.Margin = new System.Windows.Forms.Padding(3); 247 261 this.absoluteErrorLabel.Name = "absoluteErrorLabel"; 248 this.absoluteErrorLabel.Size = new System.Drawing.Size(1 06, 13);262 this.absoluteErrorLabel.Size = new System.Drawing.Size(141, 13); 249 263 this.absoluteErrorLabel.TabIndex = 3; 250 this.absoluteErrorLabel.Text = "Mean Absolute Error :";264 this.absoluteErrorLabel.Text = "Mean Absolute Error (top N):"; 251 265 // 252 266 // kendallsTauView … … 255 269 this.kendallsTauView.Content = null; 256 270 this.kendallsTauView.LabelVisible = false; 257 this.kendallsTauView.Location = new System.Drawing.Point(503, 184);271 this.kendallsTauView.Location = new System.Drawing.Point(503, 243); 258 272 this.kendallsTauView.Name = "kendallsTauView"; 259 273 this.kendallsTauView.ReadOnly = true; … … 292 306 this.confusionMatrixView.ShowRowsAndColumnsTextBox = false; 293 307 this.confusionMatrixView.ShowStatisticalInformation = false; 294 this.confusionMatrixView.Size = new System.Drawing.Size(438, 244);308 this.confusionMatrixView.Size = new System.Drawing.Size(438, 134); 295 309 this.confusionMatrixView.TabIndex = 1; 296 310 // … … 324 338 this.minimumTargetLabel.TabIndex = 17; 325 339 this.minimumTargetLabel.Text = "Target:"; 340 // 341 // ncdgView 342 // 343 this.ncdgView.Caption = "StringConvertibleValue View"; 344 this.ncdgView.Content = null; 345 this.ncdgView.LabelVisible = false; 346 this.ncdgView.Location = new System.Drawing.Point(503, 184); 347 this.ncdgView.Name = "ncdgView"; 348 this.ncdgView.ReadOnly = true; 349 this.ncdgView.Size = new System.Drawing.Size(194, 21); 350 this.ncdgView.TabIndex = 2; 351 // 352 // ndcgLabel 353 // 354 this.ndcgLabel.AutoSize = true; 355 this.ndcgLabel.Location = new System.Drawing.Point(519, 165); 356 this.ndcgLabel.Margin = new System.Windows.Forms.Padding(3); 357 this.ndcgLabel.Name = "ndcgLabel"; 358 this.ndcgLabel.Size = new System.Drawing.Size(106, 13); 359 this.ndcgLabel.TabIndex = 3; 360 this.ndcgLabel.Text = "Mean NDCG (top N):"; 326 361 // 327 362 // PerformanceModelingView … … 368 403 private Data.Views.StringConvertibleMatrixView confusionMatrixView; 369 404 private System.Windows.Forms.Label predictedLabel; 370 private System.Windows.Forms.Label actualLabel;371 405 private System.Windows.Forms.Label absoluteLogErrorLabel; 372 406 private Data.Views.StringConvertibleValueView absoluteLogErrorView; 407 private System.Windows.Forms.ComboBox topNComboBox; 408 private System.Windows.Forms.Label topNlabel; 409 private System.Windows.Forms.Label ndcgLabel; 410 private Data.Views.StringConvertibleValueView ncdgView; 373 411 } 374 412 } -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/PerformanceModelingView.cs
r13797 r13803 59 59 } 60 60 UpdateCharacteristics(); 61 UpdateTopNCombobox(); 61 62 } 62 63 … … 66 67 recommendStartButton.Enabled = Content != null && recommenderComboBox.SelectedIndex >= 0 && characteristics.CheckedItems.Any(); 67 68 characteristicsViewHost.Enabled = Content != null; 68 xValidateButton.Enabled = Content != null && recommenderComboBox.SelectedIndex >= 0 && characteristics.CheckedItems.Any() ;69 xValidateButton.Enabled = Content != null && recommenderComboBox.SelectedIndex >= 0 && characteristics.CheckedItems.Any() && topNComboBox.SelectedIndex >= 0; 69 70 } 70 71 … … 101 102 } 102 103 } 104 105 private void UpdateTopNCombobox() { 106 if (InvokeRequired) { Invoke((Action)UpdateTopNCombobox); return; } 107 108 int selected = 3; 109 if (topNComboBox.SelectedIndex >= 0) selected = (int)topNComboBox.SelectedItem; 110 topNComboBox.Items.Clear(); 111 if (Content == null) return; 112 113 var algInstances = Content.AlgorithmInstances.Count; 114 for (var i = 1; i <= algInstances; i++) { 115 topNComboBox.Items.Add(i); 116 } 117 topNComboBox.SelectedIndex = Math.Min(selected, topNComboBox.Items.Count) - 1; 118 } 103 119 #endregion 104 120 … … 108 124 UpdateCharacteristics(); 109 125 SetEnabledStateOfControls(); 126 } 127 128 protected override void OnAlgorithmInstancesChanged() { 129 base.OnAlgorithmInstancesChanged(); 130 UpdateTopNCombobox(); 110 131 } 111 132 #endregion … … 133 154 var recommender = (IAlgorithmInstanceRecommender)recommenderComboBox.SelectedItem; 134 155 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) { 140 var features = characteristics.CheckedItems.Select(x => x.Value.Value).ToArray(); 141 var trainingSet = Content.ProblemInstances.Where(x => !Content.IsCurrentInstance(x)).ToArray(); 142 143 var absErr = 0.0; 144 var absLogError = 0.0; 145 var confMatrix = new int[6, 6]; 146 var tau = 0.0; 147 // leave one out crossvalidation 148 var count = 0; 149 foreach (var pi in trainingSet) { 150 progress.Status = pi.Name + "..."; 151 var model = recommender.TrainModel(trainingSet.Where(x => x != pi).ToArray(), Content, features); 152 var predicted = model.GetRanking(pi).ToDictionary(x => x.Key, x => x.Value); 153 var observed = Content.GetAlgorithmPerformance(pi); 154 absErr += AbsoluteError(observed, predicted); 155 absLogError += AbsoluteLogError(observed, predicted); 156 var confMat = ConfusionMatrix(observed, predicted); 157 for (var i = 0; i < confMat.GetLength(0); i++) { 158 for (var j = 0; j < confMat.GetLength(1); j++) 159 confMatrix[i, j] += confMat[i, j]; 156 var topN = (int)topNComboBox.SelectedItem; 157 Task.Factory.StartNew(() => { DoCrossvalidate(recommender, topN, progress); }, TaskCreationOptions.LongRunning); 158 } 159 160 private void topNComboBox_SelectedIndexChanged(object sender, EventArgs e) { 161 SetEnabledStateOfControls(); 162 } 163 #endregion 164 165 #region Other Event Handlers 166 private void CharacteristicsOnCheckedItemsChanged(object sender, EventArgs e) { 167 SetEnabledStateOfControls(); 168 } 169 #endregion 170 171 private void DoCrossvalidate(IAlgorithmInstanceRecommender recommender, int topN, IProgress progress) { 172 try { 173 var features = characteristics.CheckedItems.Select(x => x.Value.Value).ToArray(); 174 var trainingSet = Content.ProblemInstances.Where(x => !Content.IsCurrentInstance(x)).ToArray(); 175 176 var absErr = 0.0; 177 var absErrCnt = 0; 178 var absLogErr = 0.0; 179 var absLogErrCnt = 0; 180 var confMatrix = new int[1, 6]; 181 var tau = 0.0; 182 var tauCnt = 0; 183 var ndcg = 0.0; 184 var ndcgCnt = 0; 185 // leave one out crossvalidation 186 var count = 0; 187 foreach (var pi in trainingSet) { 188 var observed = Content.GetAlgorithmPerformance(pi); 189 if (observed.Count == 0) continue; 190 progress.Status = pi.Name + "..."; 191 var model = recommender.TrainModel(trainingSet.Where(x => x != pi).ToArray(), Content, features); 192 var predictedTopN = model.GetRanking(pi).Take(topN).ToDictionary(x => x.Key, x => x.Value); 193 var predicted = model.GetRanking(pi).ToDictionary(x => x.Key, x => x.Value); 194 var ae = AbsoluteError(observed, predictedTopN); 195 if (!double.IsNaN(ae)) { 196 absErr += ae; // in case we only predicted instances that have not been observed 197 absErrCnt++; 198 } 199 var ale = AbsoluteLogError(observed, predictedTopN); 200 if (!double.IsNaN(ale)) { 201 absLogErr += ale; // in case we only predicted instances that have not been observed 202 absLogErrCnt++; 203 } 204 var confMat = ConfusionMatrix(observed, predictedTopN); 205 for (var i = 0; i < confMat.Length; i++) { 206 confMatrix[0, i] += confMat[i]; 207 } 208 var kt = KendallsTau(observed, predicted); 209 if (!double.IsNaN(kt)) { 210 tau += kt; 211 tauCnt++; 212 } 213 var gain = NDCG(observed, model.GetRanking(pi).Take(topN).Select(x => x.Key).ToList()); 214 if (!double.IsNaN(gain)) { 215 ndcg += gain; 216 ndcgCnt++; 217 } 218 // mean reciprocal rank 219 // optional: expected reciprocal rank 220 progress.ProgressValue = ++count / (double)trainingSet.Length; 160 221 } 161 tau += KendallsTau(observed, predicted); 162 // average NCDG ... relevance determined by clustering (unsuccessful algorithms being penalized with negative relevance) 163 // mean reciprocal rank 164 // optional: expected reciprocal rank 165 progress.ProgressValue = ++count / (double)trainingSet.Length; 166 } 167 absErr /= trainingSet.Length; 168 absLogError /= trainingSet.Length; 169 tau /= trainingSet.Length; 170 171 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 }; 175 kendallsTauView.Content = new DoubleValue(tau); 176 177 progress.Finish(); 222 absErr /= absErrCnt; 223 absLogErr /= absLogErrCnt; 224 tau /= tauCnt; 225 ndcg /= ndcgCnt; 226 227 absoluteErrorView.Content = new DoubleValue(absErr); 228 absoluteLogErrorView.Content = new DoubleValue(absLogErr); 229 var description = new[] {"A", "B", "C", "D", "E", "F"}; 230 confusionMatrixView.Content = new IntMatrix(confMatrix) {ColumnNames = description}; 231 kendallsTauView.Content = new DoubleValue(tau); 232 ncdgView.Content = new DoubleValue(ndcg); 233 } finally { progress.Finish(); } 178 234 } 179 235 180 236 private static double AbsoluteError(Dictionary<IAlgorithm, double> performance, Dictionary<IAlgorithm, double> ranking) { 181 237 var error = 0.0; 238 var count = 0; 182 239 foreach (var tuple in ranking) { 183 240 double actual; 184 241 if (!performance.TryGetValue(tuple.Key, out actual)) continue; 185 if (double.Is NaN(actual)) actual = int.MaxValue;242 if (double.IsInfinity(actual)) actual = int.MaxValue; 186 243 error += Math.Abs(actual - tuple.Value); 187 } 188 return error; 244 count++; 245 } 246 return error / count; 189 247 } 190 248 191 249 private static double AbsoluteLogError(Dictionary<IAlgorithm, double> performance, Dictionary<IAlgorithm, double> ranking) { 192 250 var error = 0.0; 251 var count = 0; 193 252 foreach (var tuple in ranking) { 194 253 double actual; 195 254 if (!performance.TryGetValue(tuple.Key, out actual)) continue; 196 if (double.Is NaN(actual)) actual = int.MaxValue;255 if (double.IsInfinity(actual)) actual = int.MaxValue; 197 256 error += Math.Abs(Math.Log10(actual) - Math.Log10(tuple.Value)); 198 } 199 return error; 200 } 201 202 private static int[,] ConfusionMatrix(Dictionary<IAlgorithm, double> performance, Dictionary<IAlgorithm, double> ranking) { 203 var confMatrix = new int[6,6]; 204 var clusteredPerformance = ClusteringHelper<IAlgorithm>.Cluster(5, performance, a => double.IsNaN(a.Value)).GetByInstance().ToDictionary(x => x.Key, x => x.Value.Item2); 205 var clusteredRanks = ClusteringHelper<IAlgorithm>.Cluster(5, ranking, x => double.IsNaN(x.Value) || x.Value >= int.MaxValue).GetByInstance().ToDictionary(x => x.Key, x => x.Value.Item2); 206 foreach (var cr in clusteredRanks) { 257 count++; 258 } 259 return error / count; 260 } 261 262 private static int[] ConfusionMatrix(Dictionary<IAlgorithm, double> performance, Dictionary<IAlgorithm, double> ranking) { 263 var confMatrix = new int[6]; 264 var clusteredPerformance = ClusteringHelper<IAlgorithm>.Cluster(5, performance, a => double.IsInfinity(a.Value)).GetByInstance().ToDictionary(x => x.Key, x => x.Value.Item2); 265 266 foreach (var cr in ranking) { 207 267 int realRank; 208 268 if (!clusteredPerformance.TryGetValue(cr.Key, out realRank)) continue; 209 confMatrix[realRank , cr.Value]++;269 confMatrix[realRank]++; 210 270 } 211 271 return confMatrix; … … 215 275 var commonKeys = performance.Keys.Intersect(ranking.Keys).ToList(); 216 276 var n = commonKeys.Count; 217 if (n == 0) return 0.0;277 if (n == 0) return double.NaN; 218 278 219 279 var actualRanks = commonKeys.Select((x, i) => new { Alg = x, Index = i, Perf = performance[x] }) … … 223 283 .OrderBy(x => x.Perf).Select((x, i) => new { Index = x.Index, Rank = i }) 224 284 .OrderBy(x => x.Index).Select(x => x.Rank).ToList(); 225 285 226 286 var paired = actualRanks.Zip(predictedRanks, (a, p) => new { Actual = a, Predicted = p }).ToList(); 227 287 var concordant = 0; 228 288 var discordant = 0; 229 289 for (var i = 0; i < paired.Count - 1; i++) 230 for (var j = 0; j < paired.Count; j++) {290 for (var j = i + 1; j < paired.Count; j++) { 231 291 if (paired[i].Actual > paired[j].Actual && paired[i].Predicted > paired[j].Predicted 232 292 || paired[i].Actual < paired[j].Actual && paired[i].Predicted < paired[j].Predicted) … … 236 296 return (2.0 * (concordant - discordant)) / (n * (n - 1)); 237 297 } 238 #endregion 239 240 #region Other Event Handlers 241 private void CharacteristicsOnCheckedItemsChanged(object sender, EventArgs e) { 242 SetEnabledStateOfControls(); 243 } 244 #endregion 298 299 private static double NDCG(Dictionary<IAlgorithm, double> performance, List<IAlgorithm> ranking) { 300 var k = 5; 301 var relevance = ClusteringHelper<IAlgorithm>.Cluster(k, performance, a => double.IsInfinity(a.Value)).GetByInstance().ToDictionary(x => x.Key, x => k - x.Value.Item2); 302 303 var i = 0; 304 var dcgp = 0.0; 305 for (; i < ranking.Count; i++) { 306 int rel; 307 if (!relevance.TryGetValue(ranking[i], out rel)) 308 continue; 309 dcgp = rel; 310 i++; 311 break; 312 } 313 for (; i < ranking.Count; i++) { 314 int rel; 315 if (!relevance.TryGetValue(ranking[i], out rel)) 316 continue; 317 dcgp += rel / Math.Log(i + 1, 2); 318 } 319 var ideal = relevance.Select(x => x.Value).OrderByDescending(x => x).Take(ranking.Count).ToArray(); 320 double idcgp = ideal[0]; 321 for (i = 1; i < ideal.Length; i++) 322 idcgp += ideal[i] / Math.Log(i + 1, 2); 323 324 return dcgp / idcgp; 325 } 245 326 } 246 327 }
Note: See TracChangeset
for help on using the changeset viewer.